Microprocessor Week 9: Timer and Counter

30
MICROCONTROLLER MCS- 51: TIMER / COUNTER Arkhom JODTANG Civil Aviation Training Center

Transcript of Microprocessor Week 9: Timer and Counter

Page 1: Microprocessor Week 9: Timer and Counter

MICROCONTROLLER MCS-51: TIMER / COUNTERArkhom JODTANGCivil Aviation Training Center

Page 2: Microprocessor Week 9: Timer and Counter

2

Timer / CounterContents Timer and Counter Timer Register Timer Mode Timer Mode 1 Sample: Daley Programming Sample: Pulse generation programming

Page 3: Microprocessor Week 9: Timer and Counter

3

MCS51 Timer / Counter The 8051 has two timers/counters are Timer 0 and

Timer 1 Both of them can be used either as timers to generate a

time delay or as counters to count events from external signal coming to microcontroller.

By Set interval value, enable timer to start running while monitoring overflow bit to know the completion interval.

Timer 0 Timer 1

Page 4: Microprocessor Week 9: Timer and Counter

4

Timer Register

TMOD (Timer operation Mode) register TCON (Timer Control) register THx Register (TH1 for Timer 1, TH0 for

Timer 0) TLx Register (TL1 for Timer 1, TL0 for

Timer 0)

Page 5: Microprocessor Week 9: Timer and Counter

5

Mode 1 Equivalent circuit

Modified from http://blog.circuits4you.com/2015/07/programming-timers-on-8051.html

/ 12

THx TLx

ClockGen. Machine Cycle

Page 6: Microprocessor Week 9: Timer and Counter

6

TMOD (Timer operation Mode) register

Gate: “0” : enable timer by TRx bit “1” : enable timer by TRx bit with external signal at pin INTx

C/T: Counter and Timer selector 1 for Count signal from Tx Pin (Counter) 0 for Count internal clock signal (Timer)

M1: Mode bit 1 (MSB) M0: Mode bit 0 (LSB)

TMODTimer 1 Timer 0

Gate C/T M1 M0 Gate C/T M1 M00 0 0 1 0 0 0 0

Page 7: Microprocessor Week 9: Timer and Counter

7

MCS51 Pin configuration

Page 8: Microprocessor Week 9: Timer and Counter

8

Mode of Operation

Mode 0: 13-bits THx (8-bits) and TLx (5-bits) combine as 13-

bits register Mode 1: 16-bits

THx and TLx combine as 16-bits register Mode 2: 8-bit Auto reload

THx is master value of TLx

Gate C/T M1 M0 Gate C/T M1 M00 1 1 0

Page 9: Microprocessor Week 9: Timer and Counter

9

Setting Mode OperationGate C/T M1 M0 Gate C/T M1 M0

0 1 0 1

Setting mode of operation by assign appropriate value to TMOD register

Timer 1 Mode 1 MOV TMOD, #00010000B MOV TMOD, #10H

Timer 0 Mode 1 MOV TMOD, #00000001B MOV TMOD, #01H

Page 10: Microprocessor Week 9: Timer and Counter

10

Example 9.1

TMODGate C/T M1 M0 Gate C/T M1 M0

x x x x x x x x

Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to start and stop the timer. Timer 1 in mode 2, count from external and start counting by instruction.

01100001

Page 11: Microprocessor Week 9: Timer and Counter

11

Mode 1 Equivalent circuit

Modified from http://blog.circuits4you.com/2015/07/programming-timers-on-8051.html

/ 12

THx TLx

ClockGen. Machine Cycle

Page 12: Microprocessor Week 9: Timer and Counter

12

Example 9.1

TMODTimer 1 Timer 0

Gate C/T M1 M0 Gate C/T M1 M00 0 0 0 x x x x

Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to start and stop the timer.Solution: Timer 0, So, we care only Timer 0

Nibble

Page 13: Microprocessor Week 9: Timer and Counter

13

Example 9.1

TMODTimer 1 Timer 0

