UART (Universal Asynchronous Receiver Transmitter), a hardware component which are available in almost every microcontroller is used to transmit a byte of data serially through a single wire. It converts a parallel sequence of data into serial data i,e bitwise data to communicate between devices through single wire.
To perform communication between a PC and devices supporting UART, a line driver is required which converts a line voltage (RS232 voltage level in serial port of PC) to TTL level. One of most commonly used line driver is MAX232. But in this recent period most of electronics devices are available which can serially transmit data without the use of line driver. It means they are TTL compatible.
Serial Interface:-
AT89s52 provides synchronous and asynchronous communication modes. It operates as a UART in three full-duplex mode(Mode1, Mode 2, Mode3). Data transfer rates known as Baud Rate in serial communication are measured in bps(bits per second).Standard Baud Rate are multiple of 9600bps.
Among 4 communication mode (Mode0, Mode1, Mode2, Mode3), Mode 2 is mostly used. So, we will learn to communicate in this mode.Two special function registers SBUF and SCON are used. Data to be transmitted is placed in SBUF reg and data received is hold by SBUF reg. SCON reg is programmable reg used to specify the mode of communication and control the transmission and reception of data serially. Details on these registers are recommended to study from datasheet.
Generating Baud Rate:-
The machine cycle frequency is equal to Xtal frequency divided by 12 and UART frequency (Timer 1) is equal to Machine cycle frequency divided by 32.For accurate Baud Rate generation we use xtal frequency of 11.0592MHZ.
To generate Baud Rate for eg 9600bps, we first divide Xtal freq by 12. The obtained value is again divided by 32. Now, the obtained value is divided by desired baud rate. The negation of obtained result is then placed in TH1 register for generation of Baud Rate.
11.0592MHZ / 12 = 921600HZ
921600HZ / 32 = 28800HZ
28800HZ / 9600 = 3
Thus, -3 (decimal value) is to be loaded in TH1 reg.To obtain Hexa Decimal value add decimal value to FFh and again add 1.
eg:- FFh - 3 + 1 = FDh
Now, I will present you the step by step procedure to transmit and receive a byte of data.
To serially transmit a byte of data:-
- First, TMOD reg is loaded with value of 20h (Timer 1in Mode 2 to set Baud Rate).
- Then, TH1 is loaded with required baud rate value as calculated above.
- SCON reg is loaded with 50h (Mode 1 serial communication).
- TR1 is set to start timer.
- TI is cleared.
- The data to be transferred is placed in SBUF reg.
- Then TI flag is monitored. If TI flag is set, then a byte of data is transmitted successfully.
The source routine to transmit a byte of data looks like:-
unsigned char receive (unsigned char d )
{
TMOD=0x20; // timer1 8 bit auto reload mode
TH1=0xfd; //set 9600 baud rate
SCON=0x50; //serial mode 1 with receive enable
TR1=1; // start timer
SBUF=d; while(TI==0);
TI=0;
}
unsigned char receive (unsigned char d )
{
TMOD=0x20; // timer1 8 bit auto reload mode
TH1=0xfd; //set 9600 baud rate
SCON=0x50; //serial mode 1 with receive enable
TR1=1; // start timer
SBUF=d; while(TI==0);
TI=0;
}
To serially receive a byte of data:-
- First, TMOD reg is loaded with value of 20h (Timer 1in Mode 2 to set Baud Rate).
- Then, TH1 is loaded with required baud rate value as calculated above.
- SCON reg is loaded with 50h (Mode 1 serial communication).
- TR1 is set to start timer.
- RI is cleared.
- Then RI flag is monitored. If RI flag is set, then a byte of data is received serially.
- The received byte is stored in SBUF reg and needs to be transferred to another register or location to receive another byte of data.
The source routine to receive a byte of data looks like:-
unsigned char receive ( )
{
unsigned char rec;
TMOD=0x20; // timer1 8 bit auto reload mode
TH1=0xfd; //set 9600 baud rate
SCON=0x50; //serial mode 1 with receive enable
TR1=1; // start timer
while(RI==0);
rec=SBUF;
RI=0;
return rec;
}
unsigned char receive ( )
{
unsigned char rec;
TMOD=0x20; // timer1 8 bit auto reload mode
TH1=0xfd; //set 9600 baud rate
SCON=0x50; //serial mode 1 with receive enable
TR1=1; // start timer
while(RI==0);
rec=SBUF;
RI=0;
return rec;
}
No comments:
Post a Comment