Serial communication data transmission to pc using USART of 8051(89c51,89c52) Microcontroller
This is a simple project on how to transfer serial data to PC(personal computer) using 8051(89c51) microcontroller usart(universal synchronous asynchronous receiver transmitter). Learning about 8051 microcontroller serial communication is the main task of the tutorial. Serial data can be transferred to PC(personal computer) using serial port of the computer. Serial port of computers are used to connect printers, scanners, fax machines and old shaped/pined round shaped mouse and keypad connectors.
There are two serial ports in all the systems(computer, laptop) DB-9 and DB-25. You can transfer data serially to PC using these ports, DB-9 has 9 pins and DB-25 has 25 pins, both ports comes in male and female package. Below is pin out of both the male and female connectors
How serial data can be transferred using 8051 microcontroller?
In order to transfer serial data between two systems both must have a single UART(universal synchronous asynchronous receiver transmitter) port. Serial communication is accomplished using this UART protocol and serial port. In UART serial communication we have to set few things on both the receiver and transmitter side. First both must be working on the same BAUD RATE. Baud rate is data transfer rate. Such as 9600 bps OR 9600 bits per second. Secondly we have to define the code bits in transfer data. Like 8- bit data transfer, stop bits and parity etc. Once on the both the side we defined the same settings its time to talk with each other.
Why use of Level converter – Max232
8051 series microcontrollers works on TTL(transistor transistor logic) and our PC(personal computers) works on rs-232 level wave form. 8051 microcontroller outputs data serially as TTL wave form, and our PC works on rs-232 level so we have to convert TTL level wave form in to equivalent rs-232 level wave form before sending it to PC. To carry out this task we use MAX-232. It is an ic whic converts TTL wave form in its equivalent rs-232 level and rs232 signal in to its equivalent TTL signal. I recommend you to first take a small tutorial on MAX-232 ic its pin-out and working etc.
8051 microcontroller serial data transfer – Project code
Coming to the code first i included necessary header file reg51.h, this header file must be included in every project that uses keil as software to compile the code and generating hex file for 89c51 microcontroller.
Now about the internal registers of 8051 which are used in the code. TMOD (Timer mode) this register is used to initialize the mode of the timer. The only mode which can be used to transfer data serially to pc is 8-bit auto reload. To initialize the timer in this mode use command 0x20 which loads the timer 1 in 8-bit auto reload mode. I am using timer 1 in my program so i used the command 0x20, but if you want to use timer 0 you can use it and the command for it is different its 0x02 take a look at the diagram below.
Now about the internal registers of 8051 which are used in the code. TMOD (Timer mode) this register is used to initialize the mode of the timer. The only mode which can be used to transfer data serially to pc is 8-bit auto reload. To initialize the timer in this mode use command 0x20 which loads the timer 1 in 8-bit auto reload mode. I am using timer 1 in my program so i used the command 0x20, but if you want to use timer 0 you can use it and the command for it is different its 0x02 take a look at the diagram below.
Then the timer 1 high byte TH1 is loaded with 0xFD to generate a baud rate of 9600 bps(bits per second). Now what does baud rate means. Its actually the protocol between the two machines which are going to transfer data between them serially. Baud rate is measured in bps bits per second, both of the systems should be at the same baud rates for successful data transfer. There are many baud rates now how to calculate the value for each baud rate below is a simple formula for it.
8051 microcontrollers completes one machine cycle after every 12 clock cycles. So our Instruction execution frequency is crystal frequency/12. Now if our crystal is of 11.0592 MHz then our effective frequency is 11.0592/12 MHz => 921.6 KHz. 8051 UART further divides this frequency (921.6 KHz) by 32 to generate its baud rate. Therefore effective frequency available to generate baud rates is 921.6 KHz/32 = 28800 Hz.
For different baud rates the values of TH1 will be different for Baud Rate 9600 TH1 will be=0xFD because 28800/9600 = 3. 3 is 11 in binary take its two’s complement -3 and load the value in TH1 for 9600 baud rate. Calculate different baud rates for different crystals using the same above formula. Different baud rates with 11.0592 MHz crystal are below. SCON(serial control) register is used to control the flow of data. This is an 8-bit register and also it is bit addressable You can access its single bit. Here is a diagram of SCON with individual bits and modes which you can specify.
I am using mode 1, to select Scon in mode 1 make SM0=0 and SM1=1, you can also use other modes mentioned in the above diagram. SM2 enables the multiprocessing capability of the 8051, here we make SM2=0 because we are not dealing with 8th bit. REN is Receive Enable, when REN=1 it allows the 8051 to receive data . If you want 8051 to both transfer and receive data, REN must be set to ‘1’. when REN=0 it means receiver is disabled. TB8 and RB8 is used for serial modes 2 and 3, so here we make them 0. TI and RI are important bits.
TI is Transmit Interrupt flag, When 8051 microcontroller finishes the transfer of 8-bit character, it raises the TI flag to indicate that it is ready to transfer another byte.
RI is Receive Interrupt flag, When 8051 receive data via RxD, it get rid of the start and stop bits and places the byte in the SBUF. Then it raises the RI flag to indicate that a byte has been received and should be picked up before it is lost.
SBUF(serial buffer) is an 8-bit register it is not bit addressable when we have to transfer character we place it in SBUF register and poles the TI flag. When data is received which is sended by some other source it is also placed in SBUF register, and we pick it from here.The circuit diagram is simple and is shown below.
SBUF(serial buffer) is an 8-bit register it is not bit addressable when we have to transfer character we place it in SBUF register and poles the TI flag. When data is received which is sended by some other source it is also placed in SBUF register, and we pick it from here.The circuit diagram is simple and is shown below.
After you make connections go to Start->Programs->Accessories->Communications->HyperTerminal open new connection name it what ever you want then click OK. Now a window appears select COM1 from connect using drop down menu click OK Now set COM1 properties bits per second as you specified in your program in mine case 9600, Data bits=8, Parity=none, Stop bits=1, Flow control=none then click OK. Now switch on the power of your circuit, you will see what you specified in your code.
#include<reg51.h> | |
void main() | |
{ | |
char A[]="www.microcontroller-project.com"; | |
unsigned int i=0; | |
P3=0x03; //Initializing 8051 UART | |
TMOD=0x20; // Timer1 Mode2 8 bit auto reload | |
TH1=0xFD; // 9600 bps | |
SCON=0x50; // 8 Data bit, 1 start bit, bit 1 stop | |
TR1=1; // Timer1 ON | |
while(1) | |
{ | |
while(A[i]!='\0'){ | |
SBUF=A[i]; // Placing character one by one in the serial buffer register | |
while(TI==0); // It automatically poles up to 1 when character is transmitted | |
TI=0; | |
i++; | |
} | |
i=0; | |
} | |
} |
I am sending my web site name www.microcontroller-project.com to PC on UART protocol. The final output received on Hyperterminal of PC is given below.
Download the project files and code compiled in keil uvision 4 with hex file , and if you have any queries let me know that.Please Write your comments below.
No comments:
Post a Comment