Echzeituhr DS1307

Echtzeituhr

Ansteuerung (Schreiben):

Initialisierung:

Zu Beginn muß der Chip initialisiert werden, damit der Oszillator läuft. Hierzu ist Bit 7 (Clock halt bit) an Adresse 0 auf 0 zu setzen! Soll der 1Hz-Ausgang aktiviert werden, muss das Kontrollregister (Adresse 7) entsprechend gesetzt werden (Bit 4 = 1).

Ansteuerung (Lesen):

Beispiel-Programm(Bascom):

$regfile = "8535def.dat"
$crystal = 4000000
$hwstack = 100

$baud = 9600

' I2C-Pins
Config Sda = Portc.1
Config Scl = Portc.0

'variables for real time clock
Dim Seconds As Byte
Dim Minutes As Byte
Dim Hours As Byte
Dim Weekday As Byte
Dim Day As Byte
Dim Month As Byte
Dim Year As Byte

'------------------------------------------------------------------------------

Main:

Do

Gosub Readclock
Gosub Displayclock

Wait 1

If Inkey() = "s" Then Gosub Setclock

Loop

'-------------------------------------------------------------------------------
Displayclock:
Print Day;
Print "." ;
Print Month;
Print "." ;
Print Year;
Print " ";
Print Hours;
Print ":";
Print Minutes;
Print ":";
Print Seconds
Return
'-------------------------------------------------------------------------------
Readclock:
'Read real time clock DS1307

'set register pointer to 0
I2cstart
I2cwbyte &HD0
I2cwbyte 0
I2cstop
'read registers

I2cstart
I2cwbyte &HD1
I2crbyte Seconds , Ack
I2crbyte Minutes , Ack
I2crbyte Hours , Ack
I2crbyte Weekday , Ack
I2crbyte Day , Ack
I2crbyte Month , Ack
I2crbyte Year , Nack
I2cstop
' Jahr
'convert BCD to decimal
Seconds = Makedec(seconds)
Minutes = Makedec(minutes)
Hours = Makedec(hours)
Day = Makedec(day)
Month = Makedec(month)
Year = Makedec(year)

Return
'-------------------------------------------------------------------------------
Setclock:
'set new time and date and eventually turn on oscillator + SQW out
'to init clock: oscillator must be turned on (bit7=0 in register 0 )
'to turn on SQW/OUT: bit 4 = 1 in register 7 (Control register)
'both are done automatically (if Seconds <60)
'input new time
Input "Day (1-31):" , Day
Input "Month (1-12):" , Month
Input "Year (00-...):" , Year
Input "Hour (0-23):" , Hours
Input "Minutes (0-59):" , Minutes
Input "Seconds (0-59):" , Seconds

Seconds = Makebcd(seconds)
Minutes = Makebcd(minutes)
Hours = Makebcd(hours)
Day = Makebcd(day)
Month = Makebcd(month)
Year = Makebcd(year)

'write to DS1307
I2cstart
I2cwbyte &HD0
I2cwbyte 0 'Register 0 for secs
I2cwbyte Seconds 'Seconds
I2cwbyte Minutes 'Minutes
I2cwbyte Hours 'Hours
I2cwbyte Weekday 'Weekday
I2cwbyte Day 'Day
I2cwbyte Month 'Month
I2cwbyte Year 'Year
I2cwbyte &H10 'SQW enabled
I2cstop
Return
Zurück