Sie sind auf Seite 1von 52

NATIONAL TAIWAN

OCEAN UNIVERSITY
國立台灣海洋大學

Chapter 9
COUNTER/TIMER
PROGRAMMING IN THE 8051

Microcomputers and Microprocessor


2002/4/8 s
Outlines
 List the timers of the 8051 and their associated
registers
 Describe the various modes of the 8051 timers
 Program the 8051 timers to generate time delays
 Program the 8051 counters as event counters

2002 MuDer Jeng


Timers and Counters
The 8051 comes equipped with two
timers, both of which may
be controlled, set, read, and
Configured individually.
The 8051 timers have three general
functions:
1) Keeping time and/or calculating
the amount of time between events,
2) Counting the events themselves,
or
3) Generating baud rates for the
serial port

2002 MuDer Jeng


Timers and Counters
Could use software techniques,
but this keeps the processor
occupied. Better to use interrupts
& the two 16- bit count- up
timers. Can either be
programmed to:
1. count internal - acting as timer
2. count external - acting as
counter
All counter action is controlled
by the TMOD (timer mode
register) and the TCON
(timer/counter control register).

2002 MuDer Jeng


PROGRAMMING 8051 TIMERS
Timer 0 registers
TL0 ( timer 0 low byte ) (8A H)
TH0 ( timer 0 high byte )(8C H)

2002 MuDer Jeng


Timer 1 registers
TL1 ( timer 1 low byte ) (8B H)
TH1 ( timer 1 high byte ) (8D H)

2002 MuDer Jeng


TMOD (timer mode) register

2002 MuDer Jeng


2002 MuDer Jeng
TCON Register (8 bit)
 TCON Timer control SFR contains timer
1& 2 overflow flags, external interrupt
flags, timer control bits, falling edge/ Low
level selector bit etc.

2002 MuDer Jeng


TCON- Timer control reg
TFx Timer flag bit - if set implies overflow in
timer – reset timer then clear TFx &
continue
TF1 TR1 TFO TRO IE1 IT1 IE0 IT0

TRx Timer run bit - if set – starts the timer


if 0 stops the timer – timer value
remains unchanged

2002 MuDer Jeng


I Ex when /INTx goes high to low

2002 MuDer Jeng


2002 MuDer Jeng
2002 MuDer Jeng
Mode 1 programming
1.Loaded value into TL and TH
2.”SETB TR0” for timer 0 ;”SETB TR1” for timer 1
3.If TF (timer flag) = high “CLR TR0” or “CLR TR1”
4.Reloaded TH and TL value, TF reset to 0

2002 MuDer Jeng


Steps to program in mode 1
1.Load the TMOD value
2.Load registers TL and TH
3.Start the timer (SETB TR0 or SETB TR1)
4.Keep monitoring the timer flag (TF)
5.Stop the timer (CLR TR0 or CLR TR1)
6.Clear the TF flag
7.Go back to step 2

2002 MuDer Jeng


Calculate Timer Delay

2002 MuDer Jeng


2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
Finding values to be loaded into the timer
Assuming XTAL =11.0592MHz from Example 9-10

1.Divide the desired time delay by 1.085μs


2.Perform 65536-n, where n is the decimal value we got in
Step 1
3.Convert the result of Step 2 to hex, where yyxx is the
initial hex value to be loaded into the timer’s registers
4.Set TL = xx and TH = yy

2002 MuDer Jeng


2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
Mode 0
Like mode 1 except that it is a 13-bit timer

Mode 2 Programming
1.Loaded value into TH (8-bit timer)
2.”SETB TR0” for timer 0 ;”SETB TR1” for timer 1
3.If TF (timer flag) = high “CLR TR0” or “CLR TR1”
4.Reloaded TL value kept by TH

2002 MuDer Jeng


Steps to program in mode 2
1.Load the TMOD value
2.Load the TH registers
3.Start the timer
4.Keep monitoring the timer flag (TF)
5.Clear the TF flag
7.Go back to step 4

2002 MuDer Jeng


2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
C/T bit in TMOD register

2002 MuDer Jeng


2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
2002 MuDer Jeng
The case of GATE =1 in TMOD

2002 MuDer Jeng


2002 MuDer Jeng
Part 1
Intel 8051: Serial Port Interface
 Full-duplex
 Serial port receive and transmit registers
are both accessed at Special Function Register SBUF
– Writing to SBUF loads the transmit register
– Reading from SBUF accesses a physically separated receive register
 Four modes of operation
– In all four modes transmission is initiated by
any instruction that uses SBUF as destination register
– Reception is initiated in Mode 0 by condition RI=0 and REN=1
In other modes by the incoming start bit if REN=1
 SCON - Serial Port Control Register

2002 MuDer Jeng 44/175


Part 2

Intel 8051: Serial Port Interface


