Author Topic: ATMega: Getting full 8bit pwm res for only a subset of the full cycle  (Read 668 times)

0 Members and 1 Guest are viewing this topic.

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7691
  • Country: nz
I would like to generate a 10-30khz PWM signal with a span between 22% and  64% but still get full 8bit resolution.

I could maybe do this by using two timers one for PWM and another for adding low and high dead zones of the right length.
But before i spend all night coding this i thought i would ask if anyone knows of any better tricks to achieve this.

MCU is ATMega328p at 8Mhz

The way i currently plan to do it is this
-Setup TIM2 timer to fire an interrupt on COMP1 and COMP2 compare interrupts
-Setup TIM0 timer for PWM in one shot mode (using the trick of setting TOP below MATCH so it never fires unless forced to see https://hackaday.com/2015/03/24/avr-hardware-timer-tricked-into-one-shot

-Use the interrupts to create a fixed amount of high/low dead time.

When COMP1 fires the PWM timer one shot is started and one cycle of pwm is produced at whatever pwm value i needed.
When COMP2 fires the PWM will be over and the output forced low.
TIM2 will overflow and start the cycle again.

There are some issues with this.
- At 31khz i am generating lots of interrupts. The handlers will only set maybe 2 registers, so not too bad but still a lot of cpu overhead.
- I'm not sure how i will get the two timers running at the right speed to make it work. I may have to do the high and low dead time separately instead of within a single TIM2 cycle. (using one COMP). Then both timers can run at the same speed.

Any thoughts on a better way to do this on AVR?

[attachimg=1]

« Last Edit: August 08, 2019, 07:57:47 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 614
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #1 on: August 08, 2019, 10:15:12 am »
I would like to generate a 10-30khz PWM signal with a span between 22% and  64% but still get full 8bit resolution.
8e6/3e4=266.(6) so with this clock it is not possible to have >=256 steps in between 22% and 64% at 30kHz.
Either use 16-bit timer with TOP set to 799 or use 8-bit timer to emulate higher resolution timer. 10kHz should be doable.
 

Online HwAoRrDk

  • Frequent Contributor
  • **
  • Posts: 762
  • Country: gb
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #2 on: August 08, 2019, 02:28:48 pm »
Yes, is there a reason why you can't use the 16-bit Timer 1?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7691
  • Country: nz
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #3 on: August 09, 2019, 06:03:13 am »
I can use it, but because it's 16bit its PWM freq is much slower than 8bit timers.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 620
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #4 on: August 09, 2019, 07:34:58 am »
To get 256 counts from 22% to 64% the count for 100% must be 256/(64%-22%) = 610. At 30kHz that is 30,000*610 clocks = 18.3MHz. But your ATmega328p is only clocked at 8MHz, so it can't be done. The best you can do is a little over 13kHz.
 
The following users thanked this post: Psi

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 516
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #5 on: August 09, 2019, 10:23:07 am »
So just run at 20Mhz.

30Khz-
20Mhz/30khz = 666
timer1 mode 15 OCR1A=TOP=666
OCR1B=match (non-inverting mode)
min = 666*0.22 = 146
max = 666*0.64 = 426
range = 425 - 146 = 279
set OCR1B 146-425

10Khz
20Mhz/10khz = 2000
timer1 mode 15 OCR1A=TOP=2000
min = 2000*0.22 = 440
max = 2000*0.64 = 1280
range = 1280-440 = 840
set OCR1B 440-1280

both OCR1A(TOP) and OCR1B are double buffered in fast pwm mode

no isr required.

edit- and of course -1 those OCRx numbers
« Last Edit: August 09, 2019, 12:10:21 pm by cv007 »
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 614
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #6 on: August 09, 2019, 11:31:28 am »
I can use it, but because it's 16bit its PWM freq is much slower than 8bit timers.
It seems to me you did not RTFM timers secton.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7691
  • Country: nz
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #7 on: August 09, 2019, 12:31:45 pm »
I can use it, but because it's 16bit its PWM freq is much slower than 8bit timers.
It seems to me you did not RTFM timers secton.

It's not going to run any faster than the 8bit timer. Timer clock is still maxed out at 1:1 with 8mhz system clock
The 328p is not one of those AVRs which has the internal 64mhz timer clock.
« Last Edit: August 09, 2019, 12:34:04 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1305
  • Country: gb
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #8 on: August 09, 2019, 02:20:10 pm »
To get 256 counts from 22% to 64% the count for 100% must be 256/(64%-22%) = 610. At 30kHz that is 30,000*610 clocks = 18.3MHz. But your ATmega328p is only clocked at 8MHz, so it can't be done. The best you can do is a little over 13kHz.

So Bruce's calculations satisfy your 10-30kHz requirement.  Not trying to steal Bruce's thunder here, just pointing it out again.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7691
  • Country: nz
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #9 on: August 10, 2019, 09:57:51 am »
To get 256 counts from 22% to 64% the count for 100% must be 256/(64%-22%) = 610. At 30kHz that is 30,000*610 clocks = 18.3MHz. But your ATmega328p is only clocked at 8MHz, so it can't be done. The best you can do is a little over 13kHz.

Thanks
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline PCB.Wiz

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: au
Re: ATMega: Getting full 8bit pwm res for only a subset of the full cycle
« Reply #10 on: August 12, 2019, 10:25:16 pm »
I would like to generate a 10-30khz PWM signal with a span between 22% and  64% but still get full 8bit resolution.
..
MCU is ATMega328p at 8Mhz

As mentioned above those are mutually exclusive numbers.
If you want best granularity, you needs highest SysCLK.

You will need to run the AVR at 20MHz, (external oscillator?) or choose an AVR clone that runs at 32MHz or choose a MCU that runs at 50Mhz...

20MHz gives you 280 steps between 22% and 64% at 30kHz
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf