52
8051 Timers / Counters GROUP 4 DEEC MPIN FEUP April, 2010 April 10 José Borges ,Patrício Lima, Marcos Brito, Marek Mastyło, Jakub Nyk

8051 Timers / Counters

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 8051 Timers / Counters

8051 Timers / Counters

GROUP 4

DEEC –MPIN

FEUP

April, 2010

April 10

José Borges ,Patrício Lima, Marcos Brito, Marek Mastyło, Jakub Nyk

Page 2: 8051 Timers / Counters

Outlines

April 10 2

1. Introduction

2. 8051 Timer/Counter

3. Operating Modes

4. Timer Vs Counter

5. T/C and Interruptions

6. Application Examples

7. Conclusion

Page 3: 8051 Timers / Counters

Introduction

April 10 3

General Function:

• Calculating the amounts of time between

events

• Counting events

• Generating baud rate for serial port

Page 4: 8051 Timers / Counters

Introduction

April 10 4

Applications:

• Communication Generating rectangular pulses (signal

modulation)

Watchdog timers

• Manufacturing Industry- Counting objects

- Measuring intervals

• Etc

Page 5: 8051 Timers / Counters

Introduction

April 10 5

8051 contains two 16-bits timers• T0

• T1

Two different types of timer:

• Interval timer

• Counter

Maximum value is 65536

Initial state can be set by user

Page 6: 8051 Timers / Counters

Timer/Counter

April 10 6

Special Function Registers

Timers/Counters can be operated by user with

special function registers

T0 and T1 share two SFRs: TMOD and TCON

Each timer has also two registers dedicated to

itself: TH0/TL0 and TH1/TL1

Page 7: 8051 Timers / Counters

Timer/Counter

April 10 7

Special Function Registers-TMOD

TMOD (Timer Mode Register) is a non-bit-

addressable, 8-bit register:

Reference:

http://fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/timer2.pdf

Page 8: 8051 Timers / Counters

Timer/Counter

April 10 8

Special Function Registers-TMOD

Lower 4 bits are for Timer0

Upper 4 bits are for Timer1

GATE bit is used for choice of internal or external control:• GATE=0 is for internal control, start and stop are

controlled by software

• GATE=1 is for external control, start and stop are controlled by software and and external source

C/T bit decides about timer type: interval timer or counter

Page 9: 8051 Timers / Counters

Timer/Counter

April 10 9

Special Function Registers-TMOD

M0 and M1 bits are used to set timer

mode (the same for Timer0 and Timer1)

8051 delivers 4 timer modes:

M1 M0 Mode Description

0 0 Mode 0 13-bit timer

0 1 Mode 1 16-bit timer

1 0 Mode 2 8-bit auto reload

1 1 Mode 3 Split timer mode

Page 10: 8051 Timers / Counters

Timer/Counter

April 10 10

Special Function Registers-TCON

TCON (Timer Control Register) is a bit-

addressable, 8-bit register where 4 upper bits

are responsible for timers/counters:

Reference:

http://fivedots.coe.psu.ac.th/~cj/asm/slides/mcs51/timer2.pdf

Page 11: 8051 Timers / Counters

Timer/Counter

April 10 11

Special Function Registers-TCON

TR0 and TR1 are set by user to turn on (or turn

off) Timer0 or Timer1:

• TR=0 – turn off

• TR=1 – turn on

TF0 and TF1 are Timer Flags informing about

overflow (then TF=1 and interrupt could be

activate if it’s set, should be cleaned)

Page 12: 8051 Timers / Counters

Timer/Counter

April 10 12

Special Function Registers-TCON

Equivalent instructions for TCON:

• Timer0:- SETB TR0 = SETB TCON.4

- CLR TR0 = CLR TCON.4

- SETB TF0 = SETB TCON.5

- CLR TF0 = CLR TCON.5

• Timer1- SETB TR1 = SETB TCON.6

- CLR TR1 = CLR TCON.6

- SETB TF1 = SETB TCON.7

- CLR TF1 = CLR TCON.7

