···
www.simsso.de

Timer Interrupts - Arduino


ATmega328 timer interrupt calculator

Atmega328 Datenblatt


Wichtige Register

Abkürzung Bezeichung Funktion
TCNTX timer / counter Zählerwert
OCRXA output compare register A Vergleichswert
TIMSKX timer interrupt mask register Interrupt aktivieren
TCCRXA timer control register A Interrupt aktivieren
TCCRXB timer control register A Prescaler


Prescaler

CSX2 CSX1 CSX0 Beschreibung
0 0 0 Timer angehalten
0 0 1 1:1 (kein Prescaling)
0 1 0 1:8
0 1 1 1:64
1 0 0 1:256
1 0 1 1:1024
1 1 0 externe Taktquelle am Pin T0, Takt bei negativer Taktflanke
1 1 1 externe Taktquelle am Pin T0, Takt bei positiver Taktflanke


Timer zurücksetzen

// reset a timer unit (replace X by timer number)
TCCRXA = 0; // set TCCRXA register to 0
TCCRXB = 0; // set TCCRXB register to 0
TCNTX  = 0; // reset counter value


Timer aktivieren

TCCR0A |= (1 << WGM01); // enable timer0 CTC mode
TIMSK0 |= (1 << OCIE0A); // enable timer0 compare interrupt

TCCR1B |= (1 << WGM12); // enable timer1 CTC mode
TIMSK1 |= (1 << OCIE1A); // enable timer1 compare interrupt

TCCR2A |= (1 << WGM21); // enable timer2 CTC mode
TIMSK2 |= (1 << OCIE2A); // enable timer2 compare interrupt

Hinweis: Bei Timer1 unterscheidet sich die Bezeichungen teilweise.


Timer Vergleichswert setzen

OCR0A = 124; // set compare match register of timer 0 (max. value: 255 = 2^8 - 1)
OCR1A = 20233; // set compare match register of timer 1 (max. value: 65535 = 2^16 - 1)
OCR2A = 20; // set compare match register of timer 2 (max. value: 255 = 2^8 - 1)


Prescaler einstellen

// no prescaling for timer0
TCCR0B |= (1 << CS00);  

// 1:1024 prescaling for timer1
TCCR1B |= (1 << CS12) | (1 << CS10);   

// 1:1024 prescaling for timer 2
TCCR2B |= (1 << CS22) | (1 << CS20);   


Bei Interrupt aufgerufenen Funktion

ISR(TIMER0_COMPA_vect) { // called by timer0
  
}

ISR(TIMER1_COMPA_vect) { // called by timer1
  
}

ISR(TIMER2_COMPA_vect) { // called by timer2
  
}


Beispiel mit Timer1

long i = 1;

void setup() {
  cli(); // disable interrupts
  
  // reset
  TCCR1A = 0; // set TCCR1A register to 0
  TCCR1B = 0; // set TCCR1B register to 0
  TCNT1  = 0; // reset counter value
  
  OCR1A = 31249; // compare match register

  // set prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);   
  
  TCCR1B |= (1 << WGM12); // turn on CTC mode
  TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
  
  sei(); // allow interrupts
  
  
  Serial.begin(9600); // start serial connection
}

ISR(TIMER1_COMPA_vect) { // function which will be called when an interrupt occurs at timer 1

  Serial.println(i); // send current value of i to the pc
}

void loop() {
  i++; // increment i
}


Beispiel mit allen drei Timern

// timer0 will interrupt at 2kHz
// timer1 will interrupt at 1Hz
// timer2 will interrupt at 8kHz

void setup() {
  cli(); // disable interrupts
  

  // timer0
  
  TCCR0A = 0; // set TCCR0A register to 0
  TCCR0B = 0; // set TCCR0B register to 0
  TCNT0  = 0; // set counter value to 0
  
  OCR0A = 124; // set compare match register
  
  TCCR0B |= (1 << CS01) | (1 << CS00); // Set CS01 and CS00 bits for 1:64 prescaler

  TCCR0A |= (1 << WGM01); // turn on CTC mode
  TIMSK0 |= (1 << OCIE0A); // enable timer compare interrupt
  

  // timer1
  
  TCCR1A = 0; // set TCCR1A register to 0
  TCCR1B = 0; // set TCCR1B register to 0
  TCNT1  = 0; // set counter value to 0
  
  OCR1A = 15624; // set compare match register
  
  TCCR1B |= (1 << CS12) | (1 << CS10); // Set CS12 and CS10 bits for 1:1024 prescaler

  TCCR1B |= (1 << WGM12); // turn on CTC mode
  TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
  

  // timer2
  
  TCCR2A = 0; // set TCCR2A register to 0
  TCCR2B = 0; // set TCCR2B register to 0
  TCNT2  = 0; // set counter value to 0
  
  OCR2A = 249; // set compare match register
  
  TCCR2B |= (1 << CS21); // Set CS21 bits for 1:8 prescaler

  TCCR2A |= (1 << WGM21); // turn on CTC mode
  TIMSK2 |= (1 << OCIE2A); // enable timer compare interrupt
  
  
  sei(); // allow interrupts

}


ISR(TIMER0_COMPA_vect) {
  // timer0 interrupt to-do code here
}

ISR(TIMER1_COMPA_vect) {
  // timer1 interrupt to-do code here
}
  
ISR(TIMER2_COMPA_vect) {
  // timer2 interrupt to-do code here
}


void loop() {
  // other code
}



Loading... © Timo Denk 2017
Feedback

Feedback