#define FOSC (long) 4000000 // for some reason this macro generates a 'loss of significant bits' // compile error when used, so use the baud rate constants below. // (This might be a limitation of the freeware cc5x compiler.) // #define BAUD_FACTOR(bit_rate) (char) (FOSC / (bit_rate * 16) - 1) // baud rate constants (assuming 4Mhz clock) #define B2400 103 #define B9600 25 // macros #define uart_byte_ready() RCIF #define uart_clear_error() CREN = 0; uart_init(B9600) void uart_init(char W) // initialize the uart // W is baud rate constant (see above) { SYNC = 0; // asynchronous serial i/o BRGH = 1; // high speed clock SPBRG = W; // set baud rate register SPEN = 1; // enable serial port TX9 = 0; // transmit 8 data bits TXEN = 1; // enable serial transmit CREN = 1; // enable serial receive RX9 = 0; // receive 8 data bits } void uart_putb(char W) // transmit a byte via the uart // W is the byte to transmit { while (!TXIF) // wait until transmit register is empty ; TXREG = W; // put the byte in the transmit register } char uart_getb(void) // wait for a byte from the uart // returns the byte received { while (!RCIF) // wait for a byte to be received ; return RCREG; // fetch & return the byte } void uart_puthex(char c) // transmit a byte as 2 hex characters { char dig; dig = c >> 4; // get high digit if (dig <= 9) dig = dig + '0'; else dig = dig + 'A' - 10; uart_putb(dig); dig = c & 0x0F; // get low digit if (dig <= 9) dig = dig + '0'; else dig = dig + 'A' - 10; uart_putb(dig); } #define uart_error() (RCSTA & 0b00000110)