Hallo zusammen,

ich habe an meinem Mega32 gerade eine DS1307 RTC über I2C angeschlossen.
Das folgende Programm stellt die Softclock und auch die RTC.

1. Zeile Softclock
2. Zeile DCF77
3. Zeile dcf_status und dcf_bits
4. Zeile DS1307 Zeit und Datum und Wochentag


Gruß Peter


Code:
' LCD auf Port A
' RTC DS1307 Dallas Echtzeituhr auf I2E  Portc.0 und Portc.1
' DCF77 Modul an Portd.3
' der DCF77 Empfänger stellt die Softuhr und die DS1307 Echtzeituhr
' V1.0 vom 24.04.07 by Peter Plischka
' Name: DCF77 Bascom mit DS1307 V1.0

$regfile = "m32def.dat"
$crystal = 16000000
$baud = 115200
$hwstack = 128
$swstack = 128
$framesize = 128
$lib "I2C_TWI.LBX"

'----------------- LCD Display an Port A
'Config Date = Dmy , Separator = .
Config Lcdpin = Pin , Db4 = Porta.3 , Db5 = Porta.2 , Db6 = Porta.1 , Db7 = Porta.0 , E = Porta.5 , Rs = Porta.7       ' Pinbelegung "LCD-Port)"
'Config Lcd = 20 * 4a , Chipset = Ks077                      '        LCD 204 DIP 20x4
'Config Lcd = 16 * 2 , Chipset = Dogm163v5 , Contrast = &H702       ' EA DOGM162W-A
'Config Lcd = 16 * 2
Config Lcd = 20 * 4
Config Porta = Output
Config Lcdbus = 4
Initlcd
Deflcdchar 1 , 3 , 3 , 32 , 32 , 32 , 32 , 32 , 32          ' Grad
Deflcdchar 2 , 32 , 4 , 4 , 4 , 4 , 21 , 14 , 4             ' Pfeil runter
Deflcdchar 3 , 4 , 14 , 21 , 4 , 4 , 4 , 4 , 32             ' Pfeil rauf
Cls
Waitms 100
Cursor Off Noblink                                          'hide cursor
'----------------- Ende LCD

'----------------- Config DCF77
'Config Dcf77 = Pind.2 , Debug = 1 , Inverted = 0 , Check = 2 , Update = 0 , Updatetime = 30 , Switchpower = 0 , Secondticks = 50 , Timer1sec = 1 , Powerlevel = 1 , Timer = 1
Config Dcf77 = Pind.3 , Timer = 1 , Inverted = 1 , Timer1sec = 1 , Update = 0 , Check = 1, Debug = 1
Enable Interrupts
Config Date = Dmy , Separator = .

Dim I As Integer
Dim Sec_old As Byte , Dcfsec_old As Byte
Sec_old = 99 : Dcfsec_old = 99                              ': DCF_Debug_Timer = 0
'----------------- Ende Config DCF77

'----------------- für die Uhr DS1307
' TWI init
Twcr = &B00000100                                           ' erstmal nur TWI aktivieren
Twsr = 0                                                    ' Status und Prescaler Register
Twbr = 152                                                  ' Bit Rate Register, 72 = 100kHz   152 = 50kHz(0 bis 255)
Config I2cdelay = 40

Config Scl = Portc.0
Config Sda = Portc.1
Config Twi = 100000

Dim Ds_weekday As Byte
Dim Ds_day As Byte
Dim Ds_month As Byte
Dim Ds_year As Byte
Dim Ds_sec As Byte
Dim Ds_min As Byte
Dim Ds_hour As Byte
Dim Minute_t As Byte
Dim Stunde_t As Byte
Dim Sekunde_t As Byte

Dim Ds1307w As Byte
Dim Ds1307r As Byte

Ds1307w = &B11010000
Ds1307r = &B11010001

'----------------- Uhr Ende

' Testroutine für die DCF77 Clock
Locate 1 , 1
Lcd "Test DCF77 V1.00"
Wait 1

