簡體   English   中英

Windows IoT Raspberry Pi 3 C#RTC DS3231

[英]Windows IoT Raspberry Pi 3 C# RTC DS3231

這篇文章似乎與Windows IoT和DS3231 RTC時鍾重復,但對我似乎不起作用。 我沒有在這種環境下使用i2c的經驗。

我的預期功能是

  1. 在啟動時檢查是否有網絡連接來更新系統時鍾
  2. 如果離線,系統將從DS3231讀取datetime時間並更新系統datetime
  3. 任何變更datetimedatepickertimepicker它將更新DS3231

我的問題是

  1. 如何檢查網絡可用性以更新系統時鍾。
  2. 使用i1cdevice.writereadDS3231讀取需要單獨write read address 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. 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); } 

謝謝。

1.如何檢查網絡可用性以更新系統時鍾。

您可以使用NetworkInterface類的GetIsNetworkAvailable方法檢查Internet是否連接。 您可以從本主題獲得有關如何在Universal Windows Platform中檢查Internet連接類型的更多知識。

bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();

2.使用i1cdevice從DS3231讀取.writeread是否需要單獨寫入讀取地址? i2c是否會自動讀取所有數據,直到收到停止位或我必須設置一個計數器?

不必分開寫讀。 WriteRead方法執行原子操作,以將數據寫入與設備相連的內部集成電路(I2 C)總線,然后從中讀取數據,並在寫入和讀取操作之間發送重啟條件。 第二個參數是要從I2 C總線讀取數據的緩沖區。 緩沖區的長度決定了要向設備請求的數據量,直到接收到停止位后它才會自動停止。如果I2 C設備在讀取整個緩沖區之前否定了數據傳輸的操作,則會出現錯誤(錯誤代碼0x8007045D)。

3.要寫入DS3231,設置日期時間時需要設置日期和時間選擇器,如何更新到DS3231

正如TRS在Windows IoT和DS3231 RTC時鍾主題中所回答的那樣,他/她提供了設置時鍾的方法。

新更新:

    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