Gate C/T M1 M0 Gate C/T M1 M00 0 0 0 x x 0 1

Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer.Solution: Timer 0: So, we care only Timer 0 Nibble Mode 1: M1 = 0, M0 = 1

Page 14: Microprocessor Week 9: Timer and Counter

14

Example 9.1

TMODTimer 1 Timer 0

Gate C/T M1 M0 Gate C/T M1 M00 0 0 0 x 0 0 1

Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer.Solution: Timer 0: So, we care only Timer 0 Nibble Mode 1: M1 = 0, M0 = 1 Internal Clock: That mean Timer. So, C/T = 0

Page 15: Microprocessor Week 9: Timer and Counter

15

Example 9.1

TMODTimer 1 Timer 0

Gate C/T M1 M0 Gate C/T M1 M00 0 0 0 0 0 0 1

Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer.Solution: Timer 0: So, we care only Timer 0 Nibble Mode 1: M1 = 0, M0 = 1 Internal Clock: That mean Timer. So, C/T = 0 Enable by TR0 bits: Gate = 0

Page 16: Microprocessor Week 9: Timer and Counter

16

Example 9.1

TMODTimer 1 Timer 0

Gate C/T M1 M0 Gate C/T M1 M00 0 0 0 0 0 0 1

Find the value for TMOD if we want to program Timer 0 in mode 1, count from internal clock source, and use instructions to enable the timer.Solution: Timer 0: So, we care only Timer 0 Nibble Mode 1: M1 = 0, M0 = 1 Internal Clock: That mean Timer. So, C/T = 0 Enable by TR0 bits: Gate = 0Answer: Value of TMOD is #00000001B or #01H(Use instruction MOV TMOD, #01H)

Page 17: Microprocessor Week 9: Timer and Counter

17

TCON (Timer Control register)

TF1: Timer 1 Over Flow bit This bit will set when timer is over flow

TR1: Timer 1 Enable bit Start running of Timer 1

TF0: Timer 0 Over Flow bit This bit will set when timer is over flow

TR0: Timer 0 Enable bit Start running of Timer 0

IE1, IT1, IE0, IT0 will describe in next chapter

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT00 0 0 1 0 0 0 0

Page 18: Microprocessor Week 9: Timer and Counter

18

Mode 1’s OperationTHx TLx

0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1

THx and TLx store start value Timer operation always count-up (increase) on [THx, TLX] each

machine cycle SETB TRx instruction to start running timer Over flow when Increase FFFF to 0000

Interval value = FFFF – [TH,TL] FFFF give minimum interval time 0000 give maximum interval time Example:

MOV TH0, #22H MOV TL0, #00H ; Then interval is FFFF – 2200 = DDFF Machine Cycle.

Tips: Monitor TFx with instruction “MonitorLable : JNB TFx, MonitorLable”

Page 19: Microprocessor Week 9: Timer and Counter

19

Example 9.2Calculate amount of time delay in the DELAY subroutine below generated by the timer. Assume that XTAL = 11.0592 MHz.

Delay:MOV TMOD, #01MOV TL0, #0F2HMOV TH0, #0FFHSETB TR0MonitorTX : JNB TF0, MonitorTXCLR TR0CLR TF0RET

Page 20: Microprocessor Week 9: Timer and Counter

20

