Sunteți pe pagina 1din 42

Programming internal

compoenents

Contents
Timer

& counters
Serial communication
Interrupts

Timers

Timer & Counter

8051 has 2 built-in timers: timer 0 and timer 1


- They can be used to generate a specific time delay
(more accurate and easier than loops) Or,
- can be used as counters to count events happening
outside the uC.
Both timers are 16 bits wide.

Three important register associated with


Timer register - (TH0-TL0) (TH1-TL1)
Timer mode register - TMOD
Timer control register -TCON

Timer
registers
Timer registers (2)
The contents of each timer is stored in two 8-bit registers

Timer 0:
TH0 (higher byte), TL0

(lower byte)

Timer 1:
TH1 (higher byte), TL1 (lower byte)

TMOD (timer mode) register


TMOD: 8-bit register, used to set the operation modes of timer :higher nibble: timer 1; lower nibble: timer 0
M1, M0: the combination of the two bits specify the operation modes of
the timer
(M1,M0)= 00: mode 0; 13 bit timer
01: mode 1; 16 bit timer
10: mode 2; auto reload 8bit
11: mode 3.
C/T (counter/timer)
Whether the timer will be used as a delay generator (timer) or
event counter
C/T = 0: used as timer for time delay generation
C/T = 1: event counter
Gate
Gate = 0: the timer is started or stopped by software
Gate = 1: the timer is started or stopped by external
hardware

TCON register

8-bit register
TR0, TF0, TR1, TF1

TR0 & TR1 :- Start timer 0 or 1


- SETB TR0 is the same as SETB TCON.4
TF0 & TF1 :- Timer flag will be 1 when timer 0 or 1
overflows

IE1, IT1, IE0, IT0 are used by interrupt

Mode-1
Mode 1 16-bit timer: all 16 bits of timer are used.
Steps: (we use timer0 as example)
1. Load TMOD value (set the operation mode of timer)
E.g. MOV TMOD, #01H; timer 0, C/T=0, GATE=0, mode = 1
2. Load TL and TH with initial count values

E.g. MOV TL0, #0F2H ; load init value to timer 0

MOV TH0, #0FFH; init value: FFF2


