Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By matherp
#32912 This code needs V1.13 or later to run.

It includes an example of reading from a DS3231 RTC module, e.g.
http://www.ebay.co.uk/itm/DS3231-AT24C32-Memory-Module-For-Arduino-IIC-Precision-RTC-Real-Time-Clock-/400624291795?hash=item5d47118fd3:g:IOoAAOxyXDhSorpn

It also includes, commented out, the required code for updating the clock.

Given this works, it should be possible to read/write most i2c devices.

The pin connection is GPIO0 - SDA, GPIO2 - SCL. Don't forget to use appropriate pullup resistors on both lines (2K7 - 10K should work fine)

Code: Select alllet address = 104 '7-bit address of DS3231 RTC
let numchars = 7 'Number of characters to read
'
'  Example of updating the time and/or date
'
'i2c.begin(address) 'start a transaction
'i2c.write(2) 'point to the hour address location
'i2c.write(8) 'Set to BCD 08
'i2c.end() 'finish transaction
delay 10
'
' 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 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
Last edited by matherp on Wed Nov 11, 2015 2:55 am, edited 1 time in total.
User avatar
By Mmiscool
#32923 This is awesome!!

Do you mind if I use this as the I2c example on the esp8266basic.com

Will state with the example it was provided by matherp if you don't mind.