UART communication with a PIC
UART communication is an easy way to communicate between two microcontrollers, between a microcontroller and some peripheral, or even between the microcontroller and the computer. UART serial communication is the equivalent of RS-232 on a computer, though the logic levels are different and requires a converter. The UART is easy to use, but you have to get the setup exactly right or else it won’t work. It would help to have access to an oscilloscope to make sure your output looks reasonable, but after we’re through, hopefully you won’t need it.
On the hardware side of things, life is great. Hook the transmit end of one device to the receive end of the other. Do the same thing with the other device. And there you are. No capacitors, no pull-up resistors, nothing complicated. Quite nice.
With that out of the way, let’s approach the not much more difficult software side of things. First, we should familiarize ourselves with some of the variables that we’re going to need and how they’re going to affect our communication.
SPBRG – Serial Port Baud Rate Generator: This eight bit register is used in conjunction with SPBRGH to set the baud rate based on the frequency of the clock.
SPBRGH- Serial Port Baud Rate Generator High: This is used if you want to slow down the baud rate even more than you can with SPBRG. As you can see in the equation below, the bigger these two numbers are, the lower your baud rate will be. This equation assumed asynchronous, 8-bit communication.
BRG16 – 16 bit Baud Rate Generator: Set this bit if you want 16-bit communication instead of 8. Setting this will automatically cause BRGH to be ignored.
BRGH – High Baud Rate Generator: This changes that 64 in the equation up there to 16, allowing faster baud rates.
SYNC – Synchronous/Asynchronous Communication: Set this bit if you have a peripheral that you want to run off a master clock in synchronous mode. Clearing the bit allows asynchronous communication.
TXEN – Transmit Enable: This enables the transmitter circuitry of the UART.
SPEN – Send Enable: This starts the UART circuitry and automatically makes some configuration changes to enable sending through the UART.
CREN – Receive Enable: This starts the UART circuitry and automatically makes some configuration changes to enable receiving through the UART.
RCREG – Receive Register: The value received will be placed in this 8-bit register.
TXREG – Transmit Register: The 8-bit value to be transmitted gets placed in this register. Placing the value inside this register initiates the transmission.
RCIF – Receive Interrupt Flag: When the RCREG is full, this bit gets set.
TXIF – Transmit Interrupt Flag: This bit goes high when the transmission register becomes empty, signifying that transmission is complete.
Almost all of this information was gathered from different parts of data sheets from Microchip. I maintain that your best reference is the data sheet for your PIC. However, I’m just trying to make it so you don’t have to wade through so much.
In the next post, we’ll put these pieces together to make a fully functional UART and figure out the little quirks for setting everything up.