3. Start timer
SETB TR0 ; start timer 0
TR0(timer start 0): when set to 1, timer 0 will start
Once timer is started, values in (TH0, TL0) will automatically
increase by 1for every timer tick (every 1.085us
when (TH0, TL0) = FFFFH, it will overflow in the next clock
period FFFFH will become 0000H,
- When overflow, TF0 (timer flag for timer 0) will automatically
-By monitoring TF0, we will know when timer expires

et to 1.

Steps (Contd)
4. Monitoring timer flag
- HERE: JNB TF0, HERE ; exit loop when TF0 = 1 (timer expires)
5. When timer overflows, stop timer
CLR TR0 ; set TR0 = 0 will stop timer 0
The duration of timer is determined by the initial value of
timer
6. Clear timer flag for next round
CLR TF0
The duration of timer is determined by the initial value in
timer and timer clock

(FFFFH YYXXH + 1)*clock period


E.g. before timer starts, (TH0)=FFH, (TL0)=F2H. XTAL =
11.0592MHz.What is the duration of the timer (how long
it akes for timer to overflow)?
{((FFFF)H-(FFF2)H)+1} =14*1.085 usec=15.19usec

Example
Write a program to generate the delay timer 0 .
CLR P2.3
MOV TMOD, #01
; 1. timer 0, mode 1 (16-bit)
HERE: MOV TL0, #3EH
;2. load init value
MOV TH0, #0B8H
SETB P2.3
SETB TR0
;3. start timer
AGAIN: JNB TF0, AGAIN
;4. monitor TF0 until timer overflows
CLR TR0 ; 5. stop timer
CLR TF0 ; 6. clear flag
CLR P2.3

Find the maximum delay that can be incurred by a 16-bit time


To generate larger delay, use loops to repeat the timer

Write a program generate a square waveform on


P1.5. The waveform has 50%
duty cycle with period 2 ms.
MOV TMOD, #10 ; timer 1, mode 1
HERE: SETB P1.5
ACALL DELAY
CLR P1.5
ACALL DELAY
SJMP HERE
;-----------delay subroutine------------------DELAY:
MOV TL1, #YYH ; load initial value
MOV TH1, #XXH
SETB TR1 ; start timer
AGAIN: JNB TF1, AGAIN ; monitor flag
CLR TR1 ; stop timer
CLR TF1 ; clear flag
RET

Mode 0: 13-timer
The timer will set TF = 1 when it reaches 1FFFH
MOV TMOD, #00H (mode 0)
The remaining operation is the same as mode 1
Mode 2: auto-reload 8-bit timer
Timer range: 00H - FFH
Operations (use timer 0 as example)
1. Set timer 0 to mode 2: MOV TMOD, #02H (timer 0, mode 2)
2. Load 8-bit init value to TH0: MOV TH0, #32H
3. 8051 automatically copies the init value to TL0
4. Start timer: SETB TR0
TL0 starts counting until it reaches FFH
5. Monitor TF0
When TL0 overflows from FFH to 00H, it sets TF = 1
6. When TL0 overflows to 00H and TF = 1, the init value is
automatically
reloaded from TH0 to TL0.
7. Clear TF. The timer will continue to run (go back to step 5)

Counter (C/T=1)

Counting the events happening outside of 8051.


Counter v.s. timer
If C/T=0, timer. The clock source is XTAL/12
The value of TH0, TL0 increases by 1 for each
clock
If C/T = 1, counter. The clock source is an outside
pulse to be counted
The value of TH0, TL0 increases by 1 for each
outside pulse
We can use the counter to count the # of pulses
The remaining operations are the same for counter
and timer (e.g. mode 0, 1, 2)
Connection
External pulses are connected through P3.4 (T0)
or P3.5 (T1)

Example
Assume a 1 Hz frequency pulse is connected to input pin
P3.5 (T1). Use mode 2 of counter 1 to count the pulses and
display results on P2
MOV TMOD, #01100000B ; counter 1, mode 2, C/T
=1
MOV TH1, #0 ; counting from 0
SETB P3.5 ; make T1 input mode
AGAIN: SETB TR1 ; start timer 1
BACK: MOV A, TL1 ; get copy of count from TL1 (mode 2!)
MOV P2, A ; send result to P2 (LCD)
JNB TF1, BACK ; keep doing it if TF=0 (no overflow)
CLR TR1 ; stop timer
CLR TF1 ; clear TF
SJMP AGAIN

Serial Communication

Serial communication v.s. parallel communication

Computer transfer data in two ways: parallel, serial


Parallel communication
8 or more parallel lines are used to transfer data
E.g. connecting 8051 to LCD,
data bus, connecting to hard drive
More data can be transferred in unit period of time
Usually used for short distance data transfer
Long parallel wires function like antenna, and will leak signal
during Transmission
The leaked signals will cause mutual interference for signals
wire (cross-talk)
Serial communication
Use 1 data line to transfer data
Data is transmitted 1 bit a time
Usually used for data transfer over longer distance

in

Simplex, Half-duplex, and full-duplex

Simplex: communication can occur in only one direction


E.g. pager, broadcast radio

Half-duplex: communication can happen in both directions, but


only one at a time (A to B, or B to A, but not simultaneously)

E.g. Police radio (walki-talki)


Only 1 channel (data line) is enough
Full-duplex: communication can happen in both directions
simultaneously

E.g. telephone
Two channels (data lines) are required.
Full-duplex = two simplex

serial communication

Asynchronous

Data is transmitted in bursts without following a specific


clock
- Data can be transmitted at any time.
Synchronous
- data can only be transmitted at special instants
When there is no data, channel remains constant to indicate
idle
(no information).
e.g. some system use high voltage to indicate idle
How does the receiver tell the difference between idle
and
11111111?
Framing

Data characters are placed between start and stop bits


Start bit: 1 bit (e.g. low)
Stop bit: 1 bit (e.g. high), or, 2 bits (e.g. high, low)
E.g. 8-bit ASCII + 1 start bit + 1 stop bit = 10 bits/frame

SERIAL COMMUNICATION: STANDARD


Communication standard
A set of rules that must be followed by communication devices
To ensure that communication devices from different
manufactures can interoperate with each other
Example rules:
Which voltage used to represent 0, which voltage used to
represent 1
How many start bits, how many stop bits
Which voltage(s) used for start bits, which voltage(s) used for stop
bits
How many bits in one frame (7 bits, 8 bits, 10 bits, )
The format of control signals, how many control pins
Example standards
RS232, IEEE 802.11 (WiFi), IEEE 802.16 (WiMax), CDMA,

RS232
The most popular serial communication standard
Developed by Electronics Industries Association (EIA) in the 1960s
Still widely used today

Signal level of RS232


1 is represented by -3 to -25 V
0 is represented by +3 to +25 V
They are not TTL (transistor-transistor logic) compatible. In TTL
1: 2.2 to 5 V
0: 0 to 0.8 V
8051 is TTL compatible:
-8051 and RS232 devices cannot be directly connected together!

MAX232
A voltage converter (line driver) that can convert RS232 signal to
TTL voltage level
1 MAX232 chip can be used to drive 2 RS232 ports

8051 AND UART

8051
Most 8051 has 1 serial port
P3.0 (RXD0), P3.1 (TXD0)

UART
Universal asynchronous receiver transmitter
An integrated circuit commonly used in conjunction with
RS232
Its built inside 8051
The circuit can interpret communication command
When an ASCII code is sent to UART, it will automatically add
start
and stop bits before transmit it through serial port
When receiving data from serial port, UART will automatically
detect start bit and stop bit, remove the start and stop bits
from the
received data, and send the pure data to the CPU
UART saves us from the details of communication standards.

Baud rate in 8051 and timer

Baud rate in 8051 and timer


The baud rates supported by 8051 (unit: bps): 9600, 4800,
2400,
1200)
How to set the baud rate?
Baud rate in 8051 can be set via timer 1 in mode 2 (8-bit
autoreload)
When used for serial port, the frequency of timer tick is
determined by(XTAL/12)/32

