Varing PWM duty cycle when using timer0

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

With ATmega644
when I used Timer1 I use ICR1 to set the TOP
and then use OCR1A and OCR1B to vary the duty cycle on each channel.

But if I want to use Timer0 i.e. 8-bit and there is no ICR0, only OCR0A and OCR0B and TOP fixed to 0xFF. How can I achieve two PWM that I can change the duty cycles, using this timer.

I just realized that OCR0A/OCR0B can be set as TOP, but that would only change the frequency of PWM, not the Duty cycle. Correct me if my understanding isn't right

Thanks,
K

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

TOP changes the frequency and OCR registers (when not being used to set TOP) change the duty. The datasheet documents WGM bits and the table will show what modes are available and how TOP and OCR registers may be used. If there aren't sufficient PWM outputs then implement soft PWM.

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

Does that mean I cannot use Timer0 on atmega644p to generate a PWM of 540 Hz and vary its duty cycle?

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

Yes. Use the internal RC and use 8.85MHz div256.
OSCCAL will achieve this frequency quite legally.

You might even find a crystal with this value.

David.

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

David is saying to NOT use one of the compare registers to set TOP. Use the default value of 255. Then, set the frequency with the prescaler, tweaking the RC clock frequency slightly to get your desired PWM frequency. This leaves you with two compare registers that will generate two PWM signals.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Since you are using TOP=255, you will get an OVF interrupt.
So you could alter TCNT0 in the ISR(OVF) e.g. TCNT0=10;
You still change duty-cycle with OCR0A and OCR0B but the lower-limits will be 10 and not 0.

So you either use a non-standard F_CPU or accept a slight jitter in the PWM.

Why is 540Hz critical ?

David.

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

That isn't very critical i have 11.0592MHz on my board, it has lot of other stuffs tied to it like wheel sensors bluetooth UART, LCD 16x2 a servo etc.

I already tuned my motors PWM and PID, and it seem to work fine at 540Hz to 560 Hz.

I appended this sonar HCSA04 and that ideally needs a 16-bit timer, and that's the reason I shift my motor control PWM to 8-bit timer.

PS: I tried to think if I could somehow use ICP for HCSC04 and still have my PWM for motor control on Timer1, but I got nowhere.

Thanks,
K