Κώδικας:
program onewire
' Set TEMP_RESOLUTION to the corresponding resolution of your DS18x20 sensor:
' 18B20: 12
const TEMP_RESOLUTION as byte = 12
dim
j1 ,j2 as byte
temp1 ,temp2 as word
tair , eair ,twet ,ewet , eoliko ,rh as float
apotelesmarh ,tairstring ,twetstring as string[5]
sub procedure Read_Temperature()
dim buf1 ,buf2 as longint
Ow_Reset(PORTA,5) ' Onewire reset signal
Ow_Write(PORTA,5,0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTA,5,0x44) ' Issue command CONVERT_T
Delay_ms(200) ' καθυστεριση για να σβυσει
Ow_Reset(PORTA,1) ' Onewire reset signal
Ow_Write(PORTA,1,0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTA,1,0x44) ' Issue command CONVERT_T
'Delay_ms(200) ' καθυστεριση για να σβυσει
Ow_Reset(PORTA,5)
Ow_Write(PORTA,5,0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTA,5,0xBE) ' Issue command READ_SCRATCHPAD
j1 = OW_Read(PORTA,5) ' Get temperature LSB
temp1 = OW_Read(PORTA,5) ' Get temperature MSB
buf1 = (temp1 * 256) + j1 ' buf=16bit word
buf1 = (buf1 * 100) div 16 ' μετατροπη σε πραγματικη τιμη
tair=buf1/100 'μετατροπη σε πραγματικο αρηθμο πχ 25,60
eair=(17.67*tair)/(tair+243.5) 'υπολογισμος εκθετικου αριθμου
eair = exp(eair) 'λογαριθμος του εκθετικου αρηθμου
eair = 6.112*eair 'αποτελεσμα eair
Ow_Reset(PORTA,1)
Ow_Write(PORTA,1,0xCC) ' Issue command SKIP_ROM
Ow_Write(PORTA,1,0xBE) ' Issue command READ_SCRATCHPAD
j2 = OW_Read(PORTA,1) ' Get temperature LSB
temp2 = OW_Read(PORTA,1) ' Get temperature MSB
buf2 = (temp2 * 256) + j2 ' buf=16bit word
buf2 = (buf2 * 100) div 16 ' μετατροπη σε πραγματικη τιμη
twet=buf2/100 'μετατροπη σε πραγματικο αρηθμο πχ 25,60
ewet=(17.67*twet)/(twet+243.5) 'υπολογισμος εκθετικου αριθμου
ewet = exp(ewet) 'λογαριθμος του εκθετικου αρηθμου
ewet = 6.112*ewet 'αποτελεσμα eair
eoliko=ewet-((1013.25*(tair-twet))*(0.00066*(1+(0.00115*twet))))
' υπολογισμος εξισωσης
rh=(eoliko/eair)*100 ' υπολογισμος σχετικης υγρασιας RH
end sub
' ----Main----
main:
TRISD = 0 ' Configure pins of PORTD as output
ADCON1 = 7 ' configure PORTA as digital I/O
PORTD = %00000000 'δηλωση του PORTD ολες οι εξοδοι να ειναι 0 (αρχικη κατασταση)
'Lcd_Config(PORTB, 0, 1, 2, 3, PORTB, 5, 6, 4) ' ryumish gia diki moy plaketa
Lcd_Config(PORTB, 3, 2, 1, 0, PORTB, 4, 6, 5) ' Initialize LCD connected to PORTB easypic5
Lcd_Cmd(LCD_CLEAR) ' Send command to LCD "clear display"
Lcd_Cmd(LCD_CURSOR_OFF) ' Send command cursor off
while true
Read_Temperature()
FloatToStr(tair, tairstring) 'μετατροπη του tair σε χαρακτηρες
FloatToStr(twet, twetstring) 'μετατροπη του twet σε χαρακτηρες
FloatToStr(rh, apotelesmarh) 'μετατροπη του rh σε χαρακτηρες
' Lcd_Out(1,1,tairstring) 'εγραφη του tair στο LCD
Lcd_chr(1,1,tairstring[0])
Lcd_chr(1,2,tairstring[1])
Lcd_chr(1,3,tairstring[2])
Lcd_chr(1,4,tairstring[3])
Lcd_chr(1,5,tairstring[4])
Lcd_Chr(1, 6, 223) 'εγραφη του χαρακτηρα 'degree'
Lcd_Chr(1, 7, "C") 'εγραφη του χαρακτηρα C
'Lcd_Out(1,10,twetstring) 'εγραφη του twet στο LCD
Lcd_chr(1,10,twetstring[0])
Lcd_chr(1,11,twetstring[1])
Lcd_chr(1,12,twetstring[2])
Lcd_chr(1,13,twetstring[3])
Lcd_chr(1,14,twetstring[4])
Lcd_Chr(1, 15, 223) 'εγραφη του χαρακτηρα 'degree'
Lcd_Chr(1, 16, "C") 'εγραφη του χαρακτηρα C
'Lcd_Out(2,12,apotelesmarh) 'εγραφη του tair στο LCD
Lcd_chr(2,13,apotelesmarh[0])
Lcd_chr(2,14,apotelesmarh[1])
'Lcd_Out(2,7," ") ' εγραφη κενων χαρακτηρων (διωρθωση εμφανησης)
Lcd_Out(2,16,"%") 'εγραφη %
if tair <= 26.50 then 'εαν η θερμοκρασια ειναι <= tair (26.50°C)τωτε
SetBit(PORTD, 2) 'ανοιξε την επαφη PORTD, 2 (1)
else ClearBit(PORTD, 2) 'αλοιως μηδενησε την επαφη
end if
if rh <= 60 then 'εαν η υγρασια εναι <= με rh 60 τωτε
SetBit(PORTD, 6) 'ανοιξε την επαφη PORTD, 6 (1)
else ClearBit(PORTD, 6) 'αλοιως μηδενησε την επαφη
end if
wend
end.