1. GSM Modem Interfacing with PIC
Microcontroller
Hi to all...
I am doing a project in which i will be interfacing a gsm modem with PIC
microcontroller(16f877).. when i connect the gsm modem with PC i can able to send and
read the messages in the sim using AT commands in hyperterminal...Then i iniatized UART
in PIC send and received data to PC through hyperterminal its working properly...
When i interfaced PIC with GSM Modem i can able to send AT command to GSM modem but
the reply "OK " from GSM modem to PIC is not received by PIC...
Example:
AT (Enter)-----In Hyperterminal
OK ------Message from GSM Modem
Compiler:MikroC
Baud Rate:9600 bps
GSM MOdem:Wavecom HT-W2101
I can able to send AT and Enter command from UART to PIC... But i cant able see the reply
from GSM modem... but it works well when i interface GSM modem with PC hyperterminal...
Help me...
/*Header*****************************************************
Microcontroller=16f877
Clock=10 Mhz
Project Title:Air Pollution Control Using GSM/GPRS
Developed By:Sathiesh Kumar.V
Date:31/01/2010*/
void Calculation(unsigned int ) ;
void Gsm_SMS_Init();
void Gsm_SMS_Read();
void Gsm_SMS_Send();
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
2. sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
unsigned char ch; //
char Receive;
unsigned int adc_rd; // Declare variables
char *text; //
long tlong; //
unsigned int i,j;
char return_gsm[2];
char Gsm_Receive[100];
char Gsm_Msg[10];
char uart_rd[]="AT";
char uart_rd1[]="AT +CMGF=1";
char uart_rd2[]="AT +CMGR=1" ;
char uart_rd3[]="AT +CMGS=";
char Mobile_No[]="+919444721638" ;
char Send_Msg[]="@ON";
void main()
{
INTCON = 0; // All interrupts disabled
ADCON1 = 0x82; // A/D voltage reference is VCC
TRISA = 0xFF; // All port A pins are configured as inputs
TRISC=0x01;
Delay_ms(2000); // Rest of pins are configured as digital
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
Lcd_Init(); // LCD display initialization
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR); // LCD command (clear LCD)
text = "Air Pollution "; // Define the first message
Lcd_Out(1,1,text); // Write the first message in the first line
delay_ms(500);
text = "Sensor Value"; // Define the second message
Lcd_Out(2,1,text); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
text = "GSM"; // Define the third message
while (1)
{
3. Lcd_Out(1,1,text); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
Gsm_SMS_Init();
}
}
void Gsm_SMS_Init()
{
if (UART1_Tx_Idle() == 1)
{
for(i=0;i<=1;i++)
{
UART1_Write(uart_rd); //AT
}
UART1_Write(0x0D); //Enter
Lcd_Out(2,1,"AT"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
//delay_ms(5000);
while(1)
{
if (UART1_Data_Ready())
{ // If data is received,
for(i=0;i<=1;i++)
{
return_gsm = UART1_Read(); // read the received data, Received OK
}
Lcd_Out(2,1,"OK"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
/* UART1_Write(return_gsm[0]);
Lcd_Out(2,1,"OK RESEND"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR); */
// UART1_Write(return_gsm[1]);
}
}
/*if(return_gsm[0]==0x4F && return_gsm[1]==0x4B)
{
if (UART1_Tx_Idle() == 1)
{
for(i=0;i<=8;i++)
4. {
UART1_Write(uart_rd1); //AT+CMGF=1
}
UART1_Write(0x0D); //Enter
Lcd_Out(1,1,"AT+CMGF=1");
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
}
if (UART1_Data_Ready())
{ // If data is received,
for(i=0;i<=1;i++)
{
return_gsm = UART1_Read(); // read the received data,OK
}
Lcd_Out(2,1,"Received OK");
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
}
} */
}
}
Code (text):
/*Header*****************************************************
Microcontroller=16f877
Clock=10 Mhz
Project Title:Air Pollution Control Using GSM/GPRS
Developed By:Sathiesh Kumar.V
Date:31/01/2010*/
void Calculation(unsigned int ) ;
void Gsm_SMS_Init();
void Gsm_SMS_Read();
void Gsm_SMS_Send();
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
5. unsigned char ch; //
char Receive;
unsigned int adc_rd; // Declare variables
char *text; //
long tlong; //
unsigned int i,j;
char return_gsm[2];
char Gsm_Receive[100];
char Gsm_Msg[10];
char uart_rd[]="AT";
char uart_rd1[]="AT+CMGF=1";
char uart_rd2[]="AT+CMGR=1" ;
char uart_rd3[]="AT+CMGS=";
char Mobile_No[]="+919444721638" ;
char Send_Msg[]="@ON";
void main()
{
INTCON = 0; // All interrupts disabled
ADCON1 = 0x82; // A/D voltage reference is VCC
TRISA = 0xFF; // All port A pins are configured as inputs
TRISC=0x80;
Delay_ms(2000); // Rest of pins are configured as digital
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
Lcd_Init(); // LCD display initialization
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR); // LCD command (clear LCD)
text = "Air Pollution "; // Define the first message
Lcd_Out(1,1,text); // Write the first message in the first line
delay_ms(500);
text = "Sensor Value"; // Define the second message
Lcd_Out(2,1,text); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
text = "GSM"; // Define the third message
while (1)
{
Lcd_Out(1,1,text); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
Gsm_SMS_Init();
}
}
void Gsm_SMS_Init()
{
if (UART1_Tx_Idle() == 1)
{
Lcd_Out(2,1,"AT"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
6. for(i=0;i<=1;i++)
{
UART1_Write(uart_rd[i]); //AT
}
UART1_Write(0x0D); //Enter
//delay_ms(5000);
while(1)
{
Lcd_Out(2,1,"In while"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
if (UART1_Data_Ready())
{ // If data is received,
Lcd_Out(2,1,"In IF"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
for(i=0;i<=1;i++)
{
return_gsm[i] = UART1_Read(); // read the received data, Received
OK
}
Lcd_Out(2,1,"OK"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
/* UART1_Write(return_gsm[0]);
Lcd_Out(2,1,"OK RESEND"); // Define the first message
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR); */
// UART1_Write(return_gsm[1]);
}
}
/*if(return_gsm[0]==0x4F && return_gsm[1]==0x4B)
{
if (UART1_Tx_Idle() == 1)
{
for(i=0;i<=8;i++)
{
UART1_Write(uart_rd1[i]); //AT+CMGF=1
}
UART1_Write(0x0D); //Enter
Lcd_Out(1,1,"AT+CMGF=1");
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
}
if (UART1_Data_Ready())
{ // If data is received,
for(i=0;i<=1;i++)
{
return_gsm[i] = UART1_Read(); // read the received data,OK
7. }
Lcd_Out(2,1,"Received OK");
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
}
} */
}
}
Code (text):
void sms_send();
void sms_del();
void sms_read();
char sms[11];
char output[65];
unsigned char* MSG4 = "AT+CNMI=2,1,0,0,0";
void main() {
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
// sms_del();
sms_read();
}
void sms_send()
{
int i;
/*UART1_Write_Text("ATnr");
delay_ms(500);
UART1_Write_Text("AT+CMGF=1nr");
delay_ms(500); */
UART1_Write_Text("AT+CMGS=");
UART1_Write(0x22);
UART1_Write_Text("+919444721638");
UART1_Write(0x22);
UART1_Write_Text("nr");
delay_ms(500);
for(i=0;i<64;i++)
{
if(output[i]=='0') break;//ask for null character of sms vector when find it go out
of for() loop
UART1_Write(sms[i]);
}
delay_ms(500);
UART1_Write(26);// send ctrl + Z
return;
}
void sms_del()
{
UART1_Write_Text("AT+CMGD=1nr");
delay_ms(500);
}
8. void sms_read()
{
int i;
//char sms[11];
UART1_Write_Text("ATnr");
delay_ms(500);
UART1_Write_Text(MSG4); //cpms
Delay_ms(1000);
UART1_Write(0X0D); // ENTER
Delay_ms(1000);
UART1_Write_Text("AT+CMGF=1nr");
delay_ms(500);
UART1_Write_Text("AT+CMGR=7nr");
//delay_ms(500);
while (UART1_Data_Ready() != 1);
UART1_Read_Text(output, "OK", 65); // reads text until 'OK' is found
sms_send();
//return;
}