Serial Port Complete - Latest Microcontroller projects

(lily) #1
Ports for Embedded Systems


  1. If the RCSTA register indicated an error, set CREN = 0 to clear the error. To
    enable receiving another byte, set CREN = 1.


  


Both the PICBASIC PRO and MPLAB C18 compiler provide support for con-
figuring and reading and writing to serial ports. The PICBASIC PRO compiler
defines statements for accessing up to two hardware serial ports and two firm-
ware-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 spec-
ified 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

Free download pdf