' Name : pwm_generator.BAS ' Author : Christopher Good ' Notice : Copyright (c) 2013 ' : All Rights Reserved ' Date : 2/4/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 .1MF cap ' Pin7 = GPIO.0 - i/o - LED #1 - flash0 ' Pin6 = GPIO.1 - i/o - LED #2 - flash1 ' Pin5 = GPIO.2 - i/o - PWM out - 1 to 2 ms ' Pin4 = GPIO.3 - input only - Switch down ' Pin3 = GPIO.4 - i/o - Switch up ' Pin2 = GPIO.5 - i/o - Switch display ' Pin1 = Vcc - tie to +5v, connect to pin 8 via .1MF cap Device 12C508 Config INTRC_OSC, MCLRE_OFF, WDT_OFF, CP_OFF ' set config fuses OPTION_REG.5 = 0 ' clock source internal Dim Pulselen As Byte ' Pulselen can be 0 - 255, 100 = 1 ms, 200 = 2 ms Dim Value10 As Byte dim Value1 as byte dim i as byte Pulselen = 50 ' 100 is added later; default = 150 = 1.5 ms Loop: input gpio.4 If gpio.4 = 1 and pulselen < 100 Then Pulselen = Pulselen + 1 GoSub flash0 EndIf input gpio.3 If gpio.3 = 1 and pulselen > 0 Then Pulselen = Pulselen - 1 GoSub flash1 EndIf input gpio.5 If gpio.5 = 1 Then Value10 = Pulselen / 10 Value1 = pulselen - (value10 * 10) if value10 > 0 then for i = 1 to value10 gosub flash0 next i endif if value1 > 0 then for i = 1 to value1 gosub flash1 next i endif endif PulsOut GPIO.2, 100 + Pulselen, High DelayMS 15 GoTo Loop flash0: high GPIO.0 DelayMS 125 low GPIO.0 delayms 125 Return flash1: high GPIO.1 DelayMS 125 low GPIO.1 delayms 125 Return