Arduino UNO Interrupt Timer

The Timer is a feature embedded in every microcontroller and has certain functions regarding the passage of time. The Timer function on a microcontroller can be explained specifically that the Timer functions to determine when the program will be executed and when the program will be stopped. The Timer on a microcontroller consists of several registers whose values decrease and increase automatically. On the Arduino UNO there are two types of timers, namely a timer with 8-bit resolution and a timer with 16-bit resolution. However, FS Tech will not discuss this timer more specifically because in this instruction we focus on how to run and set the timer on the Arduino UNO.

Why Use Interrupt Timer

Setting the timer on the microcontroller allows us to run programs using very specific times at certain time intervals in a multitasking manner. As we know, Arduino runs a looping program using the void loop(void) {} function in the order in which it is written, but it will be very difficult to determine the time of the events contained in this function. This happens because some commands are executed very quickly and other commands are executed very slowly, this is determined by the number of programs executed and conditional statements such as if, while, do while or for loop. The timer that runs on the Arduino loop() function is very difficult to predict because of the many functions contained in the Arduino library.

The Interrupt Timer on the microcontroller allows you to control the running time of the loop() function at precise time intervals, when the Interrupt Timer is executed, this will stop the running time in the loop() function and run a series of separate programs running on a Timer outside the loop() function and returning the Timer to run the loop() function again after another Timer has finished running the program.

Some of the benefits you get from running an Interrupt Timer include:

  • Measures incoming signals at evenly spaced intervals
  • Calculates the time between two programs
  • Transmits and generates signals at certain frequencies
  • Check incoming serial data periodically
  • Run two programs simultaneously (multitasking)
  • And many more

Prescaler and Calculating Timer Values

Arduino UNO has three Timers, Timer-0, Timer-1, and Timer-3. Each timer on the Arduino Uno has a time setting that increases with each time period until it reaches a certain value which is stored in a register, and will be reset every time the time value has been reached. And this cycle will continue when the Arduino is on. The Timer value depends on how large the Timer address bits are, for example, a timer with a value of 8 bits has a value range between 0-255 and a timer with a resolution of 16 bits has a value range of 0-65535.

To be able to calculate a timer, we can use a calculation called a prescaler, This calculation can be written with the following equation.

TimerSpeed(Hz) = ArduinoClockSpeed(16MHz) / prescaler

prescaler value at prescaler address

The table above shows the prescaler value at each prescaler address, so that the equation for the Timer calculation value can be written using the following equation.

