Hi,
I'm sure this is trivial to you experts. But this is my first project, so I'd appreciate if someone could have a quick look at the my code and circuitry to see if I'm on the right track.
All I want is to control the intensity of a high power LED (yes, another bike light project!). Please let me know if there's something I've overlooked.
I'm aware of a small bug (noted in the code), and don't know if I need a capacitor to stabilise the voltage to the Picaxe (see circuit). But there may of course be no end of other mistakes.
Thanks,
Ulrik
'Luxeon Tri-Emitter Dimmer (Bike Light)
'File name:Luxeon rebel controller v0.0.1b.bas
'Author: Ulrik H. Gade
'Heavily inspired by http://successlessness.blogspot.com/2007/05/led-bike-light-project.html (thanks Tom!)
'Absolutely no rights reserved
' pins
symbol led_pin = 2
symbol modeButton = pin4
' variables
symbol mode = b0
symbol led_duty = w5
' constants
symbol MAX_MODE = b1
' initialize
mode = 0
MAX_MODE = 5
high led_pin
poke $8F,%01010000 ' Reduce clock speed to 2 MHz by poking the OSCCON register (else PWM freq too high + save power)
main:
if modeButton = 1 then
if mode < MAX_MODE then
mode = mode + 1 'Minor bug: led_pin goes high in debugger at this point (why?)
else
mode = 1
endif
gosub changeMode
pause 200
endif
goto main
changeMode:
'BuckPuck LED driver requires inverse control signal, i.e. shorter duty cycle => brighter light
select mode
case 1 ' 25%
led_duty = 750
gosub setPWM
case 2 ' 50%
led_duty = 500
gosub setPWM
case 3 ' 75%
led_duty = 250
gosub setPWM
case 4 ' 100%
pwmout led_pin off
low led_pin
case 5 ' off
pwmout led_pin off
high led_pin
endselect
return
setPWM:
pwmout led_pin, 249, led_duty ' PWM base frequency 2 kHz
poke $12, $06 'reduce PWM frequency by 1/16 to 125 Hz
return
I'm sure this is trivial to you experts. But this is my first project, so I'd appreciate if someone could have a quick look at the my code and circuitry to see if I'm on the right track.
All I want is to control the intensity of a high power LED (yes, another bike light project!). Please let me know if there's something I've overlooked.
I'm aware of a small bug (noted in the code), and don't know if I need a capacitor to stabilise the voltage to the Picaxe (see circuit). But there may of course be no end of other mistakes.
Thanks,
Ulrik
'Luxeon Tri-Emitter Dimmer (Bike Light)
'File name:Luxeon rebel controller v0.0.1b.bas
'Author: Ulrik H. Gade
'Heavily inspired by http://successlessness.blogspot.com/2007/05/led-bike-light-project.html (thanks Tom!)
'Absolutely no rights reserved
' pins
symbol led_pin = 2
symbol modeButton = pin4
' variables
symbol mode = b0
symbol led_duty = w5
' constants
symbol MAX_MODE = b1
' initialize
mode = 0
MAX_MODE = 5
high led_pin
poke $8F,%01010000 ' Reduce clock speed to 2 MHz by poking the OSCCON register (else PWM freq too high + save power)
main:
if modeButton = 1 then
if mode < MAX_MODE then
mode = mode + 1 'Minor bug: led_pin goes high in debugger at this point (why?)
else
mode = 1
endif
gosub changeMode
pause 200
endif
goto main
changeMode:
'BuckPuck LED driver requires inverse control signal, i.e. shorter duty cycle => brighter light
select mode
case 1 ' 25%
led_duty = 750
gosub setPWM
case 2 ' 50%
led_duty = 500
gosub setPWM
case 3 ' 75%
led_duty = 250
gosub setPWM
case 4 ' 100%
pwmout led_pin off
low led_pin
case 5 ' off
pwmout led_pin off
high led_pin
endselect
return
setPWM:
pwmout led_pin, 249, led_duty ' PWM base frequency 2 kHz
poke $12, $06 'reduce PWM frequency by 1/16 to 125 Hz
return
Attachments
-
15.9 KB Views: 25