// bright is initial ON time
// dimmer is initial OFF time
// stop is end value (loop until bright = stop)
// A, B, C, and D are port values
Sub Fade(bright as word, dimmer as word, stop as word, A as byte, B as byte, C as byte, D as byte)
dim dir as boolean
// figure out direction and account for 'repeat-until' end
if (bright < dimmer) then
dir = true // increase ON time
stop = stop + 1
else
dir = false // increase OFF time
stop = stop - 1
endif
repeat
PORTA = A // ON time
PORTB = B
PORTC = C
PORTD = D
delayus(bright)
PORTA = 0 // OFF time
PORTB = 0
PORTC = 0
PORTD = 0
delayus(dimmer)
if (dir) then
bright = bright + 1 // increase ON time
dimmer = dimmer - 1 // decrease OFF time
else
bright = bright - 1 // decrease ON time
dimmer = dimmer + 1 // increase OFF time
endif
until (bright = stop)
end sub
main:
while (true)
// fade brightness up
Fade(1, 5000, 3000, %11001111, %00000111, %00010001, %10001110)
// fade brightness down
Fade(5000, 1, 1, %11001111, %00000111, %00010001, %10001110)
end while