Pran Kumar
Newbie level 5

I am trying to send AT commands from PIC16f877a to sim900.I am trying to send each command ,then wait for the response and if response is received the response is cleared.Then the next command is sent.But i am facing problem when the response is cleared.The correct response is not being detected by the microcontroller.Please help me with the code.
char *p4;
#define MAXLEN 32
unsigned char buffer[MAXLEN],str[10] ;
volatile int Index1, CptErr,i1,full;
void send_AT();
void send_CSMINS() ;
void main()
{
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE = 1; //enable interrupt.
UART1_Init(9600);
send_AT(); //send at command
Delay_ms(1000);
memset(buffer, 0, sizeof(buffer)); //clear received response
send_CSMINS(); //send next at command
}
void interrupt()
{
unsigned char c1;
if (RCIF_bit)
{
if (RCSTA.OERR==1)
{
RCSTA.CREN = 0 ; // clear OERR flag
RCSTA.CREN = 1 ;
CptErr++;
c1=0; // empty buffer
}
else
if(RCSTA.FERR==1 )
{
CptErr++;
c1=0;
}
else
{
c1 = RCREG; // no errors
if (i1>31 ) //if maximum length of array exceeded
{
full=1;
i1=0; //next character stored as first element of array buffer[o]
buffer[i1]=c1;
i1++;
}
else
{
buffer[i1]=c1;
Index1=i1;
i1++;
}
}
}
}
void send_AT()
{
do
{
UART1_Write_Text("AT\r\n"); // send command string
Delay_ms(500);
p4 =strstr(buffer,"OK"); // check for response OK in buffer
}while(p4==0); // if null pointer received,OK not found, resend command
}
void send_CSMINS()
{
UART1_Write_Text("AT+CSMINS=1\r\n");
do
{ UART1_Write_Text("AT+CSMINS?\r\n"); Delay_ms(500); // send command string
p4 = strstr(buffer,"+CSMINS:1,1");
} while(p4==0);
}
[/CODE]
Code:
[CODE]
char *p4;
#define MAXLEN 32
unsigned char buffer[MAXLEN],str[10] ;
volatile int Index1, CptErr,i1,full;
void send_AT();
void send_CSMINS() ;
void main()
{
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE = 1; //enable interrupt.
UART1_Init(9600);
send_AT(); //send at command
Delay_ms(1000);
memset(buffer, 0, sizeof(buffer)); //clear received response
send_CSMINS(); //send next at command
}
void interrupt()
{
unsigned char c1;
if (RCIF_bit)
{
if (RCSTA.OERR==1)
{
RCSTA.CREN = 0 ; // clear OERR flag
RCSTA.CREN = 1 ;
CptErr++;
c1=0; // empty buffer
}
else
if(RCSTA.FERR==1 )
{
CptErr++;
c1=0;
}
else
{
c1 = RCREG; // no errors
if (i1>31 ) //if maximum length of array exceeded
{
full=1;
i1=0; //next character stored as first element of array buffer[o]
buffer[i1]=c1;
i1++;
}
else
{
buffer[i1]=c1;
Index1=i1;
i1++;
}
}
}
}
void send_AT()
{
do
{
UART1_Write_Text("AT\r\n"); // send command string
Delay_ms(500);
p4 =strstr(buffer,"OK"); // check for response OK in buffer
}while(p4==0); // if null pointer received,OK not found, resend command
}
void send_CSMINS()
{
UART1_Write_Text("AT+CSMINS=1\r\n");
do
{ UART1_Write_Text("AT+CSMINS?\r\n"); Delay_ms(500); // send command string
p4 = strstr(buffer,"+CSMINS:1,1");
} while(p4==0);
}
Code: