$regfile = "m32def.dat"
$crystal = 8000000
$hwstack = 340
$swstack = 128
$framesize = 128
$eeprom
$include "Config_MMC.bas"
$include "Config_AVR-DOS.BAS"
Config Debounce = 30
'-------------------------------------------------------------------------------
Config Lcdpin = Pin , Db4 = Portb.2 , Db5 = Portb.4 , Db6 = Porta.0 , Db7 = Porta.1 , E = Portb.1 , Rs = Portb.0
Config Lcd = 16 * 2
Cls
Cursor Off
'Config Portd = Input 'ds18b20
Config Portc.7 = Input 'ds18b20
Config Portc.6 = Input 'czy pompa cwu pracuje
Config Portc.2 = Output 'triak1
Config Portc.3 = Output 'triak2
Config Portc.4 = Output 'triak3
Config Portc.5 = Output 'triak4
Config Porta.2 = Input 'przycisk1
Config Porta.3 = Input 'przycisk2
Config Porta.5 = Input 'termostat kominka
'-------------------------------------------------------------------------------
Config Adc = Single , Prescaler = Auto , Reference = Aref
Start Adc
Dim Kty81 As Integer
'-------------------------------------------------------------------------------
Dim Errorcode As Byte
Dim Btemp1 As Byte
'Dim Data_na_mmc As String * 16
Errorcode = Driveinit()
If Errorcode = 0 Then
Locate 1 , 1
Lcd "DriveInit: OK"
Btemp1 = Initfilesystem(1)
If Btemp1 <> 0 Then
Locate 2 , 1
Lcd "Error: " ; Btemp1
Else
Locate 2 , 1
Lcd "OK"
End If
End If
Wait 1
Cls
'-------------------------------------------------------------------------------
Config Sda = Portc.1
Config Scl = Portc.0
Config Porta.2 = Input 'przyciski
Config Porta.3 = Input
Config Porta.6 = Input
Config Porta.7 = Output
Porta.2 = 1
Porta.3 = 1
Porta.6 = 1
Porta.7 = 1
Config Debounce = 30
Dim Nazwa_pliku As String * 12
Dim Rok_s As Byte
'Dim Rok_aktualny As Integer
Dim Rok_do_nazwy_pliku As String * 2
Dim Miesiac_s As String * 2
Dim Dzien_s As String * 2
Dim Minuty_s As String * 2
Dim Godziny_s As String * 2
Dim Minuta_pomocnicza As Integer
Dim T(9) As String * 6
Dim Naglowek As String * 105
Dim Jest_plik As Byte
Dim Sekundy As Byte : Dim Minuty As Integer : Dim Godziny As Integer
Dim Dzien As Byte : Dim Miesiac As Byte
Dim Rok As Byte : Dim Dzien_tygodnia As Byte : Dim Kolejny_dzien_tygodnia As Byte
Dim Kolejny_czujnik As Byte
Kolejny_czujnik = 0
Dim Temperatura(9) As Integer
Dim Ds_t As Integer
Dim Ds_ram(9) As Byte
Dim Ds_minus As Bit
Ddrd = &B11000000 'ustawienia portu D dla czujników ds18b20
Portd = &B11111111
Dim Zezwol_na_zmiane As Byte
Zezwol_na_zmiane = 0
Declare Sub Gettime
Declare Sub Settime
Declare Sub Sendtime
Declare Sub Na_lcd
Declare Sub Zapis_mmc
Declare Sub Start_temp(byval I As Byte )
Declare Sub Get_temp(byval I As Byte )
Declare Sub Zapis_mmc_naglowek
Declare Sub Wywolaj_pomiary
Declare Sub Sprawdz_rok
Declare Sub Ustal_nazwe_pliku
Declare Sub Formatowanie_daty
Declare Sub Czy_jest_naglowek
Rok_s = 12
Writeeeprom Rok_s , 0 'to wywalić jak będzie koniec
Call Gettime
Call Ustal_nazwe_pliku
Call Czy_jest_naglowek
Minuta_pomocnicza = Minuty
Kolejny_dzien_tygodnia = Dzien_tygodnia
'-------------------------------------------------------------------------------
Do
If Zezwol_na_zmiane = 0 Then
Call Sprawdz_rok
If Sekundy = 59 Then
Minuta_pomocnicza = -1
End If
Call Gettime
Call Na_lcd
Kty81 = Getadc(5)
If Minuty > Minuta_pomocnicza Then
Locate 1 , 12
Lcd " MMC "
Call Wywolaj_pomiary
If Kolejny_dzien_tygodnia <> Dzien_tygodnia Then
Call Ustal_nazwe_pliku
Call Zapis_mmc_naglowek
Kolejny_dzien_tygodnia = Dzien_tygodnia
End If
Call Zapis_mmc
Locate 1 , 12
Lcd " "
Minuta_pomocnicza = Minuty
End If
'Wait 1
Else
Call Settime
End If
Debounce Pina.2 , 0 , Przycisk_lewy , Sub
Debounce Pina.3 , 0 , Przycisk_prawy_plus , Sub
Debounce Pina.6 , 0 , Przycisk_prawy_minus , Sub
'Waitms 500
Loop
'-------------------------------------------------------------------------------
Przycisk_lewy:
Toggle Zezwol_na_zmiane
Return
Przycisk_prawy_plus:
Return
Przycisk_prawy_minus:
Return
'-------------------------------------------------------------------------------
Sub Settime
Porta.7 = 0 '- wlacz led na lcd
Cls
Call Na_lcd
Locate 1 , 3
Do
If Pina.3 = 0 Then
Waitms 100
If Pina.3 = 0 Then
Incr Rok_s
If Rok_s > 99 Then
Rok_s = 10
End If
End If
End If
If Pina.6 = 0 Then
Waitms 100
If Pina.6 = 0 Then
Decr Rok_s
If Rok_s < 10 Then
Rok_s = 99
End If
End If
End If
Locate 1 , 3
Lcd Rok_s
Locate 2 , 3
Lcd Chr(94) ; Chr(94)
If Pina.2 = 0 Then
Waitms 100
If Pina.2 = 0 Then
Writeeeprom Rok_s , 0
Locate 2 , 3
Lcd " "
Exit Do
End If
End If
Loop
Waitms 500
Do
If Pina.3 = 0 Then
Waitms 100
If Pina.3 = 0 Then
Incr Miesiac
If Miesiac > 12 Then
Miesiac = 1
End If
End If
End If
If Pina.6 = 0 Then
Waitms 100
If Pina.6 = 0 Then
Decr Miesiac
If Miesiac < 1 Then
Miesiac = 12
End If
End If
End If
Locate 1 , 6
Call Formatowanie_daty
Lcd Miesiac_s
Locate 2 , 6
Lcd Chr(94) ; Chr(94)
If Pina.2 = 0 Then
Waitms 100
If Pina.2 = 0 Then
'Call Sendtime
Locate 2 , 6
Lcd " "
Exit Do
End If
End If
Loop
Waitms 500
Do
If Pina.3 = 0 Then
Waitms 100
If Pina.3 = 0 Then
Incr Dzien
If Dzien > 31 Then
Dzien = 1
End If
End If
End If
If Pina.6 = 0 Then
Waitms 100
If Pina.6 = 0 Then
Decr Dzien
If Dzien < 1 Then
Dzien = 31
End If
End If
End If
Locate 1 , 9
Call Formatowanie_daty
Lcd Dzien_s
Locate 2 , 9
Lcd Chr(94) ; Chr(94)
If Pina.2 = 0 Then
Waitms 100
If Pina.2 = 0 Then
'Call Sendtime
Locate 2 , 9
Lcd " "
Exit Do
End If
End If
Loop
Waitms 500
Do
If Pina.3 = 0 Then
Waitms 100
If Pina.3 = 0 Then
Incr Godziny
If Godziny > 23 Then
Godziny = 0
End If
End If
End If
If Pina.6 = 0 Then
Waitms 100
If Pina.6 = 0 Then
Decr Godziny
If Godziny < 0 Then
Godziny = 23
End If
End If
End If
Locate 1 , 12
Call Formatowanie_daty
Lcd Godziny_s
Locate 2 , 12
Lcd Chr(94) ; Chr(94)
If Pina.2 = 0 Then
Waitms 100
If Pina.2 = 0 Then
'Call Sendtime
Locate 2 , 12
Lcd " "
Exit Do
End If
End If
Loop
Waitms 500
Do
If Pina.3 = 0 Then
Waitms 100
If Pina.3 = 0 Then
Incr Minuty
If Minuty > 59 Then
Minuty = 0
End If
End If
End If
If Pina.6 = 0 Then
Waitms 100
If Pina.6 = 0 Then
Decr Minuty
If Minuty < 0 Then
Minuty = 59
End If
End If
End If
Locate 1 , 15
Call Formatowanie_daty
Lcd Minuty_s
Locate 2 , 15
Lcd Chr(94) ; Chr(94)
If Pina.2 = 0 Then
Waitms 100
If Pina.2 = 0 Then
Sekundy = 0
Call Sendtime
Locate 2 , 15
Lcd " "
Exit Do
End If
End If
Loop
' Godziny = 23
' Minuty = 58
' Sekundy = 50
' Rok = 0
' Dzien = 31
' Miesiac = 12
Zezwol_na_zmiane = 0
Locate 2 , 1
Lcd "^^^^^^^^^^^^^^^^"
Waitms 1500
Cls
Porta.7 = 1
End Sub
'-------------------------------------------------------------------------------
Sub Sprawdz_rok
If Miesiac = 12 Then
If Dzien = 31 Then
If Godziny = 23 Then
If Minuty = 59 Then
If Sekundy = 57 Then
Readeeprom Rok_s , 0
Rok_s = Rok_s + 1
Writeeeprom Rok_s , 0
'Rok_aktualny = Rok_s + 2000
End If
End If
End If
End If
End If
End Sub
'-------------------------------------------------------------------------------
Sub Wywolaj_pomiary
For Kolejny_czujnik = 0 To 8
Call Start_temp(kolejny_czujnik)
Waitms 750
Call Get_temp(kolejny_czujnik)
Next
End Sub
'-------------------------------------------------------------------------------
Sub Start_temp(byval I As Byte )
Disable Interrupts
If I = 8 Then
1wreset Pinc , 7
1wwrite &HCC , 1 , Pinc , 7
1wwrite &H44 , 1 , Pinc , 7
End If
If I < 8 Then
1wreset Pind , I
1wwrite &HCC , 1 , Pind , I
1wwrite &H44 , 1 , Pind , I
End If
Enable Interrupts
End Sub
'-------------------------------------------------------------------------------
Sub Get_temp(byval I As Byte )
If I = 8 Then
1wreset Pinc , 7
1wwrite &HCC , 1 , Pinc , 7
1wwrite &HBE , 1 , Pinc , 7
Ds_ram(1) = 1wread(9 , Pinc , 7)
End If
If I < 8 Then
1wreset Pind , I
1wwrite &HCC , 1 , Pind , I
1wwrite &HBE , 1 , Pind , I
Ds_ram(1) = 1wread(9 , Pind , I)
End If
Ds_minus = 0
If Ds_ram(2).7 = 1 Then Ds_minus = 1
Ds_t = Makeint(ds_ram(1) , Ds_ram(2))
Ds_t = Ds_t * 10
Ds_t = Ds_t / 16
If Ds_ram(9) <> Crc8(ds_ram(1) , 8) Then Ds_t = 850 'jeśli błąd to wynik pomiaru temp. równy 85
If Kolejny_czujnik = 0 Then
Temperatura(1) = Ds_t
'Ds_t = Ds_t * -1
T(1) = Str(temperatura(1))
T(1) = Format(t(1) , "00.0")
End If
If Kolejny_czujnik = 1 Then
Temperatura(2) = Ds_t
T(2) = Str(temperatura(2))
T(2) = Format(t(2) , "00.0")
End If
If Kolejny_czujnik = 2 Then
Temperatura(3) = Ds_t
T(3) = Str(temperatura(3))
T(3) = Format(t(3) , "00.0")
End If
If Kolejny_czujnik = 3 Then
Temperatura(4) = Ds_t
T(4) = Str(temperatura(4))
T(4) = Format(t(4) , "00.0")
End If
If Kolejny_czujnik = 4 Then
Temperatura(5) = Ds_t
T(5) = Str(temperatura(5))
T(5) = Format(t(5) , "00.0")
End If
If Kolejny_czujnik = 5 Then
Temperatura(6) = Ds_t
T(6) = Str(temperatura(6))
T(6) = Format(t(6) , "00.0")
End If
If Kolejny_czujnik = 6 Then
Temperatura(7) = Ds_t
T(7) = Str(temperatura(7))
T(7) = Format(t(7) , "00.0")
End If
If Kolejny_czujnik = 7 Then
Temperatura(8) = Ds_t
T(8) = Str(temperatura(8))
T(8) = Format(t(8) , "00.0")
End If
If Kolejny_czujnik = 8 Then
Temperatura(9) = Ds_t
T(9) = Str(temperatura(9))
T(9) = Format(t(9) , "00.0")
End If
End Sub
'-------------------------------------------------------------------------------
Sub Ustal_nazwe_pliku
Readeeprom Rok_s , 0
Call Formatowanie_daty
Nazwa_pliku = Dzien_s + Chr(45) + Miesiac_s + Chr(45) + Rok_do_nazwy_pliku + Chr(46) + Chr(99) + Chr(115) + Chr(118)
End Sub
'-------------------------------------------------------------------------------
Sub Czy_jest_naglowek
Jest_plik = Dir(nazwa_pliku)
If Len(jest_plik) < 1 Then
Call Zapis_mmc_naglowek
End If
End Sub
'-------------------------------------------------------------------------------
Sub Zapis_mmc_naglowek
Naglowek = Chr(34) + Chr(32) + Chr(34) + Chr(44) + Chr(34) + "cwu" + Chr(34) + Chr(44) + Chr(34) + "t. Wew" + Chr(34) + Chr(44) + Chr(34) + "t. Zew" + Chr(34) + Chr(44) + Chr(34) + "t. Dz Z." + Chr(34) + Chr(44) + Chr(34) + "t. Dz P." + Chr(34) + Chr(44) + Chr(34) + "t. Gz Z." + Chr(34) + Chr(44) + Chr(34) + "t. Gz P." + Chr(34) + Chr(44) + Chr(34) + "t. Kominek Z." + Chr(34) + Chr(44) + Chr(34) + "t. Kominek P." + Chr(34)
Open Nazwa_pliku For Append As #2
Print #2 , Naglowek
Flush #2
Close #2
End Sub
'-------------------------------------------------------------------------------
Sub Formatowanie_daty
Rok_do_nazwy_pliku = Str(rok_s)
Rok_do_nazwy_pliku = Format(rok_do_nazwy_pliku , "00")
Miesiac_s = Str(miesiac)
Miesiac_s = Format(miesiac_s , "00")
Dzien_s = Str(dzien)
Dzien_s = Format(dzien_s , "00")
Godziny_s = Str(godziny)
Godziny_s = Format(godziny_s , "00")
Minuty_s = Str(minuty)
Minuty_s = Format(minuty_s , "00")
End Sub
'-------------------------------------------------------------------------------
Sub Zapis_mmc
Call Formatowanie_daty
Naglowek = Chr(34) + Dzien_s + Chr(45) + Miesiac_s + Chr(45) + Rok_do_nazwy_pliku + Chr(32) + Godziny_s + Chr(58) + Minuty_s + Chr(34) + Chr(44) + T(1) + Chr(44) + T(2) + Chr(44) + T(3) + Chr(44) + T(4) + Chr(44) + T(5) + Chr(44) + T(6) + Chr(44) + T(7) + Chr(44) + T(8) + Chr(44) + T(9)
Open Nazwa_pliku For Append As #2
Write #2 , Naglowek
Flush #2
Close #2
End Sub
'-------------------------------------------------------------------------------
Sub Na_lcd
Locate 1 , 1
Lcd "20" ; Rok_s ; "-" ; Miesiac_s ; "-" ; Dzien_s ; " " ; Godziny_s ; ":" ; Minuty_s
End Sub
'-------------------------------------------------------------------------------
Sub Gettime()
I2cstart
I2cwbyte 160
I2cwbyte 2
I2cstart
I2cwbyte 161
I2crbyte Sekundy , Ack
I2crbyte Minuty , Ack
I2crbyte Godziny , Ack
I2crbyte Dzien , Ack
I2crbyte Miesiac , Nack
I2cstop
Sekundy = Makedec(sekundy)
Minuty = Makedec(minuty)
Godziny = Makedec(godziny)
Rok = Dzien
Shift Rok , Right , 6
Rok = Makedec(rok)
Dzien.7 = 0 : Dzien.6 = 0
Dzien = Makedec(dzien)
Dzien_tygodnia = Miesiac
Shift Dzien_tygodnia , Right , 5
Dzien_tygodnia = Makedec(dzien_tygodnia)
Miesiac.7 = 0 : Miesiac.6 = 0 : Miesiac.5 = 0
Miesiac = Makedec(miesiac)
End Sub
Sub Sendtime()
Godziny = Makebcd(godziny)
Minuty = Makebcd(minuty)
Sekundy = Makebcd(sekundy)
Rok = Rok Mod 4
Rok = Makebcd(rok)
Shift Rok , Left , 6
Dzien = Makebcd(dzien)
Dzien = Rok Or Dzien
Miesiac = Makebcd(miesiac)
Dzien_tygodnia = Miesiac
Shift Dzien_tygodnia , Left , 5
Miesiac = Dzien_tygodnia Or Miesiac
I2cstart
I2cwbyte &HA0
I2cwbyte 0
I2cwbyte 0
I2cstop
I2cstart
I2cwbyte &HA0
I2cwbyte 2
I2cwbyte Sekundy
I2cwbyte Minuty
I2cwbyte Godziny
I2cwbyte Dzien
I2cwbyte Miesiac
I2cstop
End Sub