Documente Academic
Documente Profesional
Documente Cultură
compoenents
Contents
Timer
& counters
Serial communication
Interrupts
Timers
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)
TCON register
8-bit register
TR0, TF0, TR1, TF1
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
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
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
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)
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
in
E.g. telephone
Two channels (data lines) are required.
Full-duplex = two simplex
serial communication
Asynchronous
RS232
The most popular serial communication standard
Developed by Electronics Industries Association (EIA) in the 1960s
Still widely used today
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
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.
Serial communication
:registers
SBUF register (Serial buffer)
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)
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
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
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
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.
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)
Reset (1 interrupt)
when reset is activated, the 8051 jumps to address 0 (ISR address: 0H)
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
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!
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
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
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.
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.