Timer interrupt

Hi guys ! Anyone of you know why there are two kinds of timer interrupt, a CTC mode and a normal mode? What is the different between both of them? Is there any advantages or disadvantages when you use either one of them?

CTC mode:

void setup(){

cli();//stop interrupts

//set timer0 interrupt at 2kHz
TCCR0A = 0;// set entire TCCR0A register to 0
TCCR0B = 0;// same for TCCR0B
TCNT0 = 0;//initialize counter value to 0
// set compare match register for 2khz increments
OCR0A = 124;// = (1610^6) / (200064) - 1 (must be <256)
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS01 and CS00 bits for 64 prescaler
TCCR0B |= (1 << CS01) | (1 << CS00);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);

//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (1610^6) / (11024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);

//set timer2 interrupt at 8kHz
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
// set compare match register for 8khz increments
OCR2A = 249;// = (1610^6) / (80008) - 1 (must be <256)
// turn on CTC mode
TCCR2A |= (1 << WGM21);
// Set CS21 bit for 8 prescaler
TCCR2B |= (1 << CS21);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);

sei();//allow interrupts

}//end setup

normal mode:

#include <TimerThree.h>

// This example uses the timer interrupt to blink an LED
// and also demonstrates how to share a variable between
// the interrupt and the main program.

const int led = LED_BUILTIN; // the pin with a LED

void setup(void)
{
pinMode(led, OUTPUT);
Timer3.initialize(150000);
Timer3.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
Serial.begin(9600);
}

// The interrupt will blink the LED, and keep
// track of how many times it has blinked.
int ledState = LOW;
volatile unsigned long blinkCount = 0; // use volatile for shared variables

void blinkLED(void)
{
if (ledState == LOW) {
ledState = HIGH;
blinkCount = blinkCount + 1; // increase when LED turns on
} else {
ledState = LOW;
}
digitalWrite(led, ledState);
}

// The main program will print the blink count
// to the Arduino Serial Monitor
void loop(void)
{
unsigned long blinkCopy; // holds a copy of the blinkCount

// to read a variable which the interrupt code writes, we
// must temporarily disable interrupts, to be sure it will
// not change while we are reading. To minimize the time
// with interrupts off, just quickly make a copy, and then
// use the copy while allowing the interrupt to keep working.
noInterrupts();
blinkCopy = blinkCount;
interrupts();

Serial.print("blinkCount = ");
Serial.println(blinkCopy);
delay(100);
}

What does the datasheet say?

Which of them does what you want?

...R

Hi thx for the reply.

So datasheet sheet will specifically say which timer interrupt to use?

And what do you mean by which of them does what you want? How do you know which timer interrupt to use?

Benny567:
Hi guys ! Anyone of you know why there are two kinds of timer interrupt, a CTC mode and a normal mode? What is the different between both of them? Is there any advantages or disadvantages when you use either one of them?

There are many different timer modes. The question is, what do you want to DO with the timer?

You can capture pulses and get the time between them.
You can generate a constant string of pulses at a given rate.
You can setup a PWM output, and vary the frequency of the PWM.
You can generate a time delay.
You can generate multiple time delays.

......lots of "et-ceteras" go here......

Question #1: What do you want to DO with the timer? Without that, it's impossible to go any further answering your question.

Benny567:
Hi thx for the reply.

So datasheet sheet will specifically say which timer interrupt to use?

And what do you mean by which of them does what you want? How do you know which timer interrupt to use?

The datasheet will not tell you which interrupts or timers to use. The only difference between them is some are 8 bit, others are 16 bit.

They all can be programmed to "do" anything you want.

Again, what do you want to do? A time delay? Blink an LED on and off at a certain rate? Generate an analog signal using PWM?

If can specify what you want to do, then we can go on from that point.

If you are just trying to learn, then pick something. Maybe "blink an LED on and off using a timer"?

Then, one of us can provide you with a heavily commented piece of code that shows you what to do, why it's being done and finally you can compile the code (sketch) and see how it works for yourself. Then, if you understand the example, you yourself can modify certain parts (like maybe count values) and see if, yes indeed, as you expected the LED blinks faster or slower.

But we need to know what you WANT first.

P.S. it's also important for us to know (if we are going to provide some examples) WHICH Arduino board you have (UNO, Mega, ???).

This is because different boards have different timers associated with different pins. This is transparent to the user when they use Arduino functions, but when you get into the nitty-gritty of timer numbers, ports and interrupts, you will need to know which pin goes to which port and interrupt (that's why we need to know what board you're using).

Benny567:
So datasheet sheet will specifically say which timer interrupt to use?

I just meant that you should not be writing that sort of code without having a good knowledge of the datasheet.

And what do you mean by which of them does what you want? How do you know which timer interrupt to use?

You have two programs. I presume you have tried them - which one does what you want?

...R