Pages: [1]   Go Down
Author Topic: setting PWM Counter and registers yields odd behavior  (Read 330 times)
0 Members and 1 Guest are viewing this topic.
West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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


while(1)
{
_delay_ms(10);
//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)
pwm++;


}

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


}
« Last Edit: August 07, 2013, 10:21:24 pm by eddiea6987 » Logged

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

West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

its goes
0...255
0...255
0...255
255...0
255...0
255...0

not at all what it want
any ideas?
Logged

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That doesn't compile. Where is pwm declared?
Logged

West palm beach, FL
Offline Offline
Sr. Member
****
Karma: 1
Posts: 325
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
double  pwm = 0;
int cycle = 0;
Logged

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

Australia Mate!
Offline Offline
Sr. Member
****
Karma: 3
Posts: 284
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How did you get past

Code:
TCCR0B |= (1<<CS00) ); // no prescale
Logged

Pages: [1]   Go Up
Jump to: