Need to generate PWM waveform on 0C3B of atmega328PB

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

I have a Ti DRV10970 motor control IC connected to an atmega328PB.  The OC3B (timer 3 compare output) is wired to the DRV10970 PWM control input.  The problem is that the spec's on this chip require a 15-100khz pwm waveform.  The atmega is being used with the internal RC clock.  I currently have it set at 1mhz, but can change the fuses to run at 8mhz.  I can't seem to get timer 3 to run in Fast PWM mode with output on OC3B. 

Here is a code siglet I've tried:

 

TCCR3A = (1 << COM3B1) | ( 1 << WGM30); //set FPWM mode 5 top = 0xFF output on B pin (COM3B1)
 TCCR3B = (1 << WGM32) | ( 1 << CS30); // set prescaler to 1x
 OCR3B = motorSpeed;
 TIMSK3 = 1<<TOIE3; //to generate an interrupt on overlfow, ISR can reload OCR3B to change duty cycle (is this needed?)

 

ISR(TIMER3_OVF_vect)
{
        OCR3B = motorSpeed;
}

 

Motor speed is an 8 bit variable, a value of 0 should generate narrow pulses, 127 a 50% duty cycle, and 255 a flat line high.

No mater what I set the OCR3B to I only get a low output.  The OC3B pin was set to output low before running this code.  

Right now I'm experimenting on the Arduino IDE, using the minicore board base set to the atmega328pb.   The Arduino analogWrite function on this pin DOES work, but outputs a 300hz waveform (at 1mhz clock) which is WAY too low to meet spec, even if I change the clock fuses to go to 8mhz.

Any ideas?

 

This topic has a solution.
Last Edited: Mon. Apr 27, 2020 - 07:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At 1 MHz, a 15 KHz PWM, only has at most 66 levels (equiv: about 6 bits)...so the resolution will be  poor.  If you need high freq PWM, use one of the AVR's with the PLL peripheral clock multiplier (runs the timers at 85 MHz max).

 

ex: tiny261 has it, there are others.

 

note: mHz is millihertz  MHz is mega

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

I'm stuck with the atmega328pb, need all the pins, hence running with the internal RC clock.  I can clear the divide by 8 clock fuse and run it at 8mhz, and I don't need super accurate setting of duty cycle.  Within 10% will do fine in this application.

Any good examples of how to set up the timer 3 registers?  What I posted didn't work.

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

You don't use IRQs to generate PWM, scrap that IRQ, be sure to set your com3xx bits (see table 19-9) to automatically toggle the output pin under pwm control.

Once you set the pwm registers, you leave them alone & just let the pwm run on and on.  You can change the OCR value to change the duty cycle. 

 

PWM does not functionfrom 0 to 100%, depending on the setup, either it can't go all the way to 0% (within 1 tick) or it can't go all the way to 100%.  There are easy workaround tricks if you need it to cover the entire range of 0 to 100%.

 

You should at least add a 16 MHz xtal or up to 20 MHz ext clock source, so you can gen better high freq pwms...that's a big improvement

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Apr 25, 2020 - 03:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It looks like that's what I was doing (if I leave off the interrupt mask setting)

 

TCCR3A = (1 << COM3B1) | ( 1 << WGM30); //set FPWM mode 5 top = 0xFF output on B pin (COM3B1)
 TCCR3B = (1 << WGM32) | ( 1 << CS30); // set prescaler to 1x
 OCR3B = motorSpeed;

 

ONLY the com3b1 bit should need to be set for compare output b according to table 19-9.  I think I have the WGM bits right as well.

I can get 0% by shutting down the PWM and setting the output bit to zero, 100% by setting it to 1.   I'm not sure of the difference between the inverted and non-inverted modes of setting com3 bits, I thought one setting would get you 0-50% and the other 50-100%.

I'm stuck with the 8mhz RC clock, the pins used for external clock are committed to other I/O in this design.

 

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

 I'm not sure of the difference between the inverted and non-inverted modes of setting com3 bits, I thought one setting would get you 0-50% and the other 50-100%.

It's exactly as named, one is upside down

 

non inverted can go from alllllmost zeeeero to 100%

inverted can go from zero to allllllmost 100%. 

 

You have to set your freq (top value) for pwm, depending on PWM mode.  I can't look it up now.

The pwm is controlled by TWO numbers.  OCR number controls the width (duty).  & typically the ICR register sets the freq

 

IS IT WORKING FOR YOU?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

So far no.  I don't think you can set the ICR if you select the 8,9, or 10 bit fast pwm mode, you're then stuck with TOP values of 256, 512, or 1024.    This means the frequency is fixed, and only the duty cycle is adjustable.  (Maybe my NOT writing this register is the problem?)