Example 9.2Solution:The timer works with a clock frequency of 1/12 of the X’TAL frequency; therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the timer (Machine Cycle) frequency.As a result, each clock has a period of T = 1 / 921.6 kHz = 1.085 (is. In other words, Timer 0 counts up each 1.085 s resulting in delay = number of counts x 1.085 s.

Page 21: Microprocessor Week 9: Timer and Counter

21

Example 9.2Solution:As the timer counts up, it goes through the states of FFF3, FFF4, FFF5, FFF6, FFF7, FFF8, FFF9, FFFA, FFFB, FFFC, FFFD, FFFE, and FFFFH. One more clock rolls it to 0000, raising the timer flag (TF0 = 1). At that point, the JNB instruction falls through.Timer 0 is stopped by the instruction “CLR TRO”. The DELAY subroutine ends

Page 22: Microprocessor Week 9: Timer and Counter

22

Example 9.2Place Machine cycle to the code:

Delay:MOV TMOD, #01 2MOV TL0, #0F2H 2MOV TH0, #0FFH 2SETB TR01MonitorTX : JNB TF0, MonitorTX 14CLR TR0 1CLR TF0 1RET 2Total = 25

Page 23: Microprocessor Week 9: Timer and Counter

23

Example 9.2From total machine cycle used was 25 machine cycles.

25 x 1.058 s. = 27.125 s.So, This subroutine generate delay for 27.125 s.

Conclusion: Total Machine cycle to overflow is Machine Cycle = 1 + FFFF – Interval (TH,TL)

value

Page 24: Microprocessor Week 9: Timer and Counter

24

Workshop 9.1Calculate amount of time delay in the DELAY subroutine below generated by the timer. Assume that XTAL = 22.1184 MHz.

Delay:MOV TMOD, #01MOV TL0, #034HMOV TH0, #012HSETB TR0MonitorTX : JNB TF0, MonitorTXCLR TR0CLR TF0RET

Page 25: Microprocessor Week 9: Timer and Counter

25

Example 9.3Connect Port 3.0 with Input Switch and Port 1.0 with LED Display

Write Program for MCS51 to Turn ON LED for 2 Second after user press input switch.

Show calculate delay generate by MCS51. Assume that X’TAL = 11.0592 MHz.

Start

TMOD

End

P3.0 = 1

ON Display

Delay 2 Seconds

OFF Display

WaitSW:R3

=33D

DJNZ R3, Loop

Delay

Loop:

Page 26: Microprocessor Week 9: Timer and Counter

26

Example 9.3Connect Port 3.0 with Input Switch and Port 1.0 with LED DisplayFrom code below calculate delay generate by MCS51. Assume that X’TAL = 11.0592 MHz.

MOV TMOD, #10H ; Set Timer ModeCLR P1.0WaitSW: JB P3.0, WaitSW

SETB P1.0ACALL DelayCLR P1.0SJMP $

Delay:MOV R3,#31D

MainProcess:MOV TL1,#08HMOV TH1,#1HSETB TR1

Monitoring: JNB TF1, Monitoring CLR TR1CLR TF1DJNZ R3, MainProcessRET

END

Page 27: Microprocessor Week 9: Timer and Counter

27

Page 28: Microprocessor Week 9: Timer and Counter

28

DJNZ (Sample) ; DJNZ is very convenion for ; specified number of turn

for some process.

MOV R3,#31D Loop: ; Process here DJNZ R3, Loop END

Start

R5 =10D

End

Loop:

DJNZ R5, Loop

Process

Page 29: Microprocessor Week 9: Timer and Counter

29

Example 9.4From code below calculate frequency of signal generate by MCS51. Assume that X’TAL = 11.0592 MHz.

MOV TMOD, #10HLoop:

CPL P1.1ACALL DelayJMP Loop ; All Loop All Delay + 7 = FF04H = 65286D Machine cycle

Delay:MOV TL1,#08HMOV TH1,#1HSETB TR1

Monitoring: JNB TF1, MonitoringCLR TR1CLR TF1RET ; All Delay code FEFF

END

Page 30: Microprocessor Week 9: Timer and Counter

30

Example 9.5Write assembly program for MCS51 to generate Pulse signal with duty cycle 20% to pin P1.1

MOV TMOD, #10HLoop: SETB P1.1

ACALL DelayONCLR P1.1ACALL DelayOFFJMP Loop

DelayON: MOV TL1,#00DMOV TH1,#236D ; 256-20SETB TR1

MonitoringOn: JNB TF1, MonitoringOnCLR TR1CLR TF1RET

DelayOFF: MOV TL1,#00DMOV TH1,#176D ; 256-80SETB TR1

MonitoringOff: JNB TF1, MonitoringOffCLR TR1CLR TF1RET

END