我想发送&使用GSM sim300调制解调器和Micro_controller(PIC18f4550)接收短信 我已经用PC(HyperTerminal)测试了GSM,它运行良好。 但现在我已经编写了一个小代码,以便我可以使用micro_controller发送和接收,但它不会发送任何东西!我不知道代码中的问题在哪里。 请看看并帮助我。
一月,
#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 :(得分:0)
两件事......
永远不要假设调制解调器在没有阅读它的情况下做了一些事情。发送命令,然后通过阅读它的响应进行确认。例如;你发送短信&#34;但不要ping调制解调器以进行响应。启动发送SMS命令后,程序是否处于空闲状态?你看到它回复+ OK?
此外,您需要发送等效于CTRL-Z而不是\ r来发送短信。