Docsity

Atmega32 Timers: A Comprehensive Guide to Counter and Timer Operations, Slides of Microprocessor and Assembly Language Programming

Partial preview of the text

Download Atmega32 Timers: A Comprehensive Guide to Counter and Timer Operations and more Slides Microprocessor and Assembly Language Programming in PDF only on Docsity!

Timers and Counter

Lab 04

Counter

  • (^) Counter is a device that counts the pulses at its input which can occur at regular or irregular intervals

Atmega32 Timers

  • (^) Atmega32 has three timers
  • (^) Timer 0 and Timer 2 are 8-bit timers whereas Timer 1 is the 16-bit timer.
  • (^) A timer can be used in two modes i.e. Timer and Counter
  • (^) If we use internal clock source, then the frequency of the oscillator is fed to the timer
  • (^) Timer can be used to generate the time delay
  • (^) If we use the external clock option, we feed pulses through one of the I/O pins. In this configuration, timer can be used as event counter

Timer Registers

  • (^) TCNTn: Timer/Counter Reg: Upon reset, it has zero value and counts up with each timer clock
  • (^) TCCRn: Timer/Counter Control Reg: For setting modes of operation of Timer n
  • (^) OCRn: Output Compare Reg: When contents of TCNT are equal to OCR, OCF flag is raised and value of TCNTn is reset to zero
  • (^) TOVn: Timer Overflow Flag: When overflow occurs, this flag is raised
  • (^) OCFn: Output Compare Flag: Discussed in OCRn

CTC (Clear Timer on Capture

match) Mode

  • (^) In CTC mode the counter is cleared to zero when the counter value (TCNT0) matches the OCR0. The OCR0 defines the top value for the counter.

Phase Correct & Fast PWM Mode

TIFR (Timer/Counter Interrupt Flag) Register

Timer/Counter Interrupt Mask

Register(TIMSK)

Steps to Program 8-Bit, Timer 0 in Normal Mode

  • (^) Load TCNT0 register with initial values from which the timer will count up.
  • (^) Load TCCR0 register according to the operation required (Mode selection, prescaler setting etc.)
  • (^) Keep monitoring TOV0 flag in TIFR register. If TOV0 flag is raised, timer overflow occurs. Now clear TOV0 flag and proceed to step 1 again.

Delay Calculation of Timers

For a clock generation of 5Hz (200ms), timer should be overflowed twice, so: Timer overflow @ = 200ms / 2 = 100ms (0.1s high, 0.1s low) Crystal Clock = 1MHz Prescaler used = 1024 Timer clock = 1MHz / 1024 = 976.5625 Hz Timer Period = 1/976.5625 = 1024us Timer Value = 0.1s / 1024us = 97.65625 = 98 (approx)

ISR Names for Timer Interrupt

  • (^) ISR(TIMER0_OVF_vect) //use when Timer is in Normal Mode
  • (^) ISR(TIMER0_COMP_vect) //use when Timer is in CTC Mode