- Sun Jun 04, 2017 5:30 pm
#66729
This is what I've discovered tonight...
If you want to write new time/date info to the RTC it requires at least 2 writes, the first is to set the required address offset for pointing to the appropriate byte register, which is then followed by the new data to be written to that register, optionally followed by more new data written to subsequent registers if required.
The RTC address is 104
The 'seconds' byte register is at that address, therefore the offset is 0.
The minutes byte register is offset 1 (address + 1)
hours = offset 2
day of week = offset 3
day of month = offset 4
month = offset 5
year = offset 6
If you want to write new hour info you first start transaction, write the appropriate hour offset (2), then write the required hour data.
You can keep writing additional data to subsequent offsets, then end transaction when done.
So if you only want to change the month, start transaction, write the month offset (5), write the month data, then end transaction.
If you want to update hours and minutes, start transaction, write minutes offset (1), write minutes data, write hours data, then end transaction.
If everything needs setting because the RTC came without a battery then
start transaction, write seconds offset (0), write seconds info, write minutes info, write hours info, write day of week info, write day of month info, write month info, write year, then end transaction.
The following just stuffs 1,2,3,4,5,6,7 into the consecutive registers to show what is going where.
Bear in mind that the register info should be BCD.
TrackerJ has recently created some useful Esp-Basic BCD routines if you need them...
http://www.esp8266-projects.com/search/label/ESPBasicEdit: I suspect an end transaction is probably missing from the original RTC example (even though it seemed to work ok), so I've put a comment in the code where I've added itCode: Select alladdress = 104
'goto [SHOWTIME]
i2c.begin(address) 'start a transaction
i2c.write(0) 'point to the seconds address offset
for count = 1 to 7
i2c.write(count)
next count
i2c.end() 'finish transaction
goto [SHOWTIME]
[SHOWTIME]
delay 10
i2c.begin(address) 'start another transaction
i2c.write(0) 'point to the seconds address location
i2c.end() ' Finish the write transaction
i2c.requestfrom(address,numchars) 'start a transaction to read 7 bytes
nsecs = i2c.read() 'read the seconds
nmins = i2c.read() 'read the minutes
nhrs = i2c.read() 'read the hours
nday = i2c.read() 'read the day of the week
ndate = i2c.read() 'read the day of the month
nmonth = i2c.read() 'read the month
nyear = i2c.read() 'read the year
' I suspect an end transaction is missing from here in the original example, so I've added it below.
i2c.end() ' Finish the read transaction
d = nhrs
gosub [bcd]
tm = f & ":"
d = nmins
gosub [bcd]
tm = tm & f
tm = tm & ":"
d = nsecs
gosub [bcd]
tm = tm & f
d = ndate
gosub [bcd]
da = f
da = da & "/"
d = nmonth
gosub [bcd]
da = da & f
da = da & "/"
d = nyear
gosub [bcd]
da = da & f
tm = tm & chr(32)
tm = tm & da
print tm
end
[bcd]
a = d / 16
a = int(a)
a = a + 48
c = a * 16
b = d - c
b = b + 48
e = chr(a)
f = chr(b)
f = e & f
return