Serial Interrupt Programming in 8051 Microcontroller
Baud Rate calculation:
- To meet the standard baud rates generally crystal with 11.0592 MHz is used.
- As we know, 8051 divides crystal frequency by 12 to get a machine cycle frequency of 921.6 kHz.
- The internal UART block of 8051 divides this machine cycle frequency by 32, which gives the frequency of 28800 Hz which is used by UART.
- To achieve a baud rate of 9600, again 28800 Hz frequency should be divided by 3.
- This is achieved by using Timer1 in mode-2 (auto-reload mode) by putting 253 in TH1 (8-bit reg.)
- So 28800 Hz will get divided by 3 as the timer will overflow after every 3 cycles.
- we can achieve different baud rates by putting the division factor in the TH1 register.
Division factor to achieve different baud rates
Baud Rate | TH1 (Hex) |
---|---|
9600 | FD |
4800 | FA |
2400 | F4 |
1200 | E8 |
Serial control register SCON is used to set serial communication operation modes. Also, it is used to control transmit and receive operations.
Mode | SM0 | SM1 | Mode |
---|---|---|---|
0 | 0 | 0 | 1/12 of Osc frequency shift register mode fixed baud rate |
1 | 0 | 1 | 8-bit UART with timer 1 determined baud rate |
2 | 1 | 0 | 9-bit UART with 1/32 of Osc fixed baud rate |
3 | 1 | 1 | 9-bit UART with timer 1 determined baud rate |
Normally mode-1 (SM0 =0, SM1=1) is used with 8 data bits, 1 start bit, and 1 stop bit.
Bit 5 - SM2: for Multiprocessor Communication
This bit enables a multiprocessor communication feature in mode 2 & 3.
Bit 4 - REN: Receive Enable
1 = Receive enable
0 = Receive disable
Bit 3 - TB8: 9th Transmit Bit
This is the 9th bit which is to be transmitted in mode 2 & 3 (9-bit mode)
Bit 2 - RB8: 9th Receive Bit
This is the 9th received bit in mode 2 & 3 (9-bit mode), whereas in mode 1 if SM2 = 0 then RB8 hold the stop bit that received
Bit 1 - TI: Transmit Interrupt Flag
This bit indicates the transmission is complete and gets set after transmitting the byte from the buffer. Normally TI (Transmit Interrupt Flag) is set by hardware at the end of the 8th bit in mode 0 and at the beginning of the stop bit in other modes.
This bit indicates reception is complete and gets set after receiving the complete byte in the buffer. Normally RI (Receiver Interrupt Flag) is set by hardware in receiving mode at the end of the 8th bit in mode 0 and at the stop bit receive time in other modes.
- Configure Timer 1 in auto-reload mode.
- Load TH1 with value as per required baud rate e.g. for 9600 baud rate load 0xFD. (-3 in decimal)
- Load SCON with serial mode and control bits. e.g. for mode 1 and enable reception, load 0x50.
- Start timer1 by setting TR1 bit to 1.
- Load transmitting data in the SBUF register.
- Wait until loaded data is completely transmitted by polling the TI flag.
- When the TI flag is set, clear it, and repeat from step 5 to transmit more data.
/*Write an 8051 C program to transfer "YES" serially at 9600 baud rate,
8-bit data,1 stop bit, Do it for 5 times.
*/
#include <reg51.h>
void SerTx(unsigned char);
unsigned int i = 0;
void SerTx(unsigned char ch)
{
SBUF = ch; //loading value in buffer
while(TI==0); //wait untill transmition is over
TI=0;
}
void main(void)
{
TMOD = 0x20; //use Timer 1, 8-bit auto-reload
TH1 = 0xFD; //9600 baud rate
SCON = 0x50;
TR1 = 1; //start timer
for(i;i<5;i++)
{
SerTx('Y');
SerTx('E');
SerTx('S');
}
while(1);
}
// Program in 8051 in C to receive bytes of data serially and put them in P2.
// Set baud rate at 4800, 8-bit data, and 1 stop bit.
#include <reg51.h>
void main (void)
{
unsigned char myData;
TMOD = 0x20; //use Timer-1,8-bit auto-reload
TH1 = 0xFD; //9600 baud rate
SCON = 0x50;
TR1 = 1; //start timer
while(1)
{
while(RI==0);
myData = SBUF;
P2=myData;
RI=0;
}
}
#include <reg51.h> /* Include x51 header file */
void Ext_int_Init()
{
EA = 1; /* Enable global interrupt */
ES = 1; /* Enable serial interrupt */
}
void UART_Init()
{
TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
TH1 = 0xFD; /* Load value for 9600 baud rate */
SCON = 0x50; /* Mode 1, reception enable */
TR1 = 1; /* Start timer 1 */
}
void Serial_ISR() interrupt 4
{
P2 = SBUF; /* Give received data on port 1 */
RI = 0; /* Clear RI flag */
}
void main()
{
P2 = 0x00; /* Make P2 output */
Ext_int_Init(); /* Call Ext. interrupt initialize */
UART_Init();
while(1);
}
Comments
Post a Comment