' Name : LED_flasher_04.BAS ' Author : Christopher Good ' Notice : Copyright (c) 2013 ' : All Rights Reserved ' Date : 14 Jan 2013 ' Version : 1.0 ' Notes : Free source code. Compile with Proton IDE Lite (also free). Program your PIC with IC-Prog (also free). ' ' Pin8 = Ground - tie to common, connect to pin 1 via .1uF cap ' Pin7 = GPIO.0 - i/o [out] - control headlight1 ' Pin6 = GPIO.1 - i/o [out] - control headlight2 ' Pin5 = GPIO.2 - i/o [out] - control headlight3 ' Pin4 = GPIO.3 - input only - RC PWM signal - tie to receiver via 4.7k ohm resistor ' Pin3 = GPIO.4 - i/o [in] - RC PWM signal - tie to receiver via 4.7k ohm resistor ' Pin2 = GPIO.5 - i/o [out] - control clock pulse to 4017 decade counter ' Pin1 = Vcc - tie to +5v, connect to pin 8 via .1uF cap ' outputs: ' GPIO.0, GPIO.1, and GPIO.2 control power transistors that turn on/off headlight LEDs ' GPIO.5 controls a clock pulse sent to a 4017 decade counter that cascades through 10 LED light sequences ' inputs: ' GPIO.3 is an input from an RC receiver, which controls the variable delay time between each pulse: ' PWM = 1.00 - 2.00 ms --> 440 ms to 40 ms delay time ; short PWM pulse = long delay ' GPIO.4 is an input from an RC receiver, which controls the state of the LED headlights: ' PWM = < 1.25 ms --> all LED headlights off ' PWM = 1.25 - 1.50 ms --> all LED headlights turn on/off together ' PWM = 1.50 - 1.75 ms --> headlights alternate on/off (sequence through 1..2..3..2..1) ' PWM = > 1.75 ms --> all LED headlights on Device 12C508 Config INTRC_OSC, MCLRE_OFF, WDT_OFF, CP_OFF ' set config fuses OPTION_REG.5 = 0 ' clock source internal Dim Pulselen1 As Byte ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms dim Pulselen2 as byte ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms Dim DelayTime As Word ' DelayTime can be 50 - 410 ms Dim Mask as byte ' temp holder for output on GPIO Dim Direction as bit ' Direction = (0 = going high, 1 = going low) Clear ' set all variables = 0 trisio = %011000 ' set GPIO.3 and GPIO.4 as inputs gpio = %000000 ' turn off all outputs ReadPWM: Pulselen1 = PulsIn GPIO.3, High ' pin 4 - read high pulse length, times out after .65535 seconds Pulselen2 = pulsin GPIO.4, High ' pin 3 - read high pulse length, times out after .65535 seconds if pulselen1 < 100 then pulselen1 = 100 if pulselen1 > 200 then pulselen1 = 200 delaytime = 40 + ((200 - pulselen1) * 4) ' 40 + ( 100 to 0) * 4 = 440 to 40 ms If Pulselen2 < 125 then ' if < 1.25 ms, then turn all headlights off mask = %000 elseif pulselen2 > 124 and pulselen2 < 150 then ' else if 1.25 to 1.50 ms, then if mask = %111 then ' if all headlights on, then mask = %000 ' turn all headlights off else ' else (default state) mask = %111 ' turn all headlights on = start of sequence endif elseif pulselen2 > 149 and pulselen2 < 176 then ' else if 1.50 to 1.75 ms, then if mask = %001 or mask = %100 then ' if either end bit is set, then if mask = %001 then Direction = 0 ' figure out if direction is going high if mask = %100 then direction = 1 ' figure out if direction is going low Mask = %010 ' and turn middle bit on elseif mask = %010 then ' if middle bit is on if direction = 0 then mask = %100 ' if going high, then set high low bit if Direction = 1 then mask = %001 ' if going low, then set the low bit else mask = %001 ' turn 001 on = start of sequence endif else ' default state = all headlights on Mask = %111 endif GPIO = %100000 | Mask ' clock pulse high + mask delayms 5 ' wait 5 ms gpio = %000111 & Mask ' clock pulse low + mask delayms delaytime ' wait variable time goto readpwm