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); } }
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); }