Fast PWM resolution problem? Not sure how to solve this...

Hey all,

I am using an atmega328p clocked to 20mhz. Below I have the following code that seems to mess up the light output control for dimming. I have a hunch of what the issue is, but I dont know how to manipulate it.

So the problem is, as a test, I had 3 mosfets connected to pwm pins 3,5,6 to control the brightness of some LED's to ramp up and down. For some reason, on the descent it does this jittery repeated half cycle. The power source is a buck converter that switches at 175khz (much faster than ~10khz), so I didn't think that was the problem.

This all works as expected with the prescalar set to the default values. (ramps up and down smoothly). with the frequencies of ~1khz and ~500hz respectively.

  //set timer0 to prescalar of 8, pwm frequency of pin 5 and 6 to 9765hz fast pwm method
  //millis and delay are now WRONG!!!
  TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00); 
  TCCR0B = _BV(CS01);

  //set timer2 to prescalar of 8, pwm frequency of pin 3 and 11 set to 9765hz fast pwm method
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(CS21);

I have read something about the output compare registers OCR0/2A and OCR0/2B. I think that is what I want to manipulate, but I don't know why or how.

I get any color correctly, it just jitters when fading down instead of being smooth, any thoughts?

Kind of felt dumb, but I guess I work things out when I know people are listening, haha.

Partial solved by using direct timer compare register manipulation instead of using analogwrite:

Timer output Arduino output Chip pin Pin name
OC0A 6 12 PD6
OC0B 5 11 PD5
OC1A 9 15 PB1
OC1B 10 16 PB2
OC2A 11 17 PB3
OC2B 3 5 PD3

I say partially, because I dont understand why analogwrite makes the output jitter. Where do I go to find how this function works?

Here's a tutorial on timers and the source code for analogWrite(). analogWrite()

The issue you report is not clear to me, and analogWrite() should use the timer as set up for frequency and mode. analogWrite(pin, val) is only attaching the output pin as clear on match and sets the output compare register to the value.

Please post complete versions of the code which jitters, and the code which works properly if you want assistance in getting to the bottom of the anomaly you see.

Without seeing the whole sketch there's no way to know what's happening.

daviddein:
I say partially, because I dont understand why analogwrite makes the output jitter. Where do I go to find how this function works?

analogWrite plays with the timers.

Look in wiring_analog.c