Just a simple little project, born about due to no real temp control on my car AC. Using an 18x, DS18B20 and a 2x16 display. internal temp reading is taken 5 times and the average is used for display and control. This I may alter if it proves unsuitable. Picture of unit fitted into the car is not brilliant.
Usual caveats apply with my coding (I know it could be better..)
Usual caveats apply with my coding (I know it could be better..)
Code:
#picaxe18x
'-------------------------------------------------------------------------------'
'CAR A/C TEMP CONTROL USING 18X AND DS18B20
'DISPLAYED ON 2X16 ALPHANUMERIC LCD
'-------------------------------------------------------------------------------'
symbol lcd=2
symbol temp_sens=0
symbol backlight=3
symbol car_fan=pin1
symbol setpoint_dwn=pin2
symbol setpoint_up=pin7
symbol aircon=7
symbol setpoint=b13
symbol baud=t2400
symbol read_temp=w0
symbol counter=b6
symbol timer =b7
symbol control_temp=b0
symbol ave_temp=w2
symbol car_temp=w1
initialise:
pause 250 'lets start with a short delay shall we?
high lcd 'sets output high for serial display
pause 5 'just to make sure the output is high
serout lcd,baud,(254,1) 'clears the display
pause 500 'and waits for the display to settle
high backlight 'switch on lcd backlight
setpoint=20 'initial setpoint value
begin:
setint off
gosub display_clear
gosub release_setpoint_dwn_button'wait for button to be released
low aircon 'make sure aircon is off
serout lcd,baud,(254,128,"Kev's AC CONTROL")'here's the header
serout lcd,baud,(254,192," SYSTEM OFF ")'and here's the status
do
loop until setpoint_up=1 or setpoint_dwn=1'press any button to start
gosub release_setpoint_up_button'wait for button to be released
gosub release_setpoint_dwn_button'wait for button to be released
if car_fan=0 then 'if car fan isn't running then
serout lcd,baud,(254,192," TURN ON FAN ")'alert the driver
end if
do
if setpoint_dwn=1 then goto begin'turns system off after 2sec press
loop until car_fan=1 'and wait until it is!
serout lcd,baud,(254,192," SYSTEM ON ")'and now we start checking
'-------------------------------------------------------------------------------'
'MAIN PROGRAM SUB-ROUTINE SEQUENCE
'-------------------------------------------------------------------------------'
run:
setint %10000000,%10000000 'sets interrupt for temp adjustment
gosub temp_reading 'measure the cabin temp
gosub neg_check 'lets see if temp is negative
gosub control 'lets see if we need cooling shall we?
gosub display_values 'displays readings on lcd
goto run 'here we go again
'-------------------------------------------------------------------------------'
'AVERAGE TEMP SAMPLING
'-------------------------------------------------------------------------------'
temp_reading:
if setpoint_dwn=1 or car_fan=0 then goto begin'turns system off after 2sec press or car fan stopping
counter=0 'resets timer to zero and
ave_temp=0 'average temp to zero for next batch of readings
do
readtemp12 0,read_temp 'reads the in car temp
ave_temp=ave_temp+read_temp 'adds latest temp reading to average temp
pause 1000 '1 second delay before next reading
inc counter 'adds 1 to the timer
loop until counter=5 'this allows 5 readings to be taken
ave_temp=ave_temp/5 'this divides total value by 5 for average
return
'-------------------------------------------------------------------------------'
'TEMP CALCULATION ROUTINE
'-------------------------------------------------------------------------------'
neg_check:
car_temp=ave_temp 'load 'cartemp' into w1
bit15 = car_temp/32768 'isolate bit 15
if bit15 = 1 then 'check to see if bit 15 is 1
b8 = "-" 'character if temp is 'negative
car_temp=65535-car_temp+1 '2s compliment for negative temp
else
b8 = "."
end if
temp_calc:
car_temp=car_temp*10
w2=car_temp/16 'divide by 16
b0=w2/10 'divides by 10 and leave the 'whole' number
b1=w2//10 'divides by 10 and leave the remainder
return
'-------------------------------------------------------------------------------'
'TEMP CONTROL ROUTINES
'-------------------------------------------------------------------------------'
control:
if control_temp>setpoint then 'lets see if we need cooling
high aircon 'if so, turn on the aircon
b10="o"
b11="n"
b12=" "
else if control_temp<setpoint then 'and if not
low aircon 'turn off the aircon
b10="o"
b11="f"
b12="f"
end if
return
'-------------------------------------------------------------------------------'
'DISPLAY AND INTERRUPT ROUTINES
'-------------------------------------------------------------------------------'
display_values:
if b0<10 then
serout lcd,baud,(254,192,"Temp..",b8,#b0,".",#b1,223,"C ",b10,b11,b12)
else
serout lcd,baud,(254,192,"Temp.",b8,#b0,".",#b1,223,"C ",b10,b11,b12)
end if
return
display_clear:
serout lcd,baud,(254,192," ")
return
display_setpoint:
serout lcd,baud,(254,192,"S/P ADJUST..",#setpoint,223,"C")
return
interrupt:
gosub release_setpoint_up_button'wait for button to be released
gosub display_clear
serout lcd,baud,(254,192,"S/P ADJUST..",#setpoint,223,"C")
gosub display_setpoint
for timer=0 to 250 'sets value for 'return'timer
pause 2
if setpoint_up=1 then inc_temp'jumps to 'inc_temp' or
if setpoint_dwn=1 then dec_temp'dec_temp' dependent on button push
next timer 'inc timer by 1
setint %10000000,%10000000 'resets the interrupt
timer=0 'makes sure timer is set to zero
return
inc_temp:
if setpoint>26 then let setpoint=15'resets setpoint to 15 if max 26 reached
gosub release_setpoint_up_button'wait for button to be released
end if
for timer=0 to 250 'sets value for 'return'timer
pause 3
if setpoint_up=1 then
inc setpoint 'adds one to the setpoint value
gosub display_setpoint
gosub release_setpoint_up_button'wait for button to be released
goto inc_temp 'loop if another increase is required
end if 'adds one to the setpoint
if setpoint_dwn=1 then dec_temp'jumps dependent on button push
next timer 'inc timer by 1
return
dec_temp:
if setpoint<15 then let setpoint=26'resets setpoint to 26 if min 14 reached
gosub release_setpoint_dwn_button'wait for button to be released
end if
for timer=0 to 250 'sets value for 'return'timer
pause 3
if setpoint_dwn=1 then '
dec setpoint 'takes 1 away from the setpoint value
gosub display_setpoint
gosub release_setpoint_dwn_button'wait for button to be released
goto dec_temp 'loop if another decrease is required
end if 'takes 1 away from the setpoint
if setpoint_up=1 then inc_temp'jumps dependent on button push
next timer 'inc timer by 1
return
release_setpoint_up_button:
do
loop until setpoint_up=0 'wait for button to be released
pause 10 'pause just to make sure
return
release_setpoint_dwn_button:
do
loop until setpoint_dwn=0 'wait for button to be released
pause 10 'pause just to make sure
return
Attachments
-
114.2 KB Views: 246
-
63.9 KB Views: 226