Duty Cycle adjustment Fast PWM Mode

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

*Moved because it is not discussing any of the completed projects. Moderator*

 

As per the datasheet of Atmega328, Timer0 fast PWM mode can be selected by setting WGM02:00 to either 011 or 111. When we set Fast PWM mode using 111, TOV Flag set on TOP. Also TOP is equal to OCR0A. Now ON time of PWM is controlled using OCRA. When compare match occurs, OCA0 is cleared (COM0A1:COM0A0 = 10) and it is set at the bottom. Now my question is if TCNT clears after reaching TOP (that is nothing but OCR0A), how can we alter On time with WGM02:00=111? I am not quite clear from data sheet. Even waveforms are also little confusing. Or is it that TCNT always counts from 0x00 to 0xff irrespective of TOV flag in this case? What is the use of fast PWM using WGM02:00=111? I think the issue we can see in phase correct mode as well.

This topic has a solution.

Last Edited: Sun. Sep 6, 2015 - 04:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now ON time of PWM is controlled using OCRA.

No it isn't. In mode 7 OCRA is TOP (which sets the frequency). You need to use OCRB for the duty cycle. 

Regards,
Steve A.

The Board helps those that help themselves.

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

That means I have to use OCIEnB ISR to alter the duty cycle. So obviously OCRnB has to be less than OCRnA. Also without the ISR I cannot generate PWM in mode 7. Same thing I can do even in CTC mode ! In CTC mode, OCRnA decides the frequency (on compare match I can set/reset the OCA pin) and on OCRnB match, I can complement OCA pin to control the duty cycle. What is the advantage of mode 7 then compared to CTC mode?

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

I must be missing something...

That means I have to use OCIEnB ISR to alter the duty cycle.

No, I cannot think of a single AVR8 situation where an ISR is needed to alter a PWM duty cycle.

 

 

 

So obviously OCRnB has to be less than OCRnA.

True enough, if >>you<< have chosen to use this particular AVR model and 8-bit timer and mode.  

 

Also without the ISR I cannot generate PWM in mode 7.

???  Not true.  Wasn't that gone through above?   If you choose to use that AVR model and timer and mode, then OCR0A is TOP, and the PWM output would be on OC0B and the duty cycle set with OCR0B.

 

Same thing I can do even in CTC mode ! In CTC mode, OCRnA decides the frequency (on compare match I can set/reset the OCA pin) and on OCRnB match, I can complement OCA pin to control the duty cycle. What is the advantage of mode 7 then compared to CTC mode?

Only if you have an ISR, as OCR0B compare match cannot change OCR0A value in hardware.

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.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also without the ISR I cannot generate PWM in mode 7.

As Lee said, no, that is not true. You simply set the COM0B bits. 

Regards,
Steve A.

The Board helps those that help themselves.

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

Got it. Thanks for wonderful explanation. My thinking was limited to using only OCA pin (I did not come out of CTC mode fully) and that is why I felt without OCRB interrupt we cannot alter duty cycle in Mode 7. I think AVR timer section is bit complex for beginners and needs careful study. Atmel datasheet also could have been little more elaborate. Datasheet mostly explains with reference to mode 3. For example, in page number 100 of the datasheet, the formula given for PWM frequency is fOCnxPWM = fclk_I/O/(N.256). 256 is applicable in mode 3 only and in case of mode 7, OCRA should be used right? But datasheet does not spell this out clearly I guess.

Last Edited: Sun. Sep 6, 2015 - 07:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

in page number 100 of the datasheet...

Datasheet ID and version number?  Section name?  Section number?  There may well be a score of different datasheets for that AVR family.  Each might well have different page 100 contents.

 

I think AVR timer section is bit complex for beginners and needs careful study.

I won't necessarily disagree.  Many of us have been around AVR8 timers for years.

 

Tell you what--pick 16-bit timer1 of your AVR model.  Study that, and experiement with the modes.  After that, all timer1's will be [almost] exactly the same.  And the 8-bit timers will [generally] be just degenerate cases.

 

 

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.