Вы находитесь на странице: 1из 22

Special Function Register Map

Bit Addressable

F8
F0 B
E8
E0 ACC
D8
D0 PSW
C8
C0
B8 IP
B0 P3
A8 IE
A0 P2
98 SCON SBUF
90 P1
88 TCON TMOD TL0 TL1 TH0 TH1
80 P0 SP DPH DPL PCON

8051 MICROCONTROLLER TIMERS 0 & 1 1


Special Function Registers
Interrupt control:
IE : Interrupt Enable.
IP : Interrupt Priority.

TImers:
TMOD : Timer mode.
TCON : Timer control.
TH0 : Timer 0 high byte.
TL0 : Timer 0 low byte.
TH1 : Timer 1 high byte.
TL1 : Timer 1 low byte.

8051 MICROCONTROLLER TIMERS 0 & 1 2


Counter / Timers

Two 16-bit Counter/Timers: TIMER0, TIMER1


Up counters, can interrupt on overflow.

Counts:
- CPU cycles (crystal/12). “Timer”.
- External input (max. half CPU rate). “Counter”.

Four Operation Modes.

8051 MICROCONTROLLER TIMERS 0 & 1 3


Timer Modes
- Timer Mode 0 :
Emulates 8048 counter/timer (13-bits).
8-bit counter (TL0 or TL1).
5-bit prescaler (TH0 or TH1).

- Timer Mode 1 :
Simple 16-bit counter.

- Timer Mode 2 :
8-bit auto-reload.
Counter in TL0 or TL1.
Reload value in TH0 or TH1.
Provides a periodic flag or interrupt.

8051 MICROCONTROLLER TIMERS 0 & 1 4


