2014-05-14 31 views
0

我想發送&使用GSM sim300調制解調器和微控制器(PIC18f4550)接收短信 我已經使用PC(超級終端)測試了GSM,它工作得很好。 但現在我寫了一個小代碼,以便我可以使用micro_controller發送和接收,但它不發送任何東西!我不知道代碼中的問題在哪裏。 請給它看看,並幫助我。使用PIC18f4550 + GSM發送和接收短信sim300

月,

#include<p18f4550.h> 
#include<delays.h> 

#pragma config PLLDIV = 3 
#pragma config CPUDIV = OSC1_PLL2 
#pragma config FOSC = HS 
#pragma config MCLRE = ON 
#pragma config WDT = OFF , DEBUG = OFF 
#pragma config LVP = OFF 
#pragma config PWRT = OFF 
#pragma config PBADEN = OFF 

/////////////////////////////////// 

// defining PORTs 
//--------------------------------- 
#define ldata PORTD  
#define rs PORTCbits.RC0 
#define rw PORTCbits.RC1 
#define en PORTCbits.RC2 
#define send PORTBbits.RB1 //for sending 
#define rec PORTBbits.RB2 // for receiving 

void lcdcmd(unsigned char value) 
{ 
ldata = value; 
rs = 0; 
rw = 0; 
en = 1; 
Delay100TCYx(30); 
en = 0; 
} 


//Defining the LCD display 
//------------------------------------------ 
void lcddata(unsigned char value) 
{ 
ldata = value; 
rs = 1; 
rw = 0; 
en = 1; 
Delay100TCYx(30); 
en = 0; 
} 


// LCD initialization 


void lcd_ini() 
{ 
lcdcmd(0x38); //LCD matrix 
Delay10KTCYx(75); 
lcdcmd(0x0E); //Blink curser 
Delay10KTCYx(4); 
lcdcmd(0x01); //Clear LCD 
Delay10KTCYx(4); 
lcdcmd(0x06);  //increment curser 
Delay10KTCYx(4); 
lcdcmd(0x80); //start at firs line, first position 
Delay10KTCYx(4); 
} 



// define serial communication (receiving data from GPS) 
//--------------------------------------- 
unsigned char rx_data(void); //receiving data function 
void tx_data(unsigned char serial_data);  // Transmit data function 

void display(unsigned char array[], int size); // to display any string on LCD 
void transmit(unsigned char array2[], int size2); // to send any string to TX pin 

unsigned char ch=0; 
int i=0,j,k; 
unsigned char welcome[]=" Welcome "; 
unsigned char choice[]="Send or receive? "; 
unsigned char sending[]=" Sending .. "; 
unsigned char waiting[]=" Waiting for SMS"; 
unsigned char sms_received[]=" SMS Received "; 
unsigned char character_set[]="AT+CSCS=\"GSM\"\r"; 

unsigned char sms_format[]="AT+CMGF=1\r"; // TEXT mode 
unsigned char sms_write[]="AT+CMGS=\"XXXXXXXXXX\"\r"; // 10-Digit Mobile Number 
unsigned char sms[]="Hello\r"; 
unsigned char sms_report[]="SMS Sent..."; 
unsigned char sms_indication[]="AT+CNMI=1,2,0,0,0\r"; 

unsigned char stringArray[6]; //to store the received sms information 
unsigned char sms_terminate=0x1A; //Ctrl+z 
unsigned char enter=0x0D; // Enter Key 


// ** The Main Function ** 