Do
   For I = 1 To 78
      Waitms 10
      If Sec_old <> _sec Then
         Exit For
      End If
      If Dcfsec_old <> Dcf_sec Then
         Exit For
      End If
   Next
   Waitms 220
   Sec_old = _sec
   Dcfsec_old = Dcf_sec

   Locate 1 , 1
   Lcd Time$ ; " " ; Date$ ; " "

   Locate 2 , 1
   Lcd Time(dcf_sec) ; " " ; Date(dcf_day) ; " "

   Locate 3 , 1
   Lcd Bin(dcf_status) ; " " ; Bin(dcf_bits) ; " "

'   Locate 4 , 1
'   Lcd Bdcf_impuls ; " " ; Bdcf_pause ; " " ; Dcf77timezone() ; " " ; Dcf_status.7 ; " "

   Gosub Show_clock

'   Print Time$ ; " " ; Date$ ; " " ; Time(dcf_sec) ; " " ; Date(dcf_day) ; " " ; Bin(dcf_status) ; " " ; Bin(dcf_bits) ; " " ; Bdcf_impuls ; " " ; Bdcf_pause ; " " ; Dcf77timezone()

Loop

End

'Die Impulslänge sollte zwischen 2 und 8 liegen
'Die Pausenlänge sollte zwischen 70 und 90 liegen

' Dcf77timezone()
' 0 : when there is no valid DCF77 data yet
' 1 : When In "Middle Europe Normal Time"
' 2 : When In "Middle Europe daylight saving Time"

'----------------- Datum und Zeit holen und Anzeigen
Show_clock:
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cstop
I2cstart
I2cwbyte Ds1307r
I2crbyte Ds_sec , Ack
I2crbyte Ds_min , Ack
I2crbyte Ds_hour , Ack
I2crbyte Ds_weekday , Ack
I2crbyte Ds_day , Ack
I2crbyte Ds_month , Ack
I2crbyte Ds_year , Nack
I2cstop

Gosub 1307_stellen

Stunde_t = Makedec(ds_hour)
Minute_t = Makedec(ds_min)
Sekunde_t = Makedec(Ds_sec)

Locate 4 , 1
Lcd Bcd(ds_hour) ; ":" ; Bcd(ds_min) ; " " ; Bcd(ds_sec)

Locate 4 , 10
Lcd Bcd(ds_day) ; "." ; Bcd(ds_month) ; "." ; Bcd(ds_year) ; " " ; Lookupstr(ds_weekday , Wochentag)

Return
'----------------- Ende Datum und Zeit holen und Anzeigen

'----------------- Zeit setzen
Timeset:
Ds_sec = Makebcd(Ds_sec)
Ds_min = Makebcd(Ds_min)
Ds_hour = Makebcd(Ds_hour)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cwbyte Ds_sec
I2cwbyte Ds_min
I2cwbyte Ds_hour
I2cstop
Return
'----------------- Ende Zeit setzen

'----------------- Datum setzen
Dateset:
Ds_day = Makebcd(Ds_day)
Ds_month = Makebcd(Ds_month)
Ds_year = Makebcd(Ds_year)
Ds_weekday = Makebcd(Ds_weekday)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 3
I2cwbyte Ds_weekday
I2cwbyte Ds_day
I2cwbyte Ds_month
I2cwbyte Ds_year
I2cstop
Return
'----------------- Ende Datum setzen

'----------------- RTC DS1307 nach DCF77 stellen
1307_stellen:

   If Dcf_status.7 = 1 And Ds_sec <> Dcf_sec Then           ' wenn die DCFsekunden und die RTCsekunden unterschiedlich sind, die RTC stellen
'         Led2 = 0                                           'LED2
         'Sound Portd.7 , 3500 , 140
         Ds_hour = _hour
         Ds_min = _min
         Ds_sec = _sec
         Gosub Timeset
         Ds_weekday = _weekday
         Ds_day = _day
         Ds_month = _month
         Ds_year = _year
         Gosub Dateset

'         If Dcf_min = 1 Then : Reset Dcf_status.7 : End If 'einmal stündlich das Statusbit zurücksetzen
      End If

Return
'----------------- Ende RTC DS1307 nach DCF77 stellen

'----------------- Daten für Wochentag
Wochentag:
Data "  " , "Mo" , "Di" , "Mi" , "Do" , "Fr" , "Sa" , "So"
'----------------- Ende Daten für Wochentag