Timer Modes (cont'd)
- Timer Mode 3 :
Splits timer 0 into two 8-bit counter/timers.
First counter (TLO) acts like mode 0,
without prescaler.
Second counter (TH0):
Counts CPU cycles.
Uses TR1 (timer 1 run bit) as enable.
Uses TF1 (timer 1 overflow bit) as flag.
Uses Timer 1 interrupt.

Timer 1 (when timer 0 is in mode 3 ):


Counter stopped if in mode 3.
Running in mode 0, 1, or 2.
Has gate (INT1) and external input (T1),
but no flag or interrupt.
May be used as a baud rate generator.

8051 MICROCONTROLLER TIMERS 0 & 1 5


Counter/Timer in 16-bit (Mode 1)

Osc. ÷12

Interrupt
TL1 TH1 TF1
8-bits 8-bits
T1 (Pin)
Control
TR1

Gate

INT1 (Pin)

The Gate input controls whether the Counter runs while gated by the interrupt signal or not.

8051 MICROCONTROLLER TIMERS 0 & 1 6


TMOD : Counter/Timer Mode Register

GATE C/T M1 M0 GATE C/T M1 M0

Timer 1 Timer 0

- GATE : Permits INTx pin to enable/disable counter.


- C/T : Set for counter operation, reset for timer operation.
- M1, M0 :
00 : Emulate 8048 counter/timer (13-bits).
01 :16-bit counter/timer.
10 : 8-bit auto-reload mode
11 :Timer 0 = two 8-bit timers.
Timer 1 = Counting disabled. Timing function allowed.
(Can be used as Baud Rate generator).

8051 MICROCONTROLLER TIMERS 0 & 1 7


TCON : Counter/Timer Control Register

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

- TF1, TF0 : Overflow flags for Timer 1 and Timer 0.

- TR1, TR0 : Run control bits for Timer 1 and Timer 0. Set to run, reset to hold.

- IE1, IE0 : Edge flag for external interrupts 1 and 0. *


Set by interrupt edge, cleared when interrupt is processed.

- IT1, IT0 : Type bit for external interrupts. *


Set for falling edge interrupts, reset for 0 level interrupts.

* = not related to counter/timer operation.

8051 MICROCONTROLLER TIMERS 0 & 1 8


Interrupt System

- 5 Interrupt Sources (in order of priority):


 External Interrupt 0.
 Timer 0.
 External Interrupt 1.
 Timer 1.
 Serial Port.

- Each interrupt can be enabled separately.

- Each interrupt type has a separate vector address.

- Each interrupt type can be programmed to one of two priority levels.

- External interrupts can be programmed for edge or level sensitivity.

8051 MICROCONTROLLER TIMERS 0 & 1 9


IE : Interrupt Enable Register

EA ---- ---- ES ET1 EX1 ET0 EX0

- EA : Global interrupt enable.


- ES : Serial interface.
- ET1 : Timer 1.
- EX1 : External interrupt 1.
- ET0 : Timer 0.
- EX0 : External interrupt 0.

- 0 = Disabled.
- 1 = Enabled.

8051 MICROCONTROLLER TIMERS 0 & 1 10


Interrupt Vector Addresses

Source Address
IE0 03H
TF0 0BH
IE1 13H
TF1 1BH
RI&TI 23H

The 8051 starts execution at 0000H after Reset.

8051 MICROCONTROLLER TIMERS 0 & 1 11


IP: Interrupt Priority Register

----- ----- ----- PS PT1 PX1 PT0 PX0

- PS : Serial interface.
- PT1 : Timer 1.
- PX1 : External interrupt 1.
- PT0 : Timer 0.
- PX0 : External interrupt 0.

- 0 = Low priority.
- 1 = High priority.

8051 MICROCONTROLLER TIMERS 0 & 1 12


80C51 Coding:
Ideas and Examples

8051 MICROCONTROLLER TIMERS 0 & 1 13


(1) Reading a Timer "on the Fly"

ReadTimer: MOV ValH,TH0 ;Read initial timer high and low values.
MOV ValL,TL0
MOV A,TH0 ;Read timer high byte again.
CJNE A,ValH,ChkHigh ;Has it changed?
SJMP RTEX ;If not, first sample is OK.

ChkHigh: JB ValL.7,RTEX ;Otherwise, check low byte to see if it


; changed after the original high byte
; sample.
MOV ValH,A ;If it did change, use second high byte
; sample.
RTEX: RET

8051 MICROCONTROLLER TIMERS 0 & 1 14


(2) Pulse Width Measurement

Problem: measure the width of an


input pulse.

start timer stop timer

Assumption: use external interrupt 0 for Note: to measure pulse low time in this
the pulse input. Use timer 0 in gated manner, the input must be inverted
mode. externally.

8051 MICROCONTROLLER TIMERS 0 & 1 15


(2) Pulse Width Measurement

Setup: MOV TMOD,#09h ;Timer 0 gate on, in mode 1.


MOV TCON,#01h ;Set INT0 to edge triggered mode.
MOV TH0,#0 ;Clear timer 0 for measurement.
MOV TL0,#0
SETB TR0 ;Start timer in gated mode.
SETB EX0 ;Enable external interrupt 0.
SETB EA ;Enable global interrupts.
. .
. .
. .

;External interrupt 0 service routine.


ExInt0: CLR TR0 ;Stop timer.
MOV ValH,TH0 ;Save timer value.
MOV ValL,TL0
MOV TH0,#0 ;Clear timer 0 for measurement.
MOV TL0,#0
SETB TR0 ;Restart timer.
RETI

8051 MICROCONTROLLER TIMERS 0 & 1 16


(3) Pulse Period Measurement

Problem: measure the period of


an input pulse.

start timer stop timer

Assumption: use external interrupt 0 for Note: this method may entail some loss of
the pulse input. precision due to the possibility of
variable interrupt latency.

8051 MICROCONTROLLER TIMERS 0 & 1 17


(3) Pulse Period Measurement

Setup: (same as previous example, but leave timer gate function turned off)
MOV TMOD,#01h ;Timer 0 in mode 1.

;External interrupt 0 service routine.


ExInt0: CPL TR0 ;Complement the timer run flag. This starts
; and stops the timer on alternate interrupts.
JB TR0,INT0EX ;Exit if timer is running.
MOV ValH,TH0 ;Otherwise sample the timer value.
MOV ValL,TL0
MOV TH0,#0 ;Clear timer so another sample can
MOV TL0,#0 ; be taken.
INT0EX: RETI

8051 MICROCONTROLLER TIMERS 0 & 1 18


(4) Creating an Output Pulse

Problem: create a pulse of known


duration on a port pin.

timer

start pulse, stop pulse


timer

Assumption: use any spare port bit for Note: the precision of pulses generated using
the output. this method will vary depending on the
interrupt latency of the timer interrupt.

8051 MICROCONTROLLER TIMERS 0 & 1 19


(4) Creating an Output Pulse

Setup: MOV TCON,#0h ;Make sure timer is stopped.


MOV TMOD,#01h ;Set timer to mode 1.
MOV TH0,#HiTime ;Load timer with pulse duration. The value is the
MOV TL0,#LoTime ; two's complement of the
number of machine ; cycles to use for the pulse
width.
SETB TR0 ;Start timer.
SETB P2.0 ;Start pulse (use CLR for a low going pulse).

;Tiimer 0 interrupt routine.


T0INT: CLR P2.0 ;End of pulse (use SETB for a low going pulse).
CLR TR0 ;Stop timer.
RETI

8051 MICROCONTROLLER TIMERS 0 & 1 20


(5) Programming a PWM Output

Problem: create a PWM output


on a port pin.

set timer set timer repeat


low time high time

Note: the precision of pulses generated using


this method will vary depending on the
interrupt latency of the timer interrupt.

8051 MICROCONTROLLER TIMERS 0 & 1 21


(5) Programming a PWM Output

T0INT: CLR TR0 ;Stop timer.


CPL P2.0 ;Toggle output bit.
JB P2.0,SetPWMHigh ;Is current phase high or low?
MOV TH0,PWMLowH ;Set PWM low time.
MOV TL0,PWMLowL
SJMP T0EX

SetPWMHigh: MOV TH0,PWMHighH ;Set PWM high time.


MOV TL0,PWMHighL

T0EX: SETB TR0 ;Restart timer.


RETI

Note: for higher frequency pulses, it may be possible to use the


timer reload feature (mode 2) to obtain more accurate pulse
durations.

8051 MICROCONTROLLER TIMERS 0 & 1 22

Вам также может понравиться