void main() 
{ 

TRISD = 0; //Set it output for the LCD 
TRISCbits.RC0 = 0; //Set it output for RS 
TRISCbits.RC1 = 0; //Set it output for RW 
TRISCbits.RC2 = 0; //Set it output for en 
TRISBbits.RB1 = 1; // To send an SMS 
TRISBbits.RB2= 1; // To Receive an SMS 


// serial communicatin configuration 

SPBRG= 18; //Fill SPBRG register to set the baud rate 
RCSTAbits.SPEN=1;  // To activate Serial port (TX and RX pins) 
TXSTAbits.TXEN=1;  // To enable transmission 
RCSTAbits.CREN=1; // To enable continuous reception 
PIR1bits.RCIF =0; // clear rcif interrupt flag 
PIE1bits.RCIE=1; 
INTCONbits.PEIE =1; // enable peripheral interrupt 
INTCONbits.GIE =1; // enable global interrupt 


lcd_ini(); // LCD Initializing 
Delay10KTCYx(250); 

    display(welcome, sizeof(welcome)); //display " Welcome " 
    Delay10KTCYx(250); 
Delay10KTCYx(250); 
    Delay10KTCYx(250); 



    while(1) 
    {  
     lcdcmd(0x01); // clear LCD 
     lcdcmd(0x80); // Start at first line first position 
Delay10KTCYx(250); 
     display(choice, sizeof(choice)); // display " Send or Receive ? " 
     Delay10KTCYx(250); 


      if(send==1) // if sending switch is pressed 
      { 
      lcdcmd(0x01); 
      lcdcmd(0x80); 
      Delay10KTCYx(250); 
      display(sending, sizeof(sending)); // display "sending.." 
      Delay10KTCYx(250); 

      transmit(sms_format, sizeof(sms_format)); // send to GSM (set Text mode) 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 
      transmit(character_set, sizeof(character_set)); // send to GSM 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 
      transmit(sms_write, sizeof(sms_write)); // send to GSM (to write phone number) 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 
      transmit(sms, sizeof(sms)); // send to GSM (to write the sms) 
      tx_data(sms_terminate); // ctrl+z 

      Delay10KTCYx(250); 
      Delay10KTCYx(250); 
       tx_data(enter); // enter Key 
       lcdcmd(0x01); 
      lcdcmd(0x80); 
      Delay10KTCYx(250); 
      display(sms_report, sizeof(sms_report)); // display "SMS Sent .." 
      Delay10KTCYx(250); 
      Delay10KTCYx(250); 

       } 

     if(rec==1) //if Receiving switch is pressed 
      { 

      lcdcmd(0x01); 
      lcdcmd(0x80); 
      Delay10KTCYx(250); 
      display(waiting, sizeof(waiting)); // display " waiting for sms" 
      transmit(sms_format, sizeof(sms_format)); // to set the mode   
      Delay10KTCYx(250); 
      Delay10KTCYx(250); 
      Delay10KTCYx(250); 

      transmit(sms_indication, sizeof(sms_indication)); // to choose how sms arrive 
       Delay10KTCYx(250); 
      Delay10KTCYx(250); 
      Delay10KTCYx(250); 

      ch= rx_data(); /* read char from serial port */ 
       for(k=0; k<46; k++) 
      { 
        stringArray[k] = ch; 
      } 





      // --- By this point the SMS is Received and stored in Array -- 

       lcdcmd(0x01); 
      lcdcmd(0x80); 
      Delay10KTCYx(250); 
      display(sms_received, sizeof(sms_received)); // display (sms received) 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 



      /*--- display SMS on LCD ---*/ 
       lcdcmd(0x01); 
      lcdcmd(0x80); 
      Delay10KTCYx(250); 
      display(stringArray, sizeof(stringArray)); // display the text that received 
       Delay10KTCYx(250); 
       Delay10KTCYx(250); 



     } 
} //end while1 


}// end main function 

//------ methods --------- 

unsigned char rx_data(void) //Receive data from RX pin 
{ 
while(PIR1bits.RCIF==0); // Wait until RCIF gets low 
return RCREG; // Store data in Reception register 
} 

void tx_data(unsigned char serial_data)  // Transmit data through TX pin 
{ 
    TXREG=serial_data; 
    while(PIR1bits.TXIF==0); 
} 

void display(unsigned char array[], int size) //to display any string on LCD 
{ 
    for(i=0; i<size; i++) 
     { 
     lcddata(array[i]); 
     } 
} 

void transmit(unsigned char array2[], int size2) // to send strings to tx_data method 
{ 
    for(i=0; i<size2; i++) 
    { 
     tx_data(array2[i]); 
     Delay10KTCYx(250); 
    } 
} 
+0

您是否已驗證您是否正確使用正確的波特率在USART上發送數據? – mgm

+0

某些日誌輸出對我們有用,對您有所幫助 – aldridmc

回答

0

兩件事情......

不要假設調制解調器做了一些沒有閱讀它的響應。發送一個命令,然後通過讀取它的響應來確認。例如;你「發送和短信」,但不要ping調制解調器的響應。啓動發送SMS命令後程序是否處於空閒狀態?你看到它用+ OK迴應嗎?

此外,您需要發送相當於CTRL-Z而不是\ r發送短信。