7 6 5 4 3 2 1 0
SCON SM0 SM1 SM0 REN TB8 RB8 TI RI

 SM0 SM1:
– 00: Mode 0, Shift register, fosc//12
– 01: Mode 1, 8-bit UART, variable
– 10: Mode 2, 9-bit UART, fosc//32 or fosc//64
– 11: Mode 3, 9-bit UART, variable
 SM2: Enables multiprocessor features in Mode 2 and Mode 3
– When the stop bit is received,
the interrupt will be activated only if RB8=1 (9th bit =1)
 REN: Enables serial reception when set to 1 (i.e enables RXD pin ) reset to
zero to disable reception. Set/Clear by software

45/175
2002 MuDer Jeng
Part 3

Intel 8051: Serial Port Interface


7 6 5 4 3 2 1 0
SCON SM0 SM1 SM0 REN TB8 RB8 TI RI

 TB8(Transmit bit 8) 9th data bit that will be transmitted in Mode2 and Mode3
– Set/Clear by software
 RB8: 9th data bit that was received in Mode2 and Mode3
In Mode 1, if SM2=0, is the stop bit that was received
 TI: Transmit interrupt flag
– Set by hardware. Must be cleared by software
 RI: Receive interrupt flag
– Set by hardware. Must be cleared by software

46/175
2002 MuDer Jeng
Part 4
Intel 8051: Serial Port Interface
 MODE 0:
– Serial data enters and exits through RXD
– TXD outputs shift clock
– 8 bits are transmitted/received: 8 data bits (LSB first)
– The baud rate is fixed at 1/12 oscillator frequency
 MODE 1:
– Serial data enters through RXD, exits through TXD
– 10 bits are transmitted/received:
start bit(0), 8 data bits (LSB first), stop bit(1)
- On receive the stop bit TI/RI flag is raised in SCON register
The baud rate is variable

2002 MuDer Jeng 47/175


Part 5
Intel 8051: Serial Port Interface
 MODE 2:
– Serial data enters through RXD, exits through TXD
– 11 bits are transmitted/received:
start bit(0), 8 data bits (LSB first), a programmable 9th bit, stop bit(1)
– On transmit, the 9th bit is TB8 in SCON register
– On receive, the 9th bit goes into RB8 in SCON register
– The baud rate is programmable to either
1/32 or 1/64 the oscillator frequency
 MODE 3:
– Same as MODE 2 in all respects except baud rate
– The baud rate is variable

2002 MuDer Jeng 48/175


Part 6
Intel 8051: Serial Port Interface
 Mode 0 Baud Rate = Oscillator frequency/12
 Mode 2 Baud Rate =[(2SMOD)/64]*Oscillator frequency
– SMOD is bit in Special Function Register PCON
 Mode 1 and Mode3 baud rate is
determined by Timer 1 overflow rate
 Mode 1,3 Baud Rate =[(2SMOD)/32]* Timer 1 Overflow Rate
– Timer mode, auto-reload :
Timer Overflow Rate=Oscillator frequency/[12*(256-TH1)]

2002 MuDer Jeng 49/175


Part 7

Intel 8051: Serial Port Interface


Timer1
Baud
fosc SMOD
Rate Reload
C/T Mode
Value
62.5 K 12 MHz 1 0 2 FF
19.2 K 11.059 MHz 1 0 2 FD
9.6 K 11.059 MHz 0 0 2 FD
4.8 K 11.059 MHz 0 0 2 FA
2.4 K 11.059 MHz 0 0 2 F4
1.2 K 11.059 MHz 0 0 2 E8
135.5 11.059 MHz 0 0 2 1D
110 6 MHz 0 0 2 72
110 12 MHz 0 0 1 FEEB

50/175
2002 MuDer Jeng
Part 5
Programming 8051to transfer
data
TMOD is initialized with 20h (Timer 1 in mode 2)
– TH1 is loaded with the reqd baud rate for serial data transmission
– Initialise SCON register with 50h
Start the timer1 by setting TR1
– * Transfer the character byte to SBUF register
– Monitor for TI flag(JNB TI, label)
Clear TI flag & repeat step *

2002 MuDer Jeng 51/175


WAP to transfer the message “Manipalu“
serially at 9600 baud 8bit data ,1 stop bit. Do
this continuously
Org 0000h org 400h
Ljmp 30h mess : DB “Manipalu” 0
Org 30h next end
Mov TMOD,#20H % TIMER 1 IN MODE 2 send : mov subf,a
Mov TH1, #0fd h % 4800 baud here : jnb TI,here
Mov SCON, # 50 h % mode 1 ren = 1 clr TI
Setb TR1 RET
Mov DPTR, # mess
Back : clr A
Mov A,@A+DPTR
Jz next
Acall send
Inc DPTR
Sjmp back

2002 MuDer Jeng

Das könnte Ihnen auch gefallen