-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathm_time.lua
More file actions
27 lines (24 loc) · 796 Bytes
/
m_time.lua
File metadata and controls
27 lines (24 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
local function bcdToDec(val)
return((((val/16) - ((val/16)%1)) *10) + (val%16))
end
local id=0
i2c.setup(id, ds3231_sda, ds3231_scl, i2c.SLOW)
--get time from DS3231
local function getime()
i2c.start(0)
i2c.address(0, 0x68, i2c.TRANSMITTER)
i2c.write(id, 0x00)
i2c.stop(id)
i2c.start(id)
i2c.address(0, 0x68, i2c.RECEIVER)
local c=i2c.read(id, 7)
i2c.stop(0)
return bcdToDec(tonumber(string.byte(c, 1))),
bcdToDec(tonumber(string.byte(c, 2))),
bcdToDec(tonumber(string.byte(c, 3))),
bcdToDec(tonumber(string.byte(c, 4))),
bcdToDec(tonumber(string.byte(c, 5))),
bcdToDec(tonumber(string.byte(c, 6))),
bcdToDec(tonumber(string.byte(c, 7)))
end
time_second, time_minute, time_hour, time_day, time_date, time_month, time_year = getime();