What is USART in the PIC18 microcontroller?
The computer transfers the data in two ways, serial and parallel.
Serial communication is effective for long-distance communication while parallel communication is effective for short-distance communication.
When a microcontroller communicates with the outside world it provides the data in bite-sized chunks.
Now the serial data communication also classified into two methods-
1. Synchronous
2. Asynchronous
The synchronous method transfers a block of data at a time, while the Asynchronous method transfers a single byte of data at a time.
It is possible to write software to use either of these methods, but the program can be tedious and long.
For this reason, special IC chips are made by many manufacturers for serial data communications.
These chips are commonly referred to as
1. UART (Universal asynchronous receiver transmitter)
2. USART (universal synchronous asynchronous receiver transmitter)
The PIC18 chip has built-in USART.
Asynchronous serial communication and data framing
The data coming at the receiving end of the data line in a serial data transfer is all 0s and 1s. It is difficult to make sense of the data unless the sender and receiver agree on a set of rules, a protocol on how data is packed, how many bits constitute a character and when the data begins and ends.
Start and stop bit
Asynchronous serial data communication is widely used for character-oriented transmission. while block-oriented data transfer uses the synchronous method.
In the asynchronous method, each character(ASCII) is placed between the start and stop bit this is called framing.
The start bit is always one bit but the stop bit can be 1 or 2 bit. The start bit is always low(0) and the stop bit always high(1).
bit image
Serial port connection
RS232
Tx and Rx pin of PIC18
MAX232
USART
The USART in the PIC18 has both synchronous and asynchronous features. The synchronous mode can be used to transfer data between the PIC and the external peripheral such as ADC and EEPROM.
The asynchronous mode is the one we will use to connect the PIC18 based system to the serial port for the purpose of full-duplex serial data transfer.
Registers of USART
- SPBRG
- TXREG
- RCREG
- TXSTA
- RCSTA
- PIR1
SPBRG register
TXREG register
TXREG is another 8-bit register used for serial communication in the PIC18. For a byte of data to be transferred via the Tx pin, it must be placed in the TXREG register.
TXREG is a special function register and can be accessed like any other register in the PIC18
RCREG register
When the bits are received serially via the Rx pin the PIC18 defames them by eliminating the stop and start bit making a byte out of the data received and then placing it in the RCREG register.
TXSTA(Transmit status and control register)
RCSTA (receive status and control register)
PIR1(Peripheral interrupt request register 1) register
Step for programming the PIC18 to transfer data serially
- The TXSTA register is loaded with the value 20H indicating asynchronous mode with an 8-bit data frame, low baud rate and transmit enabled.
- Make TX pin of PORTC (RC6) output for data to come out of the PIC.
- The SPBRG is loaded with one of the value.
- SPEN bit in the RCSTA register is set HIGH to enable the serial port of the PIC18.
- The character byte to be transmitted serially is written into the TXREG register.
- Monitor the TXIF bit of the PIR1 register to make sure UART ready for the next byte.
- To transfer the next byte go to step5.
Steps for programming the PIC18 to receive data serially
- The RCSTA register is loaded with the value 90H to enable the continuous receive in addition to the 8-bit data size option.
- The TXSTA register is loaded with the value of 00H to choose the low baud rate option.
- SPBRG is loaded.
- Make the RX pin of PORTC an input for data to come into the PIC18.
- The RCIF flag bit of the PIR1 register is monitored for a high to see if an entire character has been received yet.
- When RCIF is raised the RCREG register has the byte. its contents are moved into a safe place.
- To receive the next character go to step5.
CONNECT WITH US