Duty Cycle =0 & Duty cycle =255, not working

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

Hi guys, i am using atmega16 timer0 for PWM generation.

1) when i use duty cycle = 0%,(i.e. OCR0=0;) i get a small peak in oscilloscope (32us, which should be PWM generated when duty cycle is 1%). 

2) when i use duty cycle = 100%(OCR0 = 255;), i get no result, just 0V at oscilloscope. 

3) when i use OCR0 =254, i get the correct PWM. 

I don't know where i am wrong. Please see the code:

#include <avr/io.h>
#include <delay.h>
int main(void)
{
        
        DDRB = (1<<DDB3); //setting portb3 as output
        OCR0 = 254;// gives correct wave form
	TCCR0=(1<<WGM00) | (1<<COM01) | (0<<COM00) | (1<<WGM01) | (1<<CS02) | (0<<CS01) | (0<<CS00);
	TCNT0=0x00;
	_delay_ms(8000);
	TCCR0 = 0;
	_delay_ms(2000);
	while(1)
	{
	OCR0 = 0;// gives in-correct wave form, PB3 gets on
	TCCR0=(1<<WGM00) | (1<<COM01) | (0<<COM00) | (1<<WGM01) | (1<<CS02) | (0<<CS01) | (0<<CS00);
	TCNT0=0x00;
	_delay_ms(8000);
	TCCR0 = 0;
	_delay_ms(2000);

	OCR0 = 255;// gives in-correct wave form, PB3 is always off
	TCCR0=(1<<WGM00) | (1<<COM01) | (0<<COM00) | (1<<WGM01) | (1<<CS02) | (0<<CS01) | (0<<CS00);
	TCNT0=0x00;
	_delay_ms(8000);
	TCCR0 = 0;
	delay_ms(2000);
	}
}

 

This topic has a solution.
Last Edited: Mon. Jun 13, 2016 - 07:09 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From Datasheet:

The extreme values for the OCR0 Register represents special cases when generating a PWM
waveform output in the fast PWM mode. If the OCR0 is set equal to BOTTOM, the output will be
a narrow spike for each MAX+1 timer clock cycle. Setting the OCR0 equal to MAX will result in a
constantly high or low output (depending on the polarity of the output set by the COM01:0 bits.)

Edit: You may want to consider changing to Phase Correct PWM mode

The extreme values for the OCR0 Register represent special cases when generating a PWM
waveform output in the phase correct PWM mode. If the OCR0 is set equal to BOTTOM, the output
will be continuously low and if set equal to MAX the output will be continuously high for noninverted
PWM mode. For inverted PWM the output will have the opposite logic values.

David (aka frog_jr)

Last Edited: Fri. Jun 10, 2016 - 04:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but at "OCR0=254;" i get actual PWM output, not MAX+1. So in short i don't know where this error of 1 spike is fixed. i.e. at OCR0= 0 ; 1 spike is produced. Like wise at OCR0 = 1; 2 spikes are produced. but at OCR0 = 254; 254 spikes are produced.

Thanks & Regards

Last Edited: Fri. Jun 10, 2016 - 04:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For Fast PWM (WGM01 and WGM00 = 1):

   if MAX=255, there are MAX+1 clocks (0-255) = 256 clocks.

 

For COM01=1 and COM00=0, clear OC0 on compare match, set OC0 at BOTTOM, (non-inverting mode):

   when OCR0 = 0 there is 1 (high) pulse every MAX+1 cycles (1 every 256)

   when OCR0 = 254 there is a compare match and OC0 is cleared to be set again when BOTTOM (0)

   when OCR0 = 255 output is constant

David (aka frog_jr)

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

As you said, the output duty cycle is indeed (OCR0+1)/256.

So are you sure you get, for OCR0=254, 254/256 for the duty cycle and not 255/256?... because for OCR0=255 we get 100% (continuous).

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

nope at OCR0 = 255; no continous output is generated. But at OCR0 = 254, one tick is missing in scope. I think i understand it a bit , Can you point me from where you got 

output duty cycle is indeed (OCR0+1)/256.

Thanks 

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

Please tell us what you get at OCR0=255 instead of the expected continuous output (that is no pulsing; low or high always).

In the case here, OCR0 is simply equivalent to MAX and the period of PWM is 256.

 

For instance, me too I was a bit confused when I tried generating a fast PWM signal for the first time, mainly for OCRx=0.

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

hamzatheman wrote:
nope at OCR0 = 255; no continous output is generated. But at OCR0 = 254, one tick is missing in scope.

so, at 255/255 do you still have the 'scope set up to trigger on an edge?  If so:  you won't see any change till triggered, right?

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

well can you refer to any good text...

Last Edited: Mon. Jun 13, 2016 - 04:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please correct me if i am wrong.  Pulse generation though PWM is not edge triggered and it makes no sense to me. But may be i am using a lipstick. 

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

hamzatheman wrote:
Pulse generation though PWM is not edge triggered and it makes no sense to me. But may be i am using a lipstick.

 

Get out your lipstick, and tell us how you are observing the trace on the 'scope.  If it were me, I'd trigger on an edge to keep the duty cycle on a convenient part of the screen to observe/measure.  When you have 100% duty there is no edge and you 'scope won't trigger on it.

 

