How to use Timer interrupt on Arduino Uno

posted in: Microcontroller | 0

Normally when programming for arduino you only run 1 process in the loop function. But with more complex projects that will require the use of Timer interrupts. The most commonly used is to read the sensor value after a fixed period of time, for example, 2s to read the sensor value once without affecting the main program.

Today I will introduce how to use timer interrupt to run 3 processes concurrently. More specifically, blinking 3 LEDs independently of each other.

Arduino Uno supports 3 Timers:

  • Timer0: Used for delay(), millis() functions. In order not to cause program conflicts, you should not use this Timer.
  • Timer1: 16-Bit.
  • Timer2: 8-Bit.

1. How to use the Timer 1 interrupt

Figure 1. Clock selection for Timer1

The quartz frequency on the Arduino uno is 16MHz. I want Led1 to light up for 1.5s and then turn off for 1.5s, then choose the value

TCNT1 = 2^16 – 1.5s*(16*10^6)/1024 = 42099

Here 2^16 is the 16-bit Timer and 1024 is the prescaler, this value you can choose according to the table above.

2. How to use the Timer 2 interrupt

Figure 2. Clock selection for Timer2

I want Led2 to light up for 0.3s and then turn off for 0.3s, then do the following.

Suppose I choose a value so that Timer 2 has a repeat period of 0. bet365 arab 01s

TCNT2 = 2^8 – 0.01s*(16*10^6)/1024 = 100

Here 2^8 is the 8-bit Timer and 1024 is the prescaler, this value you can choose according to the table above.

To get 0.3s light off, we repeat interrupt 0. bet365 arab 3s/0.01s = 30 times.

Once the calculation is done, let’s move on to the programming part




3. Programming

I will let LED0 light off for 1s in the loop() function, LED1 on and off for 1.5s in the Timer1 interrupt program and LED2 on for 0.3s on the Timer2 interrupt program. العب كازينو  We’ve got 3 processes running independently of each other.

To configure the Timer, use the following command:

  cli(); //Tắt ngắt toàn cục
  TCCR1A = 0; //Reset Timer1
  TCCR1B = 0;      
  TIMSK1 = 0;
  //Setup Timer1
  TCCR1B |= (1 << CS12)|(1 << CS10) ; //Chọn prescaler 1024 CS12=1,CS10=1 xem bảng phía trên
  TCNT1= 42099; //Giá trị tính toán phía trên
  TIMSK1 |= (1 << TOIE1); //Overflow interrupt enable
  sei(); //Cho phép ngắt toàn cục

Declare the timer interrupt program

ISR (TIMER1_OVF_vect) //Chương trình ngắt Timer1
{ 

}

The whole program

#include <avr/interrupt.h>
#define LED1 11
#define LED2 12
#define LED0 13
int dem; // Giá trị lặp lại
ISR (TIMER2_OVF_vect) //Chương trình ngắt Timer2
{
  dem++;
  if (dem == 30){ //Đủ 30 lần thì thực hiện đảo tín hiệu chân LED2
    dem = 0;
    digitalWrite(LED2,!digitalRead(LED2));
  }
  TCNT2 = 100;
}
ISR (TIMER1_OVF_vect) //Chương trình ngắt Timer1
{ 
  digitalWrite(LED1,!digitalRead(LED1)); //Sáng tắt LED1
  TCNT1= 42099;
}
void setup() {
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  cli(); //Tắt ngắt toàn cục
  TCCR1A = 0; //Reset Timer1
  TCCR1B = 0;      
  TIMSK1 = 0;
  //Setup Timer1
  TCCR1B |= (1 << CS12)|(1 << CS10) ; //Chọn prescaler 1024 CS12=1,CS10=1 xem bảng phía trên
  TCNT1= 42099; //Giá trị tính toán phía trên
  TIMSK1 |= (1 << TOIE1); //Overflow interrupt enable
 
  TCCR2A = 0; //Reset Timer2
  TCCR2B = 0;      
  TIMSK2 = 0;
  //Setup Timer2
  TCCR2B |= (1 << CS22)|(1 << CS21)|(1 << CS20) ; //Chọn prescaler 1024 CS22=1,CS21=1, CS20=1 xem bảng phía trên
  TCNT2 = 100; //Giá trị tính toán phía trên
  TIMSK2 |= (1 << TOIE2); //Overflow interrupt enable
  sei(); //Cho phép ngắt toàn cục
  dem = 0; //Khởi động giá trị lặp lại Timer2
}

void loop() {
  digitalWrite(LED0,!digitalRead(LED0)); //Sáng tắt LED0
  delay(1000);
}

Leave a Reply

Your email address will not be published. Required fields are marked *