ATMega: Getting full 8bit pwm res for only a subset of the full cycle - Page 1
Forum section: electronics / general

author: Psi
author_rank: Super Contributor
timestamp: August 08, 2019, 07:43:55 am
content: 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 8MhzThe 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 toseehttps://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?

author: Brutte
author_rank: Frequent Contributor
timestamp: August 08, 2019, 10:15:12 am
content: Quote from: Psi on August 08, 2019, 07:43:55 am8e6/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.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: August 08, 2019, 02:28:48 pm
content: Yes, is there a reason why you can't use the 16-bit Timer 1?

author: Psi
author_rank: Super Contributor
timestamp: August 09, 2019, 06:03:13 am
content: I can use it, but because it's 16bit its PWM freq is much slower than 8bit timers.

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: August 09, 2019, 07:34:58 am
content: 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.

author: cv007
author_rank: Frequent Contributor
timestamp: August 09, 2019, 10:23:07 am
content: So just run at 20Mhz.30Khz-20Mhz/30khz = 666timer1 mode 15 OCR1A=TOP=666OCR1B=match (non-inverting mode)min = 666*0.22 = 146max = 666*0.64 = 426range = 425 - 146 = 279set OCR1B 146-42510Khz20Mhz/10khz = 2000timer1 mode 15 OCR1A=TOP=2000min = 2000*0.22 = 440max = 2000*0.64 = 1280range = 1280-440 = 840set OCR1B 440-1280both OCR1A(TOP) and OCR1B are double buffered in fast pwm modeno isr required.edit- and of course -1 those OCRx numbers

author: Brutte
author_rank: Frequent Contributor
timestamp: August 09, 2019, 11:31:28 am
content: Quote from: Psi on August 09, 2019, 06:03:13 amIt seems to me you did not RTFM timers secton.

author: Psi
author_rank: Super Contributor
timestamp: August 09, 2019, 12:31:45 pm
content: Quote from: Brutte on August 09, 2019, 11:31:28 amIt's not going to run any faster than the 8bit timer. Timer clock is still maxed out at 1:1 with 8mhz system clockThe 328p is not one of those AVRs which has the internal 64mhz timer clock.

author: Buriedcode
author_rank: Super Contributor
timestamp: August 09, 2019, 02:20:10 pm
content: Quote from: Bruce Abbott on August 09, 2019, 07:34:58 amSo Bruce's calculations satisfy your 10-30kHz requirement. Not trying to steal Bruce's thunder here, just pointing it out again.

author: Psi
author_rank: Super Contributor
timestamp: August 10, 2019, 09:57:51 am
content: Quote from: Bruce Abbott on August 09, 2019, 07:34:58 amThanks

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 12, 2019, 10:25:16 pm
content: Quote from: Psi on August 08, 2019, 07:43:55 amAs 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