1: #include 2: #include "sci.h" 3: 4: /* Routines for initialisation and use of the SCI 5: * for the PIC processor. 6: */ 7: 8: /* other options: 9: * frame errors 10: */ 11: 12: unsigned char 13: sci_Init(unsigned long int baud, unsigned char ninebits) 14: { 15: int X; 16: unsigned long tmp; 17: 18: /* calculate and set baud rate register */ 19: /* for asynchronous mode */ 20: tmp = 16UL * baud; 21: X = (int)(FOSC/tmp) - 1; 22: if((X>255) || (X<0)) 23: { 24: tmp = 64UL * baud; 25: X = (int)(FOSC/tmp) - 1; 26: if((X>255) || (X<0)) 27: { 28: return 1; /* panic - baud rate unobtainable */ 29: } 30: else 31: BRGH = 0; /* low baud rate */ 32: } 33: else 34: BRGH = 1; /* high baud rate */ 35: SPBRG = X; /* set the baud rate */ 36: 37: SYNC = 0; /* asynchronous */ 38: SPEN = 1; /* enable serial port pins */ 39: CREN = 1; /* enable reception */ 40: SREN = 0; /* no effect */ 41: TXIE = 0; /* disable tx interrupts */ 42: RCIE = 0; /* disable rx interrupts */ 43: TX9 = ninebits?1:0; /* 8- or 9-bit transmission */ 44: RX9 = ninebits?1:0; /* 8- or 9-bit reception */ 45: TXEN = 1; /* enable the transmitter */ 46: 47: return 0; 48: } 49: 50: void 51: sci_PutByte(unsigned char byte) 52: { 53: while(!TXIF) /* set when register is empty */ 54: continue; 55: TXREG = byte; 56: 57: return; 58: } 59: 60: unsigned char 61: sci_GetByte(void) 62: { 63: while(!RCIF) /* set when register is not empty */ 64: continue; 65: 66: return RCREG; /* RXD9 and FERR are gone now */ 67: } 68: 69: unsigned char 70: sci_CheckOERR(void) 71: { 72: if(OERR) /* re-enable after overrun error */ 73: { 74: CREN = 0; 75: CREN = 1; 76: return 1; 77: } 78: 79: return 0; 80: } 81: 82: #define sci_PutNinth(bitnine) (TX9D = bitnine?1:0;) 83: 84: unsigned char 85: sci_GetNinth(void) 86: { 87: while(!RCIF) 88: continue; 89: 90: return RX9D; /* RCIF is not cleared until RCREG is read */ 91: } 92: 93: unsigned char 94: sci_GetFERR(void) 95: { 96: while(!RCIF) 97: continue; 98: 99: return FERR; /* RCIF is not cleared until RCREG is read */ 100: } 101: