Evenly PWM fading a LED

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

Evenly PWM fading a LED

Postby moby » Sat Nov 21, 2020 4:06 am

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).
moby
 
Posts: 4
Joined: Sat Nov 21, 2020 3:52 am

Re: Evenly PWM fading a LED

Postby ric » Sat Nov 21, 2020 5:07 am

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?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Evenly PWM fading a LED

Postby moby » Sat Nov 21, 2020 6:10 am

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
moby
 
Posts: 4
Joined: Sat Nov 21, 2020 3:52 am

Re: Evenly PWM fading a LED

Postby ric » Sat Nov 21, 2020 6:22 am

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?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Evenly PWM fading a LED

Postby moby » Sat Nov 21, 2020 6:41 am

Using MPLab assembler.
moby
 
Posts: 4
Joined: Sat Nov 21, 2020 3:52 am

Re: Evenly PWM fading a LED

Postby ric » Sat Nov 21, 2020 6:45 am

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]
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Evenly PWM fading a LED

Postby moby » Sun Nov 22, 2020 5:57 am

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.
moby
 
Posts: 4
Joined: Sat Nov 21, 2020 3:52 am


Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 10 guests

cron