Hey (again) - Busy day today, got my panda.
I’m making a driver to read a DS1307 RTC, via i2c. I’ve got it working pretty well by now, in the sense that I’m getting all the right readings, except that every 20 seconds or so I get a “00” in one of the fields (eg seconds, minutes or hours) instead of teh actual value, at random places.
Is that a misread of some sort? Any clue what might be causing that?
Following is my code (Its right now edited to only display seconds, to shorten it down)
static I2CDevice.Configuration con =
new I2CDevice.Configuration(0x68, 100);
static I2CDevice DS1307 = new I2CDevice(con);
public static void readSeconds(object o ) {
I2CDevice.I2CTransaction[] xActions =
new I2CDevice.I2CTransaction[2];
Thread.Sleep(10);
string RegName = "";
int i;
int vLower;
int vUpper;
string sLower, sUpper, sResult = "";
//Used to be <= 7 but im testing with seconds now
for (i = 0; i <= 0; i++)
{
//Read seconds register
byte[] RegisterNum = new byte[1] { (byte)i };
xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
byte[] RegisterValue = new byte[1];
xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);
DS1307.Execute(xActions, 1000);
//Switch between different conversions for each value. only seconds enabled for now
switch (i)
{
case 0:
vLower = (int)RegisterValue[0] & 15;
vUpper = (int)RegisterValue[0] >> 4;
vUpper = vUpper & 7;
sLower = vLower.ToString();
sUpper = vUpper.ToString();
sResult = sUpper + sLower;
break;
case 1:
RegName = "Minutes";
break;
case 2:
RegName = "Hours";
break;
case 3:
RegName = "Day";
break;
case 4:
RegName = "Date";
break;
case 5:
RegName = "Month";
break;
case 6:
RegName = "Year";
break;
case 7:
RegName = "Control";
break;
}
Debug.Print(RegName + ":" + sResult);
}
}
public static void Main()
{
//Timer to get it to run every second, copied the example in manual exactly to make sure it works
Timer MyTimer =
new Timer(new TimerCallback(readSeconds), null, 5000, 1000);
Thread.Sleep(Timeout.Infinite);
}
If anyone could advise me regarding the failed readings, I would really be thankfull