예제 #1
0
 private static void InitializeClock()
 {
     try {
         _clock.Get();
     } catch (Exception e) {
         Debug.Print("Initializating the clock with default values due to: " + e);
         byte[] ram = new byte[DS1307.DS1307_RAM_SIZE];
         _clock.Set(new DateTime(2011, 1, 1, 12, 0, 0));
         _clock.Halt(false);
         _clock.SetRAM(ram);
     }
 }
예제 #2
0
        public static void InitializeClock(DateTime dateTime)
        {
            var clockSetIndicator = SdMountPoint + @"\clockSet.txt";

            try {
                if (File.Exists(clockSetIndicator) == false)
                {
                    Clock.Set(dateTime);
                    File.Create(clockSetIndicator);
                }
            } catch (Exception e) {
                LogLine("InitializeClock: " + e.Message);
                SignalCriticalError();
            }
        }
            public void synch()
            {
                var RTC_Clock = new DS1307();

                //RTC_Clock.Set(new DateTime(2018, 6, 18, 20, 00, 00)); // (year, month, day, hour, minute, second)

                RTC_Clock.Halt(false);  /* To make shure RTC is running */

                while (true)
                {
                    try
                    {
                        DateTime DateTimeNTP = NTPTime("pool.ntp.org", -180);

                        if (!(DateTimeNTP.Year == 1900))
                        {
                            // Synch RTC nd system clock from NTPTime
                            Debug.Print("Synch OK!");
                            Debug.Print("Internet Time " + DateTimeNTP.ToString());
                            Debug.Print("System Clock  Before Synch " + DateTime.Now.ToString());
                            Utility.SetLocalTime(DateTimeNTP);
                            Debug.Print("System Clock After Synch " + DateTime.Now.ToString());

                            //Debug.Print("RTC Before " + RTC_Clock.Get().ToString());
                            RTC_Clock.Halt(true);
                            RTC_Clock.Set(DateTimeNTP);
                            RTC_Clock.Halt(false);
                            Debug.Print("RTC After Synch " + RTC_Clock.Get().ToString());
                        }
                        else
                        {
                            Debug.Print("NTP socket failure!  Check Internet connection");
                            Debug.Print("Synch system Clock using RTC");
                            Utility.SetLocalTime(RTC_Clock.Get());
                            Debug.Print("System Clock " + DateTime.Now.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Print(ex.ToString());
                        Debug.Print("General Failure!  Verify internet connection and RTC");
                    }
                    /* sleep for 1 minute */
                    Thread.Sleep(60000);
                }
            }
예제 #4
0
        public static void Main()
        {
            var clock = new DS1307();

            // Set the clock to some arbitrary date / time
            clock.Set(new DateTime(2011, 1, 2, 20, 20, 20));

            // Make sure the clock is running
            clock.Halt(false);

            // Test reading RTC clock registers
            Debug.Print("Before Halt: " + clock.Get().ToString());

            // Halt the clock for 3 seconds
            clock.Halt(true);
            Thread.Sleep(3000);

            // Should be the same time as "Before Halt"
            Debug.Print("After Halt for 3 seconds (should be the same time): " + clock.Get().ToString());

            // Resume the clock
            clock.Halt(false);

            // Sleep for another 1.5 second
            Thread.Sleep(1500);

            // Should be just one second later since the clock's oscillator was resumed
            Debug.Print("Resumed Clock (should be time + ~1 sec): " + clock.Get().ToString());

            // Requires an oscilloscope or an interrupt handler on the microcontroller to see the effects
            TestSquareWaves(ref clock);

            // Test writing to arbitrary registers
            Debug.Print("Writing distinct RAM registers (writing the register number to itself)");
            for (byte I = DS1307.DS1307_RAM_START_ADDRESS; I <= DS1307.DS1307_RAM_END_ADDRESS; I++)
            {
                clock.WriteRegister(I, I);
            }

            // Test reading from arbitrary registers
            Debug.Print("Reading distinct RAM registers (the registers and values read should be the same)");
            for (byte I = DS1307.DS1307_RAM_START_ADDRESS; I <= DS1307.DS1307_RAM_END_ADDRESS; I++)
            {
                Debug.Print(I.ToString() + ": " + clock[I].ToString());
            }

            // Test writing to the RAM as a single block
            //-------------01234567890123456789012345678901234567890123456789012345
            string Text = "[There are 56 bytes in the RTC RAM buffer and that's it]";

            Debug.Print("Writing string: " + Text + " (Length=" + Text.Length.ToString() + ") to the RAM as a block.");
            var ram = new byte[DS1307.DS1307_RAM_SIZE];

            // Copy the string to the ram buffer
            for (byte I = 0; I < DS1307.DS1307_RAM_SIZE; I++)
            {
                ram[I] = (byte)Text[I];
            }
            // Write it to the RAM in the clock
            clock.SetRAM(ram);

            // Zero out the ram buffer
            ram = null;
            // Zero out the string
            Text = null;

            // Test reading from the RAM as a single block
            Debug.Print("Reading from the RAM as a block...");
            ram = clock.GetRAM();

            for (byte I = 0; I < DS1307.DS1307_RAM_SIZE; I++)
            {
                Text += (char)ram[I];
            }

            Debug.Print("RAM: " + Text + " (Length=" + Text.Length.ToString() + ")");

            // Sleep another 5 seconds before exiting
            Thread.Sleep(5 * 1000);

            // Reset the clock & RAM
            clock.Set(new DateTime(2011, 2, 17, 21, 36, 00));

            for (byte I = 0; I < DS1307.DS1307_RAM_SIZE; I++)
            {
                ram[I] = (byte)0;
            }
            // Write it to the RAM in the clock
            clock.SetRAM(ram);
        }