tonights issue is the following ,
when trying to set up counter 0 which is an 8 bit counter
i use the following code
now before you read
understand i have tried the following
Change the interrupt to overflow , on compare match
also tried having no interrupt and just updating the compare register everytime i update pwm
but the strange behavior is this..
it does what i want it to do but...wrong
i want it to fade in and out out
instead it fades from 0 to 255 THREE TIMES
and then from 255 to 0 THREE TIMES...
where in the code am i telling it do it three times in between transitions ????
i want one fade up and one fade down nice smooth transition..
unless i have a misunderstanding of how the timer works
sei(); //global interrupts
DDRD |=(1<<PIND6); // set pin d6 OC0A to output
TCCR0A |= ((1<<WGM01) | (1<<WGM00)); // enable fast pwm
TCCR0A |= (1<<COM0A1); // clear on compare math , set at bottom
TIMSK0 |= (1<<OCIE0A);// compare match interrupt
OCR0A = ((pwm / 100) * 255.0 ) ;//value to compare to timer , aka pwm dudty cycle
TCCR0B |= (1<<CS00) ); // no prescale
//just fade in and out , 8 bit mode
if (pwm >=255)
cycle = 0;
if(pwm <=1 )
cycle = 1;
if(cycle == 0 )
if (cycle == 1)
OCR0A = ((pwm / 100) * 255.0 ); // update duty cycle again since it changes everytime around main loop