To
enable receiving another byte, set CREN = 1.
Both the PICBASIC PRO and MPLAB C18 compiler provide support for configuring
and reading and writing to serial ports. The PICBASIC PRO compiler
defines statements for accessing up to two hardware serial ports and two firmware-
only serial ports. The C18 compiler provides a series of library functions
for accessing serial ports. For chips that have more than one USART, the C18
compiler enables firmware to specify which USART to access. The compiler
also provides functions that implement one or more firmware UARTs using any
spare port pins. The examples that follow are for hardware UARTs.
3
To prepare to use the serial port for asynchronous communications, firmware
must set the EUSART to asynchronous mode, set the bit rate and number of
data bits, enable the port and the receiver, and enable the transmit and receive
interrupts if desired.
DEFINE statements can set many port parameters, including the values in the
TXSTA and RCSTA registers:
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
The HSER_BAUD definition sets the value in the SPBRG register for the specified
bit rate using the selected setting of the TXSTA register??™s BRGH bit:
DEFINE HSER_BAUD 2400
An alternate way to set the bit rate is to write a value directly to SPBRG:
DEFINE HSER_SPBRG 19h
If FOSC doesn??™t equal the default value of 4 MHz, include a DEFINE OSC
statement to specify the FOSC frequency in MHz:
DEFINE OSC 20
DEFINE HSER_BAUD 2400
Chapter 11
242
The example below assumes a 4-MHz oscillator and sets the bit rate to 2400
using a 16-bit value.
Pages:
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272