Main Site Documentation

I2c Data Lost / Errorous Readings?


#1

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


#2

DS1307 driver is already made: http://www.fezzer.com/project/30/rtc1307/


#3

Ahh cool, I still want to make one myself to learn some more .netmf :slight_smile:
Its outputting the data fine now, just seems to send 00 from time to time, I recon its the default debug messages that are interrupting it, ill try disabling them and seeing what happens


#4

Xarren:

This is a thread

http://www.tinyclr.com/forum/1/1241/

that have a very good discussion regarding data reading and conversion for DS1307. :wink:


#5

Thanks for the post, that’ll help me shorten and improve my algorithm, but it doesn’t change the fact that some of the data just either isn’t be read or sent through properly. I guess I’ll just play around with it and see if I can work out where the problem is.


#6

If you think that the garbage collector is interfering with your program flow, then write your code to force a GC before you do something with i2c, then force another after, and see if that changes the behaviour.


#7

Thats a great idea, I’ve just realised I have loads of null/"" references throughout my code that might be getting trashed. Any clue how often the GC runs? Or how to know when it does? Just so that I can check if thats likely to be the cause


#8

if you have GC debug messages turned off, then turn them back on and it’ll show in the debug window :slight_smile:


#9

Ahh… Yes indeed the problems do seem to occur when all that text I haven’t quite gotten around to reading pops up. I guess I’ve got to sort the GC out myself. I might just use default values in my variables to ensure they don’t get deleted :).