--- trunk/PIC/main.c 2007/01/29 15:07:29 9 +++ trunk/PIC/main.c 2007/01/29 15:29:28 10 @@ -1,13 +1,120 @@ /* H7 -projekt opgave */ - + +/////////////////////////////////////////////////////////////////////// +//Includes #include #include "delay.h" #include "i2c.h" #include "lcd.h" - +///////////////////////////////////////////////////////////////////// +//Defines #define TC74 0x9A /* I2C TC74 IC */ +////////////// +// Enums + +enum SlipState{ + SlipNormal, + SlipEscaped, + SlipError, + SlipStopped +}; + +////////////////////////////////////////////////////////////////// +//Globale data +// Alle globale variabler bruger "global_" som prefix +unsigned char global_comm_buffer[10]; +unsigned char global_comm_length; +unsigned char global_comm_index; +bit global_comm_ready; +bit global_comm_recieving; + +unsigned char global_comm_state; + + + +/////////////////////////////////////////////////////////////////// +// Interrupt funktioner + +void recieve_interrupt(void) +{ +} + +void transmit_interrupt(void) +{ +} + +void interrupt interrupt_handler(void) +{ + if (RCIF == 1) + { + recieve_interrupt(); + RCIF = 0; + } + if (TXIF == 1) + { + transmit_interrupt(); + TXIF = 0; + } +} + +/////////////////////////////////////////////////////////////////// +// Alm funktioner + +void interrupt_init(void) +{ + TXIE = 1; //Enable AUX TX interrupt - testes med TXIF; + RCIE = 1; //Enable AUX Recieve interrupt - testes med RCIF; + + IPEN = 0; // IPEN=Interrupt Priority ENable bit - her bruges ingen prioritet + + PEIE = 1; // PEIE = PEriphal Interrupt Enable + + TXIF = 0; //nulstil intterupt flag + RCIF = 0; + + GIE = 1; //Global interrupt enable bit +} + +void serial_init(void) +{ + SPEN = 0; //Make sure serial port is disabled + + // snupset fra Kim H Pedersens serialmain.c + // Set Port C bit 6 output (TxD) and bit 7 input (RxD) + TRISC6 = 0; + TRISC7 = 1; + + SPBRG = 25; // x = Fosc/(16 * Baud Rate) - 1 + // x = 4000000/(16 * 9600) - 1 + // x = 25.0417 ~ 25 + // Baud Rate = Fosc/(16 * (x+1)) + // Baud Rate = 4000000/(16 * (25+1)) + // Baud Rate = 9615 + TXSTA = 0x24; // TXSTA7 = 0 Don't care in asynchronous + // TXSTA6 = 0 8-bit transmission + // TXSTA5 = 1 Transmit enabled + // TXSTA4 = 0 Asynchronous mode + // TXSTA3 = 0 Unimplemented + // TXSTA2 = 1 High speed + // TXSTA1 = 0 TSR empty + // TXSTA0 = 0 9th bit */ + RCSTA = 0x90; // RCSTA7 = 0 Serial port disabled + // RCSTA6 = 0 8-bit reception + // RCSTA5 = 0 Don't care in asynchronous + // RCSTA4 = 1 Receiver enabled + // RCSTA3 = 0 Don't care (8-bit operation) + // RCSTA2 = 0 No framing error + // RCSTA1 = 0 Overrun error bit + // RCSTA0 = 0 9th bit */ + + //config completed + SPEN = 1; //Enable Serial port +} + + + void i2c_init(void) { #ifdef I2C_MODULE @@ -22,21 +129,40 @@ #endif } + + char ReadTemp(void) { char temp; - i2c_WriteTo(TC74); + i2c_WriteTo(TC74); //write to, will automatically send a start condition i2c_PutByte(0x00); //tell TC74 we want to read i2c_ReadFrom(TC74); temp = i2c_GetByte(I2C_LAST); i2c_Stop(); //assert a stop condition on SDA & SCL return temp; -} +} + + +/////////////////////////////////////////////////////////////////// +//Main + void main(void) { lcd_init(0); //init in 4-bit mode i2c_init(); + serial_init(); //9600 8N1 + + interrupt_init(); + + global_comm_length = 0; + global_comm_ready = 0; + + + global_comm_state = SlipNormal; + +// initialisation completed and we are ready to recieve commands - enable serial reception + CREN = 1; while (1) {