Page 1 of 1

PWM LED question

Posted: Tue Jun 16, 2009 3:44 pm
by JWalthall
I am a newbie here using flowcode and I have a question on the code that I have written. I am ramping up a LED flashing it and ramping it back down . My problem is that the LED still has a low glow after it has been ramped down. Is this a code issue or a hardware issue. I am driving the LED from the pic. I am using a 12F629. Code attached below. Also is there a better way to get these same results so the code is not so long. I would really like to make the ramp up smoother.


Thanks !

J Walthall

Re: PWM LED question

Posted: Tue Jun 16, 2009 4:04 pm
by Benj
Hello

You should find this program behaives a lot better. I have changed the way you were changing the brighness values as well as modifying the interrupt routine slightly to give a better scalable operation. When your LED ramps down now it should be 100% off.

Re: PWM LED question

Posted: Tue Jun 16, 2009 4:19 pm
by Benj
Hello

Quick note that if the LED is coming on or off too fast then you can up the delay in the Ramp macros from 2ms to 5ms or higher. Just tried it on a ECIO and it looks really good 8)

Re: PWM LED question

Posted: Tue Jun 16, 2009 4:26 pm
by JWalthall
Thanks Benj. Your support is the best ! Thanks a bunch!

Regards,

J Walthall

Re: PWM LED question

Posted: Tue Jun 16, 2009 5:18 pm
by JWalthall
Benj,

Thanks for the new code but I have one more question. In my orginal code I ramped the led up to 50 % then flashed it before ramping back down. I tried to put the flash in the main code but that didn't work so I then tried it in the ramp up macro but I do not get the flash . can you steer me in the right direction .

Thanks again,

J Walthall

Re: PWM LED question

Posted: Wed Jun 17, 2009 9:37 am
by Benj
Hello

I have now added a third macro to allow for a single flash. I have called it multiple times from the main so you can see multiple flashes. I have also provided parameters for the ramp macros and flash macros to define the level you wish to ramp up or down to or to define that levels used for the flash. This would allow you now to ramp up or down to any value rather then the predefined 100 and 0 that was specified before.

Re: PWM LED question

Posted: Wed Jun 17, 2009 1:19 pm
by JWalthall
Thanks Benj,

I tried to do just what you did last night and I could not get the flash macro to appear in the call stack. What did I do wrong in the creation of the macro? I really appreaciate all of the work that you do on this forum , but I really do wish that there was a Flowcode for dummies video or book that would explain how to use the functions of flowcode. I spend my working days designing molds and programming CNC machines this is the most frustrating thing that I have tried to learn. Once again thanks for all of the help !!


Cheers,

J Walthall

Re: PWM LED question

Posted: Tue Aug 21, 2012 5:01 pm
by JanB
This PWM_LED_12F629.fcf works perfectly for me and is just what I’m looking for but I have a small problem with it.

I disabled the flash macro and added three more led outputs.

LED0 on port A0 (GP0) as in this .fcf
LED1 on port A5 (GP5/OSC1)
LED2 on port A2 (GP2/INT)
LED3 on port A4 (GP4/OSC2)

The problem is that LED3 will not fade / dim. This is the only LED what stay continually ON.

When I load the original program in the 12f629, LED0 will fade and LED3 is also continually ON. For what I can see in this file is that LED3 is not even programmed to do something :-)

Re: PWM LED question

Posted: Tue Aug 21, 2012 5:29 pm
by Enamul
Hi,
Have you checked your configuration option as I don't find any reason of showing unwanted behaviour in GPIO<4>. In Ben's program, he is using external clock which I think you are not using. In your case, oscillator should be "Internal RC No Clockout". And clock speed should be 4 MHz.

Re: PWM LED question

Posted: Tue Aug 21, 2012 5:42 pm
by JanB
Enamul wrote:Hi,
Have you checked your configuration option as I don't find any reason of showing unwanted behaviour in GPIO<4>. In Ben's program, he is using external clock which I think you are not using. In your case, oscillator should be "Internal RC No Clockout". And clock speed should be 4 MHz.
Thanks Enamul, this was the trick ! :-)

All four leds are now working as designed to work.

Re: PWM LED question

Posted: Tue Aug 21, 2012 5:58 pm
by Enamul
Hi,
Glad to know that it works now. Thanks for letting us know.

Re: PWM LED question

Posted: Fri Nov 08, 2013 3:45 pm
by Drakkor
Benj, Hello I am using an example of code that you wrote for ramping an LED up and down. It uses tmr0, and macros ramp up, ramp down and flash. I really am trying to see where the duty cycle is being created here? I hate when I know something works but not how it actually happens. Thanks.

Re: PWM LED question

Posted: Fri Nov 15, 2013 11:39 am
by Benj
Hello,

Right ok looking at the program the interrupt fires at 19200Hz on the hardware and as the LED count is incremented on each interrupt 0-100 this gives a PWM period of around 5.2ms. The PWM duty is based on the LED_Brightness value 0-100.

Example LED brightness responses.
0 = 0% on (0ms) , 100% off (5.2ms)
10 = 10% on (0.52ms), 90% off (4.68ms)
20 = 20% on (1.04ms), 80% off (4.16ms)

For the ramp up and ramp down macros there are 2ms delays so this operation will likely take around 200ms to complete which should give around 40 updates to the LED during this time. Changing this 2ms delay to say 6ms will give a slower transition but should allow all 100 stages to be presented to allow for a smoother transition.

When the LED brightness is ramping up from 0 to 100 it takes around 200 ms to go from 0 to 100. In this time the LED Count will have counted from 0 to 100 roughly 40 times. It is merely the fact that the interrupt timer macro is running at such high frequency that allows the led count to increment really fast while we are incrementing the LED brightness fairly slowly.

If we assume that the timer interrupt runs one hundred times for roughly 4 increments of the LED brightness then we should get something like this for each subsequent period of the PWM.

Brightness = 0
Brightness = 4
Brightness = 8
Brightness = 12
..
Brightness = 100

Currently working on a DIY 15-channel LED Xmas tree for the blog so this should be available in a week or so.