So you are watching the trace as you cycle through duty cycles -- and you don't see what you expect at 255/255.  Perhaps because you didn't change the 'scope setup from triggered operation.

 

[I'd use triggered, as mentioned, when doing such observations so that I could mark the points on the screen using my lipstick.]

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

In your first post you state:

hamzatheman wrote:
2) when i use duty cycle = 100%(OCR0 = 255;), i get no result, just 0V at oscilloscope.

Given that the code provided indicates you are using Fast PWM, non-inverting mode. The output at OC0  should be high.

theusch is asking (post #8) how you are observing the signal at OC0. If you are using a scope set to trigger on an edge, there will be no edges when OC0 is not toggling.

 

So are you getting a constant high or constant low when OCR0 = 255?

Is scope set for DC coupling or AC coupling?

What do you see on the scope when you look at a VCC pin? A GND pin?

 

Edit: theusch responded while I was typing and got distracted by "real work"...

David (aka frog_jr)

Last Edited: Mon. Jun 13, 2016 - 04:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi guys,

You are correct the scope i am using is infact detecting edge,

assuming there is a constant +5V at OCR0 =255. I performed another test: i used it as pulse enable to l293D. But the motor did not rotate, but as i decreased the  value of OCR0 to 254, motor starts rotating.

 

I can upload a video, if that could help. 

Thanks and best regards

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

So are you getting a constant high or constant low when OCR0 = 255?

I am getting a constant low at OCR0 = 255, i think that is because  of not setting PORTB |= (1<<PB3); initially 

 

Is scope set for DC coupling or AC coupling?

For coupling,i tried both coupling cases first AC coupling, then DC coupling. But output remains same. 

Last Edited: Mon. Jun 13, 2016 - 05:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I haven't used the venerable Mega16 in a while.  From the datasheet, it is certainly possible that the "special case" for that model and timer, with Fast PWM and OCR0=TOP is that the combination is "full off".  So you get for the 256 possible values:  1/255 to 254/255 for )CR0 values of 0 through 254, and full-off for OCR0 value of 255.

 

So recognize that, and if you choose to use this venerable model for PWM take the operation into account, and move on with life.

 

; "> Note: 1. A special case occurs when OCR0 equals TOP and COM01 is set. In this case, the compare
match is ignored, but the set or clear is done at TOP. See “Fast PWM Mode” on page 77 for
more details.

 

frog_jr pointed that out in post #2.

 

(if the timer follows other similar AVR8 timers, then inverting the PWM may give you full-on but not full-off.  In no case that I know of on an AVR8 can you get Fast PWM with both full-on and full-off with the same setup.)

 

 

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

 take the operation into account, and move on with life

Best advice so far.

;)

Thanks i was just confirming whether i am nuts or my uC is......  

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

So how about inverting the output and subtracting your "percent" from 255. 255-0 is 255 and that inverted will give you low. 255-255 gives you 0 and that inverted will give you 255/256ths "high."

 

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

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

there will always be a problem with an 8 bit timer and simple PWM. If you expect both 0% and 100% there is 257 combinations.

Last Edited: Tue. Jun 14, 2016 - 12:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not really buying this.

 

Your observed behaviour for OCR0 = 0 is correct.

Your observed behaviour for OCR0 = 254 is also correct.

 

Your observed behaviour for OCR0 = 255 is not correct, but I would sooner look at your 'scope setup (as already mentioned) than a previously unknown errata in the m16.  I don't have an m16 lying around to test, but I seriously doubt a flaw in the PWM generation would have gone unnoticed and unreported for over 14 years.

 

There is always the possibility that your m16 is damaged somehow.  Do you have another with which to test?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

joeymorin wrote:
Your observed behaviour for OCR0 = 255 is not correct, ...

From the datasheet quotes above, I cannot fully agree.  Isn't it possible that that model implemented that timer edge condition as "full off" rather than "full on"?  [There is a race at TOP/MAX/compare match; which one wins?]

 

In practice, while I might find it somewhat annoying to not be able to get full-on AND full-off, it isn't a killer.  For e.g. an LED, 1/255 may give a flicker so I'd code inverted to get full-off, and 254/255 can't be distinguished from full-on. 

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.

Last Edited: Tue. Jun 14, 2016 - 03:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the datasheet quotes above, I cannot fully agree.  Isn't it possible that that model implemented that timer edge condition as "full off" rather than "full on"?  [There is a race at TOP/MAX/compare match; which one wins?]

While an interpretation of the datasheet might suggest that it is 'normal' behaviour, it flies in the face of expectations.  Can you recall any thread in the last 14 years describing this problem?  I can only imagine that there would be a flood of threads on this subject if that interpretation were correct.

 

That same wording appears in later AVR models' datasheets, as well (such as the ATmega48/88/168/328), but I know those models work as expected.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

joeymorin wrote:
Can you recall any thread in the last 14 years describing this problem?

Not directly.  But wasn't there a thread maybe a few months ago where someone was concerned about the order of compare match operations?

 

Anyway, I'd have to dig to find a venerable Mega16 to check operation.  (are we sure OP doesn't have a '16A?)

 

 

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.