Before starting USART, some general terms related to communication need to be understood. These terms are explained below.
In this type of communication, both Transmitter (Tx) and Receiver (Rx) work on different clocks which means that they are not synchronized. Start and Stop bits are also sent with each Data byte to identify the data.
In this type of communication, both Tx and Rx are synchronized with the same clock and no Start or Stop bits are used.
When either of the devices can send and receive data at the same instant, they are said to have full-duplex communication.
In this type of communication, a device can either behave as Transmitter or Receiver at an instant which means that a device can’t transmit data when it is receiving and vice versa.
PIC18F4550 has an inbuilt EUSART (Enhanced USART). Normally USART can be configured as asynchronous full-duplex communication or synchronous half-duplex communication. EUSART provides additional capabilities as compared to USART, like Automatic Baud-rate Detection. Automatic baud-rate detection means that during reception there is no need to set the baud rate at controller’s side, EUSART sets it on its own which is certainly an advantage over USART. Baud rate and its calculation have been explained later in this article.
Registers of EUSART:
To use the EUSART of PIC18f4550 microcontroller following registers need to be configured.
1. TXSTA (Transmit Status and Control Register)
TRMT: This is a read only bit which shows the status of Shift register where data is stored.
1 = Transmit Shift Register Empty
0 = Transmit Shift Register Full
BRGH: This bit is configured to decide the speed of Asynchronous serial communication.
1 = High speed
0 = Low speed
SYNC: The mode of communication is selected by this bit.
1 = Synchronous mode
0 = Asynchronous mode
TXEN: This bit is set to high to enable the transmission.
TX9: This bit is set to high while sending 9-bit long data.
1 = Selects 9-bit transmission
0 = Selects 8-bit transmission
2. RCSTA (Receive Status and Control Register)
CREN: This bit is set to high for continuous reception enable. To stop reception the bit is set to zero.
RX9: This bit is used when 9-bit long data is to be received.
1 = Selects 9-bit reception
0 = Selects 8-bit reception
SPEN: This bit is used to enable/disable the serial port. (Tx and Rx pins)
1 = Serial port enabled
0 = Serial port disabled
3. TXREG (EUSART Transmit Register)
The data to be transmitted is stored in TXREG register.
4. RCREG (EUSART Receive Register)
The data to be received is stored in RCREG register.
5. PIR1 (Peripheral Interrupt Request Register 1)
TXIF: This is transmitter interrupt flag bit. It becomes high when the TXREG register is empty.
RCIF: This is receiver interrupt flag bit. It becomes high when the RCREG register is full.
6. PIE1 (Peripheral Interrupt Enable Register 1)
TXIE: This bit is set to high to enable the transmission interrupt.
RCIE: This bit is set to high to enable the reception interrupt.
7. BAUDCON (Baud Rate Control Register)
This register controls the baud rate and some special functionalities of serial communication like auto-baud rate detection, inversion of receiving data etc.
BRG16: This bit is used to enable/disable 16-bit baud rate register.
1 = 16-bit Baud Rate Generator (BRG) – SPBRGH and SPBRG
0 = 8-bit Baud Rate Generator (BRG) – SPBRG only
This means if the BRG16 bit is set to high, the baud rate is decided by both SPBRGH and SPBRG registers; and if it is set to low, only SPBRG register will set the baud rate.
7. SPBRG & SPBRGH (EUSART Baud Rate Generator Register Low Byte and High Byte)
These registers are used to set baud rate for the serial communication. The two registers SPBRG & SPBRGH store lower byte and higher byte respectively.
The baud rate is the speed of data in serial communication. It is also known as bits per second (bps). PIC’s EUSART provides different communication modes like asynchronous, synchronous, high-speed and low speed etc. The baud rate for different EUSART modes can be decided by different formulae. The following table shows the baud-rate formulae for different EUSART communication modes.
Baud Rate Formula
Fosc / [64 (n + 1)]
Fosc / [16 (n + 1)]
Fosc / [4 (n + 1)]
Here : x = Doesn’t matter
Fosc = Crystal frequency
n = Value of SPBRGH : SPBRG Register pair
For example, if a Baud rate of 9600 is to be set for a 12MHz crystal and the mode is 8-bit, Asynchronous, then,
SYNC=0, BRG16=0 and BRGH=0 and the baud rate can be calculated, by using the following formula, as given below.
Objective: To first configure PIC18f4550 for asynchronous low-speed, 8-bit serial communication at 9600 baud rate. Next program the microcontroller to receive the serial data from a PC and transmit back the same data serially to the PC.
Window’s HyperTerminal has used to send and receive the data. To know more about HyperTerminal and working with it, refer to the last section in the Description of Serial port interfacing with 8051. MAX232 has been used as level convertor between controller and PC. The connections are shown in circuit diagram tab.
1. Configuration of EUSART-
· Set the baud rate by putting appropriate value in the SPBRG register.
· SPEN bit of the RCSTA register is set to high to activate serial port (Tx and Rx pins) of the controller.
· TXEN and CREN bits (in TXSTA and RCSTA registers) are set to high to activate serial transmission and serial reception respectively.
2. For Transmission-
· Store the data into TXREG register.
· Wait until transmission flag (TXIF) is equal to zero (PIR1 register).
3. For Reception-
· Wait until reception flag (RCIF) is equal to zero (PIR1 register).
· Store the value of RCREG register in some variable. This value is the received data.