You are on page 1of 9

8051 SERIAL COMMUNICATION

Example 10-1
With XTAL=11.0592 MHz, find the TH1 value needed to have the following baud rate. (a) 9600 (b) 2400 (c) 1200

Solution:with XTAL=11.0592 MHz: Machine cycle frequency: 11.0592 MHz/12 = 921.6kHz, Frequency provided by UART: 921.6kHz/32 =28,800 Hz (if SMOD=0) or 921.6kHz/16 =57,600 Hz (if SMOD=1) should be provided by Timer 1 to set baud rate. Case I: SMOD=0. (a) 28,800/9,600 = 3. (b) 28,800/2,400 = 12. (c) 28,800/1,200 = 24. should be loaded into TH1. Case II: SMOD=1. (a) 57,600/9,600 = 6. (b) 57,600/2,400 = 24. (c) 57,600/1,200 = 48. should be loaded into TH1.

256-3 = 253 = FDh 256-12 = 244 = F4h 256-24 = 232 = E8h

256-6 = 250 = FAh 256-24 = 232 = E8h 256-48 = 232 = D0h


16 by UART

57,600 Hz

XTAL OSC

11.0592 MHz

921.6 kHz

(SMOD=1)

12
32 by UART
(SMOD=0) 28,800 Hz

Timer 1

Example 10-2
Write a program for the 8051 to

transfer letter A serially at 4800 baud, continuously.

Solution:
MOV MOV MOV SETB MOV JNB CLR SJMP TMOD,#20H TH1,#-6 SCON,#50H TR1 SBUF,#"A" TI,HERE TI AGN ;timer 1, mode 2 (auto reload) ;4800 baud rate ;8-bit, 1 stop, REN enabled ;start timer 1 ;letter "A" to be transferred ;wait for the last bit ;clear TI for next char ;keep sending A

AGN: HERE:

Example 10-3
Write a program to transfer the

message YES serially at 9600 baud, 8bit data, 1 stop bit. Do this continuously.

Solution:
MOV TMOD,#20H ;timer 1, mode 2 MOV TH1,#-3 ;9600 baud MOV SCON,#50H ;8-bit, 1 stop bit, REN enabled SETB TR1 ;start timer 1 AGN: MOV A,#"Y" ;transfer "Y" ACALL XMIT MOV A,#"E" ;transfer "E" ACALL XMIT MOV A,#"S" ;transfer "S" ACALL XMIT SJMP AGN ;keep doing it ;serial data transfer subroutine XMIT: MOV SBUF,A ;load SBUF HERE: JNB TI,HERE ;wait for last bit to transfer CLR TI ;get ready for next byte RET

Example 10-4
Program 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 MOV MOV SETB HERE: JNB MOV MOV CLR SJMP TMOD,#20H TH1,#-6 SCON,#50H TR1 RI,HERE A,SBUF P1,A RI HERE ;timer 1, mode 2(auto reload) ;4800 baud ;8-bit, 1 stop, REN enabled ;start timer 1 ;wait for char to come in ;save incoming byte in A ;send to port 1 ;get ready to receive next byte ;keep getting data

You might also like