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.
- USART asynchronous (full duplex)
- USART synchronous master (half duplex)
- 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
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.
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
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
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)
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
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.
void setting_registers() { TRISCbits.RC6 = 0; //direction of Tx and Rx pins TRISCbits.RC7 = 1; SPBRG = 31; /* set the baud rate 9600 for 20 mhz clock */ SYNC = 0; /* asynchronous */ SPEN = 1; /* enable serial port pins */ CREN = 1; /* enable reception */ TXIE = 0; /* disable tx interrupts */ RCIE = 0; /* disable rx interrupts */ TX9 = 0; /* 8- or 9-bit transmission */ RX9 = 0; /* 8- or 9-bit reception */ TXEN = 1; /* enable the transmitter */ }
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.
void transmit_character(char out) { while(TXIF==0); /*wait for transmit interrupt flag*/ TXREG=out; /*wait for transmit interrupt flag to set which indicates TXREG is ready for another transmission*/ }
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.
char USART_ReceiveChar() { while(RCIF==0); /*wait for receive interrupt flag*/ if(RCSTAbits.OERR) { CREN = 0; NOP(); CREN=1; } return(RCREG); /*received in RCREG register and return to main program */ }
Receiving text in UART communication is possible by receiving each character one by one into a character array.
void receive_string() { char rc_data[10]; unsigned int i; /*array index variable */ for(i=0;;i++) { while(!RCIF); rc_data[i]=RCREG; if(rc_data[i]=='\r') /*character is received until end of the string received */ { rc_data[i]='\0'; break; } } }
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.
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.
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.