1 bit is transmitted for each timer period (the


time duration from timer start to timer expires)

Calculation of baud rate


With XTAL = 11.0592, find the TH1 value needed to have the baud rate
9600
Clock frequency of timer clock:
f = (11.0592 MHz / 12)/32 = 28,800Hz
Time period of each clock tick:
T0 = 1/f = 1/28800
Duration of timer (1 timer cycle):
(# of clock ticks in timer)*T0
9600 baud duration of 1 symbol: 1/9600
1 timer cycle: 1/9600

1/9600 = 1/f * (# of clock ticks in timer)


# of clock ticks in timer = f/9600
= 28800/9600 = 3 ;TH1 =-3
Similarly, for baud 2400
# of clock ticks = f/2400 = 12 ; TH1 = -12
When connecting two devices through serial port, both devices
must have the same baud

Serial communication
:registers
SBUF register (Serial buffer)

An 8-bit register used for serial communication


It holds the data to be transferred or received from serial port.
E.g. to send D to serial port:
MOV SBUF, #D
The data in SBUF will be automatically processed by UART, then
sent to serial port (e.g. pin TXD0)
E.g. to receive data from serial port:
MOV A, SBUF
Once UART receives data from serial port (e.g. pin RXD0), it will
strip
the start and stop bits and then put the data in SBUF
It serves as a buffer between CPU and serial ports

Serial communication
SCON register (Serial
control register)
:registers

An 8-bit register used to program the start bit, stop bit, and
data bits of data framing, and some other serial related
processing
- SM0, SM1 (serial port mode)

Specify framing format, how to calculate baud


- (SM0, SM1) = (0,1), mode 1: 8-bit data, 1 start bit, 1 stop bit,
Variable baud set by timer. Most commonly used
The other three modes are rarely used
(SM0,SM1) = (0,0), mode 0: fixed baud = XTAL/12
(SM0, SM1) = (1,0), mode 2: 9-bit data, fixed baud
(SM0, SM1) = (1, 1), mode 3: 9-bit data, variable baud
SM2
SM2 = 0: single processor
SM2 = 1: multiprocessor communication (not required for this
course)

REN (Receive Enable)


Enable/disable reception
REN = 1: the 8051 will accept incoming data from serial port
REN = 0: the receiver is disabled
E.g. SETB REN, CLR REN, SETB SCON.4, CLR SCON.4

TB8
Used by modes 2 and 3 for the transmission bit 8 (the 9th data bit)
CLR TB8 when using mode 1
- RB8
Used by modes 2 and 3 for the reception of bit 8 (the 9th data bit)
Used by mode 1 to store the stop bit

-TI (transmit interrupt)


When 8051 finishes the transfer of the 8-bit character, it set TI to 1 to
indicate that it is ready to transfer the next character
The TI is raised at the beginning of the stop bit

RI (receive interrupt)
When 8051 receives a character
1. The UART removes start bit and stop bit
2. The UART puts the 8-bit character in SBUF
3. RI is set to 1 to indicate that a new byte is ready to be picked up
in SBUF
RI is raised halfway through the stop bit

Example
Write a program to transfer letter A serially at
4800 baud, continuously
MOV TMOD, #20H
; timer 1, mode 2 (8-bit auto-reload)
MOV TH1, #-6
; 4800 baud
MOV SCON, #50H ; 0101 0000
(mode 1,SingleProcessor,REN=1)
SETB TR1 ; start timer
AGAIN: MOV SBUF, #A
; store A in SBUF
HERE: JNB TI, HERE ; wait for TI = 1 (transmission over)
CLR TI
; clear TI for next transmission
SJMP AGAIN
; repeat
The data in SBUF is transmitted serially, one bit at a time
If we write a new character to SBUF before TI is raised (the
transmission of the previous character is not over yet), part of
the original data will be lost

Example
Program the 8051 to receive bytes of data serially,
and put them in P1. Set the baud rate at 4800, 8-bit
data, 1 stop bit
MOV TMOD, #20H
MOV TH1, #-6 ; 4800 baud
MOV SCON, #50H
; mode 1
SETB TR1
HERE: JNB RI, HERE
; wait for char to come in
MOV A, SBUF ; save incoming byte in A
MOV P1, A
; send to port 1
CLR RI ; clear, get ready for next byte
SJMP HERE

RI = 1 indicates a new byte is copied in SBUF


We need to copy the data in SBUF to another place
immediately after RI = 1
Otherwise the contents in SBUF will be overwritten by the
next character

Interrupts

What is interrupt?
Example: timer
MOV TMOD, #02 ; 1. timer 0, mode 2 (8-bit auto reload)
HERE: MOV TH0, #3EH ; 2. load init value
SETB TR0 ; 3. start timer
AGAIN: JNB TF0, AGAIN ; 4. monitor TF0 until timer overflows
CLR TR0 ; 5. stop timer
CLR TF0 ; 6. clear flag

Polling: the CPU continuously monitor the status of a device


(e.g. check the status of TF0, wait until TF0 turns to 1)
The status monitoring process wastes a lot of MCU resources

Interrupt
Whenever any device needs service (e.g. timer expires), it will
notify
the MCU by sending an interrupt signal.
Upon receiving the interrupt signal, the CPU interrupts whatever it
is
doing, and serve the device (e.g. stop timer) by executing a special
subroutine: Interrupt Service Routine (ISR)
Once ISR is done, CPU will go back to the location before interrupts.

Interrupt service routine

Special subroutines to be executed by CPU upon interrupt


Different interrupts have different subroutines
Example: A program needs to
(1) send the letter A to P1 every 100ms;
(2) continuously sensing and display temperature

1. start timer, then display temperature on display


2. when timer expires, it sends an interrupt signal to CPU
3. CPU stops temperature sensing and jump to ISR
4. When ISR is over, the CPU returns to where it left and
continue temperature sensing

Multiple devices can be connected to MCU, each of them


has their unique ISRs. Whenever a device needs service, it
will send interrupt to CPU and CPU can execute the
corresponding ISR.

Six
interrupts
in
8051
There are totally six different interrupts in 8051
Each interrupt has its own ISR
Each ISR has its unique starting address
When a particular interrupt is detected by CPU, PC will jump to the
corresponding ISR starting address to execute the ISR.
Timer interrupts (2 interrupts)
1 interrupt for timer 0 (ISR starting address: 000BH)
1 interrupt for timer 1 (ISR starting address: 001BH)

Serial ports interrupt (1 interrupt)


1 interrupt used to indicate Tx or Rx is done (ISR starting address: 0023H)

External hardware interrupts (2 interrupts)


P3.2: external interrupt 0 (INT0) (ISR starting address: 0003H)
P3.3: external interrupt 1 (INT1) (ISR starting address: 0013H)

Reset (1 interrupt)
when reset is activated, the 8051 jumps to address 0 (ISR address: 0H)

Interrupt vector table

The starting address for each ISR


If interrupts are enabled, ROM addresses 0003 0023 are
taken by interrupt vector table
- We need to bypass interrupt vector table
ORG 0
LJMP MAIN ; bypass interrupt vector table
;-------------------; ISRs can be written from 0003 0029 H
;-------------------ORG 30H
MAIN : MOV A, #23H ; main prgram

INTERRUPT ENABLE REGISTER

Enable/disable interrupts
Upon reset, all interrupts are disabled: the CPU will not respond to
any
interrupt.
In order to use an interrupt, it must be enabled by software
Register: IE (interrupt enable, bit addressable)
EA=0: disable all interrupts.
EA=1: each interrupt is enabled individually.
ET2 = 1: enable interrupt for timer 2
ES = 1: enable interrupt for serial port
ET1 = 1: enable interrupt for timer 1
EX1 = 1: enable external interrupt 1
ET0 = 1: enable interrupt for timer 0
EX0 = 1: enable external interrupt 0
Example
MOV IE, #10010110B
MOV IE, #00010110B

TIMER INTERRUPTS
Roll-over timer flag and interrupt

Recall: when timer roll-over, TF0 or TF1 is set to 1


Polling: HERE: JNB TF0, HERE
- The entire CPU is tied down and waiting for TF to be
set to 1

Interrupt
If timer interrupt is enabled (SETB IE.3 for timer 0, SETB IE.5
for timer1),
whenever TF0 or TF1 is set to 1, a timer interrupt will be
automatically generated
- Program will transfer to ISR at specified location for timer0/1

Example
Write a program that continuously (1) gets 8-bit data from P0
and sends it to P1 while simultaneously (2) create a square
wave of 200 us period on P2.1
ORG 0000H
LJMP MAIN
; by pass interrupt vector table
;------------------timer 0 ISR --------------ORG 000BH
; timer 0 ISR
CPL P2.1 ; toggle P2.1
RETI
; return from ISR
;------------------ main program-------------ORG 0030H
MAIN: MOV TMOD, #02H ; timer 0, mode 2 (auto-reload)
MOV P0, #0FFH ; input
MOV TH0, #-92 ; initial value
MOV IE, #82H ; IE = 10000010
SETB TR0 ; start timer
;--- read P0 send its data to P1----BACK: MOV A, P0
MOV P1, A
SJMP BACK
END
1. Due to interrupt, we dont need to monitor TF0
2. No need to clear TF0!

External interrupts INT0 and


INT1
INT0: P3.2, ISR starting address: 0003H
INT1: P3.3, ISR starting address: 0013H
Upon activation of these pins, the 8051 gets interrupted in
whatever it is doing and jumps to the corresponding ISR
Two types of interrupts activation signals
Level triggered interrupt (default interrupt mode)
INT0 and INT1 is normally high
If a low level signal is applied to them, interrupt is triggered.
If the low level signal is not removed after the execution of
ISR, it will be interpreted as a new interrupt.
Edge triggered interrupt
If a high-to-low signal is applied to P3.2 or P3.3, interrupt is
triggered

Example
Assume INT1 is connected to a switch that is
normally high. Whenever it goes low, it should turn
on an LED. The LED should stay on for a fraction of
ORG 0H
second.
LJMP MAIN
;----------------ISR for INT1-------------------ORG 0013H
SETB P1.3
MOV R3, #255
BACK: DJNZ R3, BACK
CLR P1.3
RETI
;-----------------Main Program---------------ORG 30H
MAIN: MOV IE, #10000100B
HERE: SJMP HERE
END

EXTERNAL INTERRUPT: EDGE TRIGGERED


Edge triggered
Level triggered interrupt is the default interrupt mode.
In order to change to edge triggered mode, we need to set the
TCON register

TF1: timer over flow flag. (HERE: JNB TF1, HERE)


TR1: start or stop timer. (SETB TR1, CLR TR1)
IT1: interrupt mode selection. Default is 0
IT1 = 0: level triggered (triggered by low level)
IT1 = 1: edge triggered (triggered by H-to-L)
IE1: external interrupt 1 edge flag.
Its set to 1 when H-to-L is detected
Its automatically cleared when the interrupt is processed (after
RETI is executed).
When IE1 = 1, no more interrupt will be recognized avoid
interrupt in an interrupt.
If IT1 = 0 (level triggered), IE1 is not used at all.

Example
Assuming P3.3 (INT1) is connected to a pulse
generator. Write a program in which the falling edge
of the pulse will send a high pulse to P1.3, which is
ORG 0000H
connected
LJMP MAIN to an LED.
;--------------ISR for INT1----------ORG 0013H
SETB P1.3
MOV R3, #255
BACK: DJNZ R3, BACK
CLR P1.3
RETI
;---------------Main-------------------MAIN: SETB TCON.2
; make INT1 edge-triggered interrupt
MOV IE, #10000100 ; enable external INT1
HERE: SJMP HERE
END

Serial port interrupt


Only 1 interrupt and 1 ISR assigned to serial port communication
for both Tx and Rx
ISR entrance address: 0023H
When TI or RI is raised, an interrupt is generated, and PC will jump
to 0023H to execute the corresponding IRS
How do we tell if the interrupt is generated by TI or RI flag?
In the ISR, we must examine the RI and TI flag to see which one
triggered the interrupt.
Before executing RETI, we must clear RI or TI flag
So in the next serial port interrupt we will still be able to
distinguish
Tx interrupt from RI interrupt

Example
Write a program in which the 8051 gets data from P1 and
sends it to P2 continuously while incoming data from the serial
port
is sent to P0. Assume that XTAL = 11.0592MHz. Set the
ORG 0
LJMPrate
MAIN at 9600.
baud
;----------------------------------ORG 23H
SER: JB TI, TX
MOV A, SBUF
MOV P0, A
CLR RI
RETI
TX: CLR TI
RETI
;--------------------------------------ORG 30H
MAIN: MOV P1, #0FFH
; make P1 an input port
MOV TMOD, #20H
; timer 1, mode 2
MOV TH1, #-3
; 9600 baud
MOV SCON, #50H
; mode1 ,receiver enable
MOV IE, #10010000B
; enable serial interrupt
SETB TR1
; start timer 1
BACK: MOV A, P1
; read data from P1
MOV P2, A ; send it to P2
SJMP BACK
END

INTERRUPT PRIORITY
Interrupt priority
What will happen if two or more interrupts are
activated at the same time?
The 6 interrupts have different priorities
When two or more interrupts are activated
simultaneously, interrupt with higher priority will
be served first.

IP register (Interrupt Priority Register)


The interrupt priority can be changed by programming the

When power up, all bits in IP register are 0 default priority order
To assign a high priority to an interrupt, set the corresponding
bit to 1
The interrupt with IP bit set to 1 has the highest priority among all
interrupts.
If more than one bit are set to 1, their relative priorities are
determined by the default priority order
Interrupts with priority bits set to 1 have higher priority than
interrupts
with priority bits being 0.

Interrupt inside an interrupt


What happens if another interrupt happens during
the execution of ISR?
If the new interrupt has a higher priority than the
interrupt that is
currently being responded, the CPU will
1. immediately jump to the ISR of the new
interrupt.
2. Upon finishing the new ISR, come back to the
original ISR being served.
If the new interrupt has a lower priority than the
current interrupt, theCPU will
1. finish the current interrupt,
2. then jump to the ISR of the new interrupt

S-ar putea să vă placă și