'************* Konfiguracja uC *****************
'***********************************************
$regfile = "m8def.dat"
$crystal = 16000000

Config Portb = &B00000000 : Portb = &B11111111
Config Portc = &B11111111 : Portc = &B11111111
Config Portd = &B11111111 : Portd = &B11111111

Config Timer0 = Timer , Prescale = 256
Enable Timer0 : On Timer0 Prztimer0
Enable Interrupts

Buzzer Alias Portc.0

'********** Konfiguracja uC Koniec  ************

'******************  Zmienne *******************
'***********************************************
Dim Flaga_4ms As Bit
Dim Mig As Bit
Dim Dwukropek As Bit
Dim Odliczanie As Bit
Dim Wyswietlanie As Bit
Dim Alarm As Bit
Dim Ktory_w As Byte

Dim Wartosc(2) As Byte                                      'godziny minuty : minuty sekundy
Dim W(4) As Byte                                            '4 cyfry wyswietlane po przeliczeniu z powyzszych

Dim Co4ms As Byte , Co1s As Byte
Dim G As Byte , M As Byte , S As Byte

Dim Buz_sygnal As Byte
Dim Sw_port As Byte , Stan_portu As Byte , Licznik_sw As Byte
Dim Autostart As Word
Dim Autooff As Word
Dim Licznik_mig As Byte
'*************  Zmienne Koniec  ****************

'************ Warunki Poczatkowe ***************
'***********************************************

'*******  Warunki Poczatkowe Koniec  ***********

'**************  Petla glowna ******************
'***********************************************
Do

If Flaga_4ms = 1 Then
  Flaga_4ms = 0

  If Buz_sygnal > 0 Then
    Buzzer = 0
    Decr Buz_sygnal
  Else
    Buzzer = 1
  End If

  If Odliczanie = 0 Then
    If Autostart > 0 Then                                   'uruchomienie autostart=750
      Decr Autostart
      If Autostart = 1 Then
        Buz_sygnal = 40
        Odliczanie = 1
        Timer0 = 6
        Co4ms = 0
        S = 0
      End If
    End If
  Else
    Autostart = 0
  End If

  If Wyswietlanie = 1 Then
    Gosub Wyswietl
    If Odliczanie = 0 And Alarm = 0 Then
      Incr Autooff
      If Autooff = 2500 Then
        Wyswietlanie = 0
      End If
    Else
      Autooff = 0
    End If
  Else
    Portd = 255
    Portc = 255
  End If



  Gosub Przyciski

End If


Loop
End
'************ Petla glowna Koniec **************


'************ Przerwanie timer 0 ***************
'***********************************************
Prztimer0:                                                  'co 4ms
Timer0 = 6
Set Flaga_4ms
Incr Co4ms
If Co4ms = 250 Then
  Co4ms = 0
  If Odliczanie = 1 Then
    Dwukropek = Not Dwukropek
    Decr S
    If S = 255 Then
      S = 59
      Decr M
      If M = 255 Then
        M = 59
        Decr G
      End If
    End If

    If G = 0 And M = 0 And S = 0 Then
      Odliczanie = 0
      Alarm = 1
    End If
  Else
    Dwukropek = 0
  End If

End If

Return
'******** Przerwanie timer 0 Koniec ************
Przyciski:
Sw_port = Pinb And &B00011111

If Sw_port <> 31 And Stan_portu = Sw_port Then
    Incr Licznik_sw
    If Licznik_sw = 120 Then
      Licznik_sw = 0
      Buz_sygnal = 10
      Autooff = 0
      Wyswietlanie = 1
      Select Case Stan_portu
        Case 30:
         If Alarm = 0 Then
           Incr M
           Autostart = 750
         End If
        Case 29:
         If Alarm = 0 Then
           M = M + 5
           Autostart = 750
         End If
        Case 27:
         If Alarm = 0 Then
           M = M + 10
           Autostart = 750
         End If
        Case 23:
         If Alarm = 0 Then
           M = M + 20
           Autostart = 750
         End If
        Case 15:
         Odliczanie = 0
         Autostart = 0
         S = 0
         M = 0
         G = 0
         Alarm = 0
      End Select

      If M > 59 Then
        Incr G
        M = M Mod 60
      End If

    End If
  Else
    Stan_portu = Sw_port
    Licznik_sw = 70
  End If
Return


'*************************************************
Wyswietl:
 Incr Ktory_w
  If Ktory_w = 4 Then Ktory_w = 0

  If G > 0 Then
    Wartosc(1) = G
    Wartosc(2) = M
  Else
    Wartosc(1) = M
    Wartosc(2) = S
  End If

  W(1) = Wartosc(1) / 10
  W(2) = Wartosc(1) Mod 10
  W(3) = Wartosc(2) / 10
  W(4) = Wartosc(2) Mod 10
  If W(1) = 0 Then W(1) = 10

  If Alarm = 1 Then
    Incr Licznik_mig
    If Licznik_mig = 50 Then
      Licznik_mig = 0
      Mig = Not Mig
    End If

    If Mig = 0 Then
      W(1) = 11
      W(2) = 11
      W(3) = 11
      W(4) = 11
      Dwukropek = 0
      Buzzer = 0
    Else
      W(1) = 10
      W(2) = 10
      W(3) = 10
      W(4) = 10
      Dwukropek = 1
      Buzzer = 1
    End If
  End If

  Select Case Ktory_w
  Case 0:
    Set Portc.2
    W(4) = Lookup(w(4) , Tabela)
    Portd = W(4)
    Portd.7 = Dwukropek
    Reset Portc.5
  Case 1:
    Set Portc.5
    W(3) = Lookup(w(3) , Tabela)
    Portd = W(3)
    Portd.7 = Dwukropek
    Reset Portc.4
  Case 2:
    Set Portc.4
    W(2) = Lookup(w(2) , Tabela)
    Portd = W(2)
    Portd.7 = Dwukropek
    Reset Portc.3
  Case 3:
    Set Portc.3
    W(1) = Lookup(w(1) , Tabela)
    Portd = W(1)
    Portd.7 = Dwukropek
    Reset Portc.2
  End Select

Return
'*************************************************
Tabela:
    Data 192 , 249 , 164 , 176 , 153 , 146 , 130 , 248 , 128 , 144 , 255 , 191
'znak:    0     1     2     3     4     5     6     7     8     9    pusty  -