--- trunk/Embedded/main.lst 2007/11/30 16:07:03 112 +++ trunk/Embedded/main.lst 2007/12/04 19:07:00 150 @@ -1,140 +1,386 @@ 1: #include 2: #include - 3: #include - 4: #include "lcd.h" - 5: // Delay.h is included inside lcd.c - 6: - 7: #define LCD_LENGTH 16 - 8: #define LCD_ROWS 2 + 3: #include + 4: #include + 5: + 6: #include "lcd.h" + 7: #include "Delay.h" + 8: // Delay.h is included inside lcd.c 9: - 10: - 11: unsigned char global_Pot_Hi; - 12: unsigned char global_Pot_Lo; - 13: unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH]; - 14: /*void AD_init(void) - 15: { - 16: ADON = 1; - 17: } - 18: */ - 19: // Nicked from H7 - 20: void ad_init(void) - 21: { - 22: // AD Conversion clock - 23: ADCS0 = 0; - 24: ADCS1 = 0; - 25: ADCS2 = 0; + 10: #define LCD_LENGTH 16 + 11: #define LCD_ROWS 2 + 12: #define BUFFER 128 + 13: #define PWRFAIL RB1 + 14: #define FIREDET RB2 + 15: #define FEEDING RB3 + 16: #define EMPTYTANK RB4 + 17: + 18: + 19: unsigned char global_Pot_Hi, global_Pot_Lo; + 20: unsigned char global_LCD_Buffer[LCD_ROWS][LCD_LENGTH]; + 21: unsigned char global_serial_send[BUFFER], global_serial_recieve_buffer[BUFFER]; + 22: bit global_recieve_done = 0, global_interval_changed = 0; + 23: unsigned int global_serial_byte_counter = 0, global_sms_counter = 1, global_time_counter = 0; + 24: unsigned int global_emergency_counter = 600, global_time_interval = 3600; + 25: unsigned char global_temp = 0; 26: - 27: //Select AN0/RA0 for AD source - 28: // In this (000) setup, it's only RA0/AN0 that does ad convertion. - 29: CHS0=0; - 30: CHS1=0; - 31: CHS2=0; - 32: - 33: //Only AN0 is selected for AD and with Vdd/Vss as limits - 34: PCFG0=0; - 35: PCFG1=1; - 36: PCFG2=1; - 37: PCFG3=1; - 38: - 39: //Result is left justified - 40: ADFM=0; - 41: - 42: //Fire up for A/D converter module - 43: ADON=1; - 44: } - 45: - 46: void rs232_init(void) - 47: { - 48: SPEN = 0; // Serial Port Enable Bit... 0 = disabled - 49: TRISC6 = 0; - 50: TRISC7 = 1; - 51: SYNC = 0; // SYNC switches between async(0) and sync(1) mode. - 52: SPBRG = 25; - 53: TXSTA = 0x24; - 54: RCSTA = 0x90; - 55: SPEN = 1; - 56: } + 27: unsigned short global_imei_tversum; + 28: + 29: unsigned char cell_nr[15] = ""; // = "21681784"; + 30: bit global_modem_init = 0; + 31: + 32: __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8'); + 33: __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0); + 34: + 35: void serial_recieved(void); + 36: void serial_send(void); + 37: void update_lcd(void); + 38: void convertTemp(void); + 39: void timer1_interrupt(void); + 40: void on_recieve(void); + 41: void on_initial_recieve(void); + 42: + 43: + 44: void reset_recieve_buffer(void) + 45: { + 46: global_recieve_done = 0; + 47: global_serial_byte_counter=0; + 48: } + 49: + 50: + 51: void ad_init(void) // Nicked from H7 + 52: { + 53: // AD Conversion clock + 54: ADCS0 = 0; + 55: ADCS1 = 0; + 56: ADCS2 = 0; 57: - 58: void interrupt_init(void) - 59: { - 60: // Assumes that all interrupts default is 0 - 61: PEIE = 1; - 62: GIE = 1; - 63: RCIE = 1; - 64: } - 65: - 66: void pic18_io_init(void) - 67: { - 68: TRISA0 = 1; // analog input - 69: TRISB1 = 1; // TRISB1-4 Digital input - 70: TRISB2 = 1; - 71: TRISB3 = 1; - 72: TRISB4 = 1; - 73: } - 74: - 75: void interrupt_recieve_handler(void) - 76: { - 77: // Handle recieve inputs... - 78: } - 79: - 80: void interrupt interrupt_handler(void) - 81: { - 82: // Finds out what interrupt have been trigged, and starts the respective function. - 83: if(RCIF == 1) - 84: { - 85: interrupt_recieve_handler(); - 86: RCIF = 0; - 87: } - 88: } - 89: - 90: void update_lcd(void) - 91: { - 92: char toLCD0[LCD_LENGTH], toLCD1[LCD_LENGTH]; - 93: char B1[LCD_LENGTH]; - 94: char sprintF[LCD_LENGTH]; - 95: lcd_goto(0x00); - 96: sprintf(toLCD0, "%s", "Ejdesgaard"); - 97: if(RB1) - 98: { - 99: sprintf(B1, "Pwr%s", "True"); - 100: } - 101: else - 102: { - 103: sprintf(B1, "%s", "False"); - 104: } - 105: lcd_puts(B1); + 58: //Select AN0/RA0 for AD source + 59: // In this (000) setup, it's only RA0/AN0 that does ad convertion. + 60: CHS0=0; + 61: CHS1=0; + 62: CHS2=0; + 63: + 64: //Only AN0 is selected for AD and with Vdd/Vss as limits + 65: PCFG0=0; + 66: PCFG1=1; + 67: PCFG2=1; + 68: PCFG3=1; + 69: + 70: //Result is right justified + 71: ADFM=1; + 72: + 73: //Fire up for A/D converter module + 74: ADON=1; + 75: } + 76: + 77: void rs232_init(void) + 78: { + 79: SPEN = 0; // Serial Port Enable Bit... 0 = disabled + 80: TRISC6 = 0; + 81: TRISC7 = 1; + 82: + 83: SPBRG = 207; // 1200 baud rate... 25 = 9600 + 84: // x = (Fosc / (16*[baud rate]) )-1 + 85: TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register + 86: RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register + 87: } + 88: + 89: void interrupt_init(void) + 90: { + 91: // Assumes that all interrupts default is 0 + 92: PEIE = 1; + 93: GIE = 1; + 94: RCIE = 1; // Recieve interrupt enable. + 95: IPEN = 0; // Nfo interrupt priority + 96: TXIE = 0; // Serial interrupt enabled + 97: TMR1IE = 1; // Enables timer 1 + 98: } + 99: + 100: void timer_init(void) + 101: { + 102: TMR1CS = 1; //use external clock + 103: + 104: T1CKPS1 = 1; //1:8 prescale + 105: T1CKPS0 = 1; 106: - 107: lcd_goto(0x40); - 108: sprintf(sprintF,"%08d%08d", global_Pot_Hi, global_Pot_Lo); - 109: lcd_puts(sprintF); - 110: - 111: } - 112: - 113: void main() - 114: { - 115: ///////////////////////////////////////////// - 116: // Running Init's - 117: - 118: // Running init for various components. - 119: //AD_init(); - 120: ad_init(); - 121: rs232_init(); - 122: pic18_io_init(); - 123: lcd_init(0); + 107: TMR1H = 0xEF; + 108: TMR1L = 0xFF; + 109: + 110: T1OSCEN = 1; //enable oscillator circuit + 111: RD16 = 0; //normal 8 bit writes + 112: TMR1ON = 1; + 113: } + 114: + 115: void pic18_io_init(void) + 116: { + 117: TRISA0 = 1; // analog input + 118: TRISA1 = 0; // Output + 119: TRISB1 = 1; // TRISB1-4 Digital input + 120: TRISB2 = 1; + 121: TRISB3 = 1; + 122: TRISB4 = 1; + 123: } 124: - 125: ///////////////////////////////////////////// - 126: // Main loop - 127: - 128: while(1) - 129: { - 130: // Checking if A/D convertion is done, and save the data in global_Pot_?? - 131: if(GODONE==0) - 132: { - 133: global_Pot_Hi = ADRESH; - 134: global_Pot_Lo = ADRESL; - 135: GODONE = 1; - 136: } - 137: - 138: update_lcd(); - 139: } - 140: } + 125: void sms_init(void) + 126: { + 127: int i; + 128: + 129: reset_recieve_buffer(); + 130: sprintf(global_serial_send,"at+cgsn\r"); + 131: serial_send(); + 132: DelaySek(1); + 133: while(!global_recieve_done) ; + 134: + 135: + 136: sprintf(global_serial_send,"%s", "at+cpin=8043\r"); + 137: serial_send(); + 138: + 139: while(global_modem_init == 0) + 140: { + 141: on_initial_recieve(); + 142: } + 143: } + 144: + 145: + 146: void interrupt interrupt_handler(void) + 147: { + 148: // Finds out what interrupt have been trigged, and starts the respective function. + 149: if(RCIF == 1) // Serial recieve interrupt + 150: { + 151: serial_recieved(); + 152: RCIF = 0; + 153: } + 154: + 155: if(TMR1IF == 1) // timer1 interrupt trigger. + 156: { + 157: timer1_interrupt(); + 158: TMR1IF = 0; + 159: } + 160: } + 161: + 162: + 163: void serial_send(void) + 164: { + 165: int i; + 166: char data_byte; + 167: for(i = 0; i < BUFFER; i++) + 168: { + 169: data_byte = global_serial_send[i]; + 170: if( data_byte == '\r') + 171: i = (BUFFER - 1); + 172: TXREG = data_byte; + 173: while(TRMT==0) ; + 174: DelayMs(10); + 175: } + 176: DelayMs(250); + 177: DelayMs(250); + 178: } + 179: + 180: void serial_recieved(void) + 181: { + 182: char data_byte, saved_data[LCD_LENGTH]; + 183: + 184: data_byte = RCREG; + 185: + 186: if (data_byte == '\n') + 187: return; + 188: + 189: global_serial_recieve_buffer[global_serial_byte_counter] = data_byte; + 190: + 191: if (data_byte == '\r') + 192: { + 193: global_recieve_done = 1; + 194: //global_serial_byte_counter = 0; + 195: global_serial_recieve_buffer[global_serial_byte_counter+1] = 0; //zero terminate + 196: } + 197: else + 198: { + 199: global_serial_byte_counter++; + 200: } + 201: + 202: } + 203: + 204: void timer1_interrupt(void) + 205: { + 206: TMR1H = 0xEF; + 207: TMR1L = 0xFF; + 208: global_time_counter++; + 209: global_emergency_counter++; + 210: RA1 = !RA1; + 211: } + 212: + 213: void update_lcd(void) + 214: { + 215: lcd_clear(); + 216: lcd_goto(0x00); + 217: lcd_puts(global_temp); + 218: + 219: } + 220: + 221: void send_update(void) + 222: { + 223: sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr); + 224: serial_send(); + 225: sprintf(global_serial_send, "%d:%d:%d:%d:%d:%d%c", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL, 0x1A); + 226: lcd_goto(40); + 227: lcd_puts(global_serial_send); + 228: serial_send(); + 229: DelayMs(250); + 230: global_sms_counter++; + 231: } + 232: + 233: void convertTemp() + 234: { + 235: short adVal; + 236: adVal = (global_Pot_Hi << 8) | global_Pot_Lo; + 237: if( adVal >=840 ) + 238: global_temp = 100; + 239: else + 240: global_temp = (adVal / 8.3886); + 241: } + 242: + 243: + 244: + 245: void eeprom_writer(void) + 246: { + 247: char len,i; + 248: + 249: len = strlen(cell_nr); + 250: eeprom_write(0, (global_time_interval/60)); + 251: eeprom_write(1, global_sms_counter>>8); + 252: eeprom_write(2, global_sms_counter); + 253: eeprom_write(3, len); + 254: + 255: for (i=0; i= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 ) + 349: { + 350: send_update(); + 351: global_emergency_counter = 0; + 352: } + 353: // Every X sec. a status sms is send. + 354: if(global_time_counter >= 3600) + 355: { + 356: send_update(); + 357: global_time_counter = 0; + 358: } + 359: // To avoid buffer overrun. + 360: if( global_emergency_counter > 7200 ) + 361: global_emergency_counter = 600; + 362: + 363: // Checks if there has been recieved a config sms. + 364: if(global_interval_changed ) + 365: { + 366: eeprom_writer(); + 367: global_interval_changed = 0; + 368: } + 369: // Checking if A/D convertion is done, and save the data in global_Pot_?? + 370: if(GODONE==0) + 371: { + 372: global_Pot_Hi = ADRESH; + 373: global_Pot_Lo = ADRESL; + 374: convertTemp(); + 375: update_lcd(); + 376: GODONE = 1; + 377: } + 378: + 379: //if (global_recieve_done) + 380: //{ + 381: on_recieve(); + 382: // global_recieve_done = 0; + 383: // global_serial_byte_counter = 0; + 384: //} + 385: } + 386: }