Not able to communicate with gsm module from controller pic16f877a

Status
Not open for further replies.

rangerskm

Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Activity points
2,663
HI FRIENDS,

I tried to interface sim300 module with pic16f877a but no response given in gsm module. i dont know whats the error in it. i want to clear some points in it . it will be thank full if any one helped me in it.

1) the modem is RS232 protocol and pic16f877a has also db9 port to connect with rs232 cable.so i soldered the pins(2-RX,3-TX,5-GROUND) of db9 ports of pic development board and sim300 modem.then i convert the rs232 signals to ttl logic through max232 ic which is present in the development and finally connected to rx and tx pin(25 and 26) of controller .when i tried to send message from controller to sim300 module .no response in it..i checked the db9 port of development with hyper terminal it shows some unknown characters in it.

please say mistake in it.how to check whether the output signals from controller is reaching gsm modem???

2)i want to known the correct baud rate value in pic16f877a.

3) shall we connect direct tx ,rx pins of db9 port of gsm module to pic rx and tx pins??if connected will it work??

4)is there any noise in 25th and 26th pin of pic as we made these pins as input pins??do these noise displaying as unknown characters??
 

If your modem provides DB9 connector (RS232 using MAX232) then you need to put another MAX232 between uC and modem's DB9 connector else uC will be damaged. If modem provides TTL Rx and Tx pins then cross the wires and connect it directly to uC UART pins.
 

bro,i am having another max232 in my development board .i taken the outputs from the max232 in development board to the controller .but not able to recognise the error in it.
 

Rx pin should be input pin and Tx pin output pin. Zip and post your project files. You can only have a certain baudrate(s) for a particular Fosc.
 
Last edited:
Code:
#include<htc.h>
#include<stdio.h>
 
//#fuses HS,NOWDT,NOPROTECT,NOLVP
//#use delay(clock=20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
/////////////
#define enter 0x0d
#define ctrlz 0x1a

unsigned int time;

void initialisemodem();
void deleteallmsg();
void sendtestmsg();
void send_data();
void snumber();			//stored number
void message_read();
void delay(int);

 
void main()
{
 
     TRISB=0x00;
     PORTB=0x00;
    	TRISC=0xFF;  // PORTC is input  
	 TXSTA=0b00100100;  // Configure serialport
     RCSTA=0b10010000;  // Configure serialport
     SPBRG=25;  // Configure serialport
/////////////////////////
//INTCON=0b00001111;
//PIE=0b00001111;



initialisemodem();
delay(1000);



deleteallmsg();
delay(1000);



sendtestmsg();
delay(1000);


time=0;
    
}
/////////////////////

void delay(int time)
{
int i,j;
for(j=1;j<=time;j++)
{
for(i=0;i<=64;i++)
{
}
}
}
void initialisemodem()
{
TXREG='A';			//change modem to text mode
delay(5);
TXREG='T';
delay(5);
TXREG='+';
delay(5);
TXREG='C';
delay(5);
TXREG='M';
delay(5);
TXREG='G';
delay(5);
TXREG='F';
delay(5);
TXREG='=';
delay(5);
TXREG='1';
delay(5);
TXREG=enter;
delay(5);
}


void snumber()
{
TXREG='A';			
delay(5);
TXREG='T';
delay(5);
TXREG='+';
delay(5);
TXREG='C';
delay(5);
TXREG='M';
delay(5);
TXREG='G';
delay(5);
TXREG='S';
delay(5);
TXREG='=';
delay(5);

TXREG=0x22;
delay(5);
TXREG='8';
delay(5);
TXREG='8';
delay(5);
TXREG='9';
delay(5);
TXREG='1';
delay(5);
TXREG='1';
delay(5);
TXREG='6';
delay(5);
TXREG='6';
delay(5);
TXREG='8';
delay(5);
TXREG='8';
delay(5);
TXREG='0';
delay(5);
TXREG=0x22;
delay(5);

TXREG=enter;
delay(5);

}			
/////////////


void deleteallmsg()
{
TXREG='A';			//DELETE ALL MESSAGES
delay(5);
TXREG='T';
delay(5);
TXREG='+';
delay(5);
TXREG='C';
delay(5);
TXREG='M';
delay(5);
TXREG='G';
delay(5);
TXREG='D';
delay(5);
//TXREG='A';
//delay(5);
TXREG='=';
//delay(5);
TXREG='1';
delay(5);
/*TXREG='D';
delay(5);
TXREG='E';
delay(5);
TXREG='L';
delay(5);
TXREG=' ';
delay(5);
TXREG='A';
delay(5);
TXREG='L';
delay(5);
TXREG='L';
delay(5);
TXREG=0x22;
delay(5);
*/
TXREG=enter;
delay(5);
}
//////////////////////

