--- trunk/Embedded/main.c 2007/11/29 13:23:57 100 +++ trunk/Embedded/main.c 2007/12/03 14:24:50 137 @@ -1,24 +1,60 @@ #include #include #include - #include "lcd.h" +#include "Delay.h" +// Delay.h is included inside lcd.c -void AD_init(void) -{ - ADON = 1; +#define LCD_LENGTH 16 +#define LCD_ROWS 2 + + +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]; +bit global_recieve_done = 0; +int global_serial_byte_counter = 0; + +void serial_recieved(void); + +// Nicked from H7 +void ad_init(void) +{ + // 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. + CHS0=0; + CHS1=0; + CHS2=0; + + //Only AN0 is selected for AD and with Vdd/Vss as limits + PCFG0=0; + PCFG1=1; + PCFG2=1; + PCFG3=1; + + //Result is left justified + ADFM=0; + + //Fire up for A/D converter module + ADON=1; } void rs232_init(void) { SPEN = 0; // Serial Port Enable Bit... 0 = disabled - TRISC6 = 0; + TRISC6 = 0; TRISC7 = 1; - SYNC = 0; // SYNC switches between async(0) and sync(1) mode. - SPBRG = 25; - TXSTA = 0x24; - RCSTA = 0x90; - SPEN = 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) @@ -26,42 +62,102 @@ // Assumes that all interrupts default is 0 PEIE = 1; GIE = 1; - RCIE = 1; + RCIE = 1; // Recieve interrupt enable. + IPEN = 0; // Nfo interrupt priority + TXIE = 0; // Serial interrupt enabled } void pic18_io_init(void) { - TRISA0 = 1; - TRISB1 = 1; + TRISA0 = 1; // analog input + TRISB1 = 1; // TRISB1-4 Digital input TRISB2 = 1; TRISB3 = 1; TRISB4 = 1; } -void interrupt_recieve_handler(void) +void sms_init(void) { - // Handle recieve inputs... } void interrupt interrupt_handler(void) -// Finds out what interrupt have been trigged, and starts the respective function. { - if(RCIF == 1) + // Finds out what interrupt have been trigged, and starts the respective function. + if(RCIF == 1) // Serial recieve interrupt { - interrupt_recieve_handler(); + serial_recieved(); RCIF = 0; } } - + + +void serial_send(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); + } +} + +void serial_recieved(void) +{ + char data, saved_data[LCD_LENGTH]; + + data = RCREG; + + global_serial_recieve_buffer[global_serial_byte_counter] = data; + if(data == '\r') + { + global_recieve_done = 1; + global_serial_byte_counter = 0; + } + else + { + global_serial_byte_counter++; + } + +} + void main() { - AD_init(); +///////////////////////////////////////////// +// Running Init's + + // Running init for various components. + //AD_init(); + ad_init(); rs232_init(); pic18_io_init(); + lcd_init(0); + interrupt_init(); - if(GODONE==0) +///////////////////////////////////////////// +// Main loop + + while(1) { + // 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; + GODONE = 1; + } + if( global_recieve_done == 1 ) + { + lcd_clear(); + lcd_goto(0x00); + lcd_puts(global_serial_recieve_buffer); + global_recieve_done = 0; + } } }