Page 13: 8051 Timers / Counters

Timer/Counter

April 10 13

Special Function Registers-TL/TH

TH0 and TL0 are upper and lower registers of Timer0

TH1 and TL1 are upper and lower registers of Timer1

They help to set initial value of timer/counter

Page 14: 8051 Timers / Counters

Timer Vs Counter

April 10 14

Differences

Timer

• Counts machine cycles

Counters:

• Counts events as a result of falling slope of external

input signal put on a pin

Timer mode and counter mode are relative to

machine cycle

Page 15: 8051 Timers / Counters

Timer Vs Counter

April 10 15

Differences

Timer • Input from internal system clock

Counters:• Show the number of events on registers

• External input from T0 input pin (P3.4) for Counter 0

• External input from T1 input pin (P3.5) for Counter 1

• External input from Tx input pin.

• We use Tx to denote T0 or T1

Page 16: 8051 Timers / Counters

Timer Modes

April 10 16

Mode 0

Mode 0 is identical for Timer0 and Timer1

Timers work as 13-bit counters, an interrupt is generated when counter overflows. It takes 8192 input pulses to generate the next interrupt

Timers use 8 bits of THi and 5 lower bits of TLi

After timer overflows TFi (Timer Flag in TCON) is set, an interrupt occurs

Where i=0,1

Page 17: 8051 Timers / Counters

Timer Modes

April 10 17

Mode 0

Structure of Timer1 in mode 0:

Reference:

Tomasz Starecki; Mikrokontrolery 8051 w praktyce, Twarda

oprawa, 2002. ISBN: 83-910067-4-3, Pic. 3.1

Page 18: 8051 Timers / Counters

Timer Modes

April 10 18

Mode 1

Similar to mode 0

Timers use 8 bits of THi and 8 bits of TLi

Timer is a 16-bit counter, it takes 65536 input

pulses to generate the next interrupt

Improved capacity

Page 19: 8051 Timers / Counters

Timer Modes

April 10 19

Mode 1

Structure of Timer0 in mode 1:

Reference:

Tomasz Starecki; Mikrokontrolery 8051 w praktyce, Twarda

oprawa, 2002. ISBN: 83-910067-4-3, Pic. 3.1

Page 20: 8051 Timers / Counters

Timer Modes

April 10 20

Mode 2

Timers are 8-bit auto reload

Timer is operated by TLi, when TLi overflows

TFi is set

TLi is auto reloaded with Thi value when

overflows

THi is never modified when TLi overflows

Page 21: 8051 Timers / Counters

Timer Modes

April 10 21

Mode 2

Structure of Timer1 in mode 2:

Reference:

Tomasz Starecki; Mikrokontrolery 8051 w praktyce, Twarda

oprawa, 2002. ISBN: 83-910067-4-3, Pic. 3.1

Page 22: 8051 Timers / Counters

Timer Modes

April 10 22

Mode 3

Split-timer mode

Timer1 can be put in other modes

Timer0 operates TL0 and TH0 as two separate 8-bit

timers/counters

TL0 works as a 8-bit timer/counter

TH0 is a 8-bit timer which counts machine cycles

Timer0 is operated with TF0 and TR0, TF1 and TR1

are not used

Page 23: 8051 Timers / Counters

Timer Modes

April 10 23

Mode 3

Structure of Timer1 in mode 3:

Reference:

Tomasz Starecki; Mikrokontrolery 8051 w praktyce, Twarda

oprawa, 2002. ISBN: 83-910067-4-3, Pic. 3.1

Page 24: 8051 Timers / Counters

Timer Modes

April 10 24

Example

Choose mode 1 for Timer:

MOV TMOD,#01H

Set the value of TH0 and TL0:

MOV TH0,#FFH

MOV TL0,#FCH

Clear Timer flag and start the timer:

CLR TF0

SETB TR0

Page 25: 8051 Timers / Counters

Timer Modes

April 10 25

Example

The 8051 starts to count up by incrementing the TH0-TL0