My test point wire broke off the board, I'll have to resolder it to back to look fpr the waveform.  Board is SMT so I can't just probe with the scope probe, and a microscope is required to see where to resolder the test point (#30 wire).  Not easy for me!

 

Last Edited: Sat. Apr 25, 2020 - 06:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The OC3B pin was set to output low before running this code.  

Hope it still is or nothing will come out the pin...DDR for PD2  must be set properly. 

 

Try setting OCR3B  = 47  ,do not use a variable..does anything come out?

 

At 8 MHz your freq will be about 31 KHz, you are correct, the freq is not adjustable in mode 5

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Apr 25, 2020 - 08:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No output from pin 0C3B with this code, pin stays low, 

DDRD |= (1 << 2); //set OC3B as output
TCCR3A = (1 << COM3B1) | ( 1 << WGM30); //set FPWM mode 5 top = 0xFF output on B pin (COM3B1)
 TCCR3B = (1 << WGM32) | ( 1 << CS30); // set prescaler to 1x
 OCR3B = 47;

 

But arduino function AnalogWrite(2,50)  produces a waveform at a frequecny of 246 hz (fuses set for 8mhz clock).

 

I think I;ll try this code for timer 1 on an arduino uno and see if that works).
 

 

Last Edited: Mon. Apr 27, 2020 - 04:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SEE THIS FOR A POSSIBLE FIX....chip bug           ...when things make no sense try looking for an errata, or run away

 

I also verified that the Output Compare Modulator issue didn't get fixed.  It is supposed to be enabled only if OC3B *and* OC4B are enabled but it appears that it is enabled if either OC3B *or* OC4B are enabled.  As a consequence of this error, you must write a 1 to bit 2 of PORTD in order to get PWM output on PD2 from either Timer3 or Timer4.  I suspect that the same workaround would need to be used any time you want OC3B or OC4B output on PD2.  If you're actually using the Output Compare Modulator you'd write a 0 or a 1 to bit 2 of PORTD depending on whether you want AND or OR modulation.

 

taken from:

https://github.com/watterott/ATmega328PB-Testing/issues/29

 

Nice to know you could play with this for weeks & never get it to work (not knowing the issue), or end up throwing your PC out the window.

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Apr 27, 2020 - 07:51 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That did it.  I set OC3B high (portd bit high and got a 31khz wave form.

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

Please mark the solution for others that follow!

Thanks

 

FF = PI > S.E.T

 

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

Where does this information appear in the chip errata listings? Does Microchip have a listing for this problem?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

Check the somewhat more extensive recent thread on the topic.

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

 

Check the somewhat more extensive recent thread on the topic.

Well, I don't see it, or don't have the correct terms.  Does microchip actually keep track and post such issues (or former such issues) on their website? Maybe the recent flavor of the chip corrected this, dunno.  I found some old 328pb errata, but htis was 't mentioned.

 

also tried Mchip site....almost useless

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. Apr 28, 2020 - 12:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
Well, I don't see it, or don't have the correct terms.  Does microchip actually keep track and post such issues (or former such issues) on their website? 

https://www.avrfreaks.net/forum/... which links to https://www.avrfreaks.net/commen...

 

I will quote post #28 in its entirety; emphasis mine:

PeterHouse wrote:

Section 23.2 of the 328PB Datasheet dated 10/2015 has the answer.

 

Short Summary: When the timers OC3B or OC4B are used, the PortD2 bit is used to control the combination of the two timer outputs to accomplish a modulated signal.

 

This is a nice feature and it would be even nicer if it was mentioned somewhere else in the datasheet with a reference as to why the OC3B or OC4B might seem to not work.

 

Bottom Line:  It is working exactly as designed.  The only error is a possible omission of information in the datasheet to help the user understand why this single channel when set for simple PWM works differently than the other channels.

 

Recent?  #30 was make on 22-April.

 

See also https://www.avrfreaks.net/forum/... Note from that that a support ticket was opened.

Another hit is https://www.avrfreaks.net/sites/...

 

Now, before you start harrumphing, "what datasheet information is important" is also a very old topic here on this forum, often discussed.  I remember one irate poster that opined, strongly, that all "important" information should be highlighted in the datasheet.  There was no response when asked to point out which sections of a typical datasheet are NOT important and not deserving of bold-face.

 

Given this, how would you word the errata?  "See section 23.2 for how feature x.y works"?

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. Apr 28, 2020 - 12:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good news...loooking at the 2018 datasheet, they mention:

 

For OC3B or OC4B when not using the Output Compare Modulator, PORTD2 must also be set in order to enable the output.

 

It is located in a somewhat poor place & could be more cleanly worded, suggestion:

To enable the OC3B or OC4B outputs, PORTD2 must be set to output a one (if not using the Output Compare Modulator).

This should also be mentioned in the output modulator section (though it could be deciphered from the diagram)

==========

 

HOWEVER, it should definitely be mentioned here, which is going to be looked at by someone trying to fix this condition:

 

– OC3B: Output Compare Match output: The PD2 pin can serve as an external output for the Timer/Counter3 Compare Match B. The PD2 pin has to be configured as an output (DDD2 set (one)) to serve this function. The OC3B pin is also the output pin for the PWM mode timer function.    ....no mention of setting the pin to 1 ????

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!