与PIC18F4520控制器接口的GSM SIM900A模块

时间:2018-12-19 13:07:02

标签: c

我正在使用与PIC18F4520微控制器接口的GSM(sim900a)模块。

在这里,我尝试通过PIC控制器将SMS串行发送到GSM模块,但是无法接收到来自GSM的任何响应,也没有接收到任何消息。

当我尝试仅将GSM模块连接到超级终端时,便能够发送SMS。以类似的方式,当我尝试将AT命令从PIC发送到HYPERTERMINAL时,我将串行接收命令。

下面的代码

#include<p18f4520.h>      /* Header File */
#include<string.h>
void delay_ms(int ms);
void Data(char Value); 
void Cmd(char Value);
char temp;
void main() 
{
   char tocheck[] ="AT";/*Basic command to test GSM is working or not */
   char sendingsmsmode[]="AT+CMGF=1";/* To change the GSM Module in SMS 
  sending Mode */
   char newsms[]="AT+CMGS=918500923915";/*Set Text mode for SMS */
 char msg[]="WelcometoGSM";/* The text which you want to send */
 char terminator=0x1A;
 int i=0;
  TRISC =   0x80;         /* RC6=0 (O/P) RC7=1(I/P)                    */           
 SPBRG  =   0x33;         /* Serial Port Baud rate Generator (9600)    */
 TXSTA  =   0X24;         /* Transmission Enabling(TXEN=1,SYNC=0,BRGH=1) 
 */
 RCSTA  =   0X90;         /* Rception Enabling (SPEN=1,CREN=1)         */
 TRISC=0X00;               /*  (RC1,RC0 ->O/P Setting by Zero)                 
   */
 TRISD=0X00;                 /*  PORTD (0 - 7)pins Config as Output                  
       */

   while(tocheck[i]!='\0'){
    while(TXSTAbits.TRMT==0);
    TXREG=tocheck[i];
    delay_ms(30);
    i++;
  }
   i=0;
   while(sendingsmsmode[i]!='\0'){
    while(TXSTAbits.TRMT==0);
    TXREG=sendingsmsmode[i];
    delay_ms(30);
    i++;
  }
  i=0;
  while(newsms[i]!='\0'){
    while(TXSTAbits.TRMT==0);
    TXREG=newsms[i];
    delay_ms(30);
    i++;
   }
   i=0;
  while(msg[i]!='\0'){
    while(TXSTAbits.TRMT==0);
    TXREG=msg[i];
    delay_ms(30);
    i++;
}
  TXREG=terminator;
 delay_ms(3000);
 while(1);
 }
  void Cmd(char Value)
   {
 PORTD=Value;
 PORTCbits.RC1=0;                /*  RC1=0(RS=0)        [Command Registr 
 Selection])    */
 PORTCbits.RC0=0;                /*  RC0=0(R/W=0)   [Write Process])                
   */
 PORTCbits.RC2=1;                /*  RC2=1(Enable=1)    [Enable Line ON]                
  */
 delay_ms(4);                    /*  Minimun Delay For Hold On Data                      
   */
 PORTCbits.RC2=0;                /*  RC2=0(Enable=0)    [Enable Line OFF]               
 */
    }   

void Data(char Value)
{
 PORTD=Value;
 PORTCbits.RC1=1;               /*  RC1=1(RS=1)     [Data Registr 
Selection])       */
 PORTCbits.RC0=0;                /*  RC0=0(R/W=0)   [Write Process])                
   */
 PORTCbits.RC2=1;               /*  RC2=1(Enable=1) [Enable Line ON]                
   */
 delay_ms(4);                   /*  Minimun Delay For Hold On Data                      
    */
 PORTCbits.RC2=0;               /*  RC2=0(Enable=0) [Enable Line OFF]               
    */
    }
  void delay_ms(int ms)
{
int i,count;

for(i=1;i<=ms;i++)
{
    count=498;
    while(count!=1)
    {
        count--;
    }
     }

   }

1 个答案:

答案 0 :(得分:1)

您需要在发送到调制解调器的每个字符串之后发送CR或CR + LF(分别为13和10个字符)。

使用超级终端等终端仿真器时,按Enter键,对吗?如果您不按该键,调制解调器将不知道该命令已终止,必须执行。必须在固件中执行相同的操作。

还有更多:您必须允许一些延迟,并且应该读回调制解调器的响应,但这可能是第二步。首先,要检查设置是否在基本级别上正常运行,必须发送“ AT”,然后发送CR,然后查看调制解调器是否以OK(+ CR和LF ...)答复。

修改

char tocheck[] ="AT";

char tocheck[] ="AT\r";  // the \r is the CR (carriage return)

如果所有连接都正确,您将看到调制解调器会答复。