'DCF77- Sender V 3.01
$regfile = "ATtiny45.dat"
'Flash: 4kB
'RAM: 256 Byte!
$crystal = 8000000

Dim Zeit As Integer

Dim Nummer As Byte
Dim Telegramm As String * 60
Dim Zeichen As String * 1
Dim Teil As String * 22
Dim H As String * 8
Dim H1 As String * 1

Dim Wert As Integer
Dim Wertigkeit(8) As Byte
Dim Preload0 As Byte
Dim Count As Byte
Dim Sekunde As Byte
Dim Minute As Byte
Dim Stunde As Byte
Dim Tag As Byte
Dim Wtag As Byte
Dim Monat As Byte
Dim Jahr As Byte
Dim Mesz As Byte

'Laufvariable:
Dim I As Byte
Dim K As Byte
Dim P As Byte
Dim T As Byte
Dim Temp As Byte

'Stringlänge:
Dim L As Byte

Dim Paritaet As Single

'Port für invertierte Signale:
Config Portb.= Output
'Port für nicht invertierte Signale:
Config Portb.= Output

Config Timer0 = Timer , Prescale = 1024
On Timer0 Kurzzeit
Enable Timer0
'Timer0,Preloadwert für 1/100 Sekunde:
Preload0 = &HB2
Enable Interrupts

'für: 14:12,Di,5.3.13:
'                      1         2       P 3    P    4         5       P 6
'                 5    0    5    0    5    0    5    0    5    0    5    0
'Telegramm = "0000000000000000000101001000001010010100001011000110010000 0"

Wertigkeit(1) = 80
Wertigkeit(2) = 40
Wertigkeit(3) = 20
Wertigkeit(4) = 10
Wertigkeit(5) = 8
Wertigkeit(6) = 4
Wertigkeit(7) = 2
Wertigkeit(8) = 1
'-------------------------------------------------------------------------------
'Hauptprogramm
Mesz = 1
Stunde = 22
Minute = 22
Tag = 22
Wtag = 4
Monat = 12
Jahr = 22

Do
   Gosub Zeittelegramm
   For Nummer = 1 To 60
      Zeichen = Mid(telegramm , Nummer , 1)
      If Zeichen = " " Then
      'Sekunde 59
         Zeit = 1000
      Else
      'Sekunde 60-58
         Wert = Val(zeichen)
         Portb.= 0
         Portb.= Not Portb.0
         If Wert = 0 Then
         'log 0
            Zeit = 100
         Else
         'log 1
            Zeit = 200
         End If

         Waitms Zeit
         Zeit = 1000 - Zeit
      End If

      Portb.= 1
      Portb.= Not Portb.0
      Waitms Zeit

   Next Nummer
Loop
End                                                         'end program
'-------------------------------------------------------------------------------
Zeittelegramm:
Telegramm = ""
For I = 1 To 15
   Telegramm = Telegramm + "0"
Next I

'Bit 16: "1": Am Ende der Std. wird auf MEZ/MESZ umgestellt
Telegramm = Telegramm + "0"

If Mesz = 0 Then
   'Bit 17: "0"=MEZ
   Telegramm = Telegramm + "0"
   'Bit 18: "1"=MEZ
   Telegramm = Telegramm + "1"
Else
   'Bit 17: "1"=MESZ
   Telegramm = Telegramm + "1"
   'Bit 18: "0"=MESZ
   Telegramm = Telegramm + "0"
End If

'Bit 19: "1"= Am Ende der Stunde wird eine Schaltsekunde eingefügt
Telegramm = Telegramm + "0"

'Bit20: Immer "1"
Telegramm = Telegramm + "1"

'Minute:
Wert = Minute
Gosub Kodieren
= Teil
Teil = Left(, 7)
Telegramm = Telegramm + Teil
Gosub Paritaet
Telegramm = Telegramm + Teil

'Stunde:
Wert = Stunde
Gosub Kodieren
= Teil
Teil = Left(, 6)
Telegramm = Telegramm + Teil
Gosub Paritaet
Telegramm = Telegramm + Teil

'Kalendertag:
Wert = Tag
Gosub Kodieren
= Teil
Teil = Left(, 6)
Telegramm = Telegramm + Teil

'Wochentag:
Wert = Wtag
Gosub Kodieren
= Teil
Teil = Left(, 3)
Telegramm = Telegramm + Teil

'Monat:
Wert = Monat
Gosub Kodieren
= Teil
Teil = Left(, 5)
Telegramm = Telegramm + Teil

'Jahr:
Wert = Jahr
Gosub Kodieren
= Teil
Teil = Left(, 8)
Telegramm = Telegramm + Teil

Teil = Mid(telegramm , 36 , 22)
Gosub Paritaet
Telegramm = Telegramm + Teil
Telegramm = Telegramm + " 0"

Return
'-------------------------------------------------------------------------------
Kodieren:

Teil = ""
For K = 1 To 8
   Temp = Wert
   Wert = Wert - Wertigkeit(k)

   If Wert < 0 Then
      Teil = "0" + Teil
      Wert = Temp
   Else
      Teil = "1" + Teil
   End If
   Temp = Wert
Next K

Return
'-------------------------------------------------------------------------------
Paritaet:
= 0
= Len(teil)
For I = 1 To L
= Mid(teil , I , 1)
= Val(h)
= P + T
Next I
'Paritaetstest
Paritaet = P / 2
Paritaet = Frac(paritaet)
If Paritaet = 0 Then
   'gerade Parität
   Teil = "0"
Else
   'Ungerade Parität
   Teil = "1"
End If

Return
'-------------------------------------------------------------------------------
Kurzzeit:
Timer0 = Preload0
Incr Count
If Count = 100 Then
   Count = 0
    Incr Sekunde
    If Sekunde > 59 Then
      Sekunde = 0
      Incr Minute
      If Minute > 59 Then
         Minute = 0
         Incr Stunde
         If Stunde > 23 Then
            Stunde = 0
            Incr Tag
            Incr Wtag
            If Wtag > 7 Then Wtag = 1
            If Tag > 31 Then
               Tag = 0
               Incr Monat
               If Monat > 12 Then
                  Monat = 0
                  Incr Jahr
                  If Jahr > 99 Then
                     Jahr = 0
                  End If
               End If
            End If
         End If
      End If
    End If
End If
Return
'-------------------------------------------------------------------------------