Das Thema ist zwar schon älter, aber ich habe ein Problem behoben. Wenn man den Code von oben verwendet, ist jede zweite Abfrage regelmäßig fehlerhaft. Die Lösung ist repeated start. Hier mein Code:
Code:
$regfile = "m32def.dat"
$crystal = 4000000
$baud = 9600
Config Scl = Portc.0
Config Sda = Portc.1
Dim _weekday As Byte
Dim _day As Byte
Dim _month As Byte
Dim _year As Byte
Dim _sec As Byte
Dim _min As Byte
Dim _hour As Byte
Dim ___lcdno As Bit
Dim Ds1307w As Byte
Dim Ds1307r As Byte
Ds1307w = &B11010000
Ds1307r = &B11010001
_day = 29
_month = 6
_year = 05
_sec = 15
_min = 25
_hour = 20
_sec = Makebcd(_sec)
_min = Makebcd(_min)
_hour = Makebcd(_hour)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cwbyte _sec
I2cwbyte _min
I2cwbyte _hour
I2cstop
_day = Makebcd(_day)
_month = Makebcd(_month)
_year = Makebcd(_year)
I2cstart
I2cwbyte Ds1307w
I2cwbyte 3
I2cwbyte _weekday
I2cwbyte _day
I2cwbyte _month
I2cwbyte _year
I2cstop
Do
Rem Datum und Zeit holen
'I2cstart
'I2cwbyte Ds1307w
'I2cwbyte 0
'I2cstop
Waitms 100 ' Diese Ansteuerung entspricht Figure 6 im Datenblatt, also erst schreiben, dann repeated start und anschließend lesen!
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cstart 'repeated Start
I2cwbyte Ds1307r
I2crbyte _sec , Ack
I2crbyte _min , Ack
I2crbyte _hour , Ack
I2crbyte _weekday , Ack
I2crbyte _day , Ack
I2crbyte _month , Ack
I2crbyte _year , Ack
I2cstop
I2cstart
I2cwbyte Ds1307w
I2cwbyte 0
I2cstop
_day = Makedec(_day)
_month = Makedec(_month)
_year = Makedec(_year)
_sec = Makedec(_sec)
_min = Makedec(_min)
_hour = Makedec(_hour)
Print _day ; "/" ; _month ; "/" ; _year
Print _hour ; "/" ; _min ; "/" ; _sec
Print "-----------------------------------------"
Waitms 2000
Loop
Lesezeichen