微算機原理與實驗 - National Chiao Tung...
Transcript of 微算機原理與實驗 - National Chiao Tung...
Principle of Microcomputer Serial Port 1
Principle of Microcomputer(UEE 2301/1071 )
Chap 10. MCS-51 Serial Port Operation
微算機原理與實驗
宋開泰
Office:EE709 Phone:5731865(校內分機:31865)
E-mail:[email protected] URL:http://isci.cn.nctu.edu.tw
Principle of Microcomputer Serial Port 2
Serial Port Operation
Outlines:
• Serial I/O Protocols
• Asynchronous Protocols (RS-232)
• MCS-51 Serial Port Operation &
Programming
Principle of Microcomputer Serial Port 3
Terminal or
Computer Data Terminal
Equipment Modem
Modem DTE
Terminal or
Computer
Transmitter
Receiver
( Phone line)
Digital Data
(Protocol is needed)
Analog Data
Digital Data
DTE
DCE
DTE DCE
System architecture of serial communication
Principle of Microcomputer Serial Port 4
RS-232 Interface
• DTE: Data Terminal Equipment (資料終端裝置)
• DCE: Data Communication Equipment(資料通訊裝置)
• MODEM: Modulator/ Demodulator
• RS-232 is a standard originally for providing a
specification for connecting remote devices
over the telephone networks through
modems. Later (as now in wide use) it is used
for terminal and equipment without using
modems.
Principle of Microcomputer Serial Port 5
RS-232 Signals(DB-25連結器)
Pin 2: TxD Transmit Data, transmits data from the DTE to DCE
Pin 3: RxD Receive Data, transmits data from the DCE to DTE
Pin 7: Signal Ground (Common)
Pin 4: RTS* Request To Send, general purpose output
Pin 5: CTS* Clear To Send, general purpose input
Pin 6: DSR* Data Set Ready, general purpose input, to signal to
the DTE that the DCE has been powered up and is ready
to go
Pin 20:DTR* Data Terminal Ready, general purpose output
Pin 8 :DCD* Data Carrier Detect, on DTE, it is frequently used
to disable data reception
Pin 1: Shield Ground,
if it is safe to connect the chassis ground
together
DB-9型連結器
DB-25連結器
Principle of Microcomputer Serial Port 13
Asynchronous Protocols
Electrical conventions: RS-232
• Timing convention :
used frequency: 300, 600, …, 9600, 19200 = BAUD rate
↓ ↓
↑
LSB MSB
Idle or
prior stop Start bit 7 data bits 1 parity bit One or two
stop bits
Principle of Microcomputer Serial Port 14
Asynchronous Protocols
• Least significant bit (LSB) first
pattern (in time) 10000010
recognized as 01000001 = 41H = “A” ASCII code
• Start and stop bit: identify the beginning and end of each transfer; permit a receiver to resynchronize a local clock to each character.
• Receiver samples bits at the center of the bit period,
plus a good estimation of the leading transition on the start bit, plus both clocks to be accurate within 5﹪error margin (no problem with crystal clocks).
• To make the leading edge discoverable, the start bit is made different from both the idle state and the stop bits.
• Fast clock is used to determine the center of the start bit, 16× clock time base.
Principle of Microcomputer Serial Port 15
Asynchronous Protocols
• Use a start and stop bit of opposite polarity
→ 20 ﹪ overhead
8 bits 16 bits 16 bits 16 bits
start bit
Principle of Microcomputer Serial Port 17
Parameters for setting up a serial port
1. The number of data bits/character
2. The number of stop bits
3. The parity bit
4. Baud rate
Principle of Microcomputer Serial Port 18
8051 Serial Port Operation
• MCS-51 has an on-chip serial port, the internal
structure of this serial port is shown below:
8051 Internal bus
Principle of Microcomputer Serial Port 19
8051 Serial Port Operation
• Full duplex, can simultaneous transmission and
reception.
• Receive buffering, allowing one character to be
received and held in a buffer while a second
character is received. The first byte (character) has to
be read before the second byte is received (with
MOV A, SBUF).
• Two special function registers provide software
access to the serial port SBUF and SCON.
• Both receiving and transmission of serial data are
through serial port buffer (SBUF, 99H).
Principle of Microcomputer Serial Port 20
8051 Serial Port Operation
• Write to SBUF loads data to be transmitted.
MOV SBUF, A
• Reading SBUF accesses received data
MOV A, SBUF
Note: SBUF in these instructions has the same name
and address, but they are two separate and distinct
registers, the transmit write-only register and the
receive read-only register.
Principle of Microcomputer Serial Port 21
SCON (98H): Serial Port Control Register (1/3)
The mode of the operation of the 8051 serial port is set
by writing to the serial port control register SCON (98H).
Principle of Microcomputer Serial Port 22
SCON (98H): Serial Port Control Register(2/3)
• RI (SCON. 0): Receive Interrupt flag
– Set by hardware and must be cleared by software
– Set at the end of character reception
– Indicate “receive buffer full”
– This condition is tested by software or
programmed to cause an interrupt
WAIT: JNB RI, WAIT ; Check RI until set
CLR RI
; Clear RI
MOV A, SBUF
; Read character
Principle of Microcomputer Serial Port 23
SCON (98H): Serial Port Control Register(3/3)
• TI (SCON. 1): Transmit Interrupt flag
– Set by hardware and must be cleared by software
– Set at the end of character transmission
– Indicate “transmit buffer empty”
– If a previous character was sent, wait until
transmission is finished before sending the next
character. WAIT: JNB TI, WAIT ; Check TI until set
CLR TI
; Clear TI
MOV SBUF, A
; Send character
Principle of Microcomputer Serial Port 25
SCON (98H): Serial Port Control Register
• RB8 (SCON. 2) Receive bit 8
– In mode 2 and mode 3, store the ninth data
bit received.
• TB8 (SCON. 3) Transmit bit 8
– In mode 2 and mode 3, the to be
transmitted ninth data bit is placed in TB8
– The bit content is set by software
Principle of Microcomputer Serial Port 26
SCON (98H): Serial Port Control Register
• REN (SCON. 4): Receive enable control bit
– 0: receive disabled
– 1: receive enable
– The bit content is set by software
• SM2 (SCON. 5): Serial port mode control bit 2
– Enables multiprocessor communications in modes
2 and 3.
– In mode 2 and mode 3,
when SM2 = 1
RI = 0 if RB8 = 0,
RI = 1 if RB8 = 1.
Principle of Microcomputer Serial Port 27
Mode of Operation
• The 8051 serial port has four modes of operation,
selectable by writing 1s or 0s to the SM0 (SCON. 7)
and SM1 (SCON. 6) bits in SCON.
Principle of Microcomputer Serial Port 28
8-bit Shift Register (Mode 0)
• Synchronous operation mode.
• 8-bit receiving and transmission serial data, no start bit and stop bit.
• Serial data enter and exit through RxD (P3.0, pin 10)
• TxD (P3.1, pin 11) outputs the shift clock (= fosc/12)
Principle of Microcomputer Serial Port 31
8-bit UART with Variable Band Rate (Mode1)
• In mode 1, 8051 serial port operates as an 8-bit UART
(Universal asynchronous receiver/transmitter) with
variable baud rate.
• TxD (P3.1, pin 11) transmits serial data, RxD (P3.0, pin
10) receives serial data.
• 10 bits are transmitted or received. This consists of a
start bit (always 0), 8 data bits (LSB first) and a stop bit
(always 1).
Principle of Microcomputer Serial Port 32
9-bit UART with Fixed Baud Rate (Mode 2)
• When SM1 = 1, SM0 = 0, the serial port operates in mode 2, a
9-bit UART with a fixed baud rate.
• Serial data consists of 11 bits
– 1 start bit
– 8 data bits
– 1 programmable 9th data bit (TB8)
– 1 stop bit.
• On transmission, the ninth data bit is whatever has been put in
TB8 (SCON. 3)
• On reception, the ninth data bit received is put into RB8(SCON.
2)
• The baud rate in mode 2 is either 1/32 or 1/64 of the on-chip
oscillator frequency.
Principle of Microcomputer Serial Port 33
9-Bit UART with Variable Baud Rate (Mode 3)
• The operation of Mode 3 is the same as
Mode 2, except the baud rate is
programmable and provided by the timer.
• Baud rate: variable
– 8051: programmed by the overflow rate of
Timer 1
– 8052: programmed by the overflow rate of
Timer1 or Timer2
Principle of Microcomputer Serial Port 34
Serial Port Baud Rates(1/4)
1. Mode 0 Baud rate
• Fixed baud rate = fosc/12
For example: 12 MHz crystal 8051
Baud rate = 12 MHz/12 = 1MHz = 1Mbps
2. Mode 2 Baud rate
The baud rate is affected by a bit in the power control register PCON, PCON.7 (SMOD bit)
• SMOD = 1, Baud rate = fosc/32
• SMOD = 0, Baud rate = fosc/64
By default, following a system reset, the mode 2 baud rate = fosc/64
for example, MOV A, PCON
SETB ACC.7
MOV PCON, A ; setting SMOD.
Principle of Microcomputer Serial Port 35
Serial Port Baud Rates(2/4)
3. Mode 1 and Mode 3 Baud rate
• Determined by the Timer 1 overflow rate
• The overflow is divided by 32 (16 if SMOD = 1) before providing the baud rate clock to the serial port.
• Timer works in Mode 2 (Auto-reload mode)
For example, 1200 baud rate
1200 = Timer 1 overflow rate ÷ 32
→ Timer 1 overflow rate = 38.4 KHz
if 12 MHz crystal, Timer 1 clock rate = 1 MHz
Overflow is required every 1000 KHz ÷38.4 KHz = 26.04 (round to 26) clocks
)]1TH(256[12
fosc
32
2rate Baud
SMOD
rate Baud384
fosc22561TH
SMOD
Principle of Microcomputer Serial Port 36
Serial Port Baud Rates(3/4)
Timer 1 counts up and overflow on FFH-to-00H
transition of the count, 26 counts less than “0” is the
required reload for TH1.
MOV TH1, # - 26
or MOV TH1, # 0E6H
• There is a slight error in the resulting baud rate, due
to rounding
• A 5﹪error is tolerable using asynchronous protocol
• If 11.059 MHz crystal is used, exact baud rates are
possible.
Principle of Microcomputer Serial Port 38
Example: Initialize the Serial Port
• To initialize the serial port to operate as an 8-bit UART at 2400 Baud, using Timer 1.
– Four registers must be initialized: SMOD, TMOD, TCON, TH1
– SM0/SM1 = 0/1 → 8-bit UART mode
– REN = 1 enable the serial port to receive characters.
– Setting TI = 1, transmission of the first character indicating that the transmit buffer is empty
– For TMOD, Timer 1 in 8-bit auto-reload mode, →M0/M1 = 1/0.
– TR1 = 1 turns the Timer 1 on.
– TH1 value provides the required overflow rate, 2400× 32 = 76.8 KHz
12 MHz crystal for 8051, Timer 1 is clocked at 1 MHz.
1000÷76.8 = 13.02 (round to 13) → the reload value = -13 (0F3H)
Principle of Microcomputer Serial Port 39
Example 1: Initialize the Serial Port
ORG 8100H
INIT: MOV SCON, 52H ; serial port mode1
MOV TMOD, # 20H ; timer1, mode2
MOV TH1, #-13 ; reload count for 2400 baud
SETB TR1 ; start timer1
(to be
continued)
Principle of Microcomputer Serial Port 40
Output Character Subroutine
(From previous page)
OUTCHAR: MOV C,P ;Put parity bit in C Flag
CPL C ;Change to odd parity
MOV ACC.7,C ;Add to character code, mode 1
;8 data bits transmitted
AGAIN: JNB TI, AGAIN ;TxD buffer empty?
CLR TI ;Yes, Clear flag and
MOV SBUF, A ; send character
CLR ACC.7 ;Strip off parity
RET ;do nothing
END
Principle of Microcomputer Serial Port 41
Example 2: Serial Port Interrupts
ORG 0000H ;Reset
LJMP MAIN
ORG 0023H ;Serial port vector
LJMP SPISR
ORG 0030H ;Main program entry
MAIN: MOV TMOD, #20H ;timer1,mode 2
MOV TH1, # -26 ;1200 Baud reload
SETB TR1 ;Start timer1
MOV SCON, #42H ; Mode 1. Set TI
MOV A, #20H ; Send ASCII space
MOV IE, #90H ;enable serial port intrrrupt
SJMP $ ;do nothing
Principle of Microcomputer Serial Port 42
SPISR: CJNE A, #7FH, SKIP ;if finished ASCII set
MOV A, #20H ;reset to space
SKIP: MOV SBUF, A ; send character
INC A ;Increment ASCII code
CLR TI ;clear inter. Flag
RETI
END
Interrupt Service Routine
實驗原理
• 8051串列傳輸為全雙工式,亦即傳送及接收可同時進行
• 對串列埠接收及傳送暫存器之存取都是在特殊功能暫存器SBUF進行,
• 寫入SBUF(MOV SBUF,A)等於資料載入傳送暫存器
• 讀取SBUF(MOV A,SBUF)實際上是由另一分開的接收暫存器取出資料。
• 8051串列埠有4種操作模式,此次實驗主要以模式1(mode 1)為主
SERIAL PORT與中斷
• 每當8051完成傳輸後,TI(傳輸中斷旗標)會被設為1,如有Enable中斷則會跳至中斷向量23H
• 每當8051收完一筆資料後,RI(接收中斷旗標)會被設為1,如有Enable中斷則亦會跳至中斷向量23H
• 因此在中斷服務程式內要先用RI/TI判斷是接收完成還是傳輸完成而產生此中斷
鮑率(BAUD RATE)
• 常用計時器1(Timer 1) 之自動重載(Auto-reload)方式產生鮑率。
• 鮑率( 以Timer 1 之mode2 產生)
=
=
2
32 12 [256-TH1]
SMOD
石英震盪器頻率
2 1
32 [256-TH1]
SMOD
機器週期
暫存器設定
SCON
PCON
SM0 SM1 SM2 REN TB8 RB8 TI RI
SMOD GF1 GF0 PD IDL
• 設定Serial port工作於Mode 1 => MOV SCON,#01010000B
• 設定SMOD為0 => ANL PCON, #01111111B
Mode 1:每次10個位元,首先為啟始位元(0),8個資
料位元(先為LSB),和一個停止位元(1)。
DEMO2參考流程圖:
利用模式1接收PC端資料再將同樣的資料回送至PC
設定傳輸模式與鮑率
主程式
設定欲傳輸資料初值
空迴圈等待
接收資料
SERIAL PORT中斷
RI==1?
關閉中斷
RETI
RETI
傳輸資料
N
Y