mayasunny
Member level 3
hie all, i'm working PIC16f877a micro controller(xc8 compiler,20MHz crystal and 9600 baudrate, Sim800 module), my project is by sending sms to GSM ,make PIc working according to sms, ex: if i sent sms as led on., led should turn on . I succeed in sending sms to GSM but the problem is GSM response is varying each time. please any suggestion to over come this problem.
</string.h></xc.h>
Code:
#pragma config FOSC = HS // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#include<string.h>
#define _XTAL_FREQ 20000000
char a[]="AT\r";
char echo[]="ATE0\r";
char sim[]="AT+CPIN?\r"; // To Check sim status
char mode_text[]="AT+CMGF=1\r"; // to set text mode
char message_rec[]="AT+CNMI=1,2,0,0,0\r"; // use to set recipient number and mesg
//char mesg[]="AT+CMGR=49\r"; // mesg we want to send
//char terminator=0x1A; // ctrl Z to send mesg
char buf,data[61],res,res1;
unsigned int i,j;
void UART_Init(int baudRate)
{
BRGH=0;
SYNC=0;
TXSTA = 0X20; // Asynchronous mode, 8-bit data & enable transmitter
RCSTA = 0X90; // Enable Serial Port and 8-bit continuous receive
SPBRG = 31; // baud rate @20MHz Clock
}
void UART_TxChar(char ch)
{
while(TXIF == 0); // Wait till the transmitter register becomes empty
TXIF = 0; // Clear transmitter flag
TXREG = ch; // load the char to be transmitted into transmit reg
}
void UART_str(char *ch) // to write data continously
{
for(i=0;ch[i]!=0;i++)
{
UART_TxChar(ch[i]);
}
}
__interrupt() void ISR(void)
{
int j,length = 61;
for(j = 0; j < length; j++)
{
while(RCIF == 0);
{
buf = RCREG; // Read The Received Data Buffer and clears RCIF flag
}
data[j] = buf; RCIE = 0;
}
}
void main(void)
{
char ch;
TRISD = 0X00;
PORTD = 0X00;
TRISC = 0x80; // Configure Rx pin(rc7) as input and Tx(rc6) as output
RCIE = 1; // UART RecIeving Interrupt Enable Bit
PEIE = 1; // Peripherals Interrupt Enable Bit
GIE = 1; // Global Interrupt Enable Bit
UART_Init(9600); // Initialize the UART module with 9600 baud rate
UART_str(a);
__delay_ms(2);
UART_str(echo);
__delay_ms(2);
UART_str(sim);
__delay_ms(2);
UART_str(mode_text);
__delay_ms(2);
UART_str(message_rec);
__delay_ms(2);
while(1)
{
}
return;
}
Last edited by a moderator: