Код достаточно сырой, но вполне рабочий. Контроллер - Atmega8 Часы - pcf2129 градусник - ds18b20 Я использовал ОЛЕД дисплей 1602. Который после команды очистки дисплея попросту вырубается. Потому для очистки оного заливаю его пробелами.
Config 1wire = Portd.3 Config Lcd = 16 * 2 Config Lcdpin = Pin , Rs = Portb.5 , E = Portb.4 , Db4 = Portb.3 , Db5 = Portb.2 , Db6 = Portb.1 , Db7 = Portb.0 Config Sda = Portd.5 : Config Scl = Portc.0 Const Pcf21w = &HA2 : Const Pcf21r = &HA3 Ddrc.1 = 0 Ddrc.2 = 0 Ddrc.3 = 0 Ddrc.4 = 0 Dim Time_w As Word , Disp_temp As String * 8 Time_w = 15625 Config Timer1 = Timer , Prescale = 256 On Ovf1 Tim1_isr Load Timer1 , Time_w Enable Interrupts Enable Timer1 L Alias Pinc.1 : Dn Alias Pinc.2 : Up Alias Pinc.3 : R Alias Pinc.4 Dim Byte0 As Byte Dim Byte1 As Byte Dim Sign As String * 1 Dim T As Byte Dim T1 As Single Dim T2 As Single Dim Seco As Byte , Mine As Byte , Hour As Byte , Day As Byte , Dat As Byte , Month As Byte , Year As Byte Dim Weekday As String * 2 , Mo As Byte dim fdp as Bit Dim Disp_time As String * 10 , Pak(8) As String * 2 , Termo(2) As String * 4 , Termo_flag As Byte Dim Count As Byte Deflcdchar 2 , 12 , 18 , 18 , 12 , 32 , 32 , 32 , 32 ' replace [x]with number (0-7) Dim A As Byte 'Initlcd Cursor Off 11: Locate 1 , 1 : Lcd " " Locate 2 , 1 : Lcd " "
Gosub Pcfr Gosub Ds_read If L = 1 Then : Waitms 300 : Goto Nastr_chasu : End If If Up = 1 Then : Waitms 100 : Incr Count : End If If Dn = 1 Then : Waitms 100 : Decr Count : End If Loop
If Mo = 0 And Up = 1 Then : Waitms 100 : Incr Hour : End If If Mo = 0 And Dn = 1 Then : Waitms 100 : Decr Hour : End If If Mo = 1 And Up = 1 Then : Waitms 100 : Incr Mine : End If If Mo = 1 And Dn = 1 Then : Waitms 100 : Decr Mine : End If If Mo = 2 And Up = 1 Then : Waitms 100 : Incr Seco : End If If Mo = 2 And Dn = 1 Then : Waitms 100 : Decr Seco : End If If Mo = 3 And Up = 1 Then : Waitms 100 : Incr Day : End If If Mo = 3 And Dn = 1 Then : Waitms 100 : Decr Day : End If If Mo = 4 And Up = 1 Then : Waitms 100 : Incr Dat : End If If Mo = 4 And Dn = 1 Then : Waitms 100 : Decr Dat : End If If Mo = 5 And Up = 1 Then : Waitms 100 : Incr Month : End If If Mo = 5 And Dn = 1 Then : Waitms 100 : Decr Month : End If If Mo = 6 And Up = 1 Then : Waitms 100 : Incr Year : End If If Mo = 6 And Dn = 1 Then : Waitms 100 : Decr Year : End If
If Hour = 24 Then : Hour = 0 : End If If Hour = 255 Then : Hour = 23 : End If If Mine = 60 Then : Mine = 0 : End If If Mine = 255 Then : Mine = 59 : End If If Seco = 60 Then : Seco = 0 : End If If Seco = 255 Then : Seco = 59 : End If If Day = 7 Then : Day = 0 : End If If Day = 255 Then : Day = 6 : End If If Dat = 32 Then : Dat = 1 : End If If Dat = 255 Then : Dat = 31 : End If If Month = 13 Then : Month = 1 : End If If Month = 255 Then : Month = 12 : End If If Year = 100 Then : Year = 0 : End If If Year = 255 Then : Year = 99 : End If
Select Case Day Case 0 : Weekday = Chr(23) + "H" '0 Case 1 : Weekday = "BT" Case 2 : Weekday = "CP" Case 3 : Weekday = Chr(249) + "T" Case 4 : Weekday = Chr(23) + "T" Case 5 : Weekday = "Cb" Case 6 : Weekday = "Hg" End Select
Locate 1 , 1 If Mo = 0 And Fdp = 1 Then : Lcd " " : Else : If Hour < 10 Then : Lcd "0" ; Hour : Else : Lcd Hour : End If : End If Lcd ":" If Mo = 1 And Fdp = 1 Then : Lcd " " : Else : If Mine < 10 Then : Lcd "0" ; Mine : Else : Lcd Mine : End If : End If Lcd ":" If Mo = 2 And Fdp = 1 Then : Lcd " " : Else : If Seco < 10 Then : Lcd "0" ; Hour : Else : Lcd Seco : End If : End If Locate 2 , 1 If Mo = 3 And Fdp = 1 Then : Lcd " " : Else : Lcd Weekday : End If Lcd "," If Mo = 4 And Fdp = 1 Then : Lcd " " : Else : If Dat < 10 Then : Lcd "0" ; Dat : Else : Lcd Dat : End If : End If Lcd "/" If Mo = 5 And Fdp = 1 Then : Lcd " " : Else : If Month < 10 Then : Lcd "0" ; Month : Else : Lcd Month : End If : End If Lcd "/20" If Mo = 6 And Fdp = 1 Then : Lcd " " : Else : If Year < 10 Then : Lcd "0" ; Year : Else : Lcd Year : End If : End If
If R = 1 Then : Waitms 250 : Incr Mo : End If If Mo => 7 Then : Mo = 0 : Gosub Pcfw : End If Loop
Pcfw: Seco = 0 Seco = Makebcd(seco) : Mine = Makebcd(mine) : Hour = Makebcd(hour) : Day = Makebcd(day) Dat = Makebcd(dat) : Month = Makebcd(month) : Year = Makebcd(year) I2cstart I2cwbyte Pcf21w I2cwbyte &H03 I2cwbyte Seco I2cwbyte Mine I2cwbyte Hour I2cwbyte Dat I2cwbyte Day I2cwbyte Month I2cwbyte Year I2cstop Gosub 11 Return
Вот почти готовый код. Есть возможность управления освещением. Плавное включение и выключение с предустановленными порогами яркости. Еще не определился конкретно к чему будет подключено освещение. К внешнему ШИМ контроллеру или микроконтроллеру, потому эта функция пока не задействована. Скорее всего будет через pca9632
Dim Time_w As Word , Disp_temp As String * 8 Time_w = 15625 Config Timer1 = Timer , Prescale = 256 On Ovf1 Tim1_isr Load Timer1 , Time_w Enable Interrupts Enable Timer1 L Alias Pinc.1 : Dn Alias Pinc.2 : Up Alias Pinc.3 : R Alias Pinc.4 Dim Byte0 As Byte Dim Byte1 As Byte Dim Sign As String * 1 Dim T As Byte Dim T1 As Single Dim T2 As Single Dim Seco As Byte , Mine As Byte , Hour As Byte , Day As Byte , Dat As Byte , Month As Byte , Year As Byte Dim Weekday As String * 2 , Mo As Byte Dim Fdp As Bit Dim Disp_time As String * 10 , Pak(8) As String * 2 , Termo(2) As String * 4 , Termo_flag As Byte Dim Count As Byte Dim Light(2) As Byte dim tim_l as Byte Dim Dimmer(2) As Byte Dim Dimmer_e(2) As Eram Byte
Dim A As Byte 'Initlcd Cursor Off 11: Locate 1 , 1 : Lcd " " Locate 2 , 1 : Lcd " "
Do Locate 1 , 1 : Lcd Disp_time Locate 1 , 11 : Lcd Disp_temp Locate 2 , 1 : Lcd Weekday ; ", " ; Pak(3) ; "/" ; Pak(2) ; " " Locate 2 , 16 : Lcd Chr(light(1)) Gosub Pcfr Gosub Ds_read If L = 1 Then : Waitms 300 : Goto Nastr_chasu : End If If R = 1 Then : Waitms 100 : Incr Light(1) : End If If Dn = 1 Then : Waitms 100 : Goto Nastr_dimmer : End If If Light(2) > 40 Then : Tim_l = 3 : Else : Tim_l = 20 : End If If Light(1) = 3 Then : Light(1) = 0 : End If If Light(1) = 0 And Light(2) > 0 Then : Waitms Tim_l : Decr Light(2) : End If If Light(1) = 1 And Light(2) < dimmer(1) Then : Waitms Tim_l : Incr Light(2) : End If If Light(1) = 1 And Light(2) > Dimmer(1) Then : Waitms Tim_l : Decr Light(2) : End If If Light(1) = 2 And Light(2) < Dimmer(2) Then : Waitms Tim_l : Incr Light(2) : End If Loop
Nastr_dimmer: Mo = 0 Locate 1 , 1 : Lcd " " Locate 2 , 1 : Lcd " " Do If Mo = 0 And Up = 1 Then : Waitms 100 : Incr Dimmer(1) : End If If Mo = 0 And Dn = 1 Then : Waitms 100 : Decr Dimmer(1) : End If If Mo = 1 And Up = 1 Then : Waitms 100 : Incr Dimmer(2) : End If If Mo = 1 And Dn = 1 Then : Waitms 100 : Decr Dimmer(2) : End If Locate 1 , 1 Lcd Chr(1) ; "-" If Mo = 0 And Fdp = 1 Then : Lcd " " : Else : Lcd Dimmer(1) ; " " : End If Locate 2 , 1 Lcd Chr(2) ; "-" If Mo = 1 And Fdp = 1 Then : Lcd " " : Else : Lcd Dimmer(2) ; " " : End If If R = 1 Then : Waitms 250 : Incr Mo : End If If Mo => 2 Then : Mo = 0 : Dimmer_e(2) = Dimmer(2) : Dimmer_e(1) = Dimmer(1) : Gosub 11 : End If Loop
Nastr_chasu: Mo = 0 Locate 1 , 1 : Lcd " " Locate 2 , 1 : Lcd " " Do If Mo = 0 And Up = 1 Then : Waitms 100 : Incr Hour : End If If Mo = 0 And Dn = 1 Then : Waitms 100 : Decr Hour : End If If Mo = 1 And Up = 1 Then : Waitms 100 : Incr Mine : End If If Mo = 1 And Dn = 1 Then : Waitms 100 : Decr Mine : End If If Mo = 2 And Up = 1 Then : Waitms 100 : Incr Seco : End If If Mo = 2 And Dn = 1 Then : Waitms 100 : Decr Seco : End If If Mo = 3 And Up = 1 Then : Waitms 100 : Incr Day : End If If Mo = 3 And Dn = 1 Then : Waitms 100 : Decr Day : End If If Mo = 4 And Up = 1 Then : Waitms 100 : Incr Dat : End If If Mo = 4 And Dn = 1 Then : Waitms 100 : Decr Dat : End If If Mo = 5 And Up = 1 Then : Waitms 100 : Incr Month : End If If Mo = 5 And Dn = 1 Then : Waitms 100 : Decr Month : End If If Mo = 6 And Up = 1 Then : Waitms 100 : Incr Year : End If If Mo = 6 And Dn = 1 Then : Waitms 100 : Decr Year : End If
If Hour = 24 Then : Hour = 0 : End If If Hour = 255 Then : Hour = 23 : End If If Mine = 60 Then : Mine = 0 : End If If Mine = 255 Then : Mine = 59 : End If If Seco = 60 Then : Seco = 0 : End If If Seco = 255 Then : Seco = 59 : End If If Day = 7 Then : Day = 0 : End If If Day = 255 Then : Day = 6 : End If If Dat = 32 Then : Dat = 1 : End If If Dat = 255 Then : Dat = 31 : End If If Month = 13 Then : Month = 1 : End If If Month = 255 Then : Month = 12 : End If If Year = 100 Then : Year = 0 : End If If Year = 255 Then : Year = 99 : End If
Select Case Day Case 0 : Weekday = Chr(23) + "H" Case 1 : Weekday = "BT" Case 2 : Weekday = "CP" Case 3 : Weekday = Chr(249) + "T" Case 4 : Weekday = Chr(23) + "T" Case 5 : Weekday = "Cb" Case 6 : Weekday = "Hg" End Select
Locate 1 , 1 If Mo = 0 And Fdp = 1 Then : Lcd " " : Else : If Hour < 10 Then : Lcd "0" ; Hour : Else : Lcd Hour : End If : End If Lcd ":" If Mo = 1 And Fdp = 1 Then : Lcd " " : Else : If Mine < 10 Then : Lcd "0" ; Mine : Else : Lcd Mine : End If : End If Lcd ":" If Mo = 2 And Fdp = 1 Then : Lcd " " : Else : If Seco < 10 Then : Lcd "0" ; Hour : Else : Lcd Seco : End If : End If Locate 2 , 1 If Mo = 3 And Fdp = 1 Then : Lcd " " : Else : Lcd Weekday : End If Lcd "," If Mo = 4 And Fdp = 1 Then : Lcd " " : Else : If Dat < 10 Then : Lcd "0" ; Dat : Else : Lcd Dat : End If : End If Lcd "/" If Mo = 5 And Fdp = 1 Then : Lcd " " : Else : If Month < 10 Then : Lcd "0" ; Month : Else : Lcd Month : End If : End If Lcd "/20" If Mo = 6 And Fdp = 1 Then : Lcd " " : Else : If Year < 10 Then : Lcd "0" ; Year : Else : Lcd Year : End If : End If
If R = 1 Then : Waitms 250 : Incr Mo : End If If Mo => 7 Then : Mo = 0 : Gosub Pcfw : End If Loop
Pcfw: Seco = 0 Seco = Makebcd(seco) : Mine = Makebcd(mine) : Hour = Makebcd(hour) : Day = Makebcd(day) Dat = Makebcd(dat) : Month = Makebcd(month) : Year = Makebcd(year) I2cstart I2cwbyte Pcf21w I2cwbyte &H03 I2cwbyte Seco I2cwbyte Mine I2cwbyte Hour I2cwbyte Dat I2cwbyte Day I2cwbyte Month I2cwbyte Year I2cstop Gosub 11 Return