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?
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).
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
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.
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.
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.