Chat freely about anything...

User avatar
By scargill
#20792 1.12 SDK - PWM high res... wonderful - it works - but... I cannot get 100% duty cycle. I'd say 90% at most. Above that the duty cycle value has no effect. I'm using 1024 for the frequency.

Any ideas anyone?
User avatar
By FlyingHacker
#20858 I have only tried it with the Arduino IDE, but compiling C to the firmware... There the PWM is 10 bit where 0 is the low and 1023 is the high. On my scope I see 100% duty cycle at 1023.

Some of that code above seems to be 8bit. (Max of 255)

Note also if you use a variable to hold the value make sure it is greater than 8bits to hold a value of 1023.

Hope that helps.
User avatar
By tytower
#20861 Here is an ArduinoIDE example but 8 bit and so 0-255 so you can do the above adjustments . It has to be on a declared PWM pin though.
If you have an arduino board you can just upload it to the board , stick an LED and resistor on the 9 pin .
/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
User avatar
By FlyingHacker
#20867 I ran that same fade test on gpio2 using the Arduino IDE. I only got 25% duty cycle at 8 bit. I had to go to 1023.

Of course if you use the above with 1023 as a max it will flicker at the ends because the number will actually go over 1023 since 1023 is not divisible by 5.

This is actually pretty dumb code (it is from the examples). It should instead check if the brightness is *greater than* 1023 (or 255 if that is what you want) or less than zero. It should them clamp the value of brightness to 0-1023 (or 0-255 if you want 8 bit). Because if the step size is not a factor of the maximum value it will not work right.