TH0-TL0= FFFCH,FFFDH,FFFEH,FFFFH,0000H

Reference:

http://www.iau-neyshabur.ac.ir/nokhodchian/5-timer(part%201).ppt

Page 26: 8051 Timers / Counters

How does a timer count?

April 10 26

Timer/Counter counts up

It is incremented by microcontroller

• Timer is incremented every machine cycle

• Coutner is incremented when event is detected

Page 27: 8051 Timers / Counters

How does a timer count?

April 10 27

A single machine cycle consists of 12

crystal pulses, thus timer will count:

11 059 000 / 12 = 921 583 per second

Page 28: 8051 Timers / Counters

How does a timer count?

April 10 28

Example: How many times will the timer be

incremented in 0.05 seconds?

0.05 * 921 583 = 46 079.15 times

Accuracy is not perfect

Page 29: 8051 Timers / Counters

How does a timer count?

April 10 29

Timer/Counter counts up

It is incremented by microcontroller

• Timer is incremented every machine cycle

• Coutner is incremented when event is detected

Page 30: 8051 Timers / Counters

How does a timer count?

April 10 30

A single machine cycle consists of 12

crystal pulses, thus timer will count:

11 059 000 / 12 = 921 583 per second

Page 31: 8051 Timers / Counters

How does a timer count?

April 10 31

Example: How many times will the timer be

incremented in 0.05 seconds?

0.05 * 921 583 = 46 079.15 times

Accuracy is not perfect

Page 32: 8051 Timers / Counters

April 10 32

Timer/ Counter

and Interruptions

Timer/ Counter can be configured to start an

interruption routine

Reference:

Philips Semiconductors – Family 8051

Page 33: 8051 Timers / Counters

How a timer

interruption occurs

Some Special Registers are used

• ET0 and ET1 from Interruption Enable

Register

• PT0 and T1 from Interruption Priority

Register

• TF0 and TF1 from T/C Control Register

April 10 33April 10 33

Page 34: 8051 Timers / Counters

How a timer

interruption occurs

Timer/counter interruption process :

• ET bit must be set

• TR bit must be set to run the timer

• The interruption is initialized when overflow

occurs

April 10 34April 10 34

Page 35: 8051 Timers / Counters

How a timer

interruption occurs

In this point the Timer overflow Flag is cleared

by hardware

Reference:

Philips Semiconductors – Family 8051

April 10 35

Page 36: 8051 Timers / Counters

Software overflow

detection

How the overflow can be detected if

interruption is not enable?

• In some cases ,it’s not necessary to enable an

interruption

• In this case the overflow is detected by software

• A routine is necessary to check constantly the

overflow occurrence

April 10 36

Page 37: 8051 Timers / Counters

Software overflow

detection

• The overflow occurs when the TF is high

• TF must be cleared to by software to turn ON

the timer

April 10 37

Page 38: 8051 Timers / Counters

Software overflow

detection

Delay routine example without enable

interruption:

cseg at 0000h

jmp main

main:

setb tr0

acall delay50ms

delay50ms:

mov TL0,#low(46080)

mov TH0,#high(46080)

clr TF0

jnb TF0,$

ret

April 10 38

Page 39: 8051 Timers / Counters

Application Example

April 10 39

Digital Clock

Circuit schematic

• Interface with LCD

display and serial port.

Page 40: 8051 Timers / Counters

Application Example

April 10 40

Digital Clock

Interruption configuration

• IEmov IE,#10010010b

Or

setb EA

setb ES

setb ET0

Page 41: 8051 Timers / Counters

Application Example

April 10 41

Digital Clock

Interruption configuration

• TCONorl TCON,#00101000b

Or

setb TR0

setb TR1

Page 42: 8051 Timers / Counters

Application Example

April 10 42

Digital Clock

Interruption configuration

• TMODmov TMOD,#00100001b

Not bit adressable

Page 43: 8051 Timers / Counters

Application Example

April 10 43

Digital Clock

Interruption configuration

• SCONmov SCON,#01110000b

Or

setb SM1

setb SM2

setb REN

