ATmega328P phase and duty correct PWM

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

Im trying to control servo with ATmega328P with 18.432 Hz crystal and PWM. I need to have PWM with frequency of 50Hz so i used phase and duty correct PWM (since any other wont let me have PWM with 50Hz frequency). Duty cycle will be variable from 2.5% to 12.5%

According to formula in datasheet PWM frequency is calculated with FCPU/(2*prescaler*top)

So i figured out that with my crystal 18.432Mhz and prescaler 64 and TOP value 2880 ill have 50Hz PWM

Here is my code

#include 
int main(void)
{
	//FCPU=18432000Hz
	//Using 16bit timer/counter1 with  		
	//phase and frequency correct PWM
	//PWM frequency 50Hz
	DDRB|=_BV(1); //set OC1A as output
	
	ICR1=2880; //TOP value
	
	TCCR1A|=_BV(COM1A1); //Clear OC1A/OC1B on Compare Match when upcounting. Set OC1A/OC1B on Compare Match when downcounting. (non inverted)

	OCR1A=72;  //set to get 2.5% duty cycle 72/2880 so during 20 ms its high for 0.5ms and low for the rest of time
	
	TCCR1B=_BV(WGM13); //mode 8, PWM, Phase and Frequency Correct, top ICR1
	
	TCCR1B=_BV(CS11); //Prescaler 64
	TCCR1B=_BV(CS10);

	while(1)
    {
    }
}

I was thinking that everything should work, the PWM will work even with main loop being empty but im missing something since my code is not working (i connected servo to right pin and its working since i tested it with simple program using delays).

Can anybody help me?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) "Hobby" servos are not critical on the 20ms/50Hz period.
2) Phase-and-frequency-correct always makes my head hurt. I can't think why an up/down mode would have any advantages in trying to create a pulse with a high time ranging from 1ms to 2ms.
3) "Duty cycle will be variable from 2.5% to 12.5%". That would be 0.5ms to 2.5ms. Is that taking into account the up/down, or does your servo really have those limits? (I >>told<< you that these modes make my head hurt.)
4) Tell more about "i used phase and duty correct PWM (since any other wont let me have PWM with 50Hz frequency)." ...

At 18.432MHz, there are probably a number of Fast PWM setups that have a 20ms period:

/8 and 0xb3ff
/64 and 0x167f
/256 and 0x059f
/1024 and 0x0167

All give exactly 20ms periods. All have exactly 1ms increments available-- e.g. 0x0900 counts (0x08ff compare match) for /8.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

TCCR1B=_BV(WGM13); //mode 8, PWM, Phase and Frequency Correct, top ICR1

TCCR1B=_BV(CS11); //Prescaler 64
TCCR1B=_BV(CS10);


What is the value of TCCR1B after the above statements are carried out?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
(since any other wont let me have PWM with 50Hz frequency)
Why would you think that? Mode 14 will work just fine. And besides, servos are pretty loose when it comes to frequency. You can be quite a bit off of 50Hz and still have it work.

   TCCR1B=_BV(WGM13); //mode 8, PWM, Phase and Frequency Correct, top ICR1
   
   TCCR1B=_BV(CS11); //Prescaler 64
   TCCR1B=_BV(CS10);

The second and third lines overwrite what you did in the first. You want:

   TCCR1B=_BV(WGM13) | _BV(CS11) | _BV(CS10);

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My bad, i checked the formula for PWM frequency for the output for fast PWM and freaked out when i couldnt get 50Hz with prescalers so i skipped to phase-and-frequency.

Ill try with fast PWM and come back since reading about phase-andfrequency made my head hurt too and id love to make it in easiest way possible.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

i checked the formula for PWM frequency for the output for fast PWM and freaked out when i couldnt get 50Hz with prescalers

???
What part of
Quote:

16.9.3 Fast PWM Mode
...
The PWM frequency for the output can be calculated by the following equation: ...

... formula precludes the numbers I gave above?

Once you work through the numbers once and understand what is going on, there are two tools AVRcalc from Jack Tidwell and KAVRcalc from Kevin Rosenburg that help with finding combinations. (In addition to CodeVisionAVR Wizard)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.