Go Down

Topic: setting PWM Counter and registers yields odd behavior (Read 907 times) previous topic - next topic


Aug 08, 2013, 05:19 am Last Edit: Aug 08, 2013, 05:21 am by eddiea6987 Reason: 1
tonights issue is the following ,
when trying to set up counter 0 which is an 8 bit counter
to fastPwM
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
Code: [Select]

int main(void)

       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 )
pwm --;

if (cycle == 1)


OCR0A =  ((pwm / 100) * 255.0 ); // update  duty cycle again since it changes everytime around main loop

I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"


interesting..i think its because of delay but then again it would happen every 10ms if it was indeed delay

its goes

not at all what it want
any ideas?
I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"


Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


sorry i didnt include the preprocessor code in my post

but i have pwm set as a global since its used all over the place
Code: [Select]

double  pwm = 0;
int cycle = 0;
I could print the Arduino logo on a box of cereal and sell it as "Arduin-O's"


How did you get past

Code: [Select]
TCCR0B |= (1<<CS00) ); // no prescale
Amateurs built the ark, Professionals built the Titanic

Go Up