' PIC Sprayer Program for PIC12F683 ' Designed to turn on a water spray for a variable on-time (0-50 seconds) ' between a variable interval time (1 to 120 minutes). ' On time control is read from AN3 ' Off time control is read from AN1 ' Pumped is driven from GP0 ' Indicator LED is connected to GP5 ' Manual operation is switched from GP2 ' Author: Barry Phillips ' Date: 6th July, 2011 ' Rev 2 ' Set up DT_INTS-14 Routine INCLUDE "DT_INTS-14.bas" INCLUDE "ReEnterPBP.bas" ASM INT_LIST macro ' IntSource, Label, Type, ResetFlag? INT_Handler INT_INT, _ManualSwitch, PBP, yes endm INT_CREATE ' Creates the Interrupt Processor ENDASM @ INT_ENABLE INT_INT ' Enable external (INT) interrupts ' Define ADCIN parameters Define ADC_BITS 10 ' Set number of bits in result Define ADC_CLOCK 3 ' Set clock source (3=rc) Define ADC_SAMPLEUS 50 ' Set sampling time in uS ' Initialize hardware. CMCON0 = 7 ' Turn comparators off TRISIO = %00010110 ' Set GP4, GP2 and GP1 to input ADCON0.7 = 1 ' Right justify result ANSEL = %01011010 ' Set Fosc/16 ' Set AN3 (On Time) as analogue input ' Set AN1 (Off Timer) as analog input Low 0 ' Set initial pump status to Off adval VAR WORD ' Define variable for ADC result On_Step VAR WORD ' Define variable for On time counter Off_Step VAR WORD ' Define variable for Off time counter Flashrate VAR WORD ' Define variable for LED Flash Rate (2-16Hz) wsave VAR BYTE $20 SYSTEM wsave1 VAR BYTE $A0 SYSTEM Start: On_Step = 0 ' Reset On time counter Off_Step = 0 ' Reset Off time counter ' Start Off Timer Routine ADCIN 2, adval ' Read initial Off time setting If adval < 9 Then ' Set minimum Off time to 1 minute adval = 9 Endif While Off_step < adval Select Case Off_step ' Select Off time indicator LED flashrate Case is < adval/2 ' 2Hz if in the first half of the off time period Flashrate = 2 Case is < adval*3/4 ' 4Hz if in the 1/2 to 3/4 span of the off period Flashrate = 4 Case is < adval*7/8 ' 8Hz if in the 3/4 to 7/8 span of the off period Flashrate = 8 Case is < adval*15/16 ' 16Hz if in the 7/8 to 15/16 span of the off period Flashrate = 16 Case Else Flashrate = 20 End Select Freqout 5, 7000, Flashrate ' Flash LED and create timing loop Off_step = Off_step + 1 ' Increment Off step counter ADCIN 2, adval ' Adjust Off Timer if control setting has been changed If adval < 9 Then ' Set minimum Off time to 1 minute adval = 9 Endif Wend ' Start On Timer Routine ADCIN 3, adval If adval < 40 Then ' Set minimum On time to 2 second adval = 40 Endif While On_step < adval Pause 50 ' Pause for 50ms to create timing loop High 0 ' Turn on sprayer High 5 ' Turn on indicator LED On_Step = On_Step + 1 ' Increment On time counter ADCIN 3, adval ' Adjust On timer if control setting has been changed If adval < 40 Then ' Set minimum On time to 2 second adval = 40 Endif Wend Low 0 ' Turn off sprayer Low 5 ' Turn off LED Goto start ' Loop for ever ' INT - Interrupt Handler ManualSwitch: High 0 ' Switch on pump While GPIO.2=1 ' Loop while switch is on Wend Low 0 ' Switch off pump @ INT_RETURN