Skip to main content
Arduino

Timeline for Increase PWM bit resolution

Current License: CC BY-SA 3.0

16 events
when toggle format what by license comment
Aug 1, 2022 at 7:20 comment added Edgar Bonet @Dima: Probably, but that won't be simple.
Aug 1, 2022 at 3:01 comment added Dima I'm trying to figure out if PWM resolution on an Atmega168P can be increased on three pins. From what I understand, the 16-bit timer only works with two pins. Is there maybe some way to use two full cycles of the 8-bit timer, using a variable that keeps track of if it is on cycle one or two? A 50% duty cycle would be on for one 8-bit cycle and off for the second.
Mar 7, 2017 at 18:54 comment added davivid @Edgar Bonet: Thanks for explaining that - the workaround works for me.
Mar 7, 2017 at 17:34 comment added Edgar Bonet @davivid: Yes, you cannot have a zero duty cycle. analogWrite16(pin, val) gives a duty cycle of (val+1)/ICR1. As a workaround, Arduino's analogWrite() does if (val == 0) digitalWrite(pin, LOW); else if (val == 255) digitalWrite(pin, HIGH);. But then you cannot get a duty cycle of 1/ICR1...
Mar 7, 2017 at 17:03 comment added davivid @Edgar Bonet This is great, however I can't seem to fully turn off an LED. I'm using ICR1 = 0x03FF and at 0 I'm seeing a tiny pulse on the scope enough to light the led. Any ideas?
Jul 13, 2015 at 14:03 comment added Edgar Bonet @FlorinAndrei: I do not know the 32u4, but according to the summary datasheet, it should work, as it has two 16-bit timers and "Four PWM Channels with Programmable Resolution from 2 to 16 Bits".
Jul 13, 2015 at 6:48 comment added Florin Andrei Fantastic answer! But would this work with an ATmega32u4 - as seen on the Arduino Micro?
Jul 1, 2015 at 15:36 comment added Edgar Bonet Remove _BV(PB1) and _BV(COM1A1) from setupPWM16(), then change change analogWrite16() so that it only works on pin 10.
Jul 1, 2015 at 10:02 comment added KoenR Am I able to use this with a CAN-bus shield which CS is on port 9? If so, is it only changing this line of code: DDRB |= _BV(PB1) | _BV(PB2);
Jun 18, 2015 at 8:59 vote accept KoenR
Jun 18, 2015 at 8:18 comment added KoenR Thank you for the explanation. Edited my question so that it covers these mistakes. So other people can see it directly. Thank you!
Jun 18, 2015 at 7:44 comment added Edgar Bonet @KoenR: No, the prescaler has no effect on the resolution, it's purpose is to slow down the counting. Setting the prescaler to 8 will give you a PWM frequency of 30.5 Hz. If you want 13 bit resolution, set ICR1 to 0x1fff, then your frequency will be 1953 Hz (F_CPU/(TOP+1)) with a prescaler at 1.
Jun 18, 2015 at 6:51 comment added KoenR Wow thank you very much, this is exactly what I need. I want my PWM resultion to be the same as my Sensor Resolution. If I change your code to <look at my edit> would it be a 13-bit resultion? If so, what would the frequency be? I will be driving a DC motor with it so 244Hz will be a bit less I guess
Jun 17, 2015 at 19:19 history edited Edgar Bonet CC BY-SA 3.0
Structured the example code.
Jun 17, 2015 at 18:17 history edited Edgar Bonet CC BY-SA 3.0
+ example code.
Jun 17, 2015 at 16:19 history answered Edgar Bonet CC BY-SA 3.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /