I explained exactly the steps you need to follow to use PWM, which will require TWO parts at a cost of almost ZERO.
1 – Include the PWM2 module (which I explained where to find).
2 – SetFreq in the PWM2 module. Use the calculator I linked to plug in a realistic value or pull one out of your butt; the PWM2 module will deal.
3 – Set the duty cycle percentage. 0% = 0 volts. 50% = ½ supply voltage, 67% = ⅔ supply voltage. 99% =:supply voltage.
4 – Start PWM.
Geez. It's taken me 10× longer to write this than it would have taken to test it.
A digital pot is not an answer for you. As far as I recall [which believe me, is way too frick'n much], you've never used SPI before. So it's just another layer not to understand.
// import PWM module...
Include "PWM.bas"
// local duty cycle as a percent, 0-100...
Dim Duty As Byte
// main program...
SetFreq(5000)
While true
Duty = 0
Repeat
SetDutyPercent(Duty)
Inc(Duty)
DelayMS(10)
Until Duty > 100
Wend
If you call SetFreq() it'll try and figure out the prescaler and settings.
If the function returns true then it was successful. If it returns false then it couldn't calc a setting to match the given freq.
You can use various calculator programs to figure out the settings and set it up yourself too.
The wiki page shows examples using both methods.
Y'all know you might as well be talking Chinese for all the good your comments will do MrDEB.
The basics. As Nigel said, you need an RC low pass filter. He suggested 1k and 10uF, which gives a cutoff frequency of 15.9 Hz. I might suggest 1k and 1 uF to make the capacitor smaller, which gives a cutoff frequency of 159 Hz.
I just 'bunged in' what I had - I wanted smooth, and settling time isn't any sort of issue for simply setting the contrast of an LCD - the scope shows how smooth it is (no sign of any ripple), and it works perfectly. My intention was to try different values, but it worked so well that I never bothered.
Post #35 wasn't directed at you Nigel Goodwin but rather at MrDEB. If he has taken time to even look at the link, he'd have seen the schematic for the RC filter.