11
11
Techniques of Microcontroller
--8051 Microcontroller and Embedded Systems
Using Assembly and C
杭州 • 浙江大学 • 2015
Chapter 11
Serial Communication
Outline
§11-1 Basics of Serial Communication
§11-2 8051 Connection to RS232
§11-3 Serial Communication
Programming
§11-4 Serial Port Programming
in C
§11-5 New communication methods
Wednesday, December 23,
2015
§ 11-1 Basics of Serial Communication
Computers transfer data in two ways:
Parallel
Often 8 or more lines (wire conductors) are used to
Parallel
transfer data to a device
Serial
that is only a few feet away
Serial Serial
To transfer to a device located many meters away,
Parallel Serial
Parallel Transfer
Serial Transfer Communication
D0
Sender Receiver
Sender Receiver
D7
One bit Serial
§ 11-1 Basics of Serial Communication
At the transmitting end, the byte of data must be converted
to serial bits using parallel-in-serial-out shift register
At the receiving end, there is a serial in-parallel-out shift
register to receive the serial data and pack them into byte
When the distance is short, the digital signal can be
transferred as it is on a simple wire and requires no
modulation
If data is to be transferred on the telephone line, it must be
converted from 0s and 1s to audio tones
This conversion is performed by a device called a modem,
“Modulator/demodulator”
Serial data communication uses two methods
Synchronous method transfers a block of data at a time
Asynchronous method transfers a single byte at a time
1 bit Receiving
Sending
rate rate
PC peripheral
Asynchronous transmission
The 0 (low) is
The transmission begins with
referred to as space
a start bit followed by D0, the
LSB, then the rest of the bits
until MSB (D7), and finally,
the one stop bit indicating the
end of the character
Due to the extended ASCII characters, 8-bit ASCII
data is common
In older systems, ASCII characters were 7-bit
In modern PCs the use of one stop bit is standard
In older systems, due to the slowness of the
receiving mechanical device, two stop bits were
used to give the device sufficient time to organize
itself before transmission of the next byte
Assuming that we are transferring a text file of
ASCII characters using 1 stop bit, we have a total of
10 bits for each character
This gives 25% overhead, i.e. each 8-bit character
PC Baud Rates
TF is set to 1 every
12 ticks, so it
functions as a
frequency divider
SBUF Register
SBUF is an 8-bit register used solely for serial
communication
For a byte data to be transferred via the TxD line, it must
be placed in the SBUF register
The moment a byte is written into SBUF, it is framed with the
start and stop bits and transferred serially via the TxD line
SBUF holds the byte of data when it is received by 8051
RxD line
When the bits are received serially via RxD, the 8051 deframes
it by eliminating the stop and start bits, making a byte out of the
data received, and then placing it in SBUF
SM2
This enables the multiprocessing capability of the 8051
REN (receive enable)
It is a bit-adressable register
When it is high, it allows 8051 to receive data on RxD pin
If low, the receiver is disable
TI (transmit interrupt)
When 8051 finishes the transfer of 8-bit character
It raises TI flag to indicate that it is ready to transfer another
byte
TI bit is raised at the beginning of the stop bit
RI (receive interrupt)
When 8051 receives data serially via RxD, it gets rid of
the start and stop bits and places the byte in SBUF
register
It raises the RI flag bit to indicate that a byte has been
received and should be picked up before it is lost
RI is raised halfway through the stop bit
Programming Serial Data Transmitting
In programming the 8051 to transfer character bytes
serially
1. TMOD register is loaded with the value 20H, indicating the use of timer1
in mode 2 (8-bit auto-reload) to set baud rate
2. The TH1 is loaded with one of the values to set baud rate for serial data
transfer
3. The SCON register is loaded with the value 50H, indicating serial mode1,
where an 8-bit data is framed with start and stop bits
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF register
7. The TI flag bit is monitored with the use of instruction JNB TI , xx to see if
the character has been transferred completely
8. To transfer the next byte, go to step 5
Programming Serial Data Transmitting
Solution:
(a) This program transfers ASCII letter B (01000010 binary) continuously
(b) With XTAL = 11.0592 MHz and SMOD = 1 in the above program, we have:
11.0592 / 12 = 921.6 kHz machine cycle frequency.
921.6 / 16 = 57,600 Hz frequency used by timer 1 to set the baud rate.
57600 / 3 = 19,200, the baud rate.
Find the TH1 value (in both decimal and hex ) to set the baud rate to each of
the following. (a) 9600 (b) 4800 if SMOD=1. Assume that XTAL 11.0592 MHz
Solution:
With XTAL = 11.0592 and SMOD = 1, we have timer frequency = 57,600 Hz.
(a) 57600 / 9600 = 6; so TH1 = -6 or TH1 = FAH
(b) 57600 / 4800 = 12; so TH1 = -12 or TH1 = F4H
Example 11-3
Find the baud rate if TH1 = -2, SMOD = 1, and XTAL = 11.0592MHz. Is this
baud rate supported by IBM compatible PCs?
Solution:
With XTAL = 11.0592 and SMOD = 1, we have timer frequency = 57,600 Hz.
The baud rate is 57,600/2 = 28,800. This baud rate is not supported by the
BIOS of the PCs; however, the PC can be programmed to do data transfer at
such a speed. Also, HyperTerminal in Windows supports this and other baud
rates.
Example 11-4
Write a program to send the message “The Earth is but One Country” to serial
port. Assume a SW is connected to pin P1.2. Monitor its status and set the
baud rate as follows:
SW = 0, 4800 baud rate
SW = 1, 9600 baud rate
Assume XTAL = 11.0592 MHz, 8-bit data, and 1 stop bit.
Solution:
SW BIT P1.2
ORG 0H ;starting position
MAIN:
MOV TMOD,#20H
MOV TH1,#-6 ;4800 baud rate (default)
MOV SCON,#50H
SETB TR1
SETB SW ;make SW an input
S1: JNB SW,SLOWSP ;check SW status
MOV A,PCON ;read PCON
SETB ACC.7 ;set SMOD high for 9600
MOV PCON,A ;write PCON
SJMP OVER ;send message
SLOWSP:
MOV A,PCON ;read PCON
SETB ACC.7 ;set SMOD low for 4800
MOV PCON,A ;write PCON
OVER: MOV DPTR,#MESS1 ;load address to message
FN: CLR A
MOVC A,@A+DPTR ;read value
JZ S1 ;check for end of line
ACALL SENDCOM ;send value to serial port
INC DPTR ;move to next value
SJMP FN ;repeat
;------------
SENDCOM:
MOV SBUF,A ;place value in buffer
HERE: JNB TI,HERE ;wait until transmitted
CLR TI ;clear
RET ;return
;------------
MESS1: DB “The Earth is but One Country”,0
END
§11-4 Serial Port Programming in C
Transmitting and Receiving Data
Example 11-7
Write a C program for 8051 to transfer the letter “A” serially at 4800 baud
continuously. Use 8-bit data and 1 stop bit.
Solution:
#include <reg51.h>
void main(void){
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1;
while (1) {
SBUF=‘A’; //place value in buffer
while (TI==0);
TI=0;
}
}
Example 11-8
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;
}
Example 11-9
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;
}
}
Example 11-10
Write an 8051 C Program to send the two messages “Normal Speed”
and “High Speed” to the serial port. Assuming that SW is connected
to pin P2.0, monitor its status and set the baud rate as follows:
SW = 0, 28,800 baud rate
SW = 1, 56K baud rate
Assume that XTAL = 11.0592 MHz for both cases.
Solution:
#include <reg51.h>
sbit MYSW=P2^0; //input switch
void main(void){
unsigned char z;
unsigned char Mess1[]=“Normal Speed”;
unsigned char Mess2[]=“High Speed”;
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFF; //28800 for normal
SCON=0x50;
TR1=1; //start timer
.....
if(MYSW==0) {
for (z=0;z<12;z++) {
SBUF=Mess1[z]; //place value in buffer
while(TI==0); //wait for transmit
TI=0;
}
}
else {
PCON=PCON|0x80; //for high speed of 56K
for (z=0;z<10;z++) {
SBUF=Mess2[z]; //place value in buffer
while(TI==0); //wait for transmit
TI=0;
}
}
}
§ 11-5 New communication methods
UART
SPI
I2C
USB
WLAN
Bluetooth
WIFI
3G
USB转TTL转换板
蓝牙转TTL转换板
WIFI转TTL转换板
CP2101(USB 转 UART 的单芯片桥接器 )
Homework
问题:假设某种信号灯通讯,不同长短交替代表
不同的信息。要求在单片机和PC间建立一个信号
传输通道(串行通信)模拟这种信号灯通讯。假
设8次不同的长短信号组合代表一个讯息(8位)
,通过长(>0.5秒)短(<0.5秒)的按键发送命
令,按键命令由单片机检测到后通过串口发送给
PC机。同时单片机有可能接收到PC机发来的命令
,命令信息经过单片机解码后,由单片机通过
P1.0经过LED显示(长显示1秒,短显示0.25秒)
。
Homework
要求:
1. 列出单片机需要哪些资源来完成这项工
作;
2. 画出单片机完成本任务的流程图
3. 讨论在题目中,有哪些任务细节没有设
置清楚,请补充你认为合理的设置;
4. 完成所有的单片机程序,汇编或C语言皆
可。(附加题)