Author Topic: Atmega328 PWM duty cicle  (Read 2716 times)

0 Members and 1 Guest are viewing this topic.

Offline cosminnciTopic starter

  • Regular Contributor
  • *
  • Posts: 85
  • Country: ro
Atmega328 PWM duty cicle
« on: October 29, 2015, 08:50:30 am »
I try to make a pwm with a 1,563 hz frequency but I have problems changing the duty cicle.
Code: [Select]
        const byte n = 9;
        TCCR2A = bit (WGM20) | bit (WGM21) | bit (COM2B1);
        TCCR2B = bit (WGM22) | bit (CS22) | bit (CS21) | bit (CS20);
        OCR2A =  n;
        OCR2B = ((n + 1) / 2) - 1; // 50% duty cycle

I know OCR2B is 8 bit and 255 values
but according to above I can use only 9 steps of duty cicle.

how can I use the full range of 255 steps and still have the 1,563 hz frequency?

thank you.
« Last Edit: October 29, 2015, 09:18:21 am by cosminnci »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: Atmega328 PWM duty cicle
« Reply #1 on: October 29, 2015, 09:01:48 am »
Have you checked whether PWM-output mode "Toggle OC2A on Compare Match" would help?
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 614
Re: Atmega328 PWM duty cicle
« Reply #2 on: October 29, 2015, 09:14:55 am »
What about formatting your code? 75 posts and no advance so far..
Quote
TCCR2A = bit (WGM20) | bit (WGM21) | bit (COM2B1); // fast PWM, clear OC2A on compare
The code does not match the comment, the comment does not match the code.
 

Offline cosminnciTopic starter

  • Regular Contributor
  • *
  • Posts: 85
  • Country: ro
Re: Atmega328 PWM duty cicle
« Reply #3 on: October 29, 2015, 09:19:24 am »

ok, I edited the post, using the code tags.
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: Atmega328 PWM duty cicle
« Reply #4 on: October 29, 2015, 11:21:19 am »
I don't see you setting the clock source / prescaler...?
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline cosminnciTopic starter

  • Regular Contributor
  • *
  • Posts: 85
  • Country: ro
Re: Atmega328 PWM duty cicle
« Reply #5 on: October 29, 2015, 11:34:22 am »

the bit (CS22) | bit (CS21) | bit (CS20) sets the prescaler to 1024
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: Atmega328 PWM duty cicle
« Reply #6 on: October 30, 2015, 02:26:32 pm »
Ok setting the WGM20:2 bits will select FastPWM mode (chapter 17.7.3). Looking at the Waveform Generation Mode description (Table 17-8) it looks like it only works on Output Compare A. The full period (cycle) is mainly determined by the prescaler.

So you should be setting the COM2A0:1 flags (not the COM2B) to output the signal. Do not forget to set the appropriate DDR register to set that pin to output - otherwise you will not see it (when pin is set to input).

Not sure what the numbers are to get your desired frequency but the formulas are also in the datasheet.

Hope it helps,
Marc
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf