--- trunk/Embedded/main.c 2007/12/04 19:07:00 150 +++ trunk/Embedded/main.c 2007/12/05 16:00:03 165 @@ -2,6 +2,7 @@ #include #include #include +#include #include "lcd.h" #include "Delay.h" @@ -17,17 +18,28 @@ unsigned char global_Pot_Hi, global_Pot_Lo; -unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH]; unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; bit global_recieve_done = 0, global_interval_changed = 0; unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0; unsigned int global_emergency_counter = 600, global_time_interval = 3600; unsigned char global_temp = 0; +unsigned char global_temp_update_display = 0; + +unsigned char global_message_buffer1[BUFFER]; +unsigned char global_message_buffer2[BUFFER]; +unsigned char global_message_buffer_length1; +unsigned char global_message_buffer_length2; + +unsigned char global_lcd_buf[16]; + unsigned short global_imei_tversum; -unsigned char cell_nr[15] = ""; // = "21681784"; +unsigned char global_cell_nr[15] = ""; // = "21681784"; bit global_modem_init = 0; +bit global_has_imei = 0; + +unsigned char global_sms_recieve_number[3]; __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8'); __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0); @@ -39,14 +51,34 @@ void timer1_interrupt(void); void on_recieve(void); void on_initial_recieve(void); +void sms_recieved(void); +void send_sms(const unsigned char* payload); void reset_recieve_buffer(void) { - global_recieve_done = 0; - global_serial_byte_counter=0; + global_recieve_done = 0; + + global_message_buffer_length1 = 0; + //global_message_buffer1[0] = 0; + global_message_buffer_length2 = 0; + //global_message_buffer2[0] = 0; + + memset(global_message_buffer1, 0, BUFFER); + memset(global_message_buffer2, 0, BUFFER); } +void pic18_io_init(void) +{ + TRISA0 = 1; // analog input + TRISA5 = 0; // Output + TRISB1 = 1; // TRISB1-4 Digital input + TRISB2 = 1; + TRISB3 = 1; + TRISB4 = 1; +} + + void ad_init(void) // Nicked from H7 { @@ -110,27 +142,36 @@ T1OSCEN = 1; //enable oscillator circuit RD16 = 0; //normal 8 bit writes TMR1ON = 1; -} - -void pic18_io_init(void) -{ - TRISA0 = 1; // analog input - TRISA1 = 0; // Output - TRISB1 = 1; // TRISB1-4 Digital input - TRISB2 = 1; - TRISB3 = 1; - TRISB4 = 1; } void sms_init(void) { int i; + char buf[2]; + reset_recieve_buffer(); sprintf(global_serial_send,"at+cgsn\r"); serial_send(); DelaySek(1); - while(!global_recieve_done) ; + //while(!global_recieve_done) ; + + while (global_has_imei == 0) + { + if (strstr(global_message_buffer1,"OK") != 0) + { + global_imei_tversum = 0; + for (i=0; i<15; ++i) + { + buf[0] = global_message_buffer2[i]; + buf[1] = 0; + global_imei_tversum += atoi(buf); + //global_imei_tversum += (global_message_buffer2[i] - '0'); + } + + global_has_imei = 1; + } + } sprintf(global_serial_send,"%s", "at+cpin=8043\r"); @@ -140,6 +181,12 @@ { on_initial_recieve(); } + DelayMs(5); + + reset_recieve_buffer(); + sprintf(global_serial_send, "at+cmgd=1,4\r"); + serial_send(); + while ( strstr(global_message_buffer1,"OK") == 0) ; } @@ -173,32 +220,51 @@ while(TRMT==0) ; DelayMs(10); } - DelayMs(250); - DelayMs(250); + DelayMs(150); + global_serial_send[0] = 0; + DelayMs(150); } -void serial_recieved(void) +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Takes the recieving data and fills it in a buffer, when we meet a '\r' we fill the data from, +// global_serial_recieve_buffer, to global_message_buffer1, and sets the flag global_recieve_done. +void serial_recieved(void) { - char data_byte, saved_data[LCD_LENGTH]; + char data_byte; data_byte = RCREG; - if (data_byte == '\n') + if (data_byte == '\n') // Cant be bothered to do anyting if the byte is a '\n'. return; - - global_serial_recieve_buffer[global_serial_byte_counter] = data_byte; - if (data_byte == '\r') + if (global_serial_byte_counter == 0 && data_byte == '\r') // don't care about '\r', if it's the first byte we recieve. + return; + + + if ( global_serial_byte_counter < BUFFER) //Prevent buffer overrun + global_serial_recieve_buffer[ global_serial_byte_counter++ ] = data_byte; // fills the data_byte into our buffer. + + + + if (data_byte == '\r') // when we meet a '\r', the transmission is done, and we fill the constxt of + // global_message_buffer1 into global_message_buffer2 our main buffer into + // global_message_buffer2, and the same with our recieve buffer, global_serial_recieve_buffer + // into global_message_buffer1. { - global_recieve_done = 1; - //global_serial_byte_counter = 0; - global_serial_recieve_buffer[global_serial_byte_counter+1] = 0; //zero terminate + global_recieve_done = 1; // indicates the recieve transmission is done. + global_serial_recieve_buffer[global_serial_byte_counter] = 0; //zero terminate + + // global_message_buffer1 -> global_message_buffer2 + strcpy(global_message_buffer2, global_message_buffer1); + global_message_buffer_length2 = global_message_buffer_length1; + + // global_serial_recieve_buffer -> global_message_buffer1 + strcpy(global_message_buffer1, global_serial_recieve_buffer); + global_message_buffer_length1 = global_serial_byte_counter; + + + global_serial_byte_counter = 0; } - else - { - global_serial_byte_counter++; - } - } void timer1_interrupt(void) @@ -212,22 +278,35 @@ void update_lcd(void) { - lcd_clear(); - lcd_goto(0x00); - lcd_puts(global_temp); + if(global_temp_update_display != global_time_counter) + { + lcd_clear(); + lcd_goto(0x00); + + sprintf(global_lcd_buf, "Temp: %3d", global_temp); + lcd_puts(global_lcd_buf); + global_temp_update_display = global_time_counter; + } } void send_update(void) { - sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr); + char update[40]; + sprintf(update, "%d:%d:%d:%d:%d:%d", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL); + send_sms(update); + + global_sms_counter++; +} + +void send_sms(const unsigned char* payload) +{ + sprintf(global_serial_send, "at+cmgs=\"%s\"\r", global_cell_nr); serial_send(); - sprintf(global_serial_send, "%d:%d:%d:%d:%d:%d%c", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL, 0x1A); - lcd_goto(40); - lcd_puts(global_serial_send); + sprintf(global_serial_send, "%s%c", payload, 0x1A); serial_send(); - DelayMs(250); - global_sms_counter++; + DelayMs(150); + } void convertTemp() @@ -237,7 +316,7 @@ if( adVal >=840 ) global_temp = 100; else - global_temp = (adVal / 8.3886); + global_temp = (unsigned char) (adVal / 8.3886); } @@ -246,7 +325,7 @@ { char len,i; - len = strlen(cell_nr); + len = strlen(global_cell_nr); eeprom_write(0, (global_time_interval/60)); eeprom_write(1, global_sms_counter>>8); eeprom_write(2, global_sms_counter); @@ -254,7 +333,7 @@ for (i=0; i