简体   繁体   English

Windows IoT Raspberry Pi 3 C#RTC DS3231

[英]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. 这篇文章似乎与Windows IoT和DS3231 RTC时钟重复,但对我似乎不起作用。 i don't have experience using i2c in this environment. 我没有在这种环境下使用i2c的经验。

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 如果离线,系统将从DS3231读取datetime时间并更新系统datetime
  3. any changes of datetime from datepicker or timepicker it will update DS3231 任何变更datetimedatepickertimepicker它将更新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? 使用i1cdevice.writereadDS3231读取需要单独write read address does the i2c automatically reads all the data until stop bit is received or i have to setup a counter ? i2c自动读取所有数据,直到收到stop位或我必须设置一个计数器?

     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 DS3231dateickertimepicker的时间设定在设定datetime ,我怎么能升级到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. 1.如何检查网络可用性以更新系统时钟。

You can use GetIsNetworkAvailable method of NetworkInterface class to check whether internet connected or not. 您可以使用NetworkInterface类的GetIsNetworkAvailable方法检查Internet是否连接。 You can get more knowleage about how to check internet connectivity type in Universal Windows Platform from this topic . 您可以从本主题获得有关如何在Universal Windows Platform中检查Internet连接类型的更多知识。

bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();

2.to read from DS3231 using i1cdevice.writeread requires to write read address separately? 2.使用i1cdevice从DS3231读取.writeread是否需要单独写入读取地址? does the i2c automatically reads all the data until stop bit is received or i have to setup a counter ? i2c是否会自动读取所有数据,直到收到停止位或我必须设置一个计数器?

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. WriteRead方法执行原子操作,以将数据写入与设备相连的内部集成电路(I2 C)总线,然后从中读取数据,并在写入和读取操作之间发送重启条件。 The second parameter is the buffer to which you want to read the data from the I2 C bus. 第二个参数是要从I2 C总线读取数据的缓冲区。 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). 缓冲区的长度决定了要向设备请求的数据量,直到接收到停止位后它才会自动停止。如果I2 C设备在读取整个缓冲区之前否定了数据传输的操作,则会出现错误(错误代码0x8007045D)。

3.to write to DS3231 there are dateicker & timepicker for time setting upon setting the datetime how could I update to DS3231 3.要写入DS3231,设置日期时间时需要设置日期和时间选择器,如何更新到DS3231

As TRS replied in the topic Windows IoT and DS3231 RTC clock , he/she provided the method to set clock. 正如TRS在Windows IoT和DS3231 RTC时钟主题中所回答的那样,他/她提供了设置时钟的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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