Aim:
This tutorial shows the use of timers and interrupts for Arduino boards. As Arduino programmer you will have used timers and interrupts without knowledge, bcause all the low level hardware stuff is hidden by the Arduino API. Many Arduino functions uses timers, for example the time functions: delay(), millis() and micros() and delayMicroseconds(). The PWM functions analogWrite() uses timers, as the tone() and the noTone() function does. Even the Servo libraryuses timers and interrupts.
Description:
What is a timer?
A timer or to be more precise a timer / counter is a piece of hardware builtin the Arduino controller (other controllers have timer hardware, too). It is like a clock, 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 ATmega168 or the ATmega328. These chips are pin compatible and only differ in the size of internal memory. Both have 3 timers, called timer0, timer1 and timer2. Timer0 and timer2 are 8bit timer, 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.
The controller for the Arduino Mega series is the Atmel AVR ATmega1280 or the ATmega2560. Also identical only differs in memory size. These controllers have 6 timers. Timer 0, timer1 and timer2 are identical to the ATmega168/328. The timer3, timer4 and timer5 are all 16bit timers, similar to timer1.
All timers depends on the system clock of your Arduino system. Normally the system clock is 16MHz, but for the Arduino Pro 3,3V it is 8Mhz. So be careful when writing your own timer functions.
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 gerally enabled.
Timer0:
Timer0 is a 8bit timer.
In the Arduino world timer0 is been used for the timer functions, like delay(), millis() and micros(). If you change timer0 registers, this may influence the Arduino timer function. So you should know what you are doing.
Timer1:
Timer1 is a 16bit timer.
In the Arduino world the Servo library uses timer1 on Arduino Uno (timer5 on Arduino Mega).
Timer2:
Timer2 is a 8bit timer like timer0.
In the Arduino work the tone() function uses timer2.
Timer3, Timer4, Timer5:
Timer 3,4,5 are only available on Arduino Mega boards. These timers are all 16bit timers.
Timer Register
You can change the Timer behaviour through the timer register. The most important timer registers are:
TCCRx - Timer/Counter Control Register. The prescaler can be configured here.
A timer or to be more precise a timer / counter is a piece of hardware builtin the Arduino controller (other controllers have timer hardware, too). It is like a clock, 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 ATmega168 or the ATmega328. These chips are pin compatible and only differ in the size of internal memory. Both have 3 timers, called timer0, timer1 and timer2. Timer0 and timer2 are 8bit timer, 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.
The controller for the Arduino Mega series is the Atmel AVR ATmega1280 or the ATmega2560. Also identical only differs in memory size. These controllers have 6 timers. Timer 0, timer1 and timer2 are identical to the ATmega168/328. The timer3, timer4 and timer5 are all 16bit timers, similar to timer1.
All timers depends on the system clock of your Arduino system. Normally the system clock is 16MHz, but for the Arduino Pro 3,3V it is 8Mhz. So be careful when writing your own timer functions.
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 gerally enabled.
Timer0:
Timer0 is a 8bit timer.
In the Arduino world timer0 is been used for the timer functions, like delay(), millis() and micros(). If you change timer0 registers, this may influence the Arduino timer function. So you should know what you are doing.
Timer1:
Timer1 is a 16bit timer.
In the Arduino world the Servo library uses timer1 on Arduino Uno (timer5 on Arduino Mega).
Timer2:
Timer2 is a 8bit timer like timer0.
In the Arduino work the tone() function uses timer2.
Timer3, Timer4, Timer5:
Timer 3,4,5 are only available on Arduino Mega boards. These timers are all 16bit timers.
Timer Register
You can change the Timer behaviour through the timer register. The most important timer registers are:
TCCRx - Timer/Counter Control Register. The prescaler can be configured here.
TCNTx - Timer/Counter Register. The actual timer value is stored here.
OCRx - Output Compare Register
ICRx - Input Capture Register (only for 16bit timer)
TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.
TIFRx - Timer/Counter Interrupt Flag Register. Indicates a pending timer interrupt.
Clock select and timer frequency
Different clock sources can be selected for each timer independently. To calculate the timer frequency (for example 2Hz using timer1) you will need:
- CPU frequency 16Mhz for Arduino
- maximum timer counter value (256 for 8bit, 65536 for 16bit timer)
- Divide CPU frequency through the choosen prescaler (16000000 / 256 = 62500)
- Divide result through the desired frequency (62500 / 2Hz = 31250)
- Verify the result against the maximum timer counter value (31250 < 65536 success) if fail, choose bigger prescaler.
Timer modes
Timers can be configured in different modes.
PWM mode. Pulth width modulation mode. the OCxy outputs are used to generate PWM signals
CTC mode. Clear timer on compare match. When the timer counter reaches the compare match register, the timer will be cleared
Block Diagram
Schematic
Code
// **********************************************
// Project: Interfacing timer0 to Arduino
// Author: Hack Projects India
// Module description: Operate LED
// **********************************************
int outPin = 13; // digital pin 8
void setup()
{
pinMode(outPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(outPin, HIGH); // sets the pin on
for(int i=0;i<100;i++)
delayMicroseconds(10000);
digitalWrite(outPin, LOW); // sets the pin off
for(int i=0;i<100;i++)
delayMicroseconds(10000);
}
Downloads:
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
No comments:
Post a Comment