void sendtestmsg()
{
snumber();

TXREG='T';
delay(5);
TXREG='E';
delay(5);
TXREG='S';
delay(5);
TXREG='T';
delay(5);
TXREG=ctrlz;
delay(5);
}			

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

void message_read()
{
TXREG='A';
delay(5);
TXREG='T';
delay(5);
TXREG='+';
delay(5);
TXREG='C';
delay(5);
TXREG='M';
delay(5);
TXREG='G';
delay(5);
TXREG='R';
delay(5);
TXREG='=';
delay(5);
TXREG='1';
}

I dont know whether this code works or not .but it builds sucessfully.i just need to send a sample message from the gsm module through controller to my mobile.
please find the mistakes in it and try to help me in this..
actually my project is to control an led by sending a message from mobile.before that i have to test the connections in it..
 

Your code will not work.

Code C - [expand]
1
TRISC = 0x80

should be used as Rx pin is input pin. While(1) loop is missing. See code at this link. https://saeedsolutions.blogspot.in/2012/11/pic16f877-uart-code-proteus-simulation.html

Try to write a new code and post if you face any problem.


Edit:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Author: Jayanth Devarayanadurga 
 * 
*/
 
#define _XTAL_FREQ 16000000  
 
#include <htc.h> 
#define BAUDRATE 9600  
 
    __CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON 
            & LVP_OFF & CPD_OFF & DEBUG_OFF);
        
void interrupt ISR(void);
void UART_Init(void);
void UART_Write(unsigned char);
unsigned char UART_Read(void);
void UART_Write_Text(const unsigned char*);
void delaySec(unsigned int sec);
void sendSMS(unsigned char *phoneNo);
 
unsigned char uart_rd[20], k = 0, m = 0;
 
void interrupt ISR(void)
{
    if(RCIF)  
    {
        if(OERR) 
        {
            CREN = 0;
            CREN = 1;
        }
 
        uart_rd[m] = RCREG;
            
        if(uart_rd[m] == '"'){
                k++;
                m = 0;
                
        }
 
        if(k == 6){
 
            uart_rd[m] = '\0';
            k = 0;
            m = 0;
        }
            
    }
}
 
void UART_Init(void)
{
    TRISC = 0x80;                       
    
    TXSTA = 0x24;
    RCSTA = 0x90;
    
    SPBRG = 0x67;       //Fosc = 16 MHz                     
    //SBBRG = 0x81;     //Fosc = 20 MHz
}
 
 
void UART_Write(unsigned char uartData)  
{
    while(!TXIF);  
    TXREG = uartData;
}
 
unsigned char UART_Read(void)  
{
    if(OERR) 
    {
        CREN = 0;
        CREN = 1;
    }
    
    while(!RCIF);  
    
    return RCREG;
}
 
void UART_Write_Text(const unsigned char *uartString)
{
    while(*uartString)
        UART_Write(*uartString++);
}
 
 
void delaySec(unsigned int sec){
    
      unsigned int i = 0;
      
      for(i = 0; i < sec; i++){
          
           __delay_ms(250);
           __delay_ms(250);
           __delay_ms(250);
           __delay_ms(250);
      }
    
}
 
void sendSMS(unsigned char *phoneNo){
 
        UART_Write_Text("AT");
        UART_Write(0x0D);
        UART_Write(0x0A);
        delaySec(2);
        UART_Write_Text("AT+CMGF=1");
        UART_Write(0x0D);
        UART_Write(0x0A);
        delaySec(2);
        UART_Write_Text("AT+CMGS=");
        UART_Write(0x22);
        UART_Write_Text(phoneNo);
        UART_Write(0x22);
        UART_Write(0x0D);
        UART_Write(0x0A);
        delaySec(3);
        UART_Write_Text("SMS from RangerSkm");
        UART_Write(0x0D);
        UART_Write(0x1A);
        UART_Write(0x0D);
}   
 
void main(){
    
        TRISC = 0x80;
            
        UART_Init();
        __delay_ms(100);
        
        GIE  = 1;                           
        PEIE = 1;
                
        while(1){
            
            
            sendSMS("0000000000");      //"8891166880"
            delaySec(10);
            
        }
    
}

 
Last edited:
Code:
//==========================================================================

//	Project description		: GSM Enabled Smart Home.
//							  This sample source code is valid for 
//							  20MHz crystal.
//
//==========================================================================

//	include
//==========================================================================
#include <pic.h>
#include <string.h>

