I'm trying to set time on a DS3231 RTC with i2c protocol.
I have written a program that function OK only with DEBUG option (setting step time to 100msec) but DON'T work in RUN mode.
memclear
i2c.setup(D1,D2)
let address = 104 '7-bit address of DS3231 RTC
let numchars = 7 'Number of characters to read
button "scrivi",[scrivi]
button "leggi",[leggi]
button "prova",[prova]
button "esci",[esci]
wait
[esci]
end
[prova]
rtcsec=time(sec)
rtcmin=time(min)
rtchour=time(hour)
print "sec="&rtcsec&"min="&rtcmin&"ore="&rtchour
wait
[scrivi]
time.setup(2,1,"ntp1.inrim.it")
' Example of updating the time and/or date
rtcsec=time(sec)
rtcmin=time(min)
rtchour=time(hour)
datum=rtcsec
gosub [trasf]
rtcsec=result
delay 100
datum=rtcmin
gosub [trasf]
rtcmin=result
delay 100
datum=rtchour
gosub [trasf]
rtchour= result
if time(dow)=="Mon" then
rtcdow=2
endif
if time(dow)=="Tue" then
rtcdow=3
endif
if time(dow)=="Wed" then
rtcdow=4
endif
if time(dow)=="Thu" then
rtcdow=5
endif
if time(dow)=="Fri" then
rtcdow=6
endif
if time(dow)=="Sat" then
rtcdow=7
endif
if time(dow)=="Sun" then
rtcdow=1
endif
datum=time(day)
gosub [trasf]
rtcday=result
if time(month)=="Jan" then
datum=1
endif
if time(month)=="Feb" then
datum=2
endif
if time(month)=="Mar" then
datum=3
endif
if time(month)=="Apr" then
datum=4
endif
if time(month)=="May" then
datum=5
endif
if time(month)=="Jun" then
datum=6
endif
if time(month)=="Jul" then
datum=7
endif
if time(month)=="Aug" then
datum=8
endif
if time(month)=="Sep" then
datum=9
endif
if time(month)=="Oct" then
datum=10
endif
if time(month)=="Nov" then
datum=11
endif
if time(month)=="Dec" then
datum=12
endif
gosub [trasf]
rtcmonth=result
datum=time("year")
datum=datum -2000
gosub [trasf]
rtcyear=result
i2c.begin(address) 'start a transaction
i2c.write(0) 'point to the seconds address location
i2c.write(rtcsec)
i2c.write(rtcmin)
i2c.write(rtchour)
i2c.write(rtcdow)
i2c.write(rtcday)
i2c.write(rtcmonth)
i2c.write(rtcyear)
i2c.end() 'finish transaction
print "sec="&rtcsec&"min="&rtcmin&"ore="&rtchour
wait
[leggi]
' Read the time and date
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
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 time()
print tm
wait
[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
[trasf]
result = 0
decine = datum / 10
decine= int(decine)
nin= decine * 10
unita = datum - nin
result = decine * 16
result = result + unita
return
The button "leggi" is OK and the time and date is correctly read in RUN mode.
The button "scrivi" DOES NOT FUNCTION in RUN mode (values of sec,min and hour are ALL 0 and 00 hours 00 mins and 00 secs are sent to the DS3231 that stores it correctly)
The button "scrivi" is OK only in DEBUG mode (time 100msec)
the DS 3231 is powered with 3,3v from the Nodemcu module.
on both SDA and SCL lines are present two 4,7K pullup resistor tied to 3,3v
What's wrong ??
Thanks all.