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 disables
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);
Ports for Embedded Systems
243
Using a 16-bit value to set the baud rate requires setting BRG16 = 1 and storing
a value in SPBRGH. This example sets a bit rate of 2400 bps assuming FOSC =
4 MHz:
BAUDCONbits.BRG16 = 1;
SPBRGH = 1;
OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,
0x9f);
! $
Firmware can write individual bytes or arrays of bytes to a port.
Pages:
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273