I have to make this project for my school but i can't make it run. I've problems with message extraction and with the interrupt. Please help. This is my code
#include <reg51.h> #include <string.h> sbit Water=P2^0; sbit Window=P2^1; //declarations void clear(void); void tx0(unsigned char); void delay_sms (unsigned int); void SMSString(char*text) ; void init(); void read_text(unsigned char * , unsigned char * , unsigned char *); unsigned char j,abc; unsigned char idata msg1[150]; unsigned char rec_no[20]; unsigned char time_date[20]; unsigned char choice=0; unsigned char idata tmp10[16]; void serial () interrupt 4 { msg1[abc]=SBUF; abc++; RI=0; } void main (void) { clear(); init(); Window=0; Water=0; SMSString("AT\r"); // AT commands to initialize gsm modem delay_sms(1000); SMSString( "ATe0\r"); // turn off echo delay_sms(1000); SMSString( "AT&W\r"); // save settings delay_sms(1000); SMSString( "AT+CMGF=1\r"); // select text mode for sms delay_sms(1000); SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms delay_sms(1000); SMSString( "AT+CMGR=1\r"); // AT command to read sms delay_sms(1000); do { IE=0X90; // Enable serial interrupt delay_sms(1000); // i get a response like this // +CMGR: "REC UNREAD","+3069********",,"11/04/09,11:26:48+12" // Window on // OK // I think that's the format the 8051 get +CMTI: "SM",1\r+CMGR: "REC UNREAD","+306982037789",,"11/04/09,11:26:48+12"\rStatus\n\rOK\r // read sms and store in buffer msg1 read_text(msg1,rec_no,time_date); if(strncmp("Water on",msg1,8)==0) { Water=1; strncpy(tmp10,"Water Window ",16); tmp10[6]=Water; tmp10[15]=Window; SMSString("AT+CMGS=\""); SMSString(rec_no); SMSString("\"+<CR>"); delay_sms(1000); SMSString(tmp10); SMSString("\n\r"); delay_sms(1000); } else if(strncmp("Water off",msg1,9)==0) { Water=0; strncpy(tmp10,"Water Window ",16); tmp10[6]=Water; tmp10[15]=Window; SMSString("AT+CMGS=\""); SMSString(rec_no); SMSString("\"+<CR>"); delay_sms(1000); SMSString(tmp10); SMSString("\n\r"); delay_sms(1000); } else if(strncmp("Window on",msg1,9)==0) { Window=1; strncpy(tmp10,"Water Window ",16); tmp10[6]=Water; tmp10[15]=Window; SMSString("AT+CMGS=\""); SMSString(rec_no); SMSString("\"+<CR>"); delay_sms(1000); SMSString(tmp10); SMSString("\n\r"); delay_sms(1000); } else if(strncmp("Window off",msg1,10)==0) { Window=0; strncpy(tmp10,"Water Window ",16); tmp10[6]=Water; tmp10[15]=Window; SMSString("AT+CMGS=\""); SMSString(rec_no); SMSString("\"+<CR>"); delay_sms(1000); SMSString(tmp10); SMSString("\n\r"); delay_sms(1000); } else if(strncmp("Status",msg1,6)==0) { strncpy(tmp10,"Water Window ",16); tmp10[6]=Water; tmp10[15]=Window; SMSString("AT+CMGS=\""); SMSString(rec_no); SMSString("\"+<CR>"); delay_sms(1000); SMSString(tmp10); SMSString("\n\r"); delay_sms(1000); } else { SMSString("AT+CMGS=\""); SMSString(rec_no); SMSString("\"+<CR>\r"); delay_sms(1000); SMSString("Wrong Command"); SMSString("\n\r"); delay_sms(1000); SMSString(rec_no); delay_sms(1000); SMSString(msg1); delay_sms(1000); } SMSString("AT+CMGD=1\r"); delay_sms(1000); IE=0X00; // Disable serial interrupt delay_sms(1000); } while(1); } void init(void) { j=0; abc=0; TL1=0XFD; //9600 @ 11.0592 TH1=0xFD; TMOD=0x20; SCON=0x50; TR1=1; } void SMSString(unsigned char* text) //function to send SMS using GSM modem { while (*text) { tx0(*text++); } } void tx0(unsigned char x) //send data to serial port 0 { EA=0; SBUF=x; while(TI==0); TI=0; EA=1; } void delay_sms (unsigned int count) { unsigned int i; while(count) { i = 115; while(i>0) i--; count--; } } void read_text( unsigned char *msg,unsigned char *no ,unsigned char *time) { unsigned char *temp; temp=msg; do msg++; while(*msg!='+'); do msg++; while(*msg!='+'); do msg++; while(*msg!='+'); // reaching number do *no++=*msg++; while(*msg!='"'); // reaching time *no++='\0'; msg++; msg++; msg++; do *time++=*msg++; while(*msg!='"'); // reaching message *time='\0'; do msg++; while(*msg!='\r'); msg++; do *temp++=*msg++; while(*msg!='\n'); // reaching the end of message *temp='\0'; } void clear(void) { unsigned char a; for(a=0;a<100;a++) msg1[a]=0x00; }
void serial () interrupt 4 using 1 { if (RI) { //receive mode RI=0; abc=SBUF; if(abc<100) { msg1[abc++]=SBUF; } else { RI=1; /////////// this will get you right back into the ISR } } }
I had to reformat your code to verufy this, what is wrong with lining the braces up???, look at the comment above.
Erik
abc=SBUF; .... msg1[abc++]=SBUF;
storing in a buffer indexed by the hex value of the character received?????
It looks to me as if you do not believe in the adage "think before you do"
Brace positioning is very much a matter of strongly-held personal preference.
Many people like their braces on the end of the "controlling" line:
if( test ) { // stuff } else { // alternative stuff }
Some people (like me) like their braces alone on a line, aligned with the "controlling" line
Some people (quite inexplicably, IMO) like their braces alone on a line, aligned with the "controlled" line(s)
To name but three.
The important thing is, whatever you choose, to be consistent
plenty more here: en.wikipedia.org/.../Indent_style
I modified my braces and changed the serial interrupt but there is still a lot work to be done.
#include <reg51.h> #include <string.h> #include <stdio.h> sbit Water=P2^0; sbit Window=P2^1; //declarations void clear(void); void tx0(unsigned char); void delay_sms (unsigned int); void SMSString(char*text) ; void init(); void Status(); void init_modem(); void process_modem_character(unsigned char *); unsigned char idata phone[15]; // Store received phone number unsigned char idata msg1[100]; // Store received message unsigned char j,bufcounter; void serial () interrupt 4 using 1 { if (RI) { while(!RI) { RI=0; if(bufcounter<100) { msg1[bufcounter++]=SBUF; } } } } void main (void) { clear(); init(); init_modem(); do { IE=0X90; // Enable serial interrupt delay_sms(1000); // i get a response like this // +CMGR: "REC UNREAD","+3069********",,"11/04/09,11:26:48+12" // Window on // OK // I think that's the format the 8051 get // \+CMTI\: \"SM\",1\r+CMGR: \"REC UNREAD\"\,\"+3069********\"\,\,\"11\/04\/09\,11\:26\:48\+12\"\rStatus\n\rOK\r process_modem_character(msg1); // read sms and store it if(strncmp("Water on",msg1,8)==0) { Water=1; Status(); } else if(strncmp("Water off",msg1,9)==0) { Water=0; Status(); } else if(strncmp("Window on",msg1,9)==0) { Window=1; Status(); } else if(strncmp("Window off",msg1,10)==0) { Window=0; Status(); } else if(strncmp("Status",msg1,6)==0) { Status(); } else { SMSString("AT+CMGS=\""); SMSString(phone); SMSString("\"\n\r"); delay_sms(1000); SMSString("Wrong Command"); SMSString("\n\r"); delay_sms(1000); } SMSString("AT+CMGD=1\r"); delay_sms(1000); IE=0X00; // Disable serial interrupt delay_sms(1000); } while(1); } void init(void) { j=0; bufcounter=0; TL1=0XFD; //9600 @ 11.0592 TH1=0xFD; TMOD=0x20; SCON=0x50; TR1=1; } void SMSString(unsigned char* text) //function to send SMS using GSM modem { while (*text) { tx0(*text++); } } void tx0(unsigned char x) //send data to serial port 0 { EA=0; SBUF=x; while(TI==0); TI=0; EA=1; } void delay_sms (unsigned int count) { unsigned int i; while(count) { i = 115; while(i>0) i--; count--; } } void init_modem(void) { Window=0; Water=0; SMSString("AT\r"); // AT commands to initialize gsm modem delay_sms(1000); SMSString( "ATe0\r"); // turn off echo delay_sms(1000); SMSString( "AT&W\r"); // save settings delay_sms(1000); SMSString( "AT+CMGF=1\r"); // select text mode for sms delay_sms(1000); SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms delay_sms(1000); SMSString( "AT+CMGR=1\r"); // AT command to read sms delay_sms(1000); } void Status(void) { unsigned char tmp10 = sprintf(msg1 ,"Water is %s and Window is %s\n" , Water, Window); SMSString("AT+CMGS=\""); SMSString(phone); SMSString("\"\n"); delay_sms(1000); //SMSString(tmp10); SMSString("\n\r"); delay_sms(1000); } enum States { STATE_IDLE, STATE_FIND_CMT, STATE_CMT_GET_NUMBER, STATE_CMT_GET_COMMA, STATE_CMT_GET_MSG_START, STATE_CMT_GET_MSG, } state = STATE_IDLE; const unsigned char* pattern; int ch; void process_modem_character(unsigned char*msg) { switch (state) { case STATE_IDLE: if (msg == '+') { state = STATE_FIND_CMT; pattern = "\+CMTI\: \"SM\",1\r+CMGR: \"REC UNREAD\"\,\""; } break; case STATE_FIND_CMT: if (*pattern != msg) { state = STATE_IDLE; } else { pattern++; if (*pattern == '\0') { state = STATE_CMT_GET_NUMBER; ch = 0; } } break; case STATE_CMT_GET_NUMBER: if (msg == '\"') { state = STATE_CMT_GET_COMMA; } else if (ch >= sizeof(phone)) { // Too long phone number :( state = STATE_IDLE; } else { ch++; phone[ch] = *msg; phone[ch] = '\0'; } break; case STATE_CMT_GET_COMMA: if (msg == ' ') { ; // Ignore white space } else if (msg == ',') { state = STATE_CMT_GET_MSG_START; } else { // Invalid character received state = STATE_IDLE; } break; default: // Invalid state. state = STATE_IDLE; } // switch } // process_modem_character void clear(void) { unsigned char a; for(a=0;a<100;a++) msg1[a]=0x00; }
Also i get error C214: illegal pointer conversion
void Status(void) { unsigned char tmp10 = sprintf(msg1 ,"Water is %s and Window is %s\n" , Water, Window); SMSString("AT+CMGS=\""); SMSString(phone); SMSString("\"\n"); delay_sms(1000); SMSString(tmp10); SMSString("\n\r"); delay_sms(1000); }
and i don't really understand why.
Stop writing random noise and try to think about everything you write.
tmp10 is a character. Do SMSString expect a character? Doesn't it expect a pointer to one or more characters?
Water and Window are bits. So why do you think %s is good formatting parameters for the sprintf() call? Don't you think bits have some form of integer values? %s is used to print strings, not integers.
Don't post questions as soon as you get stuck. Read the manual. Make sure you have understood the relevant sections in it. Post questions if you can't understand a specific part of the manual or you think you have fulfilled the requirements given in the manual but still get errors. But do not post questions for every compilation error you get or you will never get done with this assignment.