#picaxe 08M
#freq m8
symbol CYCLES = 10 ' each cycle is ~2.5 msec at 8 MHz
symbol PWM_CHAN1 = 1
symbol PWM_CHAN2 = 2
symbol PWM_CHAN3 = 4
symbol duty1 = b0
symbol duty2 = b1
symbol duty3 = b2
symbol duty_incr = 2
setfreq m8
duty1 = 0
duty2 = 85
duty3 = 170
myloop:
pwm PWM_CHAN1, duty1, CYCLES
pwm PWM_CHAN2, duty2, CYCLES
pwm PWM_CHAN3, duty3, CYCLES
duty1 = duty1 + duty_incr ' wraps around whe reaching 255
duty2 = duty2 + duty_incr ' wraps around whe reaching 255
duty3 = duty3 + duty_incr ' wraps around whe reaching 255
pause 50 ' 25 msec update rate at 8 MHz
goto myloop
#picaxe 28x2
let dirsB = %11111111
let adcsetup = 0
symbol EVENTS_PER_SEC_DIV5 = 180
symbol DUMMY_VAL = 65536 - t1s_8
symbol TIMER_PRELOAD_VAL_DIFF = DUMMY_VAL / EVENTS_PER_SEC_DIV5
symbol TIMER_PRELOAD_VAL = 65536 - TIMER_PRELOAD_VAL_DIFF
symbol pwm_output1 = B.0
symbol pwm_output2 = B.1
symbol pwm_output3 = B.2
' for 20X2 at 64 MHz, try reducing to 8 (a bit more flicker, but twice as many brightness steps)
symbol PWM_INCREMENT = 16
symbol pwm_counter = b0
symbol pwm_duty1 = b1
symbol pwm_duty2 = b2
symbol pwm_duty3 = b3
setfreq em40
' duty cycle values range from 0...255
pwm_duty1 = 240 ' negative duty cycle (larger means darker)
pwm_duty2 = 120 ' negative duty cycle (larger means darker)
pwm_duty3 = 0 ' negative duty cycle (larger means darker)
' don't forget to intialize counter!
pwm_counter = 0
' initialize timer
settimer TIMER_PRELOAD_VAL ' set time preload value
timer = 0xffff ' generate interrupt at next overflow
toflag = 0 ' clear timer overflow flag
setintflags %10000000,%10000000 ' interrupt on timer overflow
eternal_loop:
pwm_duty1 = pwm_duty1 - PWM_INCREMENT
pwm_duty2 = pwm_duty2 - PWM_INCREMENT
pwm_duty3 = pwm_duty3 - PWM_INCREMENT
' can't use pause command for time delays because it would get terminated prematurely by interrupt
for w10 = 1 to 200: next w10
goto eternal_loop
interrupt:
pwm_counter = pwm_counter + PWM_INCREMENT
' small weakness - even with duty cycle = 100% LED will go dark for a short moment
' but this cuts down on execution time, thus allows faster interrupt rate and less flicker
if pwm_counter < PWM_INCREMENT then
low pwm_output1
low pwm_output2
low pwm_output3
else
if pwm_duty1 < pwm_counter then : high pwm_output1 : endif
if pwm_duty2 < pwm_counter then : high pwm_output2 : endif
if pwm_duty3 < pwm_counter then : high pwm_output3 : endif
endif
' gosub timer_setup
' inline code to reduce execution time
timer = 0xffff ' generate interrupt at next overflow
toflag = 0 ' clear timer overflow flag
setintflags %10000000,%10000000 ' interrupt on timer overflow
return
symbol dutyR = b0
symbol dutyG = b1
symbol dutyB = b2
symbol i = b3
symbol pinR = 1
symbol pinG = 2
symbol pinB = 3
' External loop. Set the duty cycle in the range 0 to 15
do
dutyR = 10
dutyG = 5
dutyB = 12
high pinR, pinG, pinB
for i = 0 to 15
if i > dutyR then : low pinR : end if
if i > dutyG then : low pinG : end if
if i > dutyB then : low pinB : end if
next i
loop
#picaxe 28x2
symbol EVENTS_PER_SEC_DIV5 = 1
symbol DUMMY_VAL = 65536 - t1s_8
symbol TIMER_PRELOAD_VAL_DIFF = DUMMY_VAL / EVENTS_PER_SEC_DIV5
symbol TIMER_PRELOAD_VAL = 65536 - TIMER_PRELOAD_VAL_DIFF
symbol pwm_output1 = B.0
symbol pwm_output2 = B.1
symbol pwm_output3 = B.2
symbol PWM_INCREMENT = 8
symbol pwm_counter = b0
symbol pwm_duty1 = b1
symbol pwm_duty2 = b2
symbol pwm_duty3 = b3
symbol pwm_incr1 = b4
symbol pwm_incr2 = b5
symbol pwm_incr3 = b6
setfreq em40
let dirsB = %11111111
let adcsetup = 0
' duty cycle values range from 0...255
pwm_duty1 = 160 ' negative duty cycle (larger means darker)
pwm_duty2 = 80 ' negative duty cycle (larger means darker)
pwm_duty3 = 0 ' negative duty cycle (larger means darker)
pwm_incr1 = 1 ' intially increase intensity
pwm_incr2 = 1 ' intially increase intensity
pwm_incr3 = 1 ' intially increase intensity
' don't forget to intialize counter!
pwm_counter = 0
' initialize timer
settimer TIMER_PRELOAD_VAL ' set time preload value
timer = 0xffff ' generate interrupt at next overflow
toflag = 0 ' clear timer overflow flag
setintflags %10000000,%10000000 ' interrupt on timer overflow
eternal_loop:
pwm_counter = pwm_counter + PWM_INCREMENT
' small weakness - even with duty cycle = 100% LED will go dark for a short moment
' but this cuts down on execution time, thus allows faster interrupt rate and less flicker
if pwm_counter < PWM_INCREMENT then
low pwm_output1
low pwm_output2
low pwm_output3
else
if pwm_duty1 < pwm_counter then : high pwm_output1 : endif
if pwm_duty2 < pwm_counter then : high pwm_output2 : endif
if pwm_duty3 < pwm_counter then : high pwm_output3 : endif
endif
goto eternal_loop
interrupt:
pwm_duty1 = pwm_duty1 - PWM_INCREMENT
pwm_duty2 = pwm_duty2 - PWM_INCREMENT
pwm_duty3 = pwm_duty3 - PWM_INCREMENT
' gosub timer_setup
' inline code to reduce execution time
timer = 0xffff ' generate interrupt at next overflow
toflag = 0 ' clear timer overflow flag
setintflags %10000000,%10000000 ' interrupt on timer overflow
return