Aim:
In this tutorial, we are going to discuss the serial/UART communication
of 8051. After understating the basics of 8051 UART module, We will see
how to use the ExploreEmbedded libraries to communicate with any of the
UART devices.
In this tutorial, we are going to discuss the serial/UART communication
of 8051. After understating the basics of 8051 UART module, We will see
how to use the ExploreEmbedded libraries to communicate with any of the
UART devices.
Description:
UART Registers
The below table shows the registers associated with 8051 UART.
REGISTER DESCRIPTION
SCON Serial Control Register
TCON Timer Control Register for Baud Rate Generator
TMOD Timer Mode Control for Baud Rate Generator
SBUFF Serial Buffer holds the data to be transmitted and the data received
The below table shows the registers associated with 8051 UART.
REGISTER | DESCRIPTION |
---|---|
SCON | Serial Control Register |
TCON | Timer Control Register for Baud Rate Generator |
TMOD | Timer Mode Control for Baud Rate Generator |
SBUFF | Serial Buffer holds the data to be transmitted and the data received |
UART Register Configuration
Now lets see how to configure the individual registers for UART communication.
SCON
7 6 5 4 3 2 1 0
SM0 SM1 SM2 REN TB8 RB8 TI RI
SM0 SM1 OPERATION DESCRIPTION BAUD RATE SOURCE
0 0 Mode 0 8-bit UART 1/12 the quartz frequency
0 1 Mode 1 8-bit UART Determined by the timer 1
1 0 Mode 2 9-bit UART 1/32 the quartz frequency
1 1 Mode 0 9-bit UART Determined by the timer 1
- SM2 – Serial port mode 2 bit,
also known as multiprocessor communication enable bit. When set, it
enables multiprocessor communication in mode 2 and 3, and eventually
mode 1. It should be cleared in mode 0.
- REN – Reception Enable bit enables serial reception when set. When cleared, serial reception is disabled.
- TB8 – Transmitter bit 8.
Since all registers are 8-bit wide, this bit solves the problem of
transmitting the 9th bit in modes 2 and 3. It is set to transmit a logic
1 in the 9th bit.
- RB8 –
Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by
hardware if 9th bit received is a logic 0. Set by hardware if 9th bit
received is a logic 1.
- TI –
Transmit Interrupt flag is automatically set at the moment the last bit
of one byte is sent. It’s a signal to the processor that the line is
available for a new byte to transmit. It must be cleared from within the
software.
- RI –
Receive Interrupt flag is automatically set upon one-byte receive. It
signals that byte is received and should be read quickly prior to being
replaced by a new data. This bit is also cleared from within the
software.
TMOD
TIMER1 TIMER 0
7 6 5 4 3 2 1 0
Gate C/T M1 M0 Gate C/T M1 M0
- Gate Control
0 = Timer enabled
1 = Timer enabled if INTx\ is high
- C/T:Counter or Timer Selector
0 = Internal count source (clock/12)
1 = External count source (Tx pin)
- M1-M0:Mode Control
00-Mode 0, 13 bit count mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Multiple mode
TCON
7 6 5 4 3 2 1 0
TF1 TR1 TF0 TR0
- TRx: Timer x run control
0 = Timer not running
1 = Timer running
- TFx: Timer x OverFlow flag
0 = Timer has not overflowed/rolled over
1 = Timer has overflowed/rolled over
Now lets see how to configure the individual registers for UART communication.
SCON | |||||||
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
SM0 | SM1 | OPERATION | DESCRIPTION | BAUD RATE SOURCE |
---|---|---|---|---|
0 | 0 | Mode 0 | 8-bit UART | 1/12 the quartz frequency |
0 | 1 | Mode 1 | 8-bit UART | Determined by the timer 1 |
1 | 0 | Mode 2 | 9-bit UART | 1/32 the quartz frequency |
1 | 1 | Mode 0 | 9-bit UART | Determined by the timer 1 |
- SM2 – Serial port mode 2 bit, also known as multiprocessor communication enable bit. When set, it enables multiprocessor communication in mode 2 and 3, and eventually mode 1. It should be cleared in mode 0.
- REN – Reception Enable bit enables serial reception when set. When cleared, serial reception is disabled.
- TB8 – Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem of transmitting the 9th bit in modes 2 and 3. It is set to transmit a logic 1 in the 9th bit.
- RB8 – Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware if 9th bit received is a logic 0. Set by hardware if 9th bit received is a logic 1.
- TI – Transmit Interrupt flag is automatically set at the moment the last bit of one byte is sent. It’s a signal to the processor that the line is available for a new byte to transmit. It must be cleared from within the software.
- RI – Receive Interrupt flag is automatically set upon one-byte receive. It signals that byte is received and should be read quickly prior to being replaced by a new data. This bit is also cleared from within the software.
TMOD | |||||||
TIMER1 | TIMER 0 | ||||||
---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Gate | C/T | M1 | M0 | Gate | C/T | M1 | M0 |
- Gate Control
0 = Timer enabled
1 = Timer enabled if INTx\ is high
1 = Timer enabled if INTx\ is high
- C/T:Counter or Timer Selector
0 = Internal count source (clock/12)
1 = External count source (Tx pin)
1 = External count source (Tx pin)
- M1-M0:Mode Control
00-Mode 0, 13 bit count mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Multiple mode
01-Mode 1, 16 bit count mode
10-Mode 2, Auto reload mode
11-Mode 3, Multiple mode
TCON | |||||||
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
TF1 | TR1 | TF0 | TR0 |
- TRx: Timer x run control
0 = Timer not running
1 = Timer running
1 = Timer running
- TFx: Timer x OverFlow flag
0 = Timer has not overflowed/rolled over
1 = Timer has overflowed/rolled over
1 = Timer has overflowed/rolled over
Baud Rate Calculation
The main criteria for UART communication is its baud rate. Both the
devices Rx/Tx should be set to same baud rate for successful
communication.
For the 8051 the Timer 1′ is used to generate the baud rate in Auto reload mode.
The crystal frequency Fclk is divided by 12 internally which is
used to execute instructions also known as Machine Clock. Mclk. The
timer again divides the Mclk by 32 and uses it as the timer frequency,
say Tclk.
Usually, an 11.0592 Mhz crystal oscillator is used to provide the clock
to 8051. The value seems to be odd but we see how it makes sense. From
above discussion
Mclk = Fclk / 12 = 11.0592MHz/12 = 921.6KHz
Tclk = Mclk/32= 921.6KHz/32 = 28.8KHz
If we look at the standard baud rates used for serial communication
shown in the table below, we can observe that all the baud rates are
factors or multiples of the Tclk (28.8K)! This results in low error
rates and hence is a commonly used crystal with 8051.
BAUD RATE FACTOR)
2400 Tclk/12
4800 Tclk/6
9600 Tclk/3
14400 Tclk/2
The above factors should be loaded to Timer1(TH1) in Mode2 in order to
generate the required baud rate. The final formula for baud rate is as
below. Baudrate = Fosc/(32 * 12 * (256-TH1)) $$TH1 = 256 – (Fosc/(32 *
12 * Baudrate))$$ //If( SMOD==0 in PCON register) $$TH1 = 256 –
(Fosc/(32 * 6 * Baudrate))$$ //If( SMOD==1 in PCON register)
Now with Fosc = 11.0592Mhz, TH1 value for 9600 baudrate will be: TH1 = 256-(11.0592*10^6)/(32 * 12 * 9600) = 253 = 0xFD = -3
The main criteria for UART communication is its baud rate. Both the
devices Rx/Tx should be set to same baud rate for successful
communication.
For the 8051 the Timer 1′ is used to generate the baud rate in Auto reload mode.
For the 8051 the Timer 1′ is used to generate the baud rate in Auto reload mode.
The crystal frequency Fclk is divided by 12 internally which is
used to execute instructions also known as Machine Clock. Mclk. The
timer again divides the Mclk by 32 and uses it as the timer frequency,
say Tclk.
Usually, an 11.0592 Mhz crystal oscillator is used to provide the clock
to 8051. The value seems to be odd but we see how it makes sense. From
above discussion
Mclk = Fclk / 12 = 11.0592MHz/12 = 921.6KHz
Tclk = Mclk/32= 921.6KHz/32 = 28.8KHz
If we look at the standard baud rates used for serial communication
shown in the table below, we can observe that all the baud rates are
factors or multiples of the Tclk (28.8K)! This results in low error
rates and hence is a commonly used crystal with 8051.
BAUD RATE | FACTOR) |
---|---|
2400 | Tclk/12 |
4800 | Tclk/6 |
9600 | Tclk/3 |
14400 | Tclk/2 |
The above factors should be loaded to Timer1(TH1) in Mode2 in order to
generate the required baud rate. The final formula for baud rate is as
below. Baudrate = Fosc/(32 * 12 * (256-TH1)) $$TH1 = 256 – (Fosc/(32 *
12 * Baudrate))$$ //If( SMOD==0 in PCON register) $$TH1 = 256 –
(Fosc/(32 * 6 * Baudrate))$$ //If( SMOD==1 in PCON register)
Now with Fosc = 11.0592Mhz, TH1 value for 9600 baudrate will be: TH1 = 256-(11.0592*10^6)/(32 * 12 * 9600) = 253 = 0xFD = -3
Block Diagram
Schematic
Code
// *****************************************************
// Project: Serial UART communication using 8051
// Author: Code Bloges
// Module description: Operate max232
// *****************************************************
#include <reg52.h>
#define Baud_rate 0xFD // BAUD RATE 9600
void USART_Init(void);
void USART_Transmit(unsigned char);
unsigned char USART_Receave();
void cct_init(void);
void delay(unsigned int t);
char x;
void main()
{
cct_init();
USART_Init();
while(1)
{
x=USART_Receave();
USART_Transmit(x);
}
}
void cct_init(void) //initialize cct
{
P0 = 0x00; //not used
P1 = 0x00; //Used for Appliances
P2 = 0x00; //not used
P3 = 0x03; //used for serial
}
void USART_Init(void) // INITIALIZE SERIAL PORT
{
TMOD = 0x20; // Timer 1 IN MODE 2 -AUTO RELOAD TO GENERATE BAUD RATE
SCON = 0x50; // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED
TH1 = Baud_rate; // LOAD BAUDRATE TO TIMER REGISTER
TR1 = 1; // START TIMER
}
void USART_Transmit(unsigned char serialdata)
{
SBUF = serialdata; // LOAD DATA TO SERIAL BUFFER REGISTER
while(TI == 0); // WAIT UNTIL TRANSMISSION TO COMPLETE
TI = 0; // CLEAR TRANSMISSION INTERRUPT FLAG
}
unsigned char USART_Receave()
{
while(RI==0);
RI=0;
return SBUF;
}
// ***************************************************** // Project: Serial UART communication using 8051 // Author: Code Bloges // Module description: Operate max232 // ***************************************************** #include <reg52.h> #define Baud_rate 0xFD // BAUD RATE 9600 void USART_Init(void); void USART_Transmit(unsigned char); unsigned char USART_Receave(); void cct_init(void); void delay(unsigned int t); char x; void main() { cct_init(); USART_Init(); while(1) { x=USART_Receave(); USART_Transmit(x); } } void cct_init(void) //initialize cct { P0 = 0x00; //not used P1 = 0x00; //Used for Appliances P2 = 0x00; //not used P3 = 0x03; //used for serial } void USART_Init(void) // INITIALIZE SERIAL PORT { TMOD = 0x20; // Timer 1 IN MODE 2 -AUTO RELOAD TO GENERATE BAUD RATE SCON = 0x50; // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED TH1 = Baud_rate; // LOAD BAUDRATE TO TIMER REGISTER TR1 = 1; // START TIMER } void USART_Transmit(unsigned char serialdata) { SBUF = serialdata; // LOAD DATA TO SERIAL BUFFER REGISTER while(TI == 0); // WAIT UNTIL TRANSMISSION TO COMPLETE TI = 0; // CLEAR TRANSMISSION INTERRUPT FLAG } unsigned char USART_Receave() { while(RI==0); RI=0; return SBUF; }
Downloads:
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
- Interfacing keypad with 8051
- Interfacing SPI communication with 8051
- 8051 Displaying Custom Characters on LCD
- 8051 Graphical LCD
- RTC interfacing using I2C in 8051
- Interfacing ultrasonic sensor with 8051
- Interfacing GPS Modu with 8051
- Interfacing GSM Module with 8051
- ADC Module interfacing with 8051
- Interfacing PWM in 8051
- Interfacing keypad with 8051
- Interfacing SPI communication with 8051
- 8051 Displaying Custom Characters on LCD
- 8051 Graphical LCD
- RTC interfacing using I2C in 8051
- Interfacing ultrasonic sensor with 8051
- Interfacing GPS Modu with 8051
- Interfacing GSM Module with 8051
- ADC Module interfacing with 8051
- Interfacing PWM in 8051
No comments:
Post a Comment