--- trunk/Embedded/main.lst 2007/11/30 13:47:05 109 +++ trunk/Embedded/main.lst 2007/12/04 17:23:27 148 @@ -1,144 +1,318 @@ 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 SEND_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[SEND_BUFFER], global_serial_recieve_buffer[SEND_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: CHS0=0; - 29: CHS1=0; - 30: CHS2=0; - 31: - 32: //Only AN0 is selected for AD and with Vdd/Vss as limits - 33: PCFG0=0; - 34: PCFG1=1; - 35: PCFG2=1; - 36: PCFG3=1; - 37: - 38: //Result is right justified - 39: ADFM=1; - 40: - 41: //Fire up for A/D converter module - 42: ADON=1; - 43: } - 44: - 45: void rs232_init(void) - 46: { - 47: SPEN = 0; // Serial Port Enable Bit... 0 = disabled - 48: TRISC6 = 0; - 49: TRISC7 = 1; - 50: SYNC = 0; // SYNC switches between async(0) and sync(1) mode. - 51: SPBRG = 25; - 52: TXSTA = 0x24; - 53: RCSTA = 0x90; - 54: SPEN = 1; - 55: } - 56: - 57: void interrupt_init(void) - 58: { - 59: // Assumes that all interrupts default is 0 - 60: PEIE = 1; - 61: GIE = 1; - 62: RCIE = 1; - 63: } - 64: - 65: void pic18_io_init(void) - 66: { - 67: TRISA0 = 1; // analog input - 68: TRISB1 = 1; // TRISB1-4 Digital input - 69: TRISB2 = 1; - 70: TRISB3 = 1; - 71: TRISB4 = 1; - 72: } - 73: - 74: void interrupt_recieve_handler(void) - 75: { - 76: // Handle recieve inputs... - 77: } - 78: - 79: void interrupt interrupt_handler(void) - 80: { - 81: // Finds out what interrupt have been trigged, and starts the respective function. - 82: if(RCIF == 1) - 83: { - 84: interrupt_recieve_handler(); - 85: RCIF = 0; - 86: } - 87: } + 27: unsigned char cell_nr[15] = ""; // = "21681784"; + 28: + 29: + 30: __EEPROM_DATA( 60, 0, 1, 8, '2', '1', '6', '8'); + 31: __EEPROM_DATA( '1', '7', '8', '4',0,0,0,0); + 32: + 33: void serial_recieved(void); + 34: void serial_send(void); + 35: void update_lcd(void); + 36: void convertTemp(void); + 37: void timer1_interrupt(void); + 38: + 39: // Nicked from H7 + 40: void ad_init(void) + 41: { + 42: // AD Conversion clock + 43: ADCS0 = 0; + 44: ADCS1 = 0; + 45: ADCS2 = 0; + 46: + 47: //Select AN0/RA0 for AD source + 48: // In this (000) setup, it's only RA0/AN0 that does ad convertion. + 49: CHS0=0; + 50: CHS1=0; + 51: CHS2=0; + 52: + 53: //Only AN0 is selected for AD and with Vdd/Vss as limits + 54: PCFG0=0; + 55: PCFG1=1; + 56: PCFG2=1; + 57: PCFG3=1; + 58: + 59: //Result is right justified + 60: ADFM=1; + 61: + 62: //Fire up for A/D converter module + 63: ADON=1; + 64: } + 65: + 66: void rs232_init(void) + 67: { + 68: SPEN = 0; // Serial Port Enable Bit... 0 = disabled + 69: TRISC6 = 0; + 70: TRISC7 = 1; + 71: + 72: SPBRG = 207; // 1200 baud rate... 25 = 9600 + 73: // x = (Fosc / (16*[baud rate]) )-1 + 74: TXSTA = 0x24; // Enables BRGH and TXEN inthe TXSTA register + 75: RCSTA = 0x90; // 0x90 enables SPEN and CREN in the RCSTA register + 76: } + 77: + 78: void interrupt_init(void) + 79: { + 80: // Assumes that all interrupts default is 0 + 81: PEIE = 1; + 82: GIE = 1; + 83: RCIE = 1; // Recieve interrupt enable. + 84: IPEN = 0; // Nfo interrupt priority + 85: TXIE = 0; // Serial interrupt enabled + 86: TMR1IE = 1; // Enables timer 1 + 87: } 88: - 89: void update_lcd(void) + 89: void timer_init(void) 90: { - 91: int i = 0, horizontal = 0, vertical = 0; - 92: char alfa[2][LCD_LENGTH]; - 93: - 94: - 95: static char current_row = 0; - 96: static char current_char = 0; - 97: char toLCD0[LCD_LENGTH], toLCD1[LCD_LENGTH]; - 98: char B1[16]; - 99: lcd_goto(0x00); - 100: sprintf(toLCD0, "%s", "Ejdesgaard"); - 101: if(RB1) - 102: { - 103: sprintf(B1, "%s", "True"); - 104: } - 105: else - 106: { - 107: sprintf(B1, "%s", "False"); - 108: } - 109: lcd_puts(B1); - 110: - 111: lcd_goto(0x40); - 112: // sprintf(toLCD1, "%s", "Test"); - 113: lcd_puts("Test1"); - 114: - 115: } - 116: - 117: void main() - 118: { - 119: ///////////////////////////////////////////// - 120: // Running Init's - 121: - 122: // Running init for various components. - 123: //AD_init(); - 124: ad_init(); - 125: rs232_init(); - 126: pic18_io_init(); - 127: lcd_init(0); - 128: - 129: ///////////////////////////////////////////// - 130: // Main loop + 91: TMR1CS = 1; //use external clock + 92: + 93: T1CKPS1 = 1; //1:8 prescale + 94: T1CKPS0 = 1; + 95: + 96: TMR1H = 0xEF; + 97: TMR1L = 0xFF; + 98: + 99: T1OSCEN = 1; //enable oscillator circuit + 100: RD16 = 0; //normal 8 bit writes + 101: TMR1ON = 1; + 102: } + 103: + 104: void pic18_io_init(void) + 105: { + 106: TRISA0 = 1; // analog input + 107: TRISA1 = 0; // Output + 108: TRISB1 = 1; // TRISB1-4 Digital input + 109: TRISB2 = 1; + 110: TRISB3 = 1; + 111: TRISB4 = 1; + 112: } + 113: + 114: void sms_init(void) + 115: { + 116: int i; + 117: + 118: sprintf(global_serial_send,"at+cgsn\r"); + 119: serial_send(); + 120: DelaySek(1); + 121: while(!global_recieve_done) ; + 122: + 123: + 124: sprintf(global_serial_send,"%s", "at+cpin=8043\r"); + 125: serial_send(); + 126: DelaySek(30); + 127: + 128: update_lcd(); + 129: DelaySek(5); + 130: } 131: - 132: while(1) - 133: { - 134: // Checking if A/D convertion is done, and save the data in global_Pot_?? - 135: if(GODONE==0) - 136: { - 137: global_Pot_Hi = ADRESH; - 138: global_Pot_Lo = ADRESL; - 139: GODONE = 0; - 140: } - 141: - 142: update_lcd(); - 143: } - 144: } + 132: + 133: void interrupt interrupt_handler(void) + 134: { + 135: // Finds out what interrupt have been trigged, and starts the respective function. + 136: if(RCIF == 1) // Serial recieve interrupt + 137: { + 138: serial_recieved(); + 139: RCIF = 0; + 140: } + 141: + 142: if(TMR1IF == 1) // timer1 interrupt trigger. + 143: { + 144: timer1_interrupt(); + 145: TMR1IF = 0; + 146: } + 147: } + 148: + 149: + 150: void serial_send(void) + 151: { + 152: int i; + 153: char data_byte; + 154: for(i = 0; i < SEND_BUFFER; i++) + 155: { + 156: data_byte = global_serial_send[i]; + 157: if( data_byte == '\r') + 158: i = (SEND_BUFFER - 1); + 159: TXREG = data_byte; + 160: while(TRMT==0) ; + 161: DelayMs(10); + 162: } + 163: DelayMs(250); + 164: DelayMs(250); + 165: } + 166: + 167: void serial_recieved(void) + 168: { + 169: char data_byte, saved_data[LCD_LENGTH]; + 170: + 171: data_byte = RCREG; + 172: + 173: global_serial_recieve_buffer[global_serial_byte_counter] = data_byte; + 174: if(data_byte == '\r') + 175: { + 176: global_recieve_done = 1; + 177: global_serial_byte_counter = 0; + 178: } + 179: else + 180: { + 181: global_serial_byte_counter++; + 182: } + 183: + 184: } + 185: + 186: void timer1_interrupt(void) + 187: { + 188: TMR1H = 0xEF; + 189: TMR1L = 0xFF; + 190: global_time_counter++; + 191: global_emergency_counter++; + 192: RA1 = !RA1; + 193: } + 194: + 195: void update_lcd(void) + 196: { + 197: if( global_recieve_done == 1 ) + 198: { + 199: lcd_clear(); + 200: lcd_goto(0x00); + 201: lcd_puts(global_serial_recieve_buffer); + 202: global_recieve_done = 0; + 203: } + 204: } + 205: + 206: void send_update(void) + 207: { + 208: sprintf(global_serial_send, "at+cmgs=\"%s\"\r", cell_nr); + 209: serial_send(); + 210: sprintf(global_serial_send, "%d:%d:%d:%d:%d:%d%c", global_sms_counter, global_temp, FIREDET, EMPTYTANK, FEEDING, PWRFAIL, 0x1A); + 211: lcd_goto(40); + 212: lcd_puts(global_serial_send); + 213: serial_send(); + 214: DelayMs(250); + 215: global_sms_counter++; + 216: } + 217: + 218: void convertTemp() + 219: + 220: { + 221: short adVal; + 222: adVal = (global_Pot_Hi << 8) | global_Pot_Lo; + 223: if( adVal >=840 ) + 224: global_temp = 100; + 225: else + 226: global_temp = (adVal / 8.3886); + 227: } + 228: + 229: + 230: + 231: void eeprom_writer(void) + 232: { + 233: char len,i; + 234: + 235: if( global_interval_changed ) + 236: { + 237: len = strlen(cell_nr); + 238: eeprom_write(0, (global_time_interval/60)); + 239: eeprom_write(1, global_sms_counter>>8); + 240: eeprom_write(2, global_sms_counter); + 241: eeprom_write(3, len); + 242: + 243: for (i=0; i= 90 || PWRFAIL == 1 || FIREDET == 0 || FEEDING == 1 || EMPTYTANK == 1) && global_emergency_counter >= 600 ) + 291: { + 292: send_update(); + 293: global_emergency_counter = 0; + 294: } + 295: + 296: if(global_time_counter >= 3600) + 297: { + 298: send_update(); + 299: global_time_counter = 0; + 300: } + 301: // to avoid buffer overrun. + 302: if( global_emergency_counter > 7200 ) + 303: global_emergency_counter = 600; + 304: + 305: // Checking if A/D convertion is done, and save the data in global_Pot_?? + 306: if(GODONE==0) + 307: { + 308: global_Pot_Hi = ADRESH; + 309: global_Pot_Lo = ADRESL; + 310: convertTemp(); + 311: + 312: GODONE = 1; + 313: } + 314: + 315: update_lcd(); + 316: } + 317: + 318: }