Professional Documents
Culture Documents
Full Duplex
RS 232 transmit (Tx) and receive (Rx) connections between a PC and microcontroller
The interfacing standard RS232 was set by the Electronics Industries Association (EIA) in 1960 The standard was set long before the advent of the TTL logic family, its input and output voltage levels are not TTL compatible In RS232, a 1 is represented by -3 ~ -25 V, while a 0 bit is +3 ~ +25 V, making -3 to +3 undefined
A line driver such as the MAX232 chip is required to convert RS232 voltage levels to TTL levels, and vice versa
Serial Port
The 8051 has a serial data communication circuit that uses register SBUF to hold data. Register SCON controls data communication, register PCON controls data rates, and pins RXD (P3.0) and TXD (P3.1) connect to the serial data network. SBUF is physically two registers. One is write only and is used to hold data to be transmitted out of the 8051 via TXD. The other is read only and holds received data from external sources via RXD. Both mutually exclusive registers use address 99h. There are four programmable modes for serial data communication that are chosen by setting the SMX bits in SCON. Baud rates are determined by the mode chosen.
SCON
7 SM0 6 SM1 5 SM2 4 REN 3 TB8 2 RB8 1 TI 0 RI
Bit 7 6 5
Function Serial port mode bit 0.Set/cleared by program to select mode. Serial port mode bit 1. Set/cleared by program to select mode. SM0 0 0 1 1 SM1 0 1 0 1 Mod 0 1 2 3 Description Shift register; baud = f/12 8-bit UART ;baud = variable 9-bit UART ;baud = f/32 or f/64 9-bit UART; baud = variable SM2 Multiprocessor communications bit. Set/cleared by program to enable multiprocessor communications in modes 2 and 3. When set to 1 an interrupt is generated if bit 9 of the received data is a 1; no interrupt is generated if bit 9 is a 0.If set to 1 for mode 1, no interrupt will be generated unless a valid stop bit is received. Clear to 0 if mode 0 is in use.
4 3 2 1 0
Receive enable bit. Set to 1 to enable reception; cleared to 0 to disable reception. Transmitted bit 8. Set/cleared by program in modes 2 and 3. Received bit 8. Bit 8 of received data in modes 2 and 3; stop bit in mode 1. Not used in mode 0.
Transmit interrupt flag. Set to one at the end of bit 7 time in mode 0, and at the beginning of the stop bit for other modes. Must be cleared by the program. Receive interrupt flag. Set to one at the end of bit 7 time in mode 0, and halfway through the stop bit for other modes. Must be cleared by the program.
PCON
7 SMOD 6 5 4 3 GF1 2 GF0 1 PD 0 IDL
Bit 7
Symbol SMOD
Function Serial baud rate modify bit. Set to 1 by program to double baud rate using timer 1 for modes 1, 2, and 3. Cleared to 0 by program to use timer 1 baud rate. General purpose user flag bit 1. Set/cleared by program. General purpose user flag bit 0. Set/cleared by program. Power down bit. Set to 1 by program to enter power down configuration for CMOS processors. Idle mode bit. Set to 1 by program to enter idle mode configuration for CMOS processors. !!!PCON is not bit addressable.
3 2 1 0
Data Transmission
Transmission of serial data bits begins anytime data is written to SBUF. TI is set to a 1 when the data has been transmitted and signifies that SBUF is empty (for transmission purposes) and that another data byte can be sent. If the program fails to wait for the TI flag and overwrites SBUF while a previous data byte is in the process of being transmitted, the results will be unpredictable.
Data Reception
Reception of serial data will begin if the receive enable bit (REN) in SCON is set to 1 for all modes. In addition, for mode 0 only, RI must be cleared to 0 also. Receiver interrupt flag RI is set after data has been received in all modes. Setting REN is the only direct program control that limits the reception of unexpected data; the requirement that RI also be 0 for mode 0 prevents the reception of new data until the program has dealt with the old data and reset RI. Reception can begin in modes 1, 2, and 3 if RI is set when the serial stream of bits begins. RI must have been reset by the program before the last bit is received or the incoming data will be lost. Incoming data is not transferred to SBUF until the last data bit has been received so that the previous transmission can be read from SBUF while new data is being received.
The 8051 designers have included four modes of serial data transmission that enable data communication to be done in a variety of ways and a multitude of baud rates. Modes are selected by the programmer by seting the mode bits SM0 and SM1 in SCON. Baud rates are fixed for mode 0 and variable, using timer 1 and the serial baud rate modify bit (SMOD) in PCON, for modes 1, 2, and 3.
Of the original ten bits, the start bit is discarded, the eight data bits go to SBUF and the stop bit is saved in bit RB8 of SCON. RI is set to 1. indicating a new data byte has been received. If RI is found to be set at the end of the reception, indicating that the previously received data byte has not been read by the program, or if the other conditions listed are not true, the new data will not be loaded and will be lost.
SMOD is the control bit in PCON and can be 0 or 1, which raises the 2 in the equation to a value of 1 or 2.
f baud
and timer 1 can be run using the internal clock or as a counter that receives clock pulses from any external source via pin T1. The oscillator frequency is chosen to help generate both standard and nonstandard baud rates. If standard baud rates are desired, then an 11.0592 megahertz crystal could be selected. To get a standard rate of 9600 hertz then, the setting of TH 1 may be found as follows(if SMOD is clear):
Bitul SMOD
Timer 1 Mod 2 2 2 2 2 2 2 2 1 TH1 FFh FDh FDh FAh F4h E8h CCh 72h FEEBh
1 1 0 0 0 0 0 0 0
f baud
Here, as in the case for mode 0, the baud rate is much higher than standard communication rates. This high data rate is needed in many multi-processor applications. Data can be collected quickly from an extensive network of communicating microcontrollers if high baud rates are employed.
Slave 1
RxD TxD
Example 2
org 0000h jmp start org 0100h message: db "hello everybody!" db 0 start: mov tmod,#20h ; timer 1, mod 2 mov th1,#-6 ; rata de transfer 4800 baud mov scon,#50h ; 8 bit, 1 stop REN enable setb tr1 ; start timer 1 mov dptr,#message ; incarca pointer pt. mesaj mov r0,#00h again: mov a, r0 ; preia caracterul movc a,@a+dptr ; daca ultimul caracter este "0" se opreste trasferul jz termina mov sbuf,a ; incarca data in portul serial here: jnb TI, here ; sta in bucla pana ultimul bit este transmis Clr TI ; urmatorul caracter poate fi transmis inc r0 sjmp again termina: sjmp termina end
Write a program for the 8051 to receive bytes of data serially, and put them in P1, set the baud rate at 4800, 8-bit data, and 1 stop bit Solution: MOV TMOD,#20H ;timer 1,mode 2(auto reload) MOV TH1,#-6 ;4800 baud rate MOV SCON,#50H ;8-bit, 1 stop, REN enabled SETB TR1 ;start timer 1 HERE: JNB RI,HERE ;wait for char to come in MOV A,SBUF ;saving incoming byte in A MOV P1,A ;send to port 1 CLR RI ;get ready to receive next ;byte SJMP HERE ;keep getting data
Example 10-5 Assume that the 8051 serial port is connected to the COM port of IBM PC, and on the PC, we are using the terminal.exe program to send and receive data serially. P1 and P2 of the 8051 are connected to LEDs and switches, respectively. Write an 8051 program to (a) send to PC the message We Are Ready, (b) receive any data send by PC and put it on LEDs connected to P1, and (c) get data on switches connected to P2 and send it to PC serially. The program should perform part (a) once, but parts (b) and (c) continuously, use 4800 baud rate. Solution: ORG 0 MOV P2,#0FFH ;make P2 an input port MOV TMOD,#20H ;timer 1, mode 2 MOV TH1,#0FAH ;4800 baud rate MOV SCON,#50H ;8-bit, 1 stop, REN enabled SETB TR1 ;start timer 1 MOV DPTR,#MYDATA ;load pointer for message H_1: CLR A MOV A,@A+DPTR ;get the character
JZ B_1 ;if last character get out ACALL SEND ;otherwise call transfer INC DPTR ;next one SJMP H_1 ;stay in loop B_1: MOV a,P2 ;read data on P2 ACALL SEND ;transfer it serially ACALL RECV ;get the serial data MOV P1,A ;display it on LEDs SJMP B_1 ;stay in loop indefinitely ;----serial data transfer. ACC has the data-----SEND: MOV SBUF,A ;load the data H_2: JNB TI,H_2 ;stay here until last bit ;gone CLR TI ;get ready for next char RET ;return to caller ;----Receive data serially in ACC---------------RECV: JNB RI,RECV ;wait here for char MOV A,SBUF ;save it in ACC CLR RI ;get ready for next char RET ;return to caller ;-----The message--------------MYDATA: DB We Are Ready,0 END
In receiving bit via its RxD pin, 8051 goes through the following steps 1. It receives the start bit Indicating that the next bit is the first bit of the character byte it is about to receive 2. The 8-bit character is received one bit at time 3. The stop bit is received When receiving the stop bit 8051 makes RI = 1, indicating that an entire character byte has been received and must be picked up before it gets overwritten by an incoming character
4. By checking the RI flag bit when it is raised, we know that a character has been received and is sitting in the SBUF register We copy the SBUF contents to a safe place in some other register or memory before it is lost 5. After the SBUF contents are copied into a safe place, the RI flag bit must be forced to 0 by CLR RI in order to allow the next received character byte to be placed in SBUF Failure to do this causes loss of the received character
By checking the RI flag bit, we know whether or not the 8051 received a character byte If we failed to copy SBUF into a safe place, we risk the loss of the received byte It must be noted that RI flag bit is raised by 8051 when it finish receive data It must be cleared by the programmer with instruction CLR RI If we copy SBUF into a safe place before the RI flag bit is raised, we risk copying garbage The RI bit can be checked by The instruction JNB RI,xx Using an interrupt
Write an 8051 C program to transfer the message YES serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously. Solution: #include <reg51.h> void SerTx(unsigned char); void main(void){ TMOD=0x20; //use Timer 1, mode 2 TH1=0xFD; //9600 baud rate SCON=0x50; TR1=1; //start timer while (1) { SerTx(Y); SerTx(E); SerTx(S); } } void SerTx(unsigned char x){ SBUF=x; //place value in buffer while (TI==0); //wait until transmitted TI=0; }
Program the 8051 in C to receive bytes of data serially and put them in P1. Set the baud rate at 4800, 8-bit data, and 1 stop bit. Solution: #include <reg51.h> void main(void){ unsigned char mybyte; TMOD=0x20; //use Timer 1, mode 2 TH1=0xFA; //4800 baud rate SCON=0x50; TR1=1; //start timer while (1) { //repeat forever while (RI==0); //wait to receive mybyte=SBUF; //save value P1=mybyte; //write value to port RI=0; } }