Page 44: 8051 Timers / Counters

Application Example

April 10 44

Digital Clock

Interruption configuration

• Serial port baud rate

• TH1 = TL1=253mov TH1,#0xFD

mov TH1, TL1

𝑏𝑎𝑢𝑑 𝑟𝑎𝑡𝑒 = 2 ∗11.0592𝑀𝐻𝑧

32 ∗ 12 ∗ 256 − 253

𝑏𝑎𝑢𝑑 𝑟𝑎𝑡𝑒 = 19200

Page 45: 8051 Timers / Counters

Application Example

April 10 45

Digital Clock

Interruption configuration

Problem :

• 8051 only count up to 65536

us

How we count 1 second ?

Page 46: 8051 Timers / Counters

Application Example

April 10 46

Digital Clock

Interruption configuration

Solution :

• Count 20*50ms

TIMER equ 65536-46080

mov TL0,#high(TIMER)

mov TH0,#low(TIMER)

50000𝑢𝑠 =12

11.0592𝑀ℎ𝑧∗ 𝐶𝑖𝑐𝑙𝑒𝑠

𝑐𝑖𝑐𝑙𝑒𝑠 = 46080

Page 47: 8051 Timers / Counters

Application Example

April 10 47

Digital Clock

Interruption configuration

mov IE,#10010010b

mov TMOD,#00100001b

orl TCON,#00101000b

mov SCON,#01110000b

mov TH1,#0xFD

mov TH1, TL1

mov TL0,#high(TIMER)

mov TH0,#low(TIMER)

Page 48: 8051 Timers / Counters

Application Example

April 10 48

Digital Clock

mov IE,#10010010b

mov TMOD,#00100001b

orl TCON,#00101000b

mov SCON,#01110000b

mov TH1,#0xFD

mov TH1, TL1

mov TL0,#high(TIMER)

mov TH0,#low(TIMER)

GND

VDD

XTAL218

XTAL119

RST9

P3.0/RXD10

P3.1/TXD11

P3.2/INT012

P3.3/INT113

P3.4/T014

P3.7/RD17

P3.6/WR16

P3.5/T115

AD[0..7]

A[8..15]

ALE30

EA31

PSEN29

P1.01

P1.12

P1.23

P1.34

P1.45

P1.56

P1.67

P1.78

U1

8051

D7

14

D6

13

D5

12

D4

11

D3

10

D2

9D

18

D0

7

E6

RW

5R

S4

VS

S1

VD

D2

VE

E3

DIGITAL CLOCK

X1

11.0592MHz

C133p

C233p

RXD

RTS

TXD

CTS

Page 49: 8051 Timers / Counters

April 10 49

Application Example

Counter w/ Ext. Enabler

01

11

1

+1

00

0

Page 50: 8051 Timers / Counters

April 10 50

Application Example

Counter w/ Ext. EnablerLeds equ P1

overflow equ R0

cseg at 00h

jmp main

cseg at 1bh

jmp Timer1 ;Counter/ Timer 1 Int.

cseg at 40h

Timer1:

inc overflow ;Do a Task

reti

main:

mov IE,#88H ;Enables the Timer1

mov TMOD,#0E0h ;Gate =1, Counter selected, Mode 2

mov TH1,#00h ;With 00h it counts 255 times

mov TL1,#00h

setb TR1 ;Begin to count

Led:

mov Leds,TL1 ; shows the value with Leds of the actual count

jmp Led

end

Initialization

Leds = TL1

Counter

Interrupt

TF1

Do a Task

Return

Page 51: 8051 Timers / Counters

Application Example

April 10 51

Digital Clock

Reference:• http://www.8052.com/tuttimer.phtml

• http://www.8052.com/tutlcd2.php

• http://www.8051projects.net/lcd-

interfacing/introduction.php

• ISIS Proteus -

http://www.labcenter.co.uk/download/prodemo_d

ownload.cfm#professional

• Philips Semiconductors - 80C51 Family

Page 52: 8051 Timers / Counters

April 10 52

Thank you!