Page 1 of 1

Evenly PWM fading a LED

PostPosted: Sat Nov 21, 2020 4:06 am
by moby
Trying to emulate an incandescent light dimmer with a LED. Using PWM module of a 16F684 (though that is not set in stone)
Problem is for PWM "on" ratios between 25% and 100% the brightness change is not that much but the last 25 % is where it all
happens, the last few steps being rather clunky. It's a bit like using a linear taper pot as a volume control where a log pot
is what is really needed). Need a slow even fade (over a set time, in my case a minute). Working in MPASM (no flash C compiler).
Anyone cracked this nut?.
Cheers
M (very amateur coder).

Re: Evenly PWM fading a LED

PostPosted: Sat Nov 21, 2020 5:07 am
by ric
Yes, that always happens with LEDs, all the variance is in ythe smaller numbers.
You need an exponential lookup table to get even steps.
Are you doing 8 bit PWM, or the full 10 bit?

Re: Evenly PWM fading a LED

PostPosted: Sat Nov 21, 2020 6:10 am
by moby
Currently using 8 but, which looks great on an oscilloscope but lousy on a LED.
Not sure how to implement a 10 bit fade. Very amateur pic coder.
Cheers, M

Re: Evenly PWM fading a LED

PostPosted: Sat Nov 21, 2020 6:22 am
by ric
I assume you are just writing your 8 bit value to CCPR1L.
Bits 5 and 4 in CCP1CON give you another two least significant bits to form the 10 bit value.
Are you using C or Assembler?

Re: Evenly PWM fading a LED

PostPosted: Sat Nov 21, 2020 6:41 am
by moby
Using MPLab assembler.

Re: Evenly PWM fading a LED

PostPosted: Sat Nov 21, 2020 6:45 am
by ric
ok, it would be easier in C. How are you at handling 16 bit values in assembler?
Have another read of the PWM chapter in the datasheet, and what the "DC1Bx" bits in that register do.
Maybe try just using your current 8 bit values, but put the top 6 in CCPR1L, and the bottom 2 in DC1B[1:0]

Re: Evenly PWM fading a LED

PostPosted: Sun Nov 22, 2020 5:57 am
by moby
Ended up by kludging it - squaring the PWM value and using the upper byte. Now halfway down the PWM value is 25% instead of 50%.
Looks far better.