Arduino Timer Tutorial

        

    Hello Friends, we are here to discuss one of  Arduino  hardware part , that's call  Timer. As per just name you get some idea what  is timer. For your information not only Arduino or AVR  microcontrollers but every microcontroller family have timer in built.   It is like a clock or counter and can be used to measure time events. The timer can be programmed by some special registers. You can configure the prescaler for the timer, or the mode of operation and many other things.
 
        The controller of the Arduino is the Atmel AVR  ATmega328.It has 3 timers, called timer0, timer1 and timer2. Timer0 and timer2 are 8bit timers, where timer1 is a 16bit timer. The most important difference between 8bit and 16bit timer is the timer resolution. 8bits means 256 values where 16bit means 65536 values for higher resolution or longer count. The timer hardware can be configured with some special timer registers. In the Arduino firmware all timers were configured to a 1kHz frequency and interrupts are generally enabled.


Many Arduino functions uses timer s0, for example the time functions:  delay(), millis()  and micros()  are use timer 0.
The PWM functions analogWrite() uses timers2.
Timer1 use for Servo based.


Arduino Timer Interrupts

 Interrupts are used to handle events that do not happen during the sequential execution of a program. There are two types of interrupts for the Arduino microcontroller. Hardware / external interrupt and Timer interrupt. We will focus on Timer interrupts of Arduino. Depending on the timer mode which is selected the timer will start by increasing its value until it reaches its maximum count then go back to 0. This creates a triangle shaped curve which the timer follows. This will cause the interrupt to occur as the value increases from 0-255 in the case of Timer2 and then goes back to 0 and repeats again.


 Timer interrupts in Arduino pause the sequential execution of a program loop() function for a predefined number of seconds (timed intervals) to execute a different set of commands. After the set commands are executed, the program resumes again from the same position. The Arduino comes with three timers known as Timer0 (8-bit timer), Timer1 (16-bit timer), and Timer2 (8-bit timer). They act as a clock and are used to keep track of time based events. These timers will be programmed using registers which we will learn about.

 There are following Timer pinout in Board 

                


Our user guide will focus on learning how to generate Timer1 and Timer2 interrupts of Arduino.


  •  Timer1 can generate Compare Match, Overflow and Input Capture interruptions. In compare match, the interrupt is triggered when the timer value in the register is equal to the compare value. If we set the compare value equal to 50 then whenever the Timer1 will reach this value an interrupt will be called

  •   The next type of interrupt is overflow. This is triggered whenever the timer value reaches its maximum value. 

  • The last type of interrupt is input capture. Whenever an external trigger occurs, the value gets saved in a different register. 
On the other hand Timer0 and Timer2 can generate Compare Match and Overflow interruptions only.





0 comments:

Post a Comment