Artificial UART (SOFT UART)

Photo of author

By Jackson Taylor

Transmission
// Here i am sending 2(ascii of 2 =00110010)
//baud rate is 9600
//time of single bit = 1/baud rate  = 1/9600 =104 Micro Seconds
//take time delay always less than to avoid distortion in data. i have taken 102 Micro Seconds
// Similarly for 19200 baud rate time = 52 Micro Seconds
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISA=0;
PORTA=0xff;  //Stop bit
PORTA=0;                 //Start bit
__delay_us(102);
PORTA=0;                //0th bit (LSB)
__delay_us(102);
PORTA=0xff;            //1st bit
__delay_us(102);
PORTA=0;  //2nd bit
__delay_us(102);
PORTA=0;   //3rd bit
__delay_us(102);
PORTA=0xff;   //4th bit
__delay_us(102);
PORTA=0xff;   //5th bit
__delay_us(102);
PORTA=0;  //6thbit
__delay_us(102);
PORTA=0;  //7th bit (MSB)
__delay_us(102);
while(1)
{
PORTA=0xff; //Stop bit
}
}
SOFT-UART Header file for pin RA0 (Transmission)
#include<pic.h>
#define _XTAL_FREQ 16000000
void uartsinglewrite (short int);  //you can select any data type of 8-bit.
                                              //Because here we are using bit level logic for sending data
void uartsinglewrite (short int a)
{
TRISA=0;
PORTA=0;  //Start bit
__delay_us(102);
PORTA=a&1;  //Lsb of bit -0th bit
__delay_us(102);
PORTA= (a&2)>>1; //1st bit
__delay_us(102);
PORTA= (a&4)>>2; //2nd bit
__delay_us(102);
PORTA=(a&8)>>3;  //3rd bit
__delay_us(102);
PORTA= ((a&16)>>4); //4th bit
__delay_us(102);
PORTA=(a&32)>>5; //5th bit
__delay_us(102);
PORTA=(a&64)>>6; //6thbit
__delay_us(102);
PORTA=(a&128)>>7;  //7th bit
__delay_us(102);
PORTA=1; //Stop bit
}
Reception 
//baud rate is 9600
// Received data is shown in PORTB
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
short int a=0;
ADCON1=0x7; //setting PORTA as digital pins
TRISA=1;
TRISB=0;
PORTA=0;
PORTB=0;
while(1)
{
if(RA0==0) //checks starting of signal
{
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 1;}
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 2;}
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 4;}
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 8;}
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 16;}
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 32;}
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 64;}
__delay_us(102);
if(RA0==0)PORTB |= 0;
else{PORTB |= 128;}
__delay_ms(1000);
PORTB=0;
}