//	configuration
//==========================================================================
//__CONFIG ( 0x3F32 );				//configuration for the  microcontroller

//	define
//==========================================================================
#define	LED2			RB3				//RS pin of the LCD display
#define	LED1			RB4				//RS pin of the LCD display
#define	rs			RB5				//E pin of the LCD display

#define	lcd_data	PORTD			//LCD 8-bit data PORT

#define	SW1			RB0			
#define	SW2			RB1			

#define	rw		RB6			
#define	e		RB7				

//	function prototype				(every function must have a function prototype)
//==========================================================================

void delay(unsigned long data);			

void send_config(unsigned char data);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);

unsigned char uart_rec(void);			//receive uart value
void uart_send(unsigned char data);
void uart_str(const char *s);

void gsm_send_command(const char *command);
void gsm_read_line(char *buffer);


//	global variable
//==========================================================================

char gsm_response[20];
char command[10];


//	main function					(main fucntion of the program)
//==========================================================================
void main()
{
	char read_sms_command[] = "AT+CMGR=1";
	char delete_sms_command[] = "AT+CMGD=1";
	
	
	//set I/O input output
	TRISB = 0b00000011;					//configure PORTB I/O direction
	TRISD = 0b00000000;					//configure PORTD I/O direction
	TRISA = 0b00000111;					//configure PORTA I/O direction
	
	
	LED1=0;								// OFF LED1
	LED2=0;								// OFF LED2

	//Configure UART
	SPBRG=10;			//set baud rate as 115200 baud
	BRGH=1;				//baud rate high speed option
	TXEN=1;				//enable transmission
	TX9 =0;				//8-bit transmission
	RX9 =0;				//8-bit reception	
	CREN=1;				//enable reception
	SPEN=1;				//enable serial port

	//setup ADC
	ADCON1 = 0b00000110;				//set ADx pin digital I/O
	
	//configure lcd
	send_config(0b00000001);			//clear display at lcd
	send_config(0b00000010);			//lcd return to home 
	send_config(0b00000110);			//entry mode-cursor increase 1
	send_config(0b00001100);			//display on, cursor off and cursor blink off
	send_config(0b00111000);			//function set

	//display startup message	
	lcd_clr();							//clear lcd
	send_string("wind magic system.");		//display "Cytron Tech."
	lcd_goto(20);						//set the lcd cursor to location 20
	send_string("Smart Home");			//display "Smart Home"

	// Delay for a while.
	delay(100000);
	
	
	
	// Clear the LCD and display the new message.
	lcd_clr();
	send_string("Testing Com...");
	
	// Make sure we can communicate with the GSM modem.
	gsm_send_command("AT");
	
	// Read the response.
	gsm_read_line(&gsm_response);
	
	// We should receive "OK" from the GSM modem.
	// If we don't, display "Error".
	if (memcmp("OK", &gsm_response, 2) != 0)
	{
		lcd_clr();
		send_string("Error...");
		while(1);
	}	
	
	
	
	// Clear the LCD and display the new message.
	lcd_clr();
	send_string("Waiting for");
	lcd_goto(20);
	send_string("Call Ready");
	
	// Waiting for the GSM modem to search for the network.
	// We will receive a blank line before receiving "Call Ready".
	gsm_read_line(&gsm_response);
	gsm_read_line(&gsm_response);
	
	// We should receive "Call Ready" from the GSM modem.
	// If we don't, display "Error".
	if (memcmp("Call Ready", &gsm_response, 10) != 0)
	{
		lcd_clr();
		send_string("Error...");
		while(1);
	}	
	
	
	
	// Clear the LCD and display the new message.
	lcd_clr();
	send_string("Setting Text");
	lcd_goto(20);
	send_string("Mode...");
	
	// Set text mode for SMS.
	gsm_send_command("AT+CMGF=1");
	
	// Read the "OK".
	gsm_read_line(&gsm_response);
	
	
	
	// Clear the LCD and display the new message.
	lcd_clr();
	send_string("Ready");
	
	

	while(1)
	{
		// Check whether there is new data from the GSM modem.
		if (RCIF == 1)
		{
			// Check is there a new SMS?
			gsm_read_line(&gsm_response);
			if (memcmp("+CMTI: \"SM\",", &gsm_response, 12) == 0)
			{
				// Get the SMS index.
				// The command array is already initialized as "AT+CMGR=1" and "AT+CMGD=1",
				// we need to changed the index to the actual one.
				read_sms_command[8] = gsm_response[12];
				delete_sms_command[8] = gsm_response[12];
				
				// Clear the LCD and display the new message.
				lcd_clr();
				send_string("Reading SMS...");
				
				// Send command to read the SMS.
				gsm_send_command(&read_sms_command);
				
				// Read the response.
				// The first line is the SMS info which we don't need.
				// We only need the second line which is the message content.
				gsm_read_line(&gsm_response);
				gsm_read_line(&command);
				
				// Read the newline and "OK".
				gsm_read_line(&gsm_response);
				gsm_read_line(&gsm_response);
				
				
				
				// Switch on the light if we received "on".
				if (memcmp("on", &command, 2) == 0)
				{
					LED1 = 1;
					LED2 = 1;
				}	
				
				// Switch off the light if we received "off".
				else if (memcmp("off", &command, 3) == 0)
				{
					LED1 = 0;
					LED2 = 0;
				}	
				
				
				
				// Send command to delete the SMS.
				gsm_send_command(&delete_sms_command);
				
				// Read the "OK".
				gsm_read_line(&gsm_response);
				
				
				
				// Clear the LCD and display the new message.
				lcd_clr();
				send_string("Ready...");
			}
		}	
		
		
		
		// Check whether SW1 is pressed.
		if (SW1 == 0)
		{
			// Clear the LCD and display the new message.
			lcd_clr();
			send_string("Sending SMS...");
			
			// Send the SMS to notify the owner.
			// Please change this to your own number.
			gsm_send_command("AT+CMGS=\"8891166390\"");
			
			// We should receive '>' from the modem.
			// If we don't, display "Error".
			if (uart_rec() != '>')
			{
				lcd_clr();
				send_string("Error...");
				while(1);
			}	
			
			// Send the message.
			uart_str("Warning: Switch 1 has been triggered !");
			
			// Send <CTRL+Z>.
			uart_send(0x1a);
			
			
			
			// Read the newline and response.
			gsm_read_line(&gsm_response);
			gsm_read_line(&gsm_response);
			
			// We should receive "+CMGS: <Message ID>" from the GSM modem.
			// If we don't, display "Error".
			if (memcmp("+CMGS:", &gsm_response, 6) != 0)
			{
				lcd_clr();
				send_string("Error...");
				while(1);
			}
			
			
			
			// Read the newline and "OK".
			gsm_read_line(&gsm_response);
			gsm_read_line(&gsm_response);
			
			
			
			// Clear the LCD and display the new message.
			lcd_clr();
			send_string("Ready...");
			
			
			
			// Wait until SW1 is released so that we don't send duplicated message.
			while (SW1 == 0);
			
		}	
	}
}

