Timer Overflow with ISR question

Hi,
I have the following code. I wanted to know whether the time delay for low(or high) is 2us or not and that the frequency of the output square at pin 2 would then be 250khz. I used (256-224)*(1/16MHz)=2us for this calculation, is this correct?

void setup () {
pinMode(2, OUTPUT);	// pin 2 is output
TCNT2 = 224; 	//load timer2 with 224
TCCR2B |= (1<<CS20);	//no prescalar, normal mode
TIMSK2 = (1<<TOIE2);	//enable overflow interrupt
sei();
}

void loop() {
}


ISR(TIMER2_OVF_vect){
    TCNT2 = 224;
	PORTD ^= (1<<PD2);
}

Note: The PIND register will toggle an output pin when written with a 1. You can toggle Pin 2 with a write instead of a read-modify-write.
PIND = 1<<PD2;

Your interrupts will be initiated 32 clock cycles after you write 224 into the count register but the interrupt will be delayed by other interrupts and there will be a number of cycles before you get to setting the count register again. If you want a STABLE 250 kHz signal, use one of modes that toggles the pin in hardware, without interrupts.

I would use WGM2 (CTC). Set TOP (OCR2A) to 31. Set Compare Output Mode 2B to 1 to toggle the pin on overflow. The drawback is that you have to use the OC2B pin (Pin 3).

void setup ()
{
  pinMode(3, OUTPUT);  //Pin 3 (OC2B) is output
  
  TCCR2B = 0; // Stop the timer
  TIMSK2 = 0; // Disable timer interrupts
  
  OCR2A = 31; // Overflow every 32 counts (500 kHz)
  
  TCCR2A = (1 << COM2B0) | (1 << WGM21);  // Toggle OC2B on overflow, WGM 2 (CTC)
  TCCR2B = (1 << CS20); // Start the timer, no prescale
}

void loop()
{
}
1 Like

hi thanks,
will keep your advice in mind. but to my question, the calculation is correct right? i get different frequency(75khz) and different delay when i simulate it so i was confused altogether, so maybe the simulation may not be incorrect. also in what application would be then the timer overflow interrupt be applicable/useful(given that the timing may not be 100% accurate)?
thanks again

hi,
your code above gives exactly 250khz frequency in simulation.
(there is typo in your code comment i think its 250khz you meant)

thanks

PIND - The Port D Input Pins Register - read only

Not. I hate dumbed down documentation, like I found first on some Arduino and other websites… having only dim recall of this

14.2.2 Toggling the Pin
Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port.

from the full data sheet. THX @johnwasser for, like, knowing this neat trick and reminded me.

a7

It is VERY handy when you need to count more clock ticks than the timer can hold. That's how millis() can count 4.2 billion milliseconds with a timer that only counts up to 1024 microseconds.

1 Like

The timer/counter "peripheral" section of a microprocessor is kinda the Swiss Army Knife for all kinds things related to, well, timing and counting.

It's mind-numbing hard to slog through the datasheet section without a specific goal, but recommended, if only so you can see what all they are capable of, and how many of what sizes and capabilities you are blessed with on the chip you are using.

Later you'll just keep coming across things that get way easier (and harder :-|) to do with timer/counters and you'll remember, maybe, to delve back in and sort the possible pesky problem of configuring the control registers and working out some constants to achieve your goal.

It can unburden the central process, if it was something you could have done in straight ahead code without the timer/counter, and for higher frequency applications make possible things you just could not do otherwise.

Interrupts play a role in this, so it is imperative that you come to this peripheral with a good hold on the hows and whys of interrupts.

The availability and feature of the timer/counter(s) is one of the central differentiating specifications of a microprocessor, right there with program memory, RAM, EEPROM and so forth.

a7

No. The overflows do happen at 500 kHz. The output pin toggles on overflow so the output frequency is 250 kHz.