InterruptFrequency(Hz) = ArduinoClockSpeed(16MHz) / ((prescaler * (compareMatchRegister + 1))

+1 due to comparison of matching register values indexed 0.

So if you want a prescaler value then the equation can be written according to the following equation.

CompareMatchRegister = 16MHz / (prescaler * DesiredInterruptFrequency) – 1

And keep in mind that if you use Timer-0 and Timer-2 the value must be less than 256 and if you use Timer-1 the value must be less than 65536. 

How to Use Timer Interrupts to Run Multitasking Programs

Timer-0 2KHz

				
					void setup(void) {
	cli();
	TCCR0A	= 0;
	TCCR0B	= 0;
	TCNT0	= 0;
	OCR0A	= 124;
	TCCR0A	|= (1<<WGM01);
	TCCR0B	|= (1<<CS01) | (1<<CS00);
	TIMSK0	|= (1<<OCIE0A);
	sei();
}
				
			

TCCR0A = 0; TCCR0B, TCNT0 = 0; is to return the time value to 0.

OCR0A set compare match register for 2KHz increments (16*10^6)/(2000*64)-1

WGM01 turn on CTC

CS01 & CS00 set CS01 & CS00 bits for 64 prescaler

TIMSK0 enable timer compare interrupt

Timer-1 1Hz

				
					void setup(void) {
	cli();
	TCCR1A	= 0;
	TCCR1B	= 0;
	TCNT1	= 0;
	OCR1A	= 15624;
	TCCR1B	|= (1<<WGM12);
	TCCR1B	|= (1<<CS12) | (1>>CS10);
	TIMSK1	|= (1<<OCIE1A);			
	sei();
}
				
			

TCCR1A = 0; TCCR1B, TCNT1 = 0; is to return the time value to 0.

OCR1A set compare match register for 1Hz increments (16*10^6)/(1*1024)-1

WGM12 turn on CTC

CS10 & CS12 set CS10 & CS12 bits for 1024 prescaler

TIMSK1 enable timer compare interrupt

Timer-2 8KHz

				
					void setup(void) {
	cli();
	TCCR2A	= 0;
	TCCR2B	= 0;
	TCNT2	= 0;
	OCR2A	= 249;
	TCCR2A	|= (1<<WGM21);
	TCCR2B	|= (1<<CS21);
	TIMSK2	|= (1<<OCIE2A);
	sei();
}
				
			

TCCR2A = 0; TCCR2B, TCNT2 = 0; is to return the time value to 0.

OCR2A set compare match register for 8KHz increments (16*10^6)/(8000*8)-1

WGM21 turn on CTC

CS21 set CS21 bits for 8 prescaler

TIMSK1 enable timer compare interrupt

Perhatikan bagaimana ke tiga Timer diatur, misalnya untuk mengaktifkan mode CTC

TCCR0A |= (1 << WGM01); // for Timer 0

TCCR1B |= (1 << WGM12); // for Timer 1

TCCR2A |= (1 << WGM21); // for Timer 2

Dan perhatikan nilai prescaler yang diatur sesuai pada tabel diatas.

TCCR1B |= (1 << CS11);  // 8 prescaler for timer 1 

TCCR2B |= (1 << CS22);  // 64 prescaler for timer 2

TCCR0B |= (1 << CS02) | (1 << CS00); // 1024 prescaler for timer 0

The command you want to run during this timer interrupt is in the Arduino sketch summarized as follows, outside the void setup(void) function and void loop(void) function, and also explained examples of each timer.

ISR(TIMER0_COMPA_vect) {

// for Timer 0

}

ISR(TIMER1_COMPA_vect) {

// for Timer 1

}

ISR(TIMER2_COMPA_vect) {

// for Timer 2

}

Arduino Program Example to Activate a Timer

Example for Timer-0 2KHz

				
					void setup(void) {
	pinMode(11, OUTPUT);
	
	cli();
	TCCR0A	= 0;
	TCCR0B	= 0;
	TCNT0	= 0;
	OCR0A	= 124;
	TCCR0A	|= (1<<WGM01);
	TCCR0B	|= (1<<CS01) | (1<<CS00);
	TIMSK0	|= (1<<OCIE0A);
	sei();
}
void loop(void) {
	// do something without delay()
}
ISR(TIMER0_COMPA_vect) {
	static boolean toggle = true;
	toggle = !toggle;
	digitalWrite(11, toggle ? HIGH : LOW);
}
				
			

Example for Timer-1 1Hz

				
					void setup(void) {
	pinMode(11, OUTPUT);
	
	cli();
	TCCR1A	= 0;
	TCCR1B	= 0;
	TCNT1	= 0;
	OCR1A	= 15624;
	TCCR1B	|= (1<<WGM12);
	TCCR1B	|= (1<<CS12) | (1>>CS10);
	TIMSK1	|= (1<<OCIE1A);			
	sei();
}
void loop(void) {
	// do something without delay()
}
ISR(TIMER1_COMPA_vect) {
	static boolean toggle = true;
	toggle = !toggle;
	digitalWrite(11, toggle ? HIGH : LOW);
}
				
			

Example for Timer-2 8KHz

				
					void setup(void) {
	pinMode(11, OUTPUT);
	
	cli();
	TCCR2A	= 0;
	TCCR2B	= 0;
	TCNT2	= 0;
	OCR2A	= 249;
	TCCR2A	|= (1<<WGM21);
	TCCR2B	|= (1<<CS21);
	TIMSK2	|= (1<<OCIE2A);
	sei();
}
void loop(void) {
	// do something without delay()
}
ISR(TIMER1_COMPA_vect) {
	static boolean toggle = true;
	toggle = !toggle;
	digitalWrite(11, toggle ? HIGH : LOW);
}
				
			

Ending

From the three examples of Timer programs, it can be seen when the Timer Interrupt is activated, it is not recommended to use the delay() function because it can interfere with all Timer activities on the microcontroller, especially if the delay() function value exceeds the prescaler value on the Timer interrupt, and if your program forces to use the delay() function, then use the millis() or macros() function on your Arduino, however, if the delay() function value does not exceed the prescaler value, then maybe it can be a little tolerable, but that’s not a good choice either, and if you don’t understand millis() function or macros() function, we can discuss it another time.

And a little addition that I may have forgotten to explain is the cli() function and the sei() function.

The cli() function is used to disable Timer interrupts

The sei() function is used to activate the Timer interrupt

Maybe that’s all we can share with you, thank you.

Regards.

Related Blogs

Arduino UNO Interrupt Timer

Arduino UNO Interrupt Timer The Timer is a feature embedded in every microcontroller and has certain functions regarding the passage of time. The Timer function

Read More »

We'd love to hear from you