UART Interfacing with PIC Microcontroller

///UART Interfacing with PIC Microcontroller
  • UART Interfacing with PIC Microcontroller

UART Interfacing with PIC Microcontroller

UART is the abbreviation for Universal Asynchronous Receiver/Transmitter and is a commonly used hardware module for serial communication based on communication protocols like RS 232. This chapter discusses how such a device is appropriately interfaced with a PIC microcontroller. The detailed technical process is given below.

USART and UART

UART is a commonly used hardware module for serial communication based on communication protocols like RS 232. Serial communication can be of a synchronous or asynchronous type. A common clock will guide both the receiver and the transmitter in synchronous communication which is absent in asynchronous communication. UART is the most common method of asynchronous serial communication between two devices. UART communication is carried out using two lines Tx (transmission line) and Rx (reception line). It is possible to transmit and receive data simultaneously (full duplex) using UART interface. PIC microcontrollers possess (most of them) an inbuilt USART hardware which can be configured in three different modes.

  1. USART asynchronous (full duplex)
  2. USART synchronous master (half duplex)
  3. USART synchronous slave (half duplex)

Rs-232 Level Converter

RS-232 is a serial communication protocol for the transmission of data between two devices. The protocol defines a logic 0 with voltage in the range of +3V to +15V and a logic 1 with voltage in between -3V to -15V. This voltage level is incompatible with a CMOS or TTL logic family device since the voltage levels are 0V for a logic 0 and 5V for logic 1. This mismatch in the voltage levels demands the requirement of a voltage level shifter to convert RS-232 logic levels into TTL logic levels and vice versa. Most commonly used voltage level shifter IC for this purpose is MAX232.

UART Interfacing with PIC Microcontroller

UART interfacing with PIC Microcontroller

UART interfacing with PIC circuit

The DB9 connector is an analog connector mainly used for serial connections for the asynchronous transmission of data as provided by RS-232 standard. Here is the pinout of PC DB9 connector.

pc db9 connector pinout

DB9 Connector pinout

USART Registers Associated with PIC 18F4550

PIC18F4550 has an in-built EUSART (Enhanced USART) module. The module has additional features like automatic baud rate detection from the conventional UART modules in the PIC microcontrollers. Automatic baud rate detection is the technique in which the baud rate is automatically detected when data is being received at the controller side. The registers associated with the module are explained below.

TXSTA

txsta-register-pic18f4550

TRMT– Status (read-only ) bit shows the status of transmit shift register. The bit is set when a transmission is complete and gets cleared when a transmission is in progress.

BRGH- Configured to determine the asynchronous communication, set for high speed and clear for low speed.

SYNC- The mode of communication is selected by this bit. The bit is made set for synchronous mode and is cleared for asynchronous mode.

TXEN- This bit is set high to enable the transmission.

TX9- This bit is set to high while sending 9-bit long data (set for 9-bit data and cleared for 8-bit data).

RCSTA

rcsta-register-pic18f4550

CREN – Set to enable continuous reception. Clearing this bit will stop reception.

RX9- This bit is used when 9-bit long data is to be received. The bit is set to select 9-bit reception cleared to select 8-bit reception.

SPEN- This bit is used to enable/disable the serial port.(Tx and Rx pins). Set to enable the port and is cleared to disable the serial port.

SREN– This bit is used only in synchronous mode.

ADDEN– Address detection enable bit used in 9-bit data format.

FERR & OERR – Framing error bit and overrun error bit. Bits are set when corresponding errors occur.

TXREG

TXREG is the transmit buffer register in which data to be transmitted is stored.

RCREG

RCREG is the receive buffer register in which the received data is stored.

PIR1(peripheral interrupt request register)

pir1-register-pic18f4550

RCIF – Receive interrupt flag sets when data received in the RCREG register. Receive interrupt occur if enabled.

TXIF – Transmit interrupt flag sets when data in the TXREG register is transmitted and is empty.

PIE1

RCIE – Setting this bit will enable the receiver interrupt.
TXIE– Setting this bit will enable the transmission interrupt.

BAUDCON

baudcon-register-pic18f4550

This register controls the baud rate generation and some other UART features such as automatic baud rate generation, inversion of receiving data etc.

BRG16- This bit enables the 16-bit baud rate generation. When this bit is set baud rate is generated by both SPBRG and SPBRGH registers when it is cleared the baud rate is generated by SPBRG register only.

Baud Rate Settings

Baud rate is the measure of the speed of data transfer in serial communication in units of symbols per second. It may also be considered as the number of bits per second under some circumstances. Baud rate can be configured for different modes of UART using different formulae.

For example, if a 9600 baud rate is to be used for 8-bit asynchronous mode of data transfer baud rate is calculated with the formula

Baud rate = FOSC/[64 x (SPBRG+1)]

SPBRG+1=FOSC/(64 x Baud rate)

SPBRG = [20000000/(64 x 9600)]-1

SPBRG =31.555 (approximately 32)

SPBRG register should be loaded with 31 in order to generate a speed of 9600 bits per second at 20 MHz clocks.

Firmware

Developing the firmware for UART communication involves configuring the registers as the first step. The direction register bits of the microcontroller should be configured in such a way that Rx pin is set as input and Tx pin as an output.

Setting the register bits appropriately would configure the UART module for desired baud rate. The transmission of a character serially using the module would require the character to be loaded to the transmit shift register and wait until the transmission is complete.

The data received from the other device will be stored in the receive buffer register. To read the character received, we have to wait until the receive buffer is full.

Receiving text in UART communication is possible by receiving each character one by one into a character array.

Debugging Techniques

Shorting Tx and Rx pins

By shorting the Tx and Rx pin of the DB9 cable we can check for the error at the cable side by observing at the serial monitor. Shorting the Tx and Rx pin will echo the character entered from the keyboard on the serial monitor.

uart-debugging-shorting

Shorting 3rd and 2nd pin will generate echo, so we can confirm whether the DB9 cable is good or not.

UART communication debugging can also be done by shorting at the level shifter output.

uart-debugging-shorting-level-shifter

Shorting the T1/T2 out and R1/R2 in the level shifter will echo the character entered from the keyboard. This method is employed to check the error with MAX232 IC.

Software/tools

Several free software are available for displaying and debugging the serial communication. RealTerm is one such software which is widely used. Baud rate, port in which DB9 connected need to be set in the RealTerm settings. This software can be used for visualisation of the serially transmitted data. A RealTerm window is shown below.

realterm-serial-capture-program

 

 

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
Spread the love, share this

2 Comments

  1. Anh Minh June 17, 2018 at 8:43 pm

    how your can return text; in void receive text(), please fix it

    • OpenLabPro June 21, 2018 at 10:02 am

      We’ve used a pointer here. You can return the first byte’s address of the array.

Leave A Comment

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
close-link
25% OFF - OpenLabPro Course
Use Coupon: CRS25UL

 
PIC Microcontroller Primer
Want to learn indepth programming, peripheral interfacing and driver development?
close-link
Launched: IoT online Course - 25% Off when you complete your order today.
Checkout Now
close-image

Subscribe To our Email Newsletter

Be the first to get latest updates and exclusive content straight to your email inbox.
Send me Tutorials and Guides
close-link