# Counter/Timer in AVR

# What is Counter/Timer ?

The counters are basically what they sound like—they keep on count of how many times some event happens and event in term of embedded is a pin or internal source has changed its voltage level.

# What is Counter/Timer application ?

The simplest counter application is counting the how many times the button is pressed . Basic work with counter are time related project like digital clock , reminder or alarm clock .

Counters really come into their own when paired up with clocks, and this is why they’re often referred to as timer. With a clock and a counter, you can measure either how long some event takes, or the event’s frequency.

Letting your code to use the timer/counter hardware lets you do things with a precision and speed that you couldn’t performed before, or even just to do two things at once.

# What are Type of Counter/Timer and their number present ?

The timer/counter peripherals provide more functionality than you would have thought. Which is great, because there are three of them.

  1. Timer 0 (8 bit) : Can count upto 2^8 = 255
  2. Timer 1 (16 bit) : can count upto 2^16 = 65536
  3. Timer 2 (8 bit ) : can count upto 2^8 = 255

# How Does Counter/Timer work ?

# Type of the register :

  1. Control register (TCCRn) : It direct’s Counter Register to count , clear and the direction of counting (increment and decrement) .
  2. Counter register (TCNTn) : It store the counting values
  3. Compare register (OCRn) : It store the value that is compared with Counter value which is used to generate different waveform or this can be used to generate internal interrupt request (will also set the Compare Flag (OCF0) ).
  4. Timer Interrupt Mask Register (TIMSK) : shared by all counter ,This enable the interrupt for the counter whenever it reached desired value .
  5. Timer Interrupt Flag Register (TIFR) : shared by all counter , This store the whenever the counter reached desired value or whether the interrupt have been occured .

# Mode of operation :

  1. Normal Mode or Overflow mode : In this mode desired function is when the counting reached maximum value automatically or any desired value by polling.

  2. CTC Mode : In this mode desired function is when the counting reached desired value without pooling .

  3. Pulse Width Modulation Mode : This is used for generating Pulse Width Modulation . Pulse width modulation, or pulse-duration modulation, is a method of reducing the average power delivered by an electrical signal, by effectively chopping it up into discrete parts

# Step to use Counter and Timer (for only overflow mode) ?

//add demo program steps like interrupts

  1. Select Clock or Event Source :The Timer/Counter can be clocked internally, via the prescaler, or by an external clock source on the T0/T1 pin.The clock source is selected by the clock select logic which is controlled by the clock select (CSn2:0) bits located in the Timer/Counter Control Register (TCCRn).

  2. Enable interrupt in TIMSK Register if necessary or poll the TIFR register TOVn flag and perform the function whenever the counter reach user defined value

//todo write how to do frequency , count and timing relation .



  • WAP to led on for three click on switch ?
  • WAP to led on for overflow from clock on 50ms?

HINT : Make student program bottom question by setting with them ?