Starting to get to grips with this now.
It's probably not very elegant but the following seems to be working from what I can see. Any comments greatly appreciated good or bad.
I2C1.setup({scl:B6,sda:B7});
var rtcTime = '';
var rtcDate = '';
var rtcDateTime = '';
var dstStatus = '';
var dstShift = 1;
i2c_address = 0x68;
function readDateTime() {
I2C1.writeTo(i2c_address, 0x00/* address*/);
var data = I2C1.readFrom(i2c_address, 7/* bytes */); //read number of bytes from address
var seconds = bcd2dec(data[0]);
var minutes = (bcd2dec(data[1]));
var hours = (data[2]);
var dow = (bcd2dec(data[3]));
var date = (bcd2dec(data[4]));
var month = (bcd2dec(data[5]));
var year = (bcd2dec(data[6]));
if ((isDST(date,month,dow)) === true) {
hours = bcd2dec((hours)+(dstShift));
dstStatus = true;
}
else {
hours = bcd2dec(hours);
dstStatus = false;
}
rtcDate = date+"/"+month+"/"+year;
rtcTime = hours+":"+minutes+":"+seconds;
rtcDateTime = rtcDate+" "+rtcTime;
return;
}
function setDate(date,month,year) {
I2C1.writeTo(i2c_address,[0x04, (dec2bcd(date))]);
I2C1.writeTo(i2c_address,[0x05, (dec2bcd(month))]);
I2C1.writeTo(i2c_address,[0x06, (dec2bcd(year))]);
}
function setTime(hour,minute) {
I2C1.writeTo(i2c_address,[0x00, 0]);
I2C1.writeTo(i2c_address,[0x01, (dec2bcd(minute))]);
if (dstStatus === true) {
I2C1.writeTo(i2c_address,[0x02, (dec2bcd(hour-1))]);
}
else
I2C1.writeTo(i2c_address,[0x02, (dec2bcd(hour))]);
}
function setDay(val) {
switch (val.toString) {
case "Monday":
I2C1.writeTo(i2c_address,[0x03, (dec2bcd(1))]);
break;
case "Tuesday":
I2C1.writeTo(i2c_address,[0x03, (dec2bcd(2))]);
break;
case "Wednesday":
I2C1.writeTo(i2c_address,[0x03, (dec2bcd(3))]);
break;
case "Thursday":
I2C1.writeTo(i2c_address,[0x03, (dec2bcd(4))]);
break;
case "Friday":
I2C1.writeTo(i2c_address,[0x03, (dec2bcd(5))]);
break;
case "Saturday":
I2C1.writeTo(i2c_address,[0x03, (dec2bcd(6))]);
break;
case "Sunday":
I2C1.writeTo(i2c_address,[0x03, (dec2bcd(7))]);
break;
default:
print(text,"Not a valid day");
return;
}
}
//DST
function isDST(date,month,dow)
{
//January, february, and december are out.
if (month < 3 || month > 11) {
return false;
}
//April to October are in
if (month > 3 && month < 11) {
return true;
}
var previousSunday = day - dow;
//In march, we are DST if our previous sunday was on or after the 8th.
if (month == 3) {
return previousSunday >= 8;
}
//In november we must be before the first sunday to be dst.
//That means the previous sunday must be before the 1st.
return previousSunday <= 0;
}
// Convert Decimal value to BCD
function dec2bcd(val) {
return parseInt(val, 16);
}
// Convert BCD value to decimal
function bcd2dec(val) {
return ("0"+val.toString(16)).substr(-2);
}
setInterval(readDateTime, 500);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Starting to get to grips with this now.
It's probably not very elegant but the following seems to be working from what I can see. Any comments greatly appreciated good or bad.