I am looking for c code of sending an sms from PIC16F877A to GSM module sim900 via RS232.
GSM responds well to all commands when connected to laptop via usb-rs232 cable. I have connected my cable as follows (2-3, and 3-2, 5-5).
I think the problem is with my code.
// Picc-Lite header file for processor special function registers and peripherals#include <pic.h>#include <htc.h>#include <stdlib.h>#include "uartUJ.h" //Uart Header file invoked#include "lcdUJ.h"void WriteToUsart(void);void ReadPortsValues(void);void InitializePorts(void);#define _XTAL_FREQ 4000000 //4MHz Oscillator#define cr 0x0D //cursor return to home position#define ff 0x0C //form feed// Configuration bit settings//__CONFIG(XT & WDTDIS & PWRTEN & BORDIS & LVPDIS & DUNPROT & WRTEN & DEBUGDIS & UNPROTECT);// Local Subroutine and Function declarationsvoid adc_init(){
ADCON1=0B10000000;//Right just, Fosc/8 conversion clock, Vref=5V
ADCON0=0B01000000;//Fosc/8 conv_clock for proper sampling of 1.6uS.
ADON=1;//Turning on the ADC module.}int adc_read(){int x=0;
GO=1;while(GO)continue;
x=ADRESH<<8;//Store 8-bit ADRESH and shift it 8 times so that it sits on bit8 to bit15
x =x|ADRESL;//Join ADRESL to ADRESH and store in Register x to form a 16-bit registerreturn x;//return the value of new register x to main loop for further processing and display.}/*const unsigned char mes5[]= "AT\r";const unsigned char mes6[]= "AT+CMGF=1\r";const unsigned char mes7[]= "AT+CMGS=\"+27797185004\"\r";const unsigned char mes8[]="0x01A";*///variable declarationsunsignedchar Vstr[5];int result,voltage;constunsignedchar mes1[]="AT";constunsignedchar mes2[]="AT+CMGF=1";constunsignedchar mes3[]="AT+CMGS=\"+27797185004\"";constunsignedchar mes4[]="JRA has been";constunsignedchar mes5[]="0x1A";//Send command (ctrl+z)constunsignedchar mes6[]="0x0d";//Enter command//Main Programvoid main(){while(!RA4){
InitializePorts();
uart_init();
ReadPortsValues();}}void InitializePorts(void){
PORTA=0;//Clear Porta
TRISA=0xFF;// Make Porta Digital
adc_init();// Convert Analog to Digital Converter}void ReadPortsValues(void){while(1){
result=adc_read();
PORTD=result;
voltage=result*5;itoa(Vstr,voltage,10);
WriteToUsart();}}// Writting a string on to a screenvoid WriteToUsart(void){//putch(ff);
putch (cr);//Take the cursor back to the first position
uart_putrs(mes1);//Write message1
putch('\n');
uart_putrs(mes6);
putch(cr);//Take the cursor back to the first position// uart_putrs(mes6);
__delay_ms(2000);
uart_putrs(mes2);
putch('\n');
uart_putrs(mes6);
putch('\n');//Take cursor to a new line
putch(cr);//Take the cursor back to the first position
__delay_ms(2000);
uart_putrs(mes3);
putch('\n');
uart_putrs(mes6);
putch('\n');//Take cursor to a new line
putch(cr);//Take the cursor back to the first position
__delay_ms(2000);
uart_putrs(mes4);
__delay_ms(2000);
putch('\n');
putch(cr);// __delay_ms(2000);
uart_putrs(mes5);
putch('\n');
putch(cr);
__delay_ms(2000);}
- - - Updated - - -
Hi Milan, even without bank switch, it does not send message to gsm from the pic
or you can send strings as literals: uart_putrs("AT\r\n"); //Write message1 for example.
Another thing to remember is the modem needs to synchronize autobauding so I would suggest that you either send AT twice or have a delay after AT before you send another command.
You should really check for the modem response after sending AT (which is \r\nOK\r\n).
Also check the hardware i.e. you have correct signal levels on the RX/TX pins of the modem as the SIM900 levels if I recall correctly are about 2.8 volts. If your PIC has different levels then you will need level shifting.