Code:
'*******************************************************************************
'* Funkuhr *
'* *
'* M.Lenkeit 12.11.2006 *
'*******************************************************************************
'200ms low = 1 / 100ms low =0 / syncbit nach 1sec ohne impuls
Declare Sub Set_softuhr()
Declare Sub Gettemp()
$regfile = "m8def.dat"
$crystal = 3686400
$baud = 19200
$hwstack = 128
$swstack = 256
$framesize = 256
'Config Watchdog = 2048
'Start Watchdog
'Timer für 40Hz (25ms)
Config Timer0 = Timer , Prescale = 1024
Const Startwert = 166
Timer0 = Startwert
On Timer0 Tim0_isr
Enable Interrupts
Enable Timer0
'Lcd ansteuerung
Config Lcd = 16 * 4
Config Portd = Output
Config Lcdpin = Pin , Rs = Portd.2 , E = Portd.3 , Db4 = Portd.4 , Db5 = Portd.5 , Db6 = Portd.6 , Db7 = Portd.7
Config Lcdbus = 4
Config Lcdmode = Port
'Pin für DS18S20
Config 1wire = Portc.0
'Pin C.5 für DCF Eingang
Ddrc.5 = 0
Portc.5 = 1
Dcf77 Alias Pinc.5
'LED Signal auf Ausgang
Ddrc.1 = 1
Deflcdchar 0 , 2 , 9 , 5 , 21 , 5 , 9 , 2 , 32
'********************* Variablen ***********************************************
Dim I As Integer
Dim Sig_error As Byte
Dim Sync_dcf77_time As Byte
Dim Empfang As Bit
Dim Sync As Bit
Dim Dcf77_time As Byte
Dim Minutenmarke As Byte
Dim Signal_high As Integer
Dim Signal_low As Integer
Dim High_old As Integer
Dim Low_old As Integer
Dim Sek_timer As Integer
Dim Syncbit(60) As Byte
Dim Syncimpuls As Byte
Dim Soft_sec As Byte , Soft_min As Byte , Soft_std As Byte
Dim Soft_tag As Byte , Soft_monat As Byte , Soft_jahr As Byte
Dim Schaltjahr As Byte
Dim Sec_str As String * 2
Dim Min_str As String * 2
Dim Std_str As String * 2
Dim Day_str As String * 2
Dim Mon_str As String * 2
Dim Jahr_str As String * 4
Dim Day As String * 2
Dim Minute As Byte , Stunde As Byte
Dim Tag As Byte , Monat As Byte , Jahr As Byte , Wochentag As Byte
Dim Min_bits As Byte , Std_bits As Byte , Day_bits As Byte
Dim Parity_min As Byte , Parity_std As Byte , Parity_datum As Byte
Dim Anzeige As Byte
Dim Dcf77_err As Byte
Dim Temp_count As Byte
Dim T1 As Byte
Dim Bd(9) As Byte
Dim Tmp As Integer
Dim T As Integer
Dim Temp As Single
Dim Busy As Byte
'Dim Crc As Byte
'************* Start Hauptprogramm ********************************************
Initlcd
Cls
Cursor Off
Dcf77_err = 0
Sig_error = 0
Sync_dcf77_time = 0
Dcf77_time = 1
Syncimpuls = 0
Sek_timer = 0
Soft_sec = 0
Soft_min = 0
Soft_std = 0
'Day = "--"
Wochentag = 0
Soft_tag = 0
Soft_monat = 0
Soft_jahr = 0
Temp = 0.0
Temp_count = 0
T1 = 0
Anzeige = 1
Do
If Anzeige = 1 Then
Anzeige = 0
Day = Lookupstr(wochentag , Wtag)
Std_str = Str(soft_std) : Std_str = Format(std_str , "00")
Min_str = Str(soft_min) : Min_str = Format(min_str , "00")
Sec_str = Str(soft_sec) : Sec_str = Format(sec_str , "00")
Day_str = Str(soft_tag) : Day_str = Format(day_str , "00")
Mon_str = Str(soft_monat) : Mon_str = Format(mon_str , "00")
Jahr_str = Str(soft_jahr) : Jahr_str = Format(jahr_str , "00")
Locate 1 , 1 : Lcd " " ; Day ; ", " ; Day_str ; "." ; Mon_str ; "." ; "20" ; Jahr_str ; " "
Locate 2 , 1 : Lcd " " ; Std_str ; ":" ; Min_str ; ":" ; Sec_str ; " "
Locate 4 , 1 : Lcd " " ; Fusing(temp , "##.#") ; Chr(223) ; "C "
End If
' : blinken lassen
If Empfang = 1 And Sek_timer > 19 Then
Locate 2 , 10 : Lcd " "
End If
' Dcf77 Zeit alle 6 Std updaten
If Sync_dcf77_time = 6 Then
Sync_dcf77_time = 0
If Empfang = 1 Then Dcf77_time = 1
End If
' bei Dcf77 Zeitabgleich Symbol anzeigen
If Minutenmarke = 1 Then
If Sek_timer > 19 Then
Locate 3 , 1 : Lcd Chr(0)
Else
Locate 3 , 1 : Lcd " "
End If
Else
Locate 3 , 1 : Lcd " "
End If
' DS1820 Temperatur berechnen
Call Gettemp()
Loop
End 'end program
'------------------------ Interrupt Routine ------------------------------------
Tim0_isr:
Timer0 = Startwert
' reset watchdog
' Sekundentakt erzeugen
Incr Sek_timer ' 40x25ms= 1 sec
If Sek_timer = 40 Then
Call Set_softuhr()
Incr Syncimpuls
If Syncimpuls > 60 Then Syncimpuls = 0
End If
' Impulsauswertung
If Dcf77 = 0 Then
Signal_low = Signal_low + 25
Low_old = Signal_low
Signal_high = 0
Empfang = 1
Portc.1 = 0
Else
Signal_high = Signal_high + 25
High_old = Signal_high
Signal_low = 0
If Signal_high > 2500 Then Empfang = 0
Portc.1 = 1
End If
If High_old > 1600 And Empfang = 1 Then
If Signal_low > 0 Then Sync = 1
Else
Sync = 0
End If
If Dcf77_time = 1 And Sync = 1 Then
Dcf77_time = 0
Sek_timer = 0
Soft_sec = 0
Minutenmarke = 1
Syncimpuls = 0
For I = 1 To 60
Syncbit(i) = 0
Next I
Minute = 0
Stunde = 0
Tag = 0
Wochentag = 0
Monat = 0
Jahr = 0
End If
If Minutenmarke = 1 Then
If Signal_low = 0 Then
Select Case Low_old
Case Is < 70 ' abbruch bei schlechtem signal
Sig_error = 1
Goto Fehlerdcf77
Case 70 To 130 '70 to 130
Syncbit(soft_sec) = 0
Case 170 To 250 '170 to 230
Syncbit(soft_sec) = 1
End Select
End If
If Syncimpuls > 59 Then
If Syncbit(20) = 0 Then Goto Fehlerdcf77
If Syncbit(21) = 1 Then Minute = Minute + 1
If Syncbit(22) = 1 Then Minute = Minute + 2
If Syncbit(23) = 1 Then Minute = Minute + 4
If Syncbit(24) = 1 Then Minute = Minute + 8
If Syncbit(25) = 1 Then Minute = Minute + 10
If Syncbit(26) = 1 Then Minute = Minute + 20
If Syncbit(27) = 1 Then Minute = Minute + 40
If Syncbit(29) = 1 Then Stunde = Stunde + 1
If Syncbit(30) = 1 Then Stunde = Stunde + 2
If Syncbit(31) = 1 Then Stunde = Stunde + 4
If Syncbit(32) = 1 Then Stunde = Stunde + 8
If Syncbit(33) = 1 Then Stunde = Stunde + 10
If Syncbit(34) = 1 Then Stunde = Stunde + 20
If Syncbit(36) = 1 Then Tag = Tag + 1
If Syncbit(37) = 1 Then Tag = Tag + 2
If Syncbit(38) = 1 Then Tag = Tag + 4
If Syncbit(39) = 1 Then Tag = Tag + 8
If Syncbit(40) = 1 Then Tag = Tag + 10
If Syncbit(41) = 1 Then Tag = Tag + 20
If Syncbit(42) = 1 Then Wochentag = Wochentag + 1
If Syncbit(43) = 1 Then Wochentag = Wochentag + 2
If Syncbit(44) = 1 Then Wochentag = Wochentag + 4
If Syncbit(45) = 1 Then Monat = Monat + 1
If Syncbit(46) = 1 Then Monat = Monat + 2
If Syncbit(47) = 1 Then Monat = Monat + 4
If Syncbit(48) = 1 Then Monat = Monat + 8
If Syncbit(49) = 1 Then Monat = Monat + 10
If Syncbit(50) = 1 Then Jahr = Jahr + 1
If Syncbit(51) = 1 Then Jahr = Jahr + 2
If Syncbit(52) = 1 Then Jahr = Jahr + 4
If Syncbit(53) = 1 Then Jahr = Jahr + 8
If Syncbit(54) = 1 Then Jahr = Jahr + 10
If Syncbit(55) = 1 Then Jahr = Jahr + 20
If Syncbit(56) = 1 Then Jahr = Jahr + 40
If Syncbit(57) = 1 Then Jahr = Jahr + 80
If Wochentag = 0 Then Goto Fehlerdcf77
For I = 21 To 27
Min_bits = Min_bits + Syncbit(i)
Next I
Parity_min = Min_bits Mod 2
If Parity_min = 0 And Syncbit(28) = 1 Then Goto Fehlerdcf77
If Parity_min = 1 And Syncbit(28) = 0 Then Goto Fehlerdcf77
Min_bits = 0
For I = 29 To 34
Std_bits = Std_bits + Syncbit(i)
Next I
Parity_std = Std_bits Mod 2
If Parity_std = 0 And Syncbit(35) = 1 Then Goto Fehlerdcf77
If Parity_std = 1 And Syncbit(35) = 0 Then Goto Fehlerdcf77
Std_bits = 0
For I = 36 To 57
Day_bits = Day_bits + Syncbit(i)
Next I
Parity_datum = Day_bits Mod 2
If Parity_datum = 0 And Syncbit(58) = 1 Then Goto Fehlerdcf77
If Parity_datum = 1 And Syncbit(58) = 0 Then Goto Fehlerdcf77
Day_bits = 0
Soft_std = Stunde
Soft_min = Minute
Soft_sec = 0
Soft_tag = Tag
Soft_monat = Monat
Soft_jahr = Jahr
Sek_timer = 0
Minutenmarke = 0
Dcf77_time = 0
Dcf77_err = 0
Sig_error = 0
End If
End If
Return
Fehlerdcf77:
Minutenmarke = 0
Dcf77_err = 1
Dcf77_time = 1
Return
Sub Set_softuhr()
Sek_timer = 0
Incr Soft_sec
Incr Temp_count
Anzeige = 1
If Soft_sec > 59 Then
Soft_sec = 0
Incr Soft_min
'test
'Incr Sync_dcf77_time
End If
If Soft_min > 59 Then
Soft_min = 0
Incr Soft_std
' jede Std Zeit Sycroniesieren
Incr Sync_dcf77_time
End If
If Soft_std > 23 Then
Soft_std = 0
Incr Soft_tag
Incr Wochentag
If Wochentag > 7 Then Wochentag = 1
End If
'Schaltjahr wenn Jahr glatt durch 4 geht
Schaltjahr = Soft_jahr Mod 4
If Soft_monat = 1 And Soft_tag > 31 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 2 Then
If Schaltjahr = 0 And Soft_tag > 29 Then
Incr Soft_monat
Soft_tag = 1
End If
If Schaltjahr > 0 And Soft_tag > 28 Then
Incr Soft_monat
Soft_tag = 1
End If
End If
If Soft_monat = 3 And Soft_tag > 31 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 4 And Soft_tag > 30 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 5 And Soft_tag > 31 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 6 And Soft_tag > 30 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 7 And Soft_tag > 31 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 8 And Soft_tag > 31 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 9 And Soft_tag > 30 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 10 And Soft_tag > 31 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 11 And Soft_tag > 30 Then
Incr Soft_monat
Soft_tag = 1
End If
If Soft_monat = 12 And Soft_tag > 31 Then
Soft_monat = 1
Soft_tag = 1
Incr Soft_jahr
End If
End Sub
Sub Gettemp()
If Temp_count = 15 Then
Temp_count = 0
T1 = 1
End If
If T1 = 0 Then Exit Sub
If T1 = 1 Then ' Temperaturmessung alle 33 sec : Pinc.0
1wreset ' Reset Bus
1wwrite &HCC ' Sende Kommando SkipRom
1wwrite &H44
T1 = 2 ' Sende Kommando Convert Temperatur
End If
If T1 = 2 Then ' Warteschleife um Temp. Wandlung im DS1820 abzuwarten
Busy = 1wread() ' Solange Busy ungleich &HFF; Wandlung läuft noch
If Busy = &HFF Then T1 = 3
End If
If T1 = 3 Then
1wreset ' Reset bus
1wwrite &HCC ' Sende Kommando SkipRom
1wwrite &HBE ' Sende Kommando Scratchpad
Bd(1) = 1wread(9) ' Lese Ram aus und speicher in Array
' Crc = 0 ' needs a 256 elements table
' For I = 1 To 9
' Tmp = Crc Xor Bd(i)
' Crc = Lookup(tmp , Crc8)
' Next
' If Crc <> 0 Then Goto Fehlertemp
1wreset
Tmp = Bd(1) And 1 ' Temperatur in 0.1C berechnen
If Tmp = 1 Then Decr Bd(1)
T = Makeint(bd(1) , Bd(2))
T = T / 2
Temp = Bd(8) - Bd(7)
Temp = Temp / Bd(8)
Temp = T + Temp
Temp = Temp - 0.25
T1 = 0
End If
'Exit Sub
'Fehlertemp:
' T = 0
' Temp_count = 0
'F1:
' Print "Debug Temperatur"
' Print "Temp_count " ; Temp_count
' Print "Busy " ; Busy
' Print "T1 " ; T1
' Print "BD(1-9) " ; Bd(1) ; " " ; Bd(2) ; " " ; Bd(3) ; " " ; Bd(4) ; " " ; Bd(5) ; " " ; Bd(6) ; " " ; Bd(7) ; " " ; Bd(8) ; " " ; Bd(9)
' Print "Tmp " ; Tmp
' Print "Temp " ; Temp
' Print "CRC " ; Crc
' Print "----------------------------"
' Print ""
End Sub
Wtag:
Data "--" , "Mo" , "Di" , "Mi" , "Do" , "Fr" , "Sa" , "So"
Ich habe mir auch ein kleines VB Programm erstellt um den Timer zu berechnen. Ich Postes es mal hier. Solltest es nicht laufen mußt du aber noch die VB Runtimes installieren (Google).
Lesezeichen