Please help! in the at_cmgl function it keeps looping back. I dont know whats going with this, all other functions work but the at_cmgl doesnt. Please can anyone tell me whats wrong with my at_cmgl function?
Program:
#include <ez8.h>
#include <sio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TESTING 0
#define MSG_LEN 250
unsigned int msg_index,info_index;
// const unsigned int SIM=40;
char msg[MSG_LEN],cnum[]="+xxxxxxxxxxx"; //cnum is the target number for sending SMS
void clear_msg(void){ //clear string for storage
int i;
for(i=0;i<MSG_LEN;i++) msg='\0';
msg_index=0;
}
void delay(int loop){ // software delay
int i,j,k;
for (i=loop; i>=0; i--) for (j=0;j<0xFF;j++) for (k=0;k<0xFF;k++);
}
void init_port(void){
PCDD &= ~0xFF; // set Port C as output
PCAF &= ~0xFF; // set alternate function off
PCOC &= ~0xFF; // push-pull mode
PCHDE |= 0xFF; // high drive enabled
PBDD |= 0xFF; // set Port B as input
PBAF &= ~0xFF; // set alternate function off
PDDD &= ~0x03; //set data direction to output
PDAF &= ~0x03; //turn alternate function off
PDOC &= ~0x03; //set output control to push-pull
PDHDE |= 0x03; //set high-drive enable on
PDOUT |= 0x03; //set initial output to high
}
#pragma interrupt // UART0 interrupt service routine
void read_uart0(void){
if (U0STAT0 & 0x80) msg[msg_index++]=U0RXD; //Read UART Register and store in a character array
}
void initial_uart0(void){ //UART0 for GSM MODULE (PA4:RX, PA5;TX)
DI();
SET_VECTOR(UART0_RX, read_uart0);
IRQ0ENH |= 0x10;
IRQ0ENL |= 0x10;
IRQ0 &= ~0x10;
U0CTL1 &= ~0x02;
EI();
}
void at_at(void){ //Test for response
select_port(_UART0);
clear_msg();
printf("AT\n");
delay(1);
if(TESTING){select_port(_UART1); printf("%s", msg); }
delay(1);
}
void at_cnmi(int setting){ //Set new message indications
do{
select_port(_UART0);
clear_msg();
printf("AT+CNMI=2,%d,0,0,0\n",setting);
delay(1);
if(TESTING){select_port(_UART1); printf("%s", msg); }
delay(1);
}while(strstr(msg,"ERROR")!=NULL); //Sometimes the module will return an ERROR even if the command is valid.
//Loop until command is accepted
}
void init_gsm(void){ //Initialize GSM module
delay(20); //Delay for GSM module boot up
PBOUT |= 0x01; //Trigger GSM module to power up
delay(20);
PBOUT &= ~0x01;
delay(1);
at_at();
do{
select_port(_UART0);
clear_msg();
printf("AT+CFUN=1\n"); //Set GSM module functionality to full
delay(1);
if(TESTING){select_port(_UART1); printf("%s", msg); }
delay(1);
}while(strstr(msg,"ERROR")!=NULL); //Check if in ERROR; if in ERROR, try again
at_at(); //Sometimes, GSM module could not recognize the next command, so an AT is buffered here
do{
select_port(_UART0);
clear_msg();
printf("AT+CMGF=1\n"); //Set message format to text
delay(1);
if(TESTING){select_port(_UART1); printf("%s", msg); }
delay(1);
}while(strstr(msg,"ERROR")!=NULL);
do{
select_port(_UART0);
clear_msg();
printf("AT+CSDH=0\n"); //Disable detailed SMS header
delay(1);
if(TESTING){select_port(_UART1); printf("%s", msg); }
delay(1);
}while(strstr(msg,"ERROR")!=NULL);
do{
select_port(_UART0);
clear_msg();
printf("AT+COPS=0\n"); //Check service provider signal availability
delay(1); //Wait for reply
if(TESTING){select_port(_UART1); printf("%s", msg); }
delay(1);
}while(strstr(msg,"ERROR")!=NULL); //Will loop forever if no service provider signal is available
do{
select_port(_UART0);
clear_msg();
printf("AT+CPMS=\"SM\",\"SM\",\"SM\"\n"); //Set preferred memory storage (to SIM)
delay(1);
if(TESTING){select_port(_UART1); printf("%s", msg); }
delay(1);
}while(strstr(msg,"ERROR")!=NULL);
//at_cnmi(0); //Set new message indications (Disabled)
clear_msg();
}
void at_cmgs(char *data){ //Function for sending SMS
at_at();
do{
clear_msg();
select_port(_UART0);
printf("AT+CMGS=\"%s\"\n",cnum);
delay(1);
//if (TESTING) {select_port(_UART1); printf("%s",msg);}
delay(1);
clear_msg();
select_port(_UART0);
printf("%s",data); putch(0x1A);
//while(strstr(msg,"ERROR")==NULL && strstr(msg,"OK")==NULL)
delay(1); //Wait for reply
//if (TESTING) {select_port(_UART1); printf("%s",msg);}
delay(1);
}while(strstr(msg,"ERROR")!=NULL); //Resend SMS if sending failed
clear_msg();
}
void relay1_on(){
PDOUT |= 0x01; //PD0 is ON(HIGH)
at_cmgs("LED D0 is ON");
}
void relay2_on(){
PDOUT |= 0x02; //PD1 is ON(HIGH)
at_cmgs("LED D1 is ON");
}
void relay1_off(){
PDOUT &= ~0x01; //PD0 is OFF(LOW)
at_cmgs("LED D0 is OFF");
}
void relay2_off(){
PDOUT &= ~0x02; //PD1 is OFF(LOW)
at_cmgs("LED D1 is OFF");
}
void relayall_on(){
PDOUT |= 0x01;
PDOUT |= 0x02; //PD0&1 are ON
at_cmgs("LED D0 & D1 are ON");
}
void relayall_off(){
PDOUT &= ~0x01;
PDOUT &= ~0x02; //PD0&1 are OFF
at_cmgs("LED D0 & D1 are OFF");
}
void delete_msg(){
select_port(_UART0);
clear_msg();
printf("AT+CMGD=1\n");
while(strstr(msg,"OK")==NULL) delay(1);
}
void at_cmgl(void){ //Function for receiving sms
at_at();
do
{
clear_msg();
select_port(_UART0);
//printf("AT+CMGL=\"ALL\"\n");
printf("AT+CMGR=1\n");
delay(10); //Wait for reply
}while(strstr(msg,"OK")==NULL); //Will loop forever if no incoming msg
if(strstr(msg,"low")!=NULL)
PDOUT |= 0x01;
else if (strstr(msg,"hi")!=NULL)
PDOUT &= ~0x01;
/*else if (strstr(msg,"LED D1 ON")!=0)
relay2_on();
else if (strstr(msg,"LED D1 OFF")!=0)
relay2_off();
else if (strstr(msg,"ALL LED ON")!=0)
relayall_on();
else if (strstr(msg,"ALL LED OFF")!=0)
relayall_off();*/
else delete_msg();
delay(1);
}
void main(void){
init_uart(_UART0, _DEFFREQ, _DEFBAUD); //UART0 for GSM MODULE (PA4:RX, PA5;TX)
// init_uart(_UART1, _DEFFREQ, _DEFBAUD); //UART1 for DAUGHTER SLIMBOARD or DEBUG (serial cable)
init_port();
initial_uart0();
init_gsm();
//initial_uart1(); //Data Logging part not yet integrated
//at_cmgs("Initialization Complete");
at_cmgl();
delay(1);
}