--- trunk/Embedded/main.c 2007/12/03 14:24:50 137 +++ trunk/Embedded/main.c 2007/12/10 08:39:47 214 @@ -1,33 +1,91 @@ #include #include +#include #include +#include + #include "lcd.h" #include "Delay.h" -// Delay.h is included inside lcd.c -#define LCD_LENGTH 16 -#define LCD_ROWS 2 +#define BUFFER 160 +#define PWRFAIL RB1 +#define FIREDET RB2 +#define FEEDING RB3 +#define EMPTYTANK RB4 unsigned char global_Pot_Hi, global_Pot_Lo; -unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH]; -unsigned char global_serial_data; -unsigned char global_serial_recieve_buffer[LCD_LENGTH]; +unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; bit global_recieve_done = 0; -int global_serial_byte_counter = 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 int 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 int global_imei_tversum; + +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); + void serial_recieved(void); +void serial_send(void); +void update_lcd(void); +void convert_temp(void); +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 eeprom_writer(void); -// Nicked from H7 -void ad_init(void) + +void reset_recieve_buffer(void) +{ + global_recieve_done = 0; + + global_message_buffer_length1 = 0; + global_message_buffer_length2 = 0; + + memset(global_message_buffer1, 0, BUFFER); + memset(global_message_buffer2, 0, BUFFER); +} +////////// INITS ////////// +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 { // AD Conversion clock ADCS0 = 0; ADCS1 = 0; ADCS2 = 0; - //Select AN0/RA0 for AD source - // In this (000) setup, it's only RA0/AN0 that does ad convertion. + //Select AN0/RA0 for AD source. + // In this (000) setup, it's only RA0/AN0 that does ad convertion. CHS0=0; CHS1=0; CHS2=0; @@ -38,13 +96,47 @@ PCFG2=1; PCFG3=1; - //Result is left justified - ADFM=0; + //Reset the A/D result registers + ADRESH = 0; + ADRESL = 0; + + //Result is right justified + ADFM=1; //Fire up for A/D converter module ADON=1; } + + +void interrupt_init(void) +{ + // Assumes that all interrupts default is 0 + PEIE = 1; + GIE = 1; + RCIE = 1; // Recieve interrupt enable. + IPEN = 0; // Nfo interrupt priority + TXIE = 0; // Serial interrupt enabled + TMR1IE = 1; // Enables timer 1 +} + + + +void timer_init(void) +{ + TMR1CS = 1; //use external clock + + T1CKPS1 = 1; //1:8 prescale + T1CKPS0 = 1; + + TMR1H = 0xEF; + TMR1L = 0xFF; + + 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 @@ -57,29 +149,81 @@ RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register } -void interrupt_init(void) +void modem_init(void) { - // Assumes that all interrupts default is 0 - PEIE = 1; - GIE = 1; - RCIE = 1; // Recieve interrupt enable. - IPEN = 0; // Nfo interrupt priority - TXIE = 0; // Serial interrupt enabled -} + int i; + char buf[2]; + + while ( strstr(global_message_buffer1,"+WIND: 7") == 0 && global_time_counter < 10 ) ; // Waiting for the modem to be ready -void pic18_io_init(void) + reset_recieve_buffer(); + sprintf(global_serial_send,"at+cgsn\r"); + serial_send(); + + 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_has_imei = 1; + } + } + + sprintf(global_serial_send,"%s", "at+cpin?\r"); + serial_send(); + DelayMs(100); // Delay to give the modem a chance to answer. + + if (strstr(global_message_buffer1, "+CPIN: SIM PIN") != 0) + { + sprintf(global_serial_send,"%s", "at+cpin=8043\r"); + serial_send(); + + while(global_modem_init == 0) + { + on_initial_recieve(); + } + } + reset_recieve_buffer(); + sprintf(global_serial_send, "at+cmgd=1,4\r"); + serial_send(); + while ( strstr(global_message_buffer1,"OK") == 0) ; +} + +void serial_send(void) { - TRISA0 = 1; // analog input - TRISB1 = 1; // TRISB1-4 Digital input - TRISB2 = 1; - TRISB3 = 1; - TRISB4 = 1; -} + int i; + char data_byte; + for(i = 0; i < BUFFER; i++) + { + data_byte = global_serial_send[i]; + if( data_byte == '\r') + i = (BUFFER - 1); + TXREG = data_byte; + while(TRMT==0) ; + DelayMs(10); + } + DelayMs(150); + global_serial_send[0] = 0; + DelayMs(150); +} -void sms_init(void) +void on_initial_recieve(void) { + if (strstr(global_serial_recieve_buffer,"+WIND: 11") != 0) + { + global_modem_init = 1; + reset_recieve_buffer(); + } } +////////// INTERRUPT HANDLER ////////// void interrupt interrupt_handler(void) { // Finds out what interrupt have been trigged, and starts the respective function. @@ -88,76 +232,284 @@ serial_recieved(); RCIF = 0; } + + if(TMR1IF == 1) // timer1 interrupt trigger. + { + timer1_interrupt(); + TMR1IF = 0; + } } +////////// INTERRUPT TRIGGED ////////// +void serial_recieved(void) +{ + char data_byte; + + data_byte = RCREG; + + if (data_byte == '\n') // Cant be bothered to do anyting if the byte is a '\n'. + return; + + if (global_serial_byte_counter == 0 && data_byte == '\r') // don't care about '\r', if it's the first byte we recieve. + return; -void serial_send(void) + 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. + else + { + global_serial_recieve_buffer[0] = 0; + global_serial_byte_counter = 0; + return; + } + + + 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; // 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; + } +} + +void timer1_interrupt(void) { - int i; - char tosend[3]; - char data; - sprintf(tosend,"%s", "at\r"); - for(i = 0; i < 3; i++) - { - data = tosend[i]; - TXREG = data; - while(TRMT==0) - DelayMs(1000); + TMR1H = 0xEF; + TMR1L = 0xFF; + global_time_counter++; + global_emergency_counter++; +// RA1 = !RA1; +} +////////// ORDENARY FUNKTIONS ////////// +void update_lcd(void) +{ + 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) +{ + 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, "%s%c", payload, 0x1A); + serial_send(); + DelayMs(150); + if(global_sms_counter % 4 == 0) + { + eeprom_writer(); + sprintf(global_serial_send, "at+cmgd=1,3\r"); + serial_send(); + while ( strstr(global_message_buffer1,"OK") == 0) ; } } -void serial_recieved(void) +void convert_temp(void) +{ + short adVal; + adVal = (global_Pot_Hi << 8) | global_Pot_Lo; + if( adVal >=840 ) + global_temp = 100; + else + global_temp = (unsigned char) (adVal / 8.3886); +} + + + +void eeprom_writer(void) { - char data, saved_data[LCD_LENGTH]; + char len,i; - data = RCREG; + 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); + eeprom_write(3, len); - global_serial_recieve_buffer[global_serial_byte_counter] = data; - if(data == '\r') + for (i=0; i= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING ==1 ) && global_emergency_counter >= 600 ) + { + send_update(); + global_emergency_counter = 0; + } + + // Every X sec. a status sms is send. + if(global_time_counter >= global_time_interval) + { + send_update(); + global_time_counter = 0; + } + // To avoid buffer overrun. + if( global_emergency_counter > 7200 ) + global_emergency_counter = 600; + // Checking if A/D convertion is done, and save the data in global_Pot_?? if(GODONE==0) { global_Pot_Hi = ADRESH; global_Pot_Lo = ADRESL; + convert_temp(); + update_lcd(); GODONE = 1; } - - if( global_recieve_done == 1 ) - { - lcd_clear(); - lcd_goto(0x00); - lcd_puts(global_serial_recieve_buffer); - global_recieve_done = 0; - } + // Handels the recieve sms'es. + on_recieve(); } }