I've tried PWM and bit banging, and achieve quite reasonable results bit-banging with the unrefined code snippet below
My LED is on Pin B.3.
In practice, making PERIOD 255 produces noticable flicker at 32MHz. (As the cycle frequency is too low.)
255 is the maximum period with Byte-sized variables, remember.
PERIOD=64 gives very good control of brightness levels and very little flicker.
I can see peripheral flicker at PERIOD=128. OK for a static light.
As a fun thought, the FASTER these loops run, the better the DIMMEST light level.
(As the ON time is smallest) I've noticed we can 'see' *tiny* ON times with LEDs.
#Picaxe 14M2
#No_data
symbol Mark=b1
symbol Space=b2
symbol period=b3
Main:
LET DIRSB=%111111 : LET DIRSC=010111
LET period=128
setfreq m32
DO
Fade_up_down:
'Fade B.3 LED up
Mark=0
do while Mark<= period 'Mark gently increases as ...
let Space=period-Mark '... Space decreases
high b.3
pause Mark 'Mark in period
low b.3 'all off for Space time0
pause Space 'Space in period
inc Mark
loop
'Fade B.3 LED down again
let Space=0
do while Space <= period 'Now increase Space
let Mark=period-Space '... as Mark decreases
high b.3
pause Mark
low b.3
pause Space
inc Space
loop
loop
As you'll see, the MARK and SPACE times are INCremented - so changed by only 1. This gives the slowest change in brightness. You could change 'inc mark' to a 'let mark = mark+delta' if you wanted to change brightness more quickly.
(... but look out for mathematical underflow and overflow (which has caught me out). In Picaxe-world, adding 3 to 254 in a byte variable produces the answer 2, which mucks up endstop tests in loops.)
Notice that this code is 'in-line' and will 'lock-out' other code while its running. Parallel tasks solve this, but parallel task code design is far more tricky. If you want to do Pulse-Width Modulation on more than 1 pin using bit-banging - the code needs careful thought and is not simple.
PWMOUT works far better for controlling brightness, and it runs in the background, once set up.
Matt Lee