jayanth.devarayanadurga
Banned
- Joined
- Dec 4, 2012
- Messages
- 4,280
- Helped
- 822
- Reputation
- 1,654
- Reaction score
- 791
- Trophy points
- 1,393
- Location
- Bangalore, India
- Activity points
- 0
I am doing a GSM Remote control. I am having a problem. The first time I send a command (SMS) like "Y LED ON#" without double quotes then it works but it doesn't work for successive commands (SMSs). My buffer size is 20 bytes. If buffer index reaches max value then it is reset. As the SMS will be the last part of the data received through USRT ISR the SMS (command) gets stored to the buffer. Next I loop till I find 'Y' or 'G' which is the beginning of command and later I loop till I find '#' which is the end of command. I arrange the command in another buffer named temp of same size and terminate the buffer with null character. I compare this buffer with 4 stored messages and if a match occurs then particular LED is turned ON or OFF.
In the real application I replace '#' by '\r' as \r\n is received at the end of read SMS.
Edit: I have removed old code and posted an updated code.
In the real application I replace '#' by '\r' as \r\n is received at the end of read SMS.
Edit: I have removed old code and posted an updated code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 // LCD module connections sbit LCD_RS at RB0_bit; sbit LCD_EN at RB1_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB0_bit; sbit LCD_EN_Direction at TRISB1_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; // End LCD module connections #define LED_YELLOW_ON PORTA.F0 = 1 #define LED_GREEN_ON PORTA.F1 = 1 #define LED_GREEN_OFF PORTA.F1 = 0 #define LED_YELLOW_OFF PORTA.F0 = 0 typedef unsigned char u8int_t; u8int_t yLEDON[] = "Y LED ON"; u8int_t yLEDOFF[] = "Y LED OFF"; u8int_t gLEDON[] = "G LED ON"; u8int_t gLEDOFF[] = "G LED OFF"; u8int_t buffer[20], temp[20], index = 0, i = 0, j = 0, parseBuff = 0, count = 0; #define BUFFERSIZE 19 void Interrupt(){ if(RCIF_bit){ if(OERR_bit){ CREN_bit = 0; CREN_bit = 1; OERR_bit = 0; } if(index > BUFFERSIZE)index = 0; buffer[index++] = RCREG; if(buffer[index - 1] == '#')parseBuff = 1; } RCIF_bit = 0; } void parse(){ index = 0; while((buffer[index] != 'Y') && (buffer[index] != 'G'))index++; count = 0; while(buffer[index] != '#'){ temp[count++] = buffer[index++]; if(index > BUFFERSIZE)index = 0; } temp[count] = '\0'; count = 0; } void clearBuffer(){ for(i = 0; i = BUFFERSIZE; i++){ temp[i] = '\0'; buffer[i] = '\0';} i = 0; index = 0; } void main() { ADCON1 = 0b10000110; TRISA = 0x00; PORTA = 0x00; TRISB = 0x00; PORTB = 0x00; TRISC = 0xC0; PORTC = 0x00; UART1_Init(9600); Delay_ms(200); RCIE_bit = 1; INTCON = 0xC0; UART1_Write_Text("AT+CNMI=3,1,2,1,1\r\n"); Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off LCD_Out(1,2,"GSM Remote"); while(1){ if(parseBuff){ parse(); strcat(temp, " "); if(strcmp(temp, yLEDON) == 0){LED_YELLOW_ON; LCD_Out(2,1,temp);} if(strcmp(temp, yLEDOFF) == 0){LED_YELLOW_OFF; LCD_Out(2,1,temp);} if(strcmp(temp, gLEDON) == 0){LED_GREEN_ON; LCD_Out(2,1,temp);} if(strcmp(temp, gLEDOFF) == 0){LED_GREEN_OFF; LCD_Out(2,1,temp);} clearBuffer(); parseBuff = 0; } } }
Attachments
Last edited: