Code:
'****************************************************************
'* Name : Oil Control.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2008 [KarenH] *
'* : All Rights Reserved *
'* Date : 06/05/2008 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
Define LOADER_USED 1
@ DEVICE pic16F877a,HS_OSC,WDT_OFF,LVP_OFF
' Define various stuff
DEFINE OSC 8
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus .7
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 4 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 5 ' Set LCD Enable bit i.e, PORTE.4
DEFINE LCD_BITS 4 ' Set LCD bus size ot 4 bit Upper Nibble (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD to 4 Lines
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us
' Allocate variables
command var byte ' Storage for command
i var byte ' Storage for loop counter
temp var word ' Storage for temperature
DQ var PORTA.5 ' Alias DS1820 data pin
DQ_DIR var TRISA.5 ' Alias DS1820 data direction pin
ADCON1 = 7 ' Set PORTA and PORTE to digital
Pause 500 ' Wait for LCD to start
Lcdout $FE,1," All Circuits", $FE,$C0, " Funtioning OK " ' Display sign-on message
Pause 3000 'Wait 3 seconds going any further
' Mainloop to read the temperature and display on LCD
mainloop:
Gosub init1820 ' Init the DS1820
command = $cc ' Issue Skip ROM command
Gosub write1820
command = $44 ' Start temperature conversion
Gosub write1820
Pause 500 ' Wait 0.5 seconds for conversion to complete
Gosub init1820 ' Do another init
command = $cc ' Issue Skip ROM command
Gosub write1820
command = $be ' Read the temperature
Gosub write1820
Gosub read1820
' Display the decimal temperature
IF PORTC.0 = 0 AND PORTC.3 = 0 then lcdout $FE,1," Diesel", $FE,$C0,"Outside ", $fe, $C9, dec (temp >> 1), ".", dec (temp.0 * 5), $DF, "C": LOW PORTE.0: LOW PORTE.1
IF PORTC.0 = 1 AND PORTC.3 = 0 then gosub Tempcheck
IF PORTC.0 = 0 AND PORTC.3 = 1 then gosub Tempcheck
If PORTC.0 = 1 AND PORTC.3 = 1 then gosub Oil
IF PORTD.1 = 1 Then GOSUB purging
Goto mainloop ' Do it forever
Stop1:
LOW PORTE.0
LOW PORTE.1
LCDOUT $FE,1," MANUAL",$FE,$C0," Diesel OVERRIDE"
while PORTD.1=0 'OVERRIDE COMMAND to MANUALLY switch back to DIESEL i.e. if veg oil runs out
wend
pause 1000 'so a very quick button press is NOT recognised and the program stays paused
Goto Tempcheck
OIL:
LCDOUT $FE,1," Veg-Oil", $FE,$C0,"Outside", $fe, $C9, dec (temp >> 1), ".", dec (temp.0 * 5), $DF, "C"
HIGH PORTE.0 'Diesel Solenoid
HIGH PORTE.1 'OIL Solenoid
return
Tempcheck:
lcdout $FE,1," TEMP LOW", $FE,$C0,"Still on DIESEL"
LOW PORTE.0: LOW PORTE.1
return
Purging:
LCDOUT $FE,1," PURGING Please",$FE,$C0," WAIT"
HIGH PORTE.0 'Diesel Solenoid
LOW PORTE.1 'Oil Solenoid
Pause 60000 'set delay of purge here 60 seconds
gosub Stop1
Goto mainloop
' Initialize DS1820 and check for presence
init1820:
Low DQ ' Set the data pin low to init
Pauseus 500 ' Wait > 500us
DQ_DIR = 1 ' Release data pin (set to input for high)
Pauseus 100 ' Wait > 100us
If DQ = 1 Then
Lcdout $fe, 1, "DS1820 not present"
Pause 500
Goto mainloop ' Try again
Endif
Pauseus 400 ' Wait for end of presence pulse
Return
' Write "command" byte to the DS1820
write1820:
For i = 1 to 8 ' 8 bits to a byte
If command.0 = 0 Then
Gosub write0 ' Write a 0 bit
Else
Gosub write1 ' Write a 1 bit
Endif
command = command >> 1 ' Shift to next bit
Next i
Return
' Write a 0 bit to the DS1820
write0:
Low DQ
Pauseus 60 ' Low for > 60us for 0
DQ_DIR = 1 ' Release data pin (set to input for high)
Return
' Write a 1 bit to the DS1820
write1:
Low DQ ' Low for < 15us for 1
@ nop ' Delay 1us at 4MHz
DQ_DIR = 1 ' Release data pin (set to input for high)
Pauseus 60 ' Use up rest of time slot
Return
' Read temperature from the DS1820
read1820:
For i = 1 to 16 ' 16 bits to a word
temp = temp >> 1 ' Shift down bits
Gosub readbit ' Get the bit to the top of temp
Next i
Return
' Read a bit from the DS1820
readbit:
temp.15 = 1 ' Preset read bit to 1
Low DQ ' Start the time slot
@ nop ' Delay 1us at 4MHz
DQ_DIR = 1 ' Release data pin (set to input for high)
If DQ = 0 Then
temp.15 = 0 ' Set bit to 0
Endif
Pauseus 60 ' Wait out rest of time slot
Return
End
Bookmarks