Site icon Ercan Koçlar

UART Communication Protocol and MicroC Library

uart-kapak

uart-kapak

This post is also available in: Türkçe (Turkish)



What is UART?

UART Communication Protocol

Pros Cons
The system is costless, only 2 wires are used and connected to GND (no GND on wireless communication) Can be used in applications where high speed is not required
System works with only 1 receiver and 1 transmitter As with other serial communication protocols, more than one unit can not be connected on the same line. There is no master-slave relationship, communication takes place between only 2 units
It’s not complicated, data is verified with parity bit quickly The communication receiver and the transmitter must communicate at the same speed
Used in systems like RF transmitter-receiver , ethernet , internet, etc. and PC – PIC or PC – PC connections Takes time in sending large data
It can be used at long distances with necessary precautions as opposed to serial communication protocols like I2C or SPI. Since there is a serial communication protocol, all data must be fragmented and sent in series.
No clock pulse is needed during communication. At one time, the data sending size is limited to a maximum of 9 bits

 

How Does UART Protocol Work

 

A-  UART Protocol Data Sending and Receiving (Tx – Rx)

Data that must be observed in the UART protocol

 

A1 – Start Bit

Sending the start bit at UART protocol

 

A2 – Main Data

In UART protocol it is started to transmit data from LSB

 

A3 – Parity Bit

Calculating Parity Bit at UART protocol

 

A4 – End Bit

UART Protocol sending end bit

 

B-  Calculate the Desired Speed and Time Required for Communication in the UART Protocol

 

UART Protocol Result


 

UART Protocol MicroC Library

Functions

 

UART_VERI_TAMPONU General Series

 

– UART_ESLIK_BIT_HESAPLAMA Function

Function : unsigned char UART_ESLIK_BIT_HESAPLAMA(unsigned char VERI)

Purpose :

Parameters :

Type of Use:

UART_ESLIK_BIT_HESAPLAMA(VERI);//variable or data can be written directly

Feedback : Returns the required parity bit

 

UART_GONDERILECEK_VERI_AYIKLAMA Function

Function: void UART_GONDERILECEK_VERI_AYIKLAMA(unsigned char VERI)

Purpose: uploads the splitting the main data to  “uart_veri_tamponu” before sending it

Parameters: 

Type of Use:

UART_GONDERILECEK_VERI_AYIKLAMA(VERI);

Feedback:

 

– UART_VERI_GONDER Function

Function :  void UART_VERI_GONDER(unsigned char VERI)

Purpose : 8 bits data is sent to receiver ( it calculates the parity bit itself and there is no need to calculate it)

Parameters :

Type of Use :

bilgi=0b00001111;

UART_VERI_GONDER(bilgi) ; // sends 8 bits of data assigned to the info variable

Feedback: Does not do feedback

UART_AYIKLANMIS_VERIYI_BIRLESTIR Function

Function: unsigned char UART_AYIKLANMIS_VERIYI_BIRLESTIR(unsigned char *veri_hatasi)

Purpose: It takes the fragmented incoming data as a whole and retrieves it from the 8 bits carrying meaningful information.

Parameters:

Type of Use:

unsigned char hata_kodu;

UART_AYIKLANMIS_VERIYI_BIRLESTIR(&hata_kodu);//the error code variable is assigned the value that the pointer receives

Feedback:

 

– UART_VERI_AL Function

Function : unsigned char UART_VERI_AL(unsigned char *hata_kodu)

Purpose: Receives and checks the incoming 8-bit data from transmitter

Parameters :

Type of Use:

veri=UART_VERI_AL(hata) ;//the incoming information is assigned to the data variable

Feedback :

 

– Determination of Communication Speed

#define UART_ILETISIM_HIZI Delay_us(833);// (1/bps)*1000000 =xxxus

 

Function Application

Transmitter Code Example

 
//UART PIN IDENTIFICATION
 sbit UART_RX at RC7_bit;
 sbit UART_TX at RC6_bit;
 sbit UART_RX_Direction at TRISC7_bit;
 sbit UART_TX_Direction at TRISC6_bit;*/

void  main()
{
    unsigned char veri;
    ADCON1=13;
    CMCON=7;

    veri=0b00001111;//8 bit our raw data

    UART_VERI_GONDER(veri) ; //datas are sent from transmitter Pin 
}

 

Receiver Code Example

//UART PIN IDENTIFICATION
 sbit UART_RX at RB2_bit;
 sbit UART_TX at RB3_bit;
 sbit UART_RX_Direction at TRISB2_bit;
 sbit UART_TX_Direction at TRISB3_bit;


void  main()
{
    unsigned char veri =0 ,hata=0;
    ADCON1=13;
    CMCON=7;
   
   //One LED is connected to the Pins and closed while starting
    trisb.rb1=0;
    portb.rb1=0;
  

 do{
     veri=UART_VERI_AL(&hata) ;//receiving data function-
   }while;
/*note:if it is desired to leave the above loop condition to the user, the 0 start bit can be waited in the wired communication,
 but at least 8 bit pre-authentication data must be used in the wireless communication, 
so the data communication start cycle is set here.*/

if(hata==0)//if there is no error in the data transfer - if the pointer read is zero, 
then there is no error.
  {  
    if(veri==0b00001111)
      {
        portb.rb1=1;// if the data is received correctly, open the LED       }
  }
}

RESULT


Library Files


References


 

This post is also available in: Türkçe (Turkish)

Exit mobile version