简体   繁体   中英

Windows IoT Raspberry Pi 3 C# RTC DS3231

this post may seems duplicate as Windows IoT and DS3231 RTC clock but it doesn't seem to work for me. i don't have experience using i2c in this environment.

My intended functionality is,

  1. at startup check if there is any network connectivity to update system clock
  2. if offline the system will read datetime from DS3231 and update the system datetime
  3. any changes of datetime from datepicker or timepicker it will update DS3231

My problem is

  1. How to check for network availibility to update system clock.
  2. to read from DS3231 using i1cdevice.writeread requires to write read address separately? does the i2c automatically reads all the data until stop bit is received or i have to setup a counter ?

     private const byte DS3231_I2C_WADDR = 0xD0; private const byte DS3231_I2C_RADDR = 0xD1; private I2cDevice DS3231_RTC; byte[] i2CWriteBuffer; byte[] i2CReadBuffer; private async void Read_DS3231_RTC() { var settings = new I2cConnectionSettings(DS3231_I2C_RADDR); settings.BusSpeed = I2cBusSpeed.StandardMode; var controller = await I2cController.GetDefaultAsync(); DS3231_RTC = controller.GetDevice(settings); try { DS3231_RTC.WriteRead(new byte[] { DS3231_I2C_RADDR }, i2CReadBuffer); } catch (Exception e) { StatusMessage.Text = "Fail to Init I2C:" + e.Message; return; } } 
  3. to write to DS3231 there are dateicker & timepicker for time setting upon setting the datetime how could I update to DS3231

     private void DatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e) { DateTimeSettings.SetSystemDateTime(e.NewDate.UtcDateTime); SetTime_DS3231(); } private void TimePicker_TimeChanged(object sender, TimePickerValueChangedEventArgs e) { var currentDate = DateTime.Now.ToUniversalTime(); var newDateTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, e.NewTime.Hours, e.NewTime.Minutes, e.NewTime.Seconds); DateTimeSettings.SetSystemDateTime(newDateTime); } 

Thank you.

1.How to check for network availibility to update system clock.

You can use GetIsNetworkAvailable method of NetworkInterface class to check whether internet connected or not. You can get more knowleage about how to check internet connectivity type in Universal Windows Platform from this topic .

bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();

2.to read from DS3231 using i1cdevice.writeread requires to write read address separately? does the i2c automatically reads all the data until stop bit is received or i have to setup a counter ?

It is not necessary to separate write read. WriteRead method Performs an atomic operation to write data to and then read data from the inter-integrated circuit (I2 C) bus on which the device is connected, and sends a restart condition between the write and read operations. The second parameter is the buffer to which you want to read the data from the I2 C bus. The length of the buffer determines how much data to request from the device.It does not stop automatically until stop bit is received.If the I2 C device negatively acknowledged the data transfer before the entire buffer was read, there will be error(Error Code 0x8007045D).

3.to write to DS3231 there are dateicker & timepicker for time setting upon setting the datetime how could I update to DS3231

As TRS replied in the topic Windows IoT and DS3231 RTC clock , he/she provided the method to set clock.

New Update:

    private async void SetTime_DS3231(DateTime dt)
    {
        int SlaveAddress = 0x68;

        try
        {
            // Initialize I2C
            var Settings = new I2cConnectionSettings(SlaveAddress);
            Settings.BusSpeed = I2cBusSpeed.StandardMode;

            if (AQS == null || DIS == null)
            {
                AQS = I2cDevice.GetDeviceSelector("I2C1");
                DIS = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(AQS);
            }

            using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings))
            {
                byte write_seconds = decToBcd((byte)dt.Second);
                byte write_minutes = decToBcd((byte)dt.Minute);
                byte write_hours = decToBcd((byte)dt.Hour);
                byte write_dayofweek = decToBcd((byte)dt.DayOfWeek);
                byte write_day = decToBcd((byte)dt.Day);
                byte write_month = decToBcd((byte)dt.Month);
                byte write_year = decToBcd((byte)(dt.Year%100));

                byte[] write_time = { 0x00, write_seconds, write_minutes, write_hours, write_dayofweek, write_day, write_month, write_year };

                Device.Write(write_time);

            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.Write(ex.Message);
        }
    }

    private static byte decToBcd(byte val)
    {
        return (byte)(((int)val / 10 * 16) + ((int)val % 10));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM