I'm trying to learn timers, to build a LED dimmer for my ceiling lamps. I am using timer2.
I use an interrupt function, check the "anim.onCycle2" boolean; if true, the "OCR2A=" on-value, if FALSE then "OCR2A=" off-value. I use serial commands to set "anim.PWMonValue2" and "anim.PWMoffValue2". This is how I expect to change the width of my wave, to dim my LEDs
After I hooked my oscilloscope up to see what was going on, I can visually see the square wave change shape as I change the ON/OFF values from serial commands. As I understand, the "ISR(TIMER2_COMPA_vect)" will execute on the pre-scaler, count whatever "OCR2A" - is this correct?
I can set the on value for 128, off value for 128 and the square wave 50% duty with frequency on my screen measures "122.044Hz". If I change on value to 10, OFF value to 10 - I see the EXACT wave, with same frequency. Same goes if I set them both, to ANY value. I was expecting, if this counter counts to 10, (2) times -vs- counts to 100 (2) times this WAVE would be wider in both on/off and give me a different frequency. Whatever this is doing I can not understand, please help me.
If I set on-Value to 10, off-value to 100 - I can shape the width of that wave is longer on the off-value. Same goes if I set on-value to 100, and off-value to 10, I see the wave shape in opposite. I can tell my interrupt compare function is processing the on/off values in to OCR2A. If I set both on & off to 10, or both to 100, or both to ANY of the same values - my scope reads the same "122.04 HZ" value. I just can not understand how the processor is interpreting these values!?!? I thought 100 pre-scale cycles took much more time to process than 10 pre-scale cycles, but my oscilloscope tells me different.
Here is my SIMPLIFIED setup function:
void setup()
{
animInit();
}
void animInit()
{
noInterrupts();
TCCR2A = 0;
TCCR2B = 0;
TCCR2B = 6; // CS22=1 CS21=1 CS20=0
TCCR2B |= (1 << WGM12); // CTC mode
TCNT2= 0;
OCR2A = 255;
TIMSK2 |= (1 << OCIE2A); // enable timer compare interrupt
interrupts();
}
ISR(TIMER2_COMPA_vect) // timer compare interrupt service routine
{
digitalWrite(anim.LED[testLed], anim.onCycle2); // toggle LED pin
OCR2A = anim.onCycle2 ? anim.PWMonValue2 : anim.PWMoffValue2;
anim.onCycle2 = ! anim.onCycle2;
}

