Ports for Embedded Systems
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. Before writing
to a port, firmware should wait for TXIF = 0, indicating that the port isn’t busy
sending a byte.
The hserout statement waits for TXIF = 0 and writes data to the port.
Each of the hserout statements below write the same value (41h, the code for
the character “A”) to the port:
hserout ["A"]
hserout [$41]
test var byte
test = $41
hserout[test]
An hserout statement can also write multiple bytes to a port. This statement
writes a string:
hserout [“hello, world"]
This statement writes the values stored in a byte array:
test var byte[2]
test[0] = "O"
test[1] = "K"
hserout[STR test]