PWM LED question
PWM LED question
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
Thanks !
J Walthall
- Attachments
-
- PWM_LED_12F629.fcf
- (8 KiB) Downloaded 1154 times
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PWM LED question
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.
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.
- Attachments
-
- PWM_LED_12F629.fcf
- (7 KiB) Downloaded 1299 times
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PWM LED question
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
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

Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: PWM LED question
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
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
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PWM LED question
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.
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.
- Attachments
-
- PWM_LED_12F629.fcf
- (8 KiB) Downloaded 1278 times
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: PWM LED question
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
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
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
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

- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
Re: PWM LED question
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.
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
Thanks Enamul, this was the trick !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.

All four leds are now working as designed to work.
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
Re: PWM LED question
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.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PWM LED question
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel