AVR Microcontrollers -Timers (Chapter 9 of the text book) 1 - - PowerPoint PPT Presentation

avr microcontrollers timers
SMART_READER_LITE
LIVE PREVIEW

AVR Microcontrollers -Timers (Chapter 9 of the text book) 1 - - PowerPoint PPT Presentation

Microprocessors, Lecture 5: AVR Microcontrollers -Timers (Chapter 9 of the text book) 1 Contents Timers 0 and 2 of ATmega32 Timer programming in C University of Tehran 2 Timers in AVR University of Tehran 3 Timer/Counter What


slide-1
SLIDE 1

AVR Microcontrollers

  • Timers

(Chapter 9 of the text book)

1

Microprocessors, Lecture 5:

slide-2
SLIDE 2

University of Tehran 2

Contents

  • Timers 0 and 2 of ATmega32
  • Timer programming in C
slide-3
SLIDE 3

University of Tehran 3

Timers in AVR

slide-4
SLIDE 4

University of Tehran 4

Timer/Counter

  • What is a timer?

– To count an event – To generate delay

slide-5
SLIDE 5

University of Tehran 5

Timers in AVR

  • ATmega32: 3 timers

– Timer0 (8-bit) – Timer1 (16-bit) – Timer2 (8-bit)

slide-6
SLIDE 6

University of Tehran 6

Timers in AVR

  • Basic registers:

– TCNTx (x=0,1,2)= timer/counter register – Keeps the timer/counter value – On reset, contains 0 – Counts up with each pulse

slide-7
SLIDE 7

University of Tehran 7

Timers in AVR

  • Basic registers:

– TOVx(x=0,1,2)= timer/counter overflow flag – TOVx Becomes 1 when TCNTx overflows

» switches from 0xFF to 0x00

– Should be reset by software

slide-8
SLIDE 8

University of Tehran 8

Timers in AVR

  • Basic registers:

– OCRx (x=1,2,3)= output compare register – Another way to count – The contents of OCRx are compared to TCNTx

» OCFx is set if they are equal

slide-9
SLIDE 9

University of Tehran 9

Timers in AVR

slide-10
SLIDE 10

University of Tehran 10

Timers in AVR

  • Basic registers:

– TCCRx (x=1,2,3)= timer/counter control register – Setting modes of operation

slide-11
SLIDE 11

University of Tehran 11

TCCR0 in AVR

slide-12
SLIDE 12

University of Tehran 12

TCCR0 in AVR

slide-13
SLIDE 13

University of Tehran 13

TCCR0 in AVR

slide-14
SLIDE 14

University of Tehran 14

Timers in AVR

  • TIFR (timer/counter interrupt flag register)
  • To keep the state of the counters
  • One register for all counter/timers
slide-15
SLIDE 15

University of Tehran 15

TIFR

slide-16
SLIDE 16

University of Tehran 16

Timer0 in normal mode

  • Set TCNT0 with proper value
  • Set TCCR0: which clock source? Which

prescalar? – When is set, the timer starts

  • Keep monitoring TOV0
  • Stop timer Set TCCR0
  • Clear TOV0
slide-17
SLIDE 17

University of Tehran 17

Timers in AVR

slide-18
SLIDE 18

University of Tehran 18

Timers in AVR

slide-19
SLIDE 19

University of Tehran 19

Timers in AVR

TCCR0=0x01 //normal mode, no prescaling TCNT0=0xCE

slide-20
SLIDE 20

University of Tehran 20

Timers in AVR

slide-21
SLIDE 21

University of Tehran 21

Timers in AVR-CTC mode

  • Compare mode (clear timer o compare)
  • Another way to count
  • 1. Increment TCNT at each clock cycle
  • 2. OCFn=1 when OCRn=TCNTn
slide-22
SLIDE 22

University of Tehran 22

Timer 2 in ATmega32

  • Just like timer 0, but no external clock

– Timer only

  • TCCR2:
slide-23
SLIDE 23

University of Tehran 23

Timer programming in C

  • We can use the register names in C codes:

– TCNT0, TCNT1, TCNT2 – TIFR0,… – TCCR0,… – ….

slide-24
SLIDE 24

University of Tehran 24

Timer programming in C

slide-25
SLIDE 25

University of Tehran 25

Timer programming in C

slide-26
SLIDE 26

University of Tehran 26

Timer 1

  • 16-bit counter/timer
  • TCNT1L and TCNT1H
  • 2 8-bit registers to control

timer 1 – TCCR1L and TCCR1H

  • 2 registers in compare

mode – OCR1A and OCR1B

slide-27
SLIDE 27

University of Tehran 27

Timer 1

TCNT1 3 flags in TIFR: TOV1 and OCF1A- OCF1B

slide-28
SLIDE 28

University of Tehran 28

Timer 1 control registers

  • 2 registers
  • Plenty of operation modes

TCCR1A TCCR1B

slide-29
SLIDE 29

University of Tehran 29

Timer 1 control registers

TCCR1A TCCR1B

In this course, we focus on modes 0 and 4

slide-30
SLIDE 30

University of Tehran 30

Timer 1 modes

  • 16 modes, we use 2 modes in this chapter:
  • Normal mode:
  • CTC mode
slide-31
SLIDE 31

University of Tehran 31

Timer 1 control registers

TCCR1A TCCR1B

slide-32
SLIDE 32

University of Tehran 32

Timer 1 control registers

TCCR1A TCCR1B

slide-33
SLIDE 33

University of Tehran 33

Timer 1 programming

slide-34
SLIDE 34

University of Tehran 34

Timer 1 programming

slide-35
SLIDE 35

University of Tehran 35

Timer 1 programming

slide-36
SLIDE 36

University of Tehran 36

Timer 1 programming

slide-37
SLIDE 37

University of Tehran 37

Accessing 16-bit registers in AVR

  • TCNT1=0x05ff, we want to save the content of

TCNT1 in R20 and R21

  • Cannot read TCNT in one cycle

– AVR is a 8-bit machine

  • Read TCNT1L (0xff) at t0, at the same cycle occurs

TCNT=0x0600

  • Read TCNT1H (0x06)
  • The content is detected as 0x06ff instead of the correct

value 0x05ff

slide-38
SLIDE 38

University of Tehran 38

Accessing 16-bit registers in AVR

  • Solution:

– AVR buffers the high byte when the lower byte is read – When the higher byte is read, the buffered value is used

  • first read the lowest byte and then the

higher byte

slide-39
SLIDE 39

University of Tehran 39

Counters in AVR

  • To count external events
slide-40
SLIDE 40

University of Tehran 40

Counter programming in AVR

slide-41
SLIDE 41

University of Tehran 41

Counter programming in AVR

  • Configure T0 (PB0) or T1 (PB1) as input
  • Set the other registers as in timers
slide-42
SLIDE 42

University of Tehran 42

Counter