'************* Konfiguracja uC ***************** '*********************************************** $regfile = "m16def.dat" $crystal = 16000000 Config Porta = &B11110011 : Porta = &B11111111 Config Portb = &B11111111 : Portb = &B11111111 Config Portc = &B11111111 : Portc = &B11111111 Config Portd = &B00000011 : Portd = &B11111111 Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Porta.5 , Db6 = Porta.6 , Db7 = Porta.7 , E = Porta.1 , Rs = Porta.0 Config Lcd = 16 * 2 Cls Cursor Off Noblink Config 1wire = Pinc.7 Config Scl = Portb.0 Config Sda = Portb.1 I2cinit Lcd_light Alias Portc.6 Sw_port Alias Pind 'nazwa portu Tr1 Alias Portc.2 Tr2 Alias Portc.1 Tr_term Alias Portc.0 Buzz Alias Portc.5 Linia_no Alias Pina.2 Linia_nc Alias Pina.3 Config Timer0 = Timer , Prescale = 256 Enable Timer0 : On Timer0 Prztimer0 Enable Interrupts '********** Konfiguracja uC Koniec ************ '****************** Zmienne ******************* '*********************************************** Dim Stan_sw As Byte , Sw As Byte , Licznik_sw As Byte 'sw Dim Wyswietlanie As Byte , Poz_wysw As Byte , Wyswietlono As Bit 'Lcd Dim Mig As Bit , Poz_ust As Byte Dim Autosave As Byte , Autolight As Byte Dim Ds1(8) As Byte , Ds2(8) As Byte , Ds3(8) As Byte , Ds(8) As Byte , Ds_adr As Byte , Ds_ok As Bit 'DS18B20 Dim Ktory_ds As Byte , Lsb As Byte , Msb As Byte , Minusy As Byte Dim T(3) As Word Dim T_ust As Word , T_his As Byte , Term_ena As Bit 'termostat Dim Dzielnik(2) As Byte 'dzielniki timera Dim Flaga_4ms As Bit , Flaga_500ms As Bit , Flaga_1s As Bit 'flagi przerwa? Dim Hbyte(2) As Byte , Hword(3) As Word , N As Byte , Hbit As Bit Dim Godziny As Byte , Minuty As Byte , Sekundy As Byte Dim Godziny_bcd As Byte , Minuty_bcd As Byte , Sekundy_bcd As Byte Dim Zapisz_czas As Bit Dim G(4) As Byte , M(4) As Byte , Ar_tr1 As Bit , Ar_tr2 As Bit 'programtot czas Dim Alarm As Byte , Salarm As Byte , A_g As Byte , A_m As Byte 'alarmy 0-wyl, 1-czuwa, 2-czas wtargniecia Dim Alarm_licz As Byte '************* Zmienne Koniec **************** '************ Warunki Poczatkowe *************** '*********************************************** Lcd_light = 0 Autolight = 20 Lcd "Sterownik Domowy" Lowerline Lcd " Wersja 1.05" Wait 1 If Sw_port = &B01001111 Then Ds_adr = 30 Gosub Ds_reg Cls Lcd "Ds1" End If If Sw_port = &B01010111 Then Ds_adr = 40 Gosub Ds_reg Cls Lcd "Ds2" End If If Sw_port = &B01011011 Then Ds_adr = 50 Gosub Ds_reg Cls Lcd "Ds term" End If For N = 1 To 8 Hbyte(1) = 30 + N Readeeprom Ds1(n) , Hbyte(1) Hbyte(1) = 40 + N Readeeprom Ds2(n) , Hbyte(1) Hbyte(1) = 50 + N Readeeprom Ds3(n) , Hbyte(1) Next N For N = 10 To 13 Readeeprom G(n -9) , N If G(n -9) > 23 Then G(n -9) = 0 Next N For N = 14 To 17 Readeeprom M(n -13) , N If M(n -13) > 59 Then M(n -13) = 0 Next N Readeeprom Hbyte(1) , 18 Ar_tr1 = Hbyte(1).0 Ar_tr2 = Hbyte(1).1 Term_ena = Hbyte(1).2 Readeeprom T_ust , 19 Readeeprom T_his , 21 If T_ust > 1000 Or T_ust < 10 Then T_ust = 450 If T_his > 100 Or T_his < 5 Then T_his = 20 Readeeprom Alarm , 22 Readeeprom Salarm , 23 Readeeprom A_g , 24 Readeeprom A_m , 25 If Alarm > 2 Then Alarm = 0 If Salarm > 2 Then Salarm = 0 If A_g > 23 Then A_g = 0 If A_m > 59 Then A_m = 0 Wait 1 Wyswietlono = 1 Wyswietlanie = 1 Autosave = 5 '******* Warunki Poczatkowe Koniec *********** '************** Petla glowna ****************** '*********************************************** Do '*********************************************** co 4ms If Flaga_4ms = 1 Then Flaga_4ms = 0 If Alarm = 1 Then If Linia_no = 0 Or Linia_nc = 1 Then Alarm = 2 A_g = Godziny A_m = Minuty End If Else If Alarm = 2 Then Buzz = Mig Else Buzz = 1 End If End If If Salarm = 1 Then If Linia_no = 0 Or Linia_nc = 1 Then Salarm = 2 A_g = Godziny A_m = Minuty End If End If Gosub Przyciski If Wyswietlono = 1 Then Gosub Wysw_lcd End If '*********************************************** '*********************************************** co 500ms If Flaga_500ms = 1 Then Flaga_500ms = 0 If Autolight > 0 Then Decr Autolight Lcd_light = 0 Else Lcd_light = 1 End If Mig = Not Mig If Poz_wysw <> 1 Or Poz_ust = 0 Then If Zapisz_czas = 1 Then Gosub Zapis_rtc 'jednorazowo zapisuje po wyj?ciu z ustawie? Zapisz_czas = 0 Else Gosub Odczyt_rtc 'odczytuje czas End If Else Zapisz_czas = 1 End If If Poz_wysw = 1 Then Wyswietlanie = 1 'uaktualniaj czas je?li jest pokazywany na LCD If Poz_wysw = 2 Then Wyswietlanie = 2 If Poz_wysw = 4 Then Wyswietlanie = 4 'je?li wyswietlony termostat to uaktualnij. If Poz_wysw = 5 Then Wyswietlanie = 5 End If '*********************************************** '*********************************************** co 1s If Flaga_1s = 1 Then Flaga_1s = 0 If Autosave < 5 Then Incr Autosave If Autosave = 5 Then For N = 10 To 13 Writeeeprom G(n -9) , N Next N For N = 14 To 17 Writeeeprom M(n -13) , N Next N Hbyte(1) = 0 Hbyte(1).0 = Ar_tr1 Hbyte(1).1 = Ar_tr2 Hbyte(1).2 = Term_ena Writeeeprom Hbyte(1) , 18 Writeeeprom T_ust , 19 Writeeeprom T_his , 21 Writeeeprom Alarm , 22 Writeeeprom Salarm , 23 Writeeeprom A_g , 24 Writeeeprom A_m , 25 Poz_ust = 0 End If End If Gosub Temperatura If Poz_wysw = 3 Then Wyswietlanie = 3 'jesli wysw temperatura to uaktualnij Gosub Sterowanie End If '*********************************************** Loop End '************ Petla glowna Koniec ************** '************ Przerwanie timer 0 *************** '*********************************************** Prztimer0: Timer0 = 5 Set Flaga_4ms Incr Dzielnik(1) If Dzielnik(1) = 125 Then Dzielnik(1) = 0 Set Flaga_500ms Incr Dzielnik(2) If Dzielnik(2) = 2 Then Dzielnik(2) = 0 Set Flaga_1s End If End If Return '******** Przerwanie timer 0 Koniec ************ '*********************************************** '***************** Przyciski ************** Przyciski: Sw = Sw_port And &B11111100 'And &B00001111 'maska na piny nieu?ywane jako sw If Sw <> &B11111100 And Stan_sw = Sw Then Incr Licznik_sw If Licznik_sw = 68 Then Licznik_sw = 0 Autolight = 20 Select Case Stan_sw Case &B11101100: 'incr '*********************************************************** Select Case Poz_wysw Case 1: Select Case Poz_ust Case 0: 'podczas wyswietania czasu If Alarm = 2 Then Alarm = 0 'If Salarm = 2 Then Salarm = 0 Case 1: Incr Godziny If Godziny = 24 Then Godziny = 0 Case 2: Incr Minuty If Minuty = 60 Then Minuty = 0 Case 3: Incr Sekundy If Sekundy = 60 Then Sekundy = 0 End Select Case 2: Select Case Poz_ust Case 0: Ar_tr1 = Not Ar_tr1 Case 1: Incr G(1) If G(1) = 24 Then G(1) = 0 Case 2: Incr M(1) If M(1) = 60 Then M(1) = 0 Case 3: Incr G(2) If G(2) = 24 Then G(2) = 0 Case 4: Incr M(2) If M(2) = 60 Then M(2) = 0 Case 5: Incr G(3) If G(3) = 24 Then G(3) = 0 Case 6: Incr M(3) If M(3) = 60 Then M(3) = 0 Case 7: Incr G(4) If G(4) = 24 Then G(4) = 0 Case 8: Incr M(4) If M(4) = 60 Then M(4) = 0 End Select Autosave = 0 Case 4: Select Case Poz_ust Case 0: Term_ena = Not Term_ena If Term_ena = 0 Then Tr_term = 1 Case 1: If T_ust > 10 Then T_ust = T_ust - 10 Case 2: If T_his > 5 Then Decr T_his End Select Autosave = 0 Case 5: Select Case Poz_ust Case 0: ' podczas wysw alarmow Case 1: If Alarm <> 0 Then Alarm = 0 Else Alarm = 1 Buzz = 1 Case 2: If Salarm <> 0 Then Salarm = 0 Else Salarm = 1 End Select Autosave = 0 End Select Case &B11111000: 'ustaw next '*********************************************************** Select Case Poz_wysw Case 1: Incr Poz_ust If Poz_ust = 4 Then Poz_ust = 0 Case 2: Incr Poz_ust If Poz_ust = 9 Then Poz_ust = 0 Case 4: Incr Poz_ust If Poz_ust = 3 Then Poz_ust = 0 Case 5: Incr Poz_ust If Poz_ust = 3 Then Poz_ust = 0 End Select Case &B11110100: 'decr '*********************************************************** Select Case Poz_wysw Case 1: Select Case Poz_ust Case 0: 'podczas wyswietania czasu If Alarm = 2 Then Alarm = 0 Case 1: Decr Godziny If Godziny = 255 Then Godziny = 23 Case 2: Decr Minuty If Minuty = 255 Then Minuty = 59 Case 3: Decr Sekundy If Sekundy = 255 Then Sekundy = 59 End Select Case 2: Select Case Poz_ust Case 0: Ar_tr2 = Not Ar_tr2 Case 1: Decr G(1) If G(1) = 255 Then G(1) = 23 Case 2: Decr M(1) If M(1) = 255 Then M(1) = 59 Case 3: Decr G(2) If G(2) = 255 Then G(2) = 23 Case 4: Decr M(2) If M(2) = 255 Then M(2) = 59 Case 5: Decr G(3) If G(3) = 255 Then G(3) = 23 Case 6: Decr M(3) If M(3) = 255 Then M(3) = 59 Case 7: Decr G(4) If G(4) = 255 Then G(4) = 23 Case 8: Decr M(4) If M(4) = 255 Then M(4) = 59 End Select Autosave = 0 Case 4: Select Case Poz_ust Case 0: Term_ena = Not Term_ena If Term_ena = 0 Then Tr_term = 1 Case 1: If T_ust > 100 Then T_ust = T_ust - 10 Case 2: If T_his > 5 Then Decr T_his End Select Autosave = 0 Case 5: Select Case Poz_ust Case 0: ' podczas wysw alarmow Case 1: If Alarm <> 0 Then Alarm = 0 Else Alarm = 1 Buzz = 1 Case 2: If Salarm <> 0 Then Salarm = 0 Else Salarm = 1 End Select Autosave = 0 End Select Case &B11011100: 'next menu '*********************************************************** Poz_ust = 0 Wyswietlanie = Poz_wysw + 1 If Wyswietlanie = 6 Then Wyswietlanie = 1 Case &B10111100: 'triak 1 '*********************************************************** Select Case Poz_wysw Case 2: Ar_tr1 = 0 Tr1 = Not Tr1 Case 4: Term_ena = 0 Tr_term = Not Tr_term Case 5: If Alarm = 2 Then Alarm = 0 'If Salarm = 2 Then Salarm = 0 End Select Autosave = 0 Case &B01111100: 'triak 2 '*********************************************************** Select Case Poz_wysw Case 2: Ar_tr2 = 0 Tr2 = Not Tr2 Autosave = 0 Case 4: Term_ena = 0 Tr_term = Not Tr_term Case 5: If Alarm = 2 Then Alarm = 0 If Salarm = 2 Then Salarm = 0 End Select Autosave = 0 End Select End If Else Stan_sw = Sw Licznik_sw = 60 End If Return '*********************************************** '*********************************************** '************** LCD *********** Wysw_lcd: If Wyswietlanie <> 0 Then Wyswietlono = 0 Select Case Wyswietlanie Case 1: 'aktualny czas '************************************************************** If Poz_wysw <> 1 Then Cls Lcd " Aktualny Czas " End If Locate 2 , 5 If Poz_ust = 1 And Mig = 1 Then Lcd " " Else If Godziny < 10 Then Lcd "0" Lcd Godziny End If If Poz_ust = 0 And Mig = 1 Then Lcd " " Else Lcd ":" If Poz_ust = 2 And Mig = 1 Then Lcd " " Else If Minuty < 10 Then Lcd "0" Lcd Minuty End If If Poz_ust = 0 And Mig = 1 Then Lcd " " Else Lcd ":" If Poz_ust = 3 And Mig = 1 Then Lcd " " Else If Sekundy < 10 Then Lcd "0" Lcd Sekundy End If Case 2: 'programator czasowy '************************************************************** If Poz_wysw <> 2 Then Cls End If Locate 1 , 1 If Poz_ust = 1 And Mig = 1 Then Lcd " " Else If G(1) < 10 Then Lcd "0" Lcd G(1) End If Lcd ":" If Poz_ust = 2 And Mig = 1 Then Lcd " " Else If M(1) < 10 Then Lcd "0" Lcd M(1) End If Lcd " - " If Poz_ust = 3 And Mig = 1 Then Lcd " " Else If G(2) < 10 Then Lcd "0" Lcd G(2) End If Lcd ":" If Poz_ust = 4 And Mig = 1 Then Lcd " " Else If M(2) < 10 Then Lcd "0" Lcd M(2) End If Lcd " " If Ar_tr1 = 1 Then Lcd "A" Else Lcd "R" Locate 2 , 1 If Poz_ust = 5 And Mig = 1 Then Lcd " " Else If G(3) < 10 Then Lcd "0" Lcd G(3) End If Lcd ":" If Poz_ust = 6 And Mig = 1 Then Lcd " " Else If M(3) < 10 Then Lcd "0" Lcd M(3) End If Lcd " - " If Poz_ust = 7 And Mig = 1 Then Lcd " " Else If G(4) < 10 Then Lcd "0" Lcd G(4) End If Lcd ":" If Poz_ust = 8 And Mig = 1 Then Lcd " " Else If M(4) < 10 Then Lcd "0" Lcd M(4) End If Lcd " " If Ar_tr2 = 1 Then Lcd "A" Else Lcd "R" Case 3: '************************************************************** If Poz_wysw <> 3 Then Deflcdchar 0 , 7 , 5 , 7 , 32 , 32 , 32 , 32 , 32 'stopien Deflcdchar 1 , 4 , 32 , 14 , 17 , 17 , 17 , 14 , 32 ' o kreskowane Cls Lcd "Dw" ; Chr(1) ; "r:" Lowerline Lcd "Dom:" End If For N = 1 To 2 Hword(1) = T(n) / 10 Hword(2) = 11 If Hword(1) >= 10 Then Decr Hword(2) If Hword(1) >= 100 Then Decr Hword(2) If Minusy.n = 1 Then Decr Hword(2) Locate N , Hword(2) Hword(2) = T(n) Mod 10 Lcd " " If Minusy.n = 1 Then Lcd "-" Lcd Hword(1) ; "." ; Hword(2) ; Chr(0) ; "C" Next N Case 4: '************************************************************** If Poz_wysw <> 4 Then Deflcdchar 2 , 4 , 4 , 31 , 4 , 4 , 32 , 31 , 32 '+- Cls Lcd "Term:" End If Hword(1) = T(3) / 10 Hword(2) = 8 If Hword(1) >= 10 Then Decr Hword(2) If Hword(1) >= 100 Then Decr Hword(2) If Minusy.3 = 1 Then Decr Hword(2) Locate 1 , Hword(2) Hword(2) = T(3) Mod 10 Lcd " " If Minusy.3 = 1 Then Lcd "-" Lcd Hword(1) ; "." ; Hword(2) ; Chr(0) ; "C " If Term_ena = 1 Then Lcd "A" Else Lcd "R" If Poz_ust = 1 And Mig = 1 Then Locate 2 , 1 Lcd " " Else 'tust Hword(1) = T_ust / 10 Hword(2) = 3 If Hword(1) >= 10 Then Decr Hword(2) If Hword(1) >= 100 Then Decr Hword(2) Locate 2 , Hword(2) Hword(2) = T_ust Mod 10 Lcd Hword(1) ; "." ; Hword(2) ; Chr(0) ; "C " ; Chr(2) End If If Poz_ust = 2 And Mig = 1 Then Locate 2 , 11 Lcd " " Else 'thist Hbyte(1) = T_his / 10 Hbyte(2) = 12 If Hbyte(1) >= 10 Then Decr Hbyte(2) If Hbyte(1) >= 100 Then Decr Hbyte(2) Locate 2 , Hbyte(2) Hbyte(2) = T_his Mod 10 Lcd Hbyte(1) ; "." ; Hbyte(2) ; Chr(0) ; "C" End If Case 5: '************************************************************** If Poz_wysw <> 5 Then Cls Lcd "Alarm: " Lowerline Lcd "C. Alarm: " End If Locate 1 , 8 If Poz_ust = 1 And Mig = 1 Then Lcd " " Else If Alarm = 0 Then Lcd "Wyl. " If Alarm = 1 Then Lcd "Czuwa" If Alarm = 2 Then If A_g < 10 Then Lcd "0" Lcd A_g ; ":" If A_m < 10 Then Lcd "0" Lcd A_m End If End If Locate 2 , 11 If Poz_ust = 2 And Mig = 1 Then Lcd " " Else If Salarm = 0 Then Lcd "Wyl. " If Salarm = 1 Then Lcd "Czuwa" If Salarm = 2 Then If A_g < 10 Then Lcd "0" Lcd A_g ; ":" If A_m < 10 Then Lcd "0" Lcd A_m End If End If End Select Poz_wysw = Wyswietlanie Wyswietlanie = 0 Wyswietlono = 1 End If Return '************** LCD Koniec *********** '********** rejestracja DS ********* Ds_reg: Disable Interrupts 1wreset 1wwrite &H33 Ds(1) = 1wread(8): Enable Interrupts If Ds(1) <> 255 Or Ds(2) <> 255 Or Ds(3) <> 255 Or Ds(4) <> 255 Or Ds(5) <> 255 Or Ds(6) <> 255 Or Ds(7) <> 255 Or Ds(8) <> 255 Then Ds_ok = 1 For N = 1 To 8 Hbyte(1) = Ds_adr + N Writeeeprom Ds(n) , Hbyte(1) Next N Else Ds_ok = 0 End If Return '****** rejestracja DS Koniec ****** '************* Temperatura *********** Temperatura: Disable Interrupts 1wreset Select Case Ktory_ds Case 0: 1wwrite &HCC 1wwrite &H44 Case 1: 1wwrite &H55 1wwrite Ds1(1) , 8 1wwrite &HBE Lsb = 1wread(): Msb = 1wread(): Case 2: 1wwrite &H55 1wwrite Ds2(1) , 8 1wwrite &HBE Lsb = 1wread(): Msb = 1wread(): Case 3: 1wwrite &H55 1wwrite Ds3(1) , 8 1wwrite &HBE Lsb = 1wread(): Msb = 1wread(): End Select Enable Interrupts If Ktory_ds > 0 Then T(ktory_ds) = Msb * 256 T(ktory_ds) = T(ktory_ds) + Lsb Minusy.ktory_ds = Msb.7 If Minusy.ktory_ds = 1 Then T(ktory_ds) = Not T(ktory_ds) T(ktory_ds) = T(ktory_ds) + 1 End If T(ktory_ds) = T(ktory_ds) * 10 T(ktory_ds) = T(ktory_ds) / 16 Else If Poz_wysw = 1 And Wyswietlono = 1 Then Wyswietlanie = 1 End If Incr Ktory_ds If Ktory_ds = 4 Then Ktory_ds = 0 Return '********** Temperatura Koniec ********* '****************** Zapis RTC ******************** Zapis_rtc: 'zapisuje czas g,m i s do uk?adu RTC Godziny_bcd = Makebcd(godziny) Minuty_bcd = Makebcd(minuty) Sekundy_bcd = Makebcd(sekundy) I2cstart I2cwbyte 162 I2cwbyte 0 'rejestr kontrolny I2cwbyte 8 'ustawienie rejestru zapisu daty I2cstop I2cstart I2cwbyte 162 I2cwbyte 2 I2cwbyte Sekundy_bcd I2cwbyte Minuty_bcd I2cwbyte Godziny_bcd I2cstop Return '************* Zapis RTC Koniec ***************** '****************** Odczyt RTC ******************** Odczyt_rtc: 'odczytuje czas z RTC do zmiennych I2cstart I2cwbyte 162 'adres zapisu I2cwbyte 2 'rejestr sekund I2cstart I2cwbyte 163 'adres odczytu I2crbyte Sekundy_bcd , Ack I2crbyte Minuty_bcd , Ack I2crbyte Godziny_bcd , Nack I2cstop Sekundy = Makedec(sekundy_bcd) Minuty = Makedec(minuty_bcd) Godziny = Makedec(godziny_bcd) Return '************** Odczyt RTC Koniec ***************** '************ Sterowanie *************** '*********************************************** Sterowanie: Hword(1) = Godziny * 60 Hword(1) = Hword(1) + Minuty 'czas w minutach 'prog czasowy For N = 1 To 3 Step 2 Hword(2) = G(n) * 60 Hword(2) = Hword(2) + M(n) 'czas w minutach Hword(3) = G(1 + N) * 60 Hword(3) = Hword(3) + M(1 + N) 'czas w minutach Hbit = 1 If Hword(3) >= Hword(2) Then If Hword(1) >= Hword(2) Then If Hword(1) < Hword(3) Then Hbit = 0 Else If Hword(1) >= Hword(2) Then Hbit = 0 If Hword(1) < Hword(3) Then Hbit = 0 End If If N = 1 Then If Ar_tr1 = 1 Then Tr1 = Hbit Else If Ar_tr2 = 1 Then Tr2 = Hbit End If Next N 'termostat If Term_ena = 1 Then Hword(1) = T_ust + T_his Hword(2) = T_ust - T_his If T(3) >= Hword(1) Then Tr_term = 1 If T(3) <= Hword(2) Then Tr_term = 0 End If Return '******** Sterowanie Koniec ************