' Name : Switch_Output.BAS ' Author : Christopher Good ' Notice : Copyright (c) 2021 ' : All Rights Reserved ' Date : 7/21/2021 ' 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 - output - 0 to 40 ms on, invert of that off = (0/25/50/75/100 % duty cyle) -> use RC filter to convert to analog signal (0/1.25/2.5/3.75/5 V) ' Pin6 = GPIO.1 - i/o - Switch 1 = 0% ' Pin5 = GPIO.2 - i/o - Switch 2 = 25% ' Pin4 = GPIO.3 - input only - Switch 3 = 50% ' Pin3 = GPIO.4 - i/o - Switch 4 = 75% ' Pin2 = GPIO.5 - i/o - Switch 5 = 100% ' Pin1 = Vcc - tie to +5v, connect to pin 8 via .1MF cap ' ' RC filter selection: R=21K Ohms, C=10 uF --> Tau (time constant) = .21 seconds Device 12C508 Config INTRC_OSC, MCLRE_OFF, WDT_OFF, CP_OFF ' set config fuses OPTION_REG.5 = 0 ' clock source internal Dim OnTime As Byte Dim OffTime as byte Ontime = 0 'default to 0% duty cycle xtal = 4 'tell compiler the internal oscillator runs at 4 MHz, this is used for the DelayMS command trisio = %111110 Loop: 'loop time = ~40 ms ' if a switch if pressed, set the OnTime duty cycle in ms (if more than one switch is pressed, higher GPIO.x has precedence) if GPIO.1 = 1 then OnTime = 0 ' 0% duty cycle if GPIO.2 = 1 then OnTime = 10 ' 25% duty cycle if Gpio.3 = 1 then OnTime = 20 ' 50% duty cycle if Gpio.4 = 1 then OnTime = 30 ' 75% duty cylce if gpio.5 = 1 then OnTime = 40 ' 100% duty cycle offtime = 40 - ontime 'OffTime is invert of OnTime ' if the duty cycle is <> 0% (i.e. if the output is ever on at all), set the output high and wait for the OnTime if Ontime <> 0 then high GPIO.0 Delayms ontime endif ' if the duty cycle is <> 100% (i.e. if the output is ever off at all), set the output low and wait for the OffTime if offtime <> 0 Then Low GPIO.0 Delayms Offtime endif GoTo Loop