Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 11


The example below assumes a 4-MHz oscillator and sets the bit rate to 2400
using a 16-bit value. In the TXSTA register, bit 2 (BRGH) is set to 1 to select
using the high-speed value. In the BAUDCON register, bit 3 (BRG16) is set to
1 to enable using a 16-bit value in SPBRG and SPBRGH. The value stored in
the registers is 19Fh.
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRGH 1
DEFINE HSER_SPBRG 9fh
BAUDCON = 8

 The OpenUSART function sets the port parameters. The statement below dis-
ables the transmit and receive interrupts, sets the EUSART to asynchronous
mode, configures the port for 8 data bits, enables the receiver, and sets the bit
rate to 2400 bps assuming FOSC = 4 MHz. The final parameter is the value for
the SPBRG register.
BAUDCONbits.BRG16 = 0;


OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,
0x19);
This example is identical except FOSC = 20 MHz, which requires a different
value in SPBRG:
OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,
0x81);
Free download pdf