Windows IoT and DS3231 RTC clock

99封情书 提交于 2019-12-05 18:34:27

Sorry for the belated reply! My solution so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
using System.Diagnostics;

namespace _015_Test15_I2C_Clock_RTC3231
{
    public class I2C_Time
    {
        private static string AQS;
        private static DeviceInformationCollection DIS;

        public static async Task GetTimeFromDS3231()
        {

            /* DS3231 I2C SLAVE address */
            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 DeviceInformation.FindAllAsync(AQS);
                }


                using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings))
                {
                    byte[] writeBuf = { 0x00 };
                    Device.Write(writeBuf);
                    byte[] readBuf = new byte[7];
                    Device.Read(readBuf);
                    byte second = bcdToDec((byte)(readBuf[0] & 0x7f));
                    byte minute = bcdToDec(readBuf[1]);
                    byte hour = bcdToDec((byte)(readBuf[2] & 0x3f));
                    byte dayOfWeek = bcdToDec(readBuf[3]);
                    byte dayOfMonth = bcdToDec(readBuf[4]);
                    byte month = bcdToDec(readBuf[5]);
                    byte year = bcdToDec(readBuf[6]);
                }
            }
            catch (Exception)
            {
                Debug.WriteLine("Error in I2C_Time Class");
            }
        }

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

To set the clock:

 using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings))
            {
                byte write_seconds = decToBcd(set_Time_now_seconds);
                byte write_minutes = decToBcd(set_Time_now_minutes);
                byte write_hours = decToBcd(set_Time_now_hours);
                byte write_dayofweek = decToBcd(set_Date_now_dayofweek);
                byte write_day = decToBcd(set_Date_now_day);
                byte write_month = decToBcd(set_Date_now_month);
                byte write_year = decToBcd(set_Date_now_year);

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

                Device.Write(write_time);

            }
        // Convert normal decimal numbers to binary coded decimal
    private static byte decToBcd(byte val)
    {
        return (byte)(((int)val / 10 * 16) + ((int)val % 10));
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!