Last active
August 29, 2015 14:09
-
-
Save electricimp/b33b215070a70dec1c99 to your computer and use it in GitHub Desktop.
Squirrel class for the Dallas/Maxim DS3234 real time clock as used on the SparkFun Dead On board
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DS3234RTC | |
{ | |
// Squirrel class for the Dallas/Maxim DS3234 real time clock used on the | |
// SparkFun DeadOn breakout board https://www.sparkfun.com/products/10160 | |
// | |
// Bus: SPI | |
// | |
// Written by Tony Smith, September 2014 | |
// Version 1.0 | |
LOW = 0 | |
HIGH = 1 | |
DS3234_CTRL_REG_WRITE = "\x8E" | |
DS3234_RAM_START_READ = "\x00" | |
DS3234_RAM_START_WRITE = "\x80" | |
_spi = null | |
_cs = null | |
constructor(bus, csPin) | |
{ | |
_spi = bus | |
_cs = csPin | |
} | |
function init() | |
{ | |
// Configure SPI bus for SPI Mode 3 | |
_spi.configure((CLOCK_IDLE_LOW | CLOCK_2ND_EDGE), 3000) | |
// Set the Chip Select pin HIGH | |
_cs.configure(DIGITAL_OUT) | |
_cs.write(HIGH) | |
imp.sleep(0.02) | |
// Initialise the DS3234 with basic settings, ie. zero Control Register | |
_cs.write(LOW) | |
_spi.write(DS3234_CTRL_REG_WRITE) | |
_spi.write("\x00") | |
_cs.write(HIGH) | |
} | |
function setDateAndTime(date, month, year, wday, hour, min, sec) | |
{ | |
// Sets the RTC's initial values - all integers | |
local dateData = [sec, min, hour, wday, date, month, (year - 2000)] | |
for (local i = 0 ; i < 7 ; i++) | |
{ | |
dateData[i] = integerToBCD(dateData[i]) | |
if (i == 2) dateData[i] = dateData[i] & 0x3F | |
// DS3234 memory is written at 0x80 and up | |
// 0x80 = seconds (0-59) | |
// 0x81 = minutes (0-59) | |
// 0x82 = hour (0-23) | |
// 0x83 = day of week (1-7) | |
// 0x84 = day of month (1-31) | |
// 0x85 = month (1-12) | |
// 0x86 = year (00-99) | |
_cs.write(LOW) | |
local r = blob(1) | |
r.writen((i + 0x80), 'b') | |
_spi.write(r) | |
local v = blob(1) | |
v.writen(dateData[i], 'b') | |
_spi.write(v) | |
_cs.write(HIGH) | |
} | |
} | |
function getDateAndTime() | |
{ | |
local b = null | |
local dateData = [0, 0, 0, 0, 0, 0, 0] | |
for (local i = 0 ; i < 7 ; i++) | |
{ | |
// DS3234 memory is read at 0x00 and up | |
// 0x00 = seconds (0-59) | |
// 0x01 = minutes (0-59) | |
// 0x02 = hour (0-23) | |
// 0x03 = day of week (1-7) | |
// 0x04 = day of month (1-31) | |
// 0x05 = month (1-12) | |
// 0x06 = year (00-99) | |
_cs.write(LOW) | |
local r = blob(1) | |
r.writen(i, 'b') | |
_spi.write(r) | |
b = _spi.readblob(1) | |
dateData[i] = bcdToInteger(b[0]) | |
_cs.write(HIGH) | |
} | |
return dateData | |
} | |
function integerToBCD(value) | |
{ | |
// DS3234 stores data in Binary Coded Decimal (BCD) | |
// Writes must be converted from integer to BCD | |
local a = value / 10 | |
local b = value - (a * 10) | |
return (a << 4) + b | |
} | |
function bcdToInteger(value) | |
{ | |
// DS3234 stores data in Binary Coded Decimal (BCD) | |
// Reads must be converted to integer from BCD | |
local a = (value & 0xF0) >> 4 | |
local b = value & 0x0F | |
return (a * 10) + b | |
} | |
}class DS3234 | |
{ | |
// Squirrel class for the Dallas/Maxim DS3234 real time clock used on the | |
// SparkFun DeadOn breakout board https://www.sparkfun.com/products/10160 | |
// | |
// Bus: SPI | |
// | |
// Written by Tony Smith, September 2014 | |
// Version 1.0 | |
LOW = 0 | |
HIGH = 1 | |
DS3234_CTRL_REG_WRITE = "\x8E" | |
DS3234_RAM_START_READ = "\x00" | |
DS3234_RAM_START_WRITE = "\x80" | |
_spi = null | |
_cs = null | |
constructor(bus, csPin) | |
{ | |
_spi = bus | |
_cs = csPin | |
} | |
function init() | |
{ | |
// Configure SPI bus for SPI Mode 3 | |
_spi.configure((CLOCK_IDLE_LOW | CLOCK_2ND_EDGE), 3000) | |
// Set the Chip Select pin HIGH | |
_cs.configure(DIGITAL_OUT) | |
_cs.write(HIGH) | |
imp.sleep(0.02) | |
// Initialise the DS3234 with basic settings, ie. zero Control Register | |
_cs.write(LOW) | |
_spi.write(DS3234_CTRL_REG_WRITE) | |
_spi.write("\x00") | |
_cs.write(HIGH) | |
} | |
function setDateAndTime(date, month, year, wday, hour, min, sec) | |
{ | |
// Sets the RTC's initial values - all integers | |
local dateData = [sec, min, hour, wday, date, month, (year - 2000)] | |
for (local i = 0 ; i < 7 ; i++) | |
{ | |
dateData[i] = integerToBCD(dateData[i]) | |
if (i == 2) dateData[i] = dateData[i] & 0x3F | |
// DS3234 memory is written at 0x80 and up | |
// 0x80 = seconds (0-59) | |
// 0x81 = minutes (0-59) | |
// 0x82 = hour (0-23) | |
// 0x83 = day of week (1-7) | |
// 0x84 = day of month (1-31) | |
// 0x85 = month (1-12) | |
// 0x86 = year (00-99) | |
_cs.write(LOW) | |
local r = blob(1) | |
r.writen((i + 0x80), 'b') | |
_spi.write(r) | |
local v = blob(1) | |
v.writen(dateData[i], 'b') | |
_spi.write(v) | |
_cs.write(HIGH) | |
} | |
} | |
function getDateAndTime() | |
{ | |
local b = null | |
local dateData = [0, 0, 0, 0, 0, 0, 0] | |
for (local i = 0 ; i < 7 ; i++) | |
{ | |
// DS3234 memory is read at 0x00 and up | |
// 0x00 = seconds (0-59) | |
// 0x01 = minutes (0-59) | |
// 0x02 = hour (0-23) | |
// 0x03 = day of week (1-7) | |
// 0x04 = day of month (1-31) | |
// 0x05 = month (1-12) | |
// 0x06 = year (00-99) | |
_cs.write(LOW) | |
local r = blob(1) | |
r.writen(i, 'b') | |
_spi.write(r) | |
b = _spi.readblob(1) | |
dateData[i] = bcdToInteger(b[0]) | |
_cs.write(HIGH) | |
} | |
return dateData | |
} | |
function integerToBCD(value) | |
{ | |
// DS3234 stores data in Binary Coded Decimal (BCD) | |
// Writes must be converted from integer to BCD | |
local a = value / 10 | |
local b = value - (a * 10) | |
return (a << 4) + b | |
} | |
function bcdToInteger(value) | |
{ | |
// DS3234 stores data in Binary Coded Decimal (BCD) | |
// Reads must be converted to integer from BCD | |
local a = (value & 0xF0) >> 4 | |
local b = value & 0x0F | |
return (a * 10) + b | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment