Sei sulla pagina 1di 6

To implement UART successfully we first need to know how the protcol works.

Diagram above shows the waveform in which the bits has to be transferred. First is the
start bit.. then 8-bit data and at last a stop bit. There is a formula to calculate the time
which is needed between bits to get correct baudrate.
Below is a software implemented UART, which can be used in C programs. It is written
for Keil software.

Formula to create baud rate:

(((Crystal freq)/12)/32)/(Required baud rate)

For ex : crystal freq=11.0592Mhz & Required baud rate=9600


Then,

(((11059200)/12)/32)/9600=3

(0 to FF) minus 3 will be the count.


There fore we need to load FD count.

Hardware Details:
P89v51rd2
Matrix Keypad:
UART section (RS232):

7-segment Display Section:


Note: Connect CN1 4 pins (1,2,3,4) to 5v to enable seven segments.
Program:

#include <REG51F.H>

/*********DELAY FUNCTION********************************/
// 1. passing parameter is int & no return

delay(int sec) //DELAY IS IN SECONDS, SEC=1 REPRESENTS 1 SEC DELAY


{
int i,j,k;
sec*=10;
for(i=0;i<sec;i++)
for(j=0;j<100;j++)
for(k=0;k<100;k++);
}
/**************DELAY END************************************/

/****************************UART FUNCTION**********************/
void uart(int num) //TO SEND DATA TO PC USING DB-9 SERIAL PORT (COM 1)
{
char alp[16]={'1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G'};

SBUF=alp[num]; //SEND DATA TO PC


while(TI==0);
TI=0; //CLEAR TI TO TRANSMIT NEXT CHAR
delay(1);
}
/******************UART END*************************/

/****DISP IN PC (CALCULATES NUMBER TO DISPLAY FROM 0 TO F)******/

void disp(int temp,int row)


{
int i,col,br,num; // BR IS TEMPORARY VARIABLE, NUM FOR NUMBER TO
DISPLAY
IE=0x00; //DISABLE ALL INTEEUPTS

for(i=0;i<4;i++) //CALCULATES COLUMN


{
(temp&0x01)?col++:br++;
temp>>=1;
if(br==1)
break;
}

row--;
num=(row*4+col); // CALCULATES NUMBER

uart(num); //CALL UART TO SEND NUMBER


}
/*************************************DISP END*******************/

/*************SERIAL INTERRUPT (WHEN WE RECEIVE DATA )***********/


void serial() interrupt 4
{
int
seg[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x
79,0x71};
int rec_data; //TO STORE RECEIVED DATA

IE=0x00; // DISABLE INTERRUPTS


RI=0; // CLEAR RECEIVE FLAG BIT

rec_data=SBUF-48; //STORE RECEIVED DATA (SHOULD BE 0 TO 9) IN INT


FORM
P1=seg[rec_data]; //TO SEVEN SEGMENT DISPLAY
delay(1);
IE=0x90; //ENABL SERIAL INTERRUPT
}
/*************************SERIAL INTERRUPT END***************/

void main()
{
int temp,row; //TEMP IS TEPORARY MEM

IE=0x90; //ENABLE SERIAL INTERRUPT


SCON=0x50; // SERIAL MODE SELECTION
TMOD=0x20; //TIMER 1 IN MODE 2 FOR AUTO RELOAD
TH1=0xFD; // TO SET BAUD RATE 9600
TR1=1; //RUN TIMER1

P2=0x0F;
P2=0x7f; //ENABLE ROW ZERO BY SETTING MSB AS LOW
ACC=P2;
temp=ACC&0x0f;
if(temp!=0x0f) //CHECK FOR KEY PRESSED
{
row=1;
disp(temp,row);
}

P2=0xBf; //ENABLE ROW 2


ACC=P2;
temp=ACC&0x0f;
if(temp!=0x0f)
{
row=2;
disp(temp,row);
}

P2=0xDf; //ENABLE ROW 3


ACC=P2;
temp=ACC&0x0f;
if(temp!=0x0f)
{
row=3;
disp(temp,row);
}

P2=0xEf; //ENABLE ROW 4


ACC=P2;
temp=ACC&0x0f;
if(temp!=0x0f)
{
row=4;
disp(temp,row);
}
}

Potrebbero piacerti anche