fbpx

UART using PIC

Description

The Universal Synchronous Asynchronous Receiver Transmitter (USART) module is one of the two serial I/O modules. It can establish communication between a microcontroller and another device such as Bluetooth, GSM, GPS, RF module, Personal computer.

  • The USART can be configured as a full-duplex asynchronous system that can communicate with peripheral devices, such as CRT terminals and personal computers.
  • It can be configured as a half-duplex synchronous system that can communicate with peripheral devices, such as A/D or D/A integrated circuits, serial EEPROM.
  • The USART can be configured in the following modes:
    1.Asynchronous (full-duplex)
    2.Synchronous – Master (half-duplex)
    3.Synchronous – Slave (half-duplex).
  • Concentrating on sending and receiving data in the UART Mode.
  • UART its name implies it is universal.
  • UART port of a microcontroller can be used to interface to an RS232 port of a personal computer, hence voltage level of UART must be converted to voltage level compatible to RS232.
  • To convert UART voltage level to the RS232 voltage level, you may use max232.
  • Other devices with UART ports such as GSM module, GPS module, Bluetooth module, RF module does not require the UART converter.
  • This device can be the interface to microcontroller directly as long as the voltage level for both ends are compatible with each other.

UART Register

  • The USART module also has a multi-processor communication capability using 9-bit address detection.
  • TXSTA: TRANSMIT STATUS AND CONTROL REGISTER.

Transmit Status And Control Register

  • Bit 7 CSRC: Clock Source Select bit
    Asynchronous mode: Don’t care.
    Synchronous mode:
    1 = Master mode (clock generated internally from BRG)
    0 = Slave mode (clock from external source).
  • Bit 6 TX9: 9-bit Transmit Enable bit
    1 = Selects 9-bit transmission
    0 = Selects 8-bit transmission.
  • Bit 5 TXEN: Transmit Enable bit
    1 = Transmit enabled
    0 = Transmit disabled.
  • Bit 4 SYNC: USART Mode Select bit
    1 = Synchronous mode
    0 = Asynchronous mode.
  • Bit 3 Unimplemented: Read as 0.
  • Bit 2 BRGH: High Baud Rate Select bit
    Asynchronous mode:
    1 = High speed
    0 = Low speed
    Synchronous mode: Unused in this mode.
  • Bit 1 TRMT: Transmit Shift Register Status bit
    1 = TSR empty
    0 = TSR full
  • Bit 0 TX9D: 9th bit of Transmit Data, can be Parity bit.

Receive Status And Control Register

  • RCSTA:
  • Bit 7 SPEN: Serial Port Enable bit
    1 = Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port  pins)
    0 = Serial port disabled
  • Bit 6 RX9: 9-bit Receive Enable bit
    1 = Selects 9-bit reception
    0 = Selects 8-bit reception.
  • Bit 5 SREN: Single Receive Enable bit
    Asynchronous mode: Don’t care.
    Synchronous mode – Master:
    1 = Enables single receive 0 = Disables single receive
    This bit is cleared after reception is complete.
    Synchronous mode – Slave: Don’t care.
  • Bit 4 CREN: Continuous Receive Enable bit
    Asynchronous mode:
    1 = Enables continuous receive 0 = Disables continuous receive
    Synchronous mode:
    1 = Enables continuous receive until enable bit CREN is cleared (CREN overrides SREN)
    0 = Disables continuous receive.
  • Bit 3 ADDEN: Address Detect Enable bit
    Asynchronous mode 9-bit (RX9 = 1):
    1 = Enables address detection, enables interrupt and load of the receive buffer when RSR(8) is set
    0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit.
  • Bit 2 FERR: Framing Error bit
    1 = Framing error (can be updated by reading RCREG register and receive next valid byte)
    0 = No framing error.
  • Bit 1 OERR: Overrun Error bit
    1 = Overrun error (can be cleared by clearing bit CREN)
    0 = No overrun error.
  • Bit 0 RX9D: 9th bit of Received Data (can be parity bit but must be calculated by user firmware).

USART Baud Rate Generator (BRG)

  • Baud Rate Generator provides the required clock for the data transmission and reception.
  • USART module has a dedicated 8-bit baud rate generator which supports both Synchronous and Asynchronous modes.
  • The 8-bit SPBRG register controls the time period of this free-running timer.
  • In Asynchronous mode BRGH, 2nd bit of the TXSTA register also controls the generated baud rate but in Synchronous mode, it is ignored.
  • Baud Rate can be calculated from the following equations, where FOSC is the clock frequency of the microcontroller.

Interfacing Circuit

Transmitting Data through UART

  • Writing a Character
    Void UART_Write(char data)
    {
    while(!TRMT); //Waiting for Previous Data to Transmit completely
    TXREG = data; //Writing data to Transmit Register, Starts transmission
    }
  • Checking Transmit Register
    char UART_TX_Empty()
    {
    return  TRMT; //Returns Transmit Shift Status bit
    }
  • This functions returns 1 if the transmit register is Empty otherwise return 0.
  • It can be used to check whether the written data is completed transmission.

Receiving Data Through UART

  • Reading a Character
    char UART_Read()
    {
    while(!RCIF); //Waits for Reception to complete
    return  RCREG; //Returns the 8 bit data
    }
  • The following function wait till the reception is complete and reads 8 bit data from the Receive Register.
  • Data Received or Not
    char UART_Data_Ready()
    {
    return RCIF;
    }
  • The following function can be used to check whether the data is ready to read from the Receive Register.
  • It uses the flag bit RCIF which will be set when the data reception is completed.

Code

Transmit

 void main()
 {
 UART1_Init(9600);
 Delay_ms(100);
 while (1)
 { 
 UART1_Write("HELLOWORLD");
 Delay_ms(500);
 }
 } 

Receive

 void main()
 {
 char receiveval;
 UART1_Init(9600);
 Delay_ms(100);
 while (1)
 { if (UART1_Data_Ready())
 {
 receiveval = UART1_Read();
 }
 }
 } 

Leave a Reply

Your email address will not be published. Required fields are marked *