#include #include "barcode.h" /************************* globals ************************/ char global_barcode_buffer[14]; /*********************** module vars *************************/ unsigned char barcode_length; bit barcode_ready; /************************* function *************************/ void barcode_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 */ SPBRG = 130; //BRG værdi for 20Mhz MCU 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 //Set module-scope variables barcode_length = 0; barcode_ready = 0; //Configure interrupts TXIE = 0; //Disable 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 barcode_reset(void) { barcode_length = 0; barcode_ready = 0; } unsigned char barcode_is_ready(void) { return barcode_ready; } unsigned char barcode_get_length() { return barcode_length; } /* interrupt entry function - barcode is the only module using interrupts */ void /*interrupt*/ barcode_interrupt(void) { char data = RCREG; RCIF = 0; if (barcode_ready == 0) { if (data == '\r') { return; } else if (data == '\n') { barcode_ready = 1; } else { global_barcode_buffer[barcode_length++] = RCREG; } } }