--- trunk/Embedded/main.c 2007/12/06 12:30:39 173 +++ trunk/Embedded/main.c 2007/12/09 20:50:42 204 @@ -6,10 +6,7 @@ #include "lcd.h" #include "Delay.h" -// Delay.h is included inside lcd.c -#define LCD_LENGTH 16 -#define LCD_ROWS 2 #define BUFFER 128 #define PWRFAIL RB1 #define FIREDET RB2 @@ -20,11 +17,11 @@ unsigned char global_Pot_Hi, global_Pot_Lo; 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_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0, global_time_counter_image = 0; unsigned int global_emergency_counter = 600, global_time_interval = 3600; unsigned char global_temp = 0; -unsigned char global_temp_update_display = 0; +unsigned int global_temp_update_display = 0; unsigned char global_message_buffer1[BUFFER]; unsigned char global_message_buffer2[BUFFER]; @@ -33,7 +30,7 @@ unsigned char global_lcd_buf[16]; -unsigned short global_imei_tversum; +unsigned int global_imei_tversum; unsigned char global_cell_nr[15] = ""; // = "21681784"; bit global_modem_init = 0; @@ -44,6 +41,7 @@ __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8'); __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0); + void serial_recieved(void); void serial_send(void); void update_lcd(void); @@ -55,6 +53,7 @@ void send_sms(const unsigned char* payload); void eeprom_writer(void); + void reset_recieve_buffer(void) { global_recieve_done = 0; @@ -67,7 +66,7 @@ memset(global_message_buffer1, 0, BUFFER); memset(global_message_buffer2, 0, BUFFER); } - +////////// INITS ////////// void pic18_io_init(void) { TRISA0 = 1; // analog input @@ -110,17 +109,7 @@ ADON=1; } -void rs232_init(void) -{ - SPEN = 0; // Serial Port Enable Bit... 0 = disabled - TRISC6 = 0; - TRISC7 = 1; - SPBRG = 207; // 1200 baud rate... 25 = 9600 - // x = (Fosc / (16*[baud rate]) )-1 - TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register - RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register -} void interrupt_init(void) { @@ -133,6 +122,8 @@ TMR1IE = 1; // Enables timer 1 } + + void timer_init(void) { TMR1CS = 1; //use external clock @@ -146,9 +137,21 @@ T1OSCEN = 1; //enable oscillator circuit RD16 = 0; //normal 8 bit writes TMR1ON = 1; -} +} + +void rs232_init(void) +{ + SPEN = 0; // Serial Port Enable Bit... 0 = disabled + TRISC6 = 0; + TRISC7 = 1; -void sms_init(void) + SPBRG = 207; // 1200 baud rate... 25 = 9600 + // x = (Fosc / (16*[baud rate]) )-1 + TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register + RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register +} + +void modem_init(void) { int i; char buf[2]; @@ -175,40 +178,26 @@ } } - - sprintf(global_serial_send,"%s", "at+cpin=8043\r"); + sprintf(global_serial_send,"%s", "at+cpin?\r"); serial_send(); + DelayMs(100); // Delay to give the modem a chance to answer. - while(global_modem_init == 0) + if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0) { - on_initial_recieve(); + sprintf(global_serial_send,"%s", "at+cpin=8043\r"); + serial_send(); + + while(global_modem_init == 0) + { + 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) ; } - -void interrupt interrupt_handler(void) -{ - // Finds out what interrupt have been trigged, and starts the respective function. - if(RCIF == 1) // Serial recieve interrupt - { - serial_recieved(); - RCIF = 0; - } - - if(TMR1IF == 1) // timer1 interrupt trigger. - { - timer1_interrupt(); - TMR1IF = 0; - } -} - - void serial_send(void) { int i; @@ -225,11 +214,47 @@ DelayMs(150); global_serial_send[0] = 0; DelayMs(150); -} +} + +void on_initial_recieve(void) +{ +// can't se what the outcommented lines in here sould do, that make any sence. +// char imei[16]; +// char* ptr; +// char i; +// char buf[2]; + + if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0) + { + global_modem_init = 1; + +// ptr = strstr(global_serial_recieve_buffer,"cgsn"); +// ptr +=4; +// strncpy(imei, ptr,15); +// imei[15] = 0; + + reset_recieve_buffer(); + } +} -/////////////////////////////////////////////////////////////////////////////////////////////////// -// 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. +////////// INTERRUPT HANDLER ////////// +void interrupt interrupt_handler(void) +{ + // Finds out what interrupt have been trigged, and starts the respective function. + if(RCIF == 1) // Serial recieve interrupt + { + serial_recieved(); + RCIF = 0; + } + + if(TMR1IF == 1) // timer1 interrupt trigger. + { + timer1_interrupt(); + TMR1IF = 0; + } +} + +////////// INTERRUPT TRIGGED ////////// void serial_recieved(void) { char data_byte; @@ -280,9 +305,9 @@ TMR1L = 0xFF; global_time_counter++; global_emergency_counter++; - RA1 = !RA1; +// RA1 = !RA1; } - +////////// ORDENARY FUNKTIONS ////////// void update_lcd(void) { if(global_temp_update_display != global_time_counter) @@ -313,7 +338,7 @@ sprintf(global_serial_send, "%s%c", payload, 0x1A); serial_send(); DelayMs(150); - if(global_sms_counter % 3 == 0) + if(global_sms_counter % 4 == 0) { eeprom_writer(); sprintf(global_serial_send, "at+cmgd=1,3\r"); @@ -322,7 +347,7 @@ } } -void convertTemp() +void convert_temp() { short adVal; adVal = (global_Pot_Hi << 8) | global_Pot_Lo; @@ -367,25 +392,6 @@ global_cell_nr[i] = 0; //zero terminated! } -void on_initial_recieve(void) -{ - char imei[16]; - char* ptr; - char i; - char buf[2]; - - if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0) - { - global_modem_init = 1; - - ptr = strstr(global_serial_recieve_buffer,"cgsn"); - ptr +=4; - strncpy(imei, ptr,15); - imei[15] = 0; - - reset_recieve_buffer(); - } -} void on_recieve(void) { @@ -399,9 +405,9 @@ if (strstr(global_message_buffer1,"CMTI") != 0) // here we handles a incomming SMS { ptr = strstr(global_message_buffer1,","); // finds the point just before the nr. of the SMS. - strcat(tmp,ptr+1); // puts that number in tmp - global_sms_recieve_number[0] = 0; // wanna be sure that we write the new number from global_sms_recieve_number[0] - strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable. + strcat(tmp,ptr+1); // puts that number in tmp + global_sms_recieve_number[0] = 0; // wanna be sure that we write the new number from global_sms_recieve_number[0] + strcat(global_sms_recieve_number, tmp); // puts the sms number into the global variable. sms_recieved(); } reset_recieve_buffer(); @@ -449,14 +455,19 @@ global_time_interval = atoi(buf); global_time_interval *= 60; - eeprom_writer(); // writes the new cell nr. and time interval to the eeprom. + eeprom_writer(); // writes the new cell nr. and time interval to the eeprom. send_sms("conf ok"); } } - +void Delay(int time) +{ + int wanted = (global_time_counter + time) % global_time_interval; + + while (global_time_counter < wanted) ; +} void main() { @@ -465,14 +476,16 @@ // Running init for various components. pic18_io_init(); - RA5 = 1; // Indicates that the board is running inits. + RA5 = 1; // Indicates that the board is running inits. rs232_init(); ad_init(); lcd_init(0); + lcd_clear(); + lcd_home(); interrupt_init(); timer_init(); - sms_init(); + modem_init(); eeprom_reader(); /////////////// // Main loop // @@ -490,8 +503,9 @@ send_update(); global_emergency_counter = 0; } + // Every X sec. a status sms is send. - if(global_time_counter >= 3600) + if(global_time_counter >= global_time_interval) { send_update(); global_time_counter = 0; @@ -511,7 +525,7 @@ { global_Pot_Hi = ADRESH; global_Pot_Lo = ADRESL; - convertTemp(); + convert_temp(); update_lcd(); GODONE = 1; }