
Originally Posted by
malc-c
Interesting thread, and quite by chance similar to a project I was contemplating.
I was thinking of making my own pulse proportional thermostat for use with reptile enclosures. Your code could be the ideal starting point for me, thanks for sharing
I've made a couple of changes since I posted that. I'm using the plusout command instead of the PWM command. There is better control with the pulsout command.
I've also added an external thermostat to turn it off. Man does it get hot!
GPIO.1 is pulled low through a 10k then goes to VDD through a wall thermostat.
Here is the final working code:
Code:
'LM34 F temperature sensor
'PIN NAME Purpose
' 1 VDD +5v
' 2 Output 10mv/degF, +5° to +300°F = 50mv-3000mv
' 3 VSS GND
'
'PIC12F675
' PIN NAME Purpose
' 1 VDD +5v VDD
' 2 GPIO.5 Serial out for debug
' 3 GPIO.4 PWM out to servo
' 4 GPIO.3 ***Programmer VPP
' 5 GPIO.2 Output to NPN transistor sinking relay.
' 6 GPIO.1 Input from thermostat /***Programmer Clk
' 7 GPIO.0 Analog input from center leg of LM34 /***Programmer Data
' 8 VSS GND VSS
'********************************* Fuses ******************************
@ device pic12F675, intrc_osc_noclkout, wdt_off, pwrt_on, mclr_off, bod_off, protect_off
'********************************* Setup *******************************
DEFINE debug_mode 1 '0=True, 1=Inverted serial data
DEFINE debug_bit 5 'sends serial data out GPIO.5 at N,8,1
DEFINE debug_baud 2400 'Default baud rate = 2400
DEFINE osc 4 'We're using a 4 MHz oscillator
Define OSCCAL_1K 1 'Set OSCCAL for 1K device
DEFINE ADC_BITS 8 'Set A/D for 8-bit operation
DEFINE ADC_SAMPLEUS 50 'Set A/D sampling time @ 50 uS
CMCON=7 '(%00000111) Turn off analog comparators
TRISIO=%00001011 '001001 Port direction register. GPIO.0 & GPIO.3 =inputs (.3 is always input)
ADCON0=%00000000 '00000000 Left justified,Vref=VDD,GP.0=ANALOG INPUT
ANSEL=%01010001 '01010001 FOSC=16=101,GPIO.0=analog
Samples VAR WORD 'Multiple A/D sample accumulator
Sample VAR BYTE 'Holds number of Samples to take
Temp VAR BYTE 'Temperature storage
Thermostat var bit 'Input from thermostat
Relay var GPIO.2 'Variable for relay output
LowTemp con 90 'Turn off relay
HighTemp con 110 'Turn on relay
Samples = 0 'Clear Samples accumulator on power-up
'****************************** Servo Stuff ****************************
Servo var GPIO.4 'Variable for PWM output
low servo 'Initilize GPIO.4 to low
Accum var byte 'GP Accumulator variable
AccLow con 100 'Accumulator Min pulse (move CCW)
AccHigh con 250 'Accumulator Max pulse (Move Cw)
Wait1 con 20 'Delay before repeating loop
Flag var bit 'Holds bit for servo position
flag=0 'initilize flag
PAUSE 500 'Wait .5 second
'********************************* Main ********************************
Main:
FOR sample = 1 TO 20 ' Take 20 Samples
ADCIN 0, temp ' Read channel 0 into temp variable
Samples = Samples + temp ' Accumulate 20 Samples
PAUSE 250 ' Wait approximately 1/4 seconds per loop
NEXT sample
temp = Samples/20*2
if temp >lowtemp and temp>hightemp and thermostat=1 then gosub onrelay
if temp <=lowtemp then gosub offrelay
DEBUG "Curr Temp: ",DEC temp,"° F ",10,13,10,13," Off Temp: ",dec lowtemp,"° F "," On Temp: ",dec hightemp,"° F ",10,13
DEBUG "CCW Pulse Width: ",dec Acclow,"ms ", "CW Pulse Width: ",dec Acchigh,",ms ", "Flag: ",dec flag,10,13
Samples = 0 ' Clear old sample accumulator
GOTO main ' Do it forever
END
'******************************* Output **********************************
OnRelay:
high relay 'Turns on GPIO.1
if flag=0 then gosub VentOpen 'If vent is closed then open it.
return
OffRelay:
low relay 'Turns off GPIO.1
if flag=1 then gosub VentClosed 'if vent is open then close it.
return
'**************************** Open Damper ********************************
'@@@@@@@@@ SWAP LINES 75 & 76 AND LINES 84 & 85 TO REVERSE ROTATION @@@@@@@@@@@@
VentOpen:
for Accum=acchigh to acclow step -1 'Move servo 90 deg CW
'for Accum=Acclow to acchigh 'Move servo 90 deg CCW
pulsout servo, Accum
pause Wait1
next
flag=1 'Set Flag to tell when open
return
'**************************** Close Damper *******************************
VentClosed:
for Accum=Acclow to acchigh 'Move servo 90 deg CCW
'for Accum=acchigh to acclow step -1 'Move servo 90 deg CW
pulsout servo, Accum
pause Wait1
next
flag=0 'Set Flag to tell when closed
return
Bookmarks