//	functions
//==========================================================================
void delay(unsigned long data)			//delay function, the delay time
{										//depend on the given value
	for( ;data>0;data--);
}

void send_config(unsigned char data)	//send lcd configuration 
{
	rs=0;
	rw=0;								//set lcd to configuration mode
	lcd_data=data;						//lcd data port = data
	e=1;								//pulse e to confirm the data
	delay(50);
	e=0;
	delay(50);
}

void send_char(unsigned char data)		//send lcd character
{
 	rs=1;
	rw=0;								//set lcd to display mode
	lcd_data=data;						//lcd data port = data
	e=1;								//pulse e to confirm the data
	delay(10);
	e=0;
	delay(10);
}

void lcd_goto(unsigned char data)		//set the location of the lcd cursor
{										//if the given value is (0-15) the 
 	if(data<16)							//cursor will be at the upper line
	{									//if the given value is (20-35) the 
	 	send_config(0x80+data);			//cursor will be at the lower line
	}									//location of the lcd cursor(2X16):
	else								// -----------------------------------------------------
	{									// | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| |
	 	data=data-20;					// | |20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| |
		send_config(0xc0+data);			// -----------------------------------------------------	
	}
}

void lcd_clr(void)						//clear the lcd
{
 	send_config(0x01);
	delay(600);	
}

void send_string(const char *s)			//send a string to display in the lcd
{          
  	while (s && *s)send_char (*s++);
}

unsigned char uart_rec(void)			//receive uart value
{
	unsigned char rec_data;
	while(RCIF==0);						//wait for data
	rec_data = RCREG;				
	return rec_data;					//return the data received
}

void uart_send(unsigned char data)
{	
	while(TXIF==0);				//only send the new data after 
	TXREG=data;					//the previous data finish sent
}


void uart_str(const char *s)
{
	while(*s)uart_send(*s++);
}


void gsm_send_command(const char *command)
{
	unsigned char rec_data;
	
	while (*command != 0)
	{
		uart_send(*command++);		// Send the AT command.
		rec_data = uart_rec();	// Read the echo.
	}	
	
	// Send <ENTER>.
	uart_str("\r\n");
	
	// Read the echo until Line Feed character is received.
	do {
		rec_data = uart_rec();
	} while (rec_data != '\n');	
}	



void gsm_read_line(char *buffer)
{
	unsigned char rec_data;
	
	// Read the data until Line Feed character is received.
	do {
		rec_data = uart_rec();
		*buffer++ = rec_data;
	} while (rec_data != '\n');	
}

can u try these and say the mistakes in it. i dont know the exact spbgr value ...

- - - Updated - - -

bro i tried your code .its compiled .but messages not sending .when i tried to view using hyper terminal some of the unknown characters are displaying.
 

Baudrate is not matching. I have used 9600 bps in my code and Fosc is 16 MHz. I have tested it and it works fine. Change baudrate in the PC terminal software you are using. If you are using 20 MHz Fosc then comment the SPBRG which is for 16 MHz and then uncomment SPBRG which is for 20 MHz.
 

bro,i got the correct message in hyper terminal .but i need to clarify some points in hadware.i got the correct output when i switched off the gsm modem,but when switch on the gsm module.the output displaying as above error.
what may be problem???
i connected between 2 db9 ports of gsm modem and pic development board by soldering 3 wires as 2nd pin to 2nd pin,3rd pin to third pin and 5th pin to 5th pin between db9 cable.is it cause trouble in sending commands to gsm module..???

kindly explain me about hardwares..

thanking you
 
Last edited:

Why don't you mention you Fosc and post your exact circuit? If you have connected uC to GSM modem then how are you connecting it to PC at the same time?

You cannot drive 2 devices (eg: GSM modem and PC) using single MAX232. Just connect the GSM modem and send SMS and see if you get the SMS. Rx and Tx pins of UC is floating. Try to pull-up Rx and Tx lines with 10k resistors.

**broken link removed**

Cross 2nd and 3rd pins of DB9 connectors i.e., cross the wires between the 2 DB9 connectors. RxD has to go to TxD and TxD to RxD. pin 5 is grounded on both sides.
 
Last edited:
fosc is 20 mhz.
i am not having circuit diagram.i having picture of sim300 modem with db9 port and pic development board with db9 port.in between these 2 db9 port i soldered both instead of using wires.
i am having diagram of pic development board and sim300 module
 

Attachments

  • PIC DEV BRD SCHEMATIC.pdf
    74.6 KB · Views: 136
  • p231_0_2.png
    108.7 KB · Views: 146

Just plug the DB9 connector of Dev board to DB9 connector of modem. Modem power supply is ok? It needs 1 to 2 A while sending SMS. So, you should have a 12V 2A DC power supply for modem.
 
bro,,modem power supply is ok i tested by connecting directly to computer.
its female db9 port in both sim and dev boards,i am not having male to male cable .only having male to female cable ...so dont know what to do.
 

bro, i got new male to male cable and connected gsm and pic development board,but no messages are receiving in mobile.
 

hi bro,,

sorry to disturb you again and again .i am having no other way..

then i tried with all available development boards with gsm interface ..same mistake i found.the hyperterminal viewing unknown characters.along with commands,i want to clarify some more points.i used the SPBRG value as 129(0x81) as you given,20 mhz crystal using in it.

1)whether the RC6 and RC7 pins are affected by any noise as they are configured as input pins??
2) is there any problem with SPBRG value ,will it differ from controller to controller??
3)guide me some points to rectify the mistakes in the gsm interface
 

If you use SPGRG value for 20 MHz then you have to change this

Code C - [expand]
1
#define _XTAL_FREQ 16000000

to

Code C - [expand]
1
#define _XTAL_FREQ 20000000

Rx pin should be set as input pin and Tx pin as output pin using TRISx.

Forget modem for a while. Connect uC board's DB9 connector to PC serial port or to USB port using serial to USB adaptor. See if you get right data in RealTerm. If yes then problem is with modem.
 
Last edited:
brother,

i defined the osc freq as 20 mhz as above.

i checked all uC boards with hyper terminal not displaying the exact commands,unknown characters are also displaying in hyper terminal,i checked the same with tera term ,same result.may be one or two time i got exact commands in hyper terminal,but not always.
i connected gsm modem directly with PC with rs232 cable and checked the AT commands it works fine.
 

thanks a lot by using your syntax i learned how to communicate controller to gsm thank you again

- - - Updated - - -

i am having one doubt sir what is the use of 0X0D,0X0A,0X1A,0X22 please reply . thanks in advance.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…