Serial Communications 8051 Serial I/O: One Subroutine For Two Conditions
Serial Communications 8051 Serial I/O: One Subroutine For Two Conditions
Serial Communications 8051 Serial I/O: One Subroutine For Two Conditions
• bytes can arrive an any time • logic to detect if the STOP bit is present
• the CPU has two status bits in PSW to inform the user • status bits to indicate when data has been received,
when either of these conditions occur: • status but showing sending data has finished being
shifted out
• RI – (Receive) - set if a new byte has arrived
• TI – (Transmit) - set if another byte may be send Serial Interface is controlled by two registers:
Sending Data Serially & Asynchronously SCON – Serial Port Control Register
To send parallel data serially (one bit at a time), a shift
register can be used:
SM0 SM1 SM2 REN TB8 RB8 TI RI
Q0 Serial Bit 7 Bit 0
data
Parallel Load SR out
Clock SR Shift Register SM0 SCON.7 Serial Port Mode see below
SM1 SCON.6
1 D7 D6 D5 D4 D3 D2 D1 D0 0
SM2 SCON.5 Enables Multiprocessor I/O in mode
Parallel Data In
2&3
REN SCON.4 Receive Enable if REN=1
If only two wires are used, the clock can't be sent BUT if TB8 SCON.3 9th Data bit to send in 9bit mode
clock period is known, then using START and Stop bits and RB8 SCON.2 in Modes 2 & 3, 9th data bit received
synchronising on falling edge of START bit will suffice. TI SCON.1 Transmit Interrupt Flag – sending
Start D0 D1 D2 D3 D4 D5 D6 D7 Stop finished
TI must be reset by software
RI SCON.0 Receive Interrupt Flag – new byte
Start 1 1 0 0 1 0 1 0 Stop available
RI must be reset by software
Start 1 1 1 1 1 1 1 1 Stop
SM0-1 Mode Bit Definition
SM0 SM1 Mode Description Baud Rate
Start 0 0 0 0 0 0 0 0 Stop
00 0 Shift Register Clock/12
X x x x x x x x x X
01 1 8 Bit UART Variable
10 2 9 Bit UART Clock/32 or 64
Wait for this edge
11 3 9 Bit UART Variable
Delay ½ bit time – then clock Receiving Shift Register at these points
dddddd UART – Universal Asynchronous Receiver/Transmitter
All of this logic is built into the 8051 Serial: Shift-registers & Stop/Start & control logic
Serial I/O and Interrupts 2 Copyright 2005 Giovanni Moretti Serial I/O and Interrupts 4 Copyright 2005 Giovanni Moretti
Polling Serial I/O Demo Interrupts
è Interrupts are Hardware-Initiated Subroutine Call
org 00h
; Set up Timer 1 to act as clock for Serial Port
to a predefined location.
Serial I/O and Interrupts 5 Copyright 2005 Giovanni Moretti Serial I/O and Interrupts 7 Copyright 2005 Giovanni Moretti
Interrupt Vectors
Interrupts The address jumped to by the hardware for each source of
Hardware-forced subroutine call – can happen between interrupt is fixed.
any two instructions.
Interrupt Vector Address
Reset 0000H (not really an Interrupt)
// Start address of Program code
Polling vs Interrupts
Polling Advantages IE0 0003H
TF0 000BH
♦ simple
IE1 0013H
♦ no unexpected events TF1 001BH
♦ easy to program/debug SBUF (TI or RI) 0023H
♦ Fast response
♦ Push PC onto stack (so can get back) Interrupts will be delayed:
Returning from Interrupt Global interrupt bit (EA) = 0 // All Interrupts Suppressed
The RETI instruction must be used (not RET)
OR
The RETI Instruction:
Device interrupt enable bit = 0 // Specific Device
// Interrupt suppressed
• pops PC off the stack (restores the PC)
Serial I/O and Interrupts 9 Copyright 2005 Giovanni Moretti Serial I/O and Interrupts 11 Copyright 2005 Giovanni Moretti
clr ti
; No - must be TI Danger of Shared Variable Access
reti ; Ignore Transmit
; Interrupts If specific measures aren't taken to stop unexpected access
;------------------------------------------- to variables shared between (or accessed by both) Interrupt
; RECEIVE INTERRUPT HANDLER Subroutines and the main program, unexpected results will
almost certainly be obtained.
RCV_ch: push psw ; Save Registers
push acc
This is because, mentally, the programmer expects
mov a,sbuf ; Get the char statements to execute in the order they're written, but
clr ri ; Clear Rcv flag between any pair of statements, an Interrupt may occur.
mov @r0,a ; Put char in IRAM This produces an unexpected (and unpredictable) change in
inc r0 the order statements are executed.
clr c
subb a,#32 ; ch to uppercase This can result in:
• unexpected changes in data values
mov sbuf,a ; now send char so
• erratic program behaviour (different each time you
; user can see it
run the program)
Exit: pop acc ; Restore Acc pop
pop psw ; and PSW These bugs are very difficult to diagnose. It is best to stop
such bugs happening rather than try and find them later
reti ; return to mainline
Serial I/O and Interrupts 13 Copyright 2005 Giovanni Moretti Serial I/O and Interrupts 15 Copyright 2005 Giovanni Moretti