Arduino Windows IoT和DS3231 RTC时钟
对于我的项目,我需要当前的时间和日期。不幸的是,当RP2关闭时,它会失去一切。下一件事是,我没有互联网连接来使用NTP。 为此,我需要实现一个DS3231 RTC模块。 所有设备的通信都通过I2C(Raspberry Arduino DS3231)进行。 此时,我的Arduino与模块通信,并将日期和时间存储在字符数组中。RP2与Arduino通信以获取日期/时间。这实际上效果不错。但我想直接与模块通信以节省Arduino上的资源(它只是一个Nano)。 因此,我想知道是否有人有使用该模块和Windows IoT的经验 您可以在下面找到我当前的解决方案: 阿杜伊诺:Arduino Windows IoT和DS3231 RTC时钟,arduino,i2c,windowsiot,Arduino,I2c,Windowsiot,对于我的项目,我需要当前的时间和日期。不幸的是,当RP2关闭时,它会失去一切。下一件事是,我没有互联网连接来使用NTP。 为此,我需要实现一个DS3231 RTC模块。 所有设备的通信都通过I2C(Raspberry Arduino DS3231)进行。 此时,我的Arduino与模块通信,并将日期和时间存储在字符数组中。RP2与Arduino通信以获取日期/时间。这实际上效果不错。但我想直接与模块通信以节省Arduino上的资源(它只是一个Nano)。 因此,我想知道是否有人有使用该模块和Wi
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
#define MyAddress 0x40 /* Define the i2c address */
char time_char[10];
char date_char[10];
byte ReceivedData;
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup()
{
Wire.begin(MyAddress);
Serial.begin(9600);
Wire.onReceive(I2CReceived);
Wire.onRequest(I2CRequest);
}
void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
String hour_str, minute_str, second_str, day_str, month_str, year_str, hour_str_orig, minute_str_orig, second_str_orig, time_str, date_str;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,&year);
if (hour<10)
{
hour_str_orig = (hour);
hour_str = ("0" + hour_str_orig);
}
else
{
hour_str = (hour);
}
if (minute<10)
{
minute_str_orig = (minute);
minute_str = ("0" + minute_str_orig);
}
else
{
minute_str = (minute);
}
if (second<10)
{
second_str_orig = (second);
second_str = ("0" + second_str_orig);
}
else
{
second_str = (second);
}
day_str = (dayOfMonth);
month_str = (month);
year_str = (year);
time_str = (hour_str + ":" + minute_str + ":" + second_str);
date_str = (day_str + "." + month_str + "." + "20" +year_str);
time_str.toCharArray(time_char, 10);
date_str.toCharArray(date_char, 10);
}
void loop()
{
displayTime(); // send the real-time clock data to IoT
delay(1000); // every second
}
void I2CReceived(int NumberOfBytes)
{
/* WinIoT have sent data byte; read it */
ReceivedData = Wire.read();
}
void I2CRequest()
{
if (ReceivedData == 50)
{
Wire.write(time_char);
}
if (ReceivedData == 51)
{
Wire.write(date_char);
}
}
#包括“Wire.h”
#定义DS3231_I2C_地址0x68
#定义MyAddress 0x40/*定义i2c地址*/
char time_char[10];
字符日期_char[10];
字节接收数据;
//将二进制编码的十进制数转换为普通十进制数
字节bcdToDec(字节val)
{
返回值((val/16*10)+(val%16));
}
无效设置()
{
Wire.begin(我的地址);
Serial.begin(9600);
线接收(I2CReceived);
在线请求(I2CRequest);
}
无效读取DS3231时间(字节*秒,字节*分钟,字节*小时,字节*星期几,字节*月日,字节*月,字节*年)
{
线路开始传输(DS3231_I2C_地址);
Wire.write(0);//将DS3231寄存器指针设置为00h
导线端传动();
Wire.requestFrom(DS3231_I2C_地址,7);
//从寄存器00h开始从DS3231请求七个字节的数据
*second=bcdToDec(Wire.read()&0x7f);
*分钟=bcdToDec(Wire.read());
*小时=bcdToDec(Wire.read()&0x3f);
*dayOfWeek=bcdToDec(Wire.read());
*dayOfMonth=bcdToDec(Wire.read());
*月份=bcdToDec(Wire.read());
*年份=bcdToDec(Wire.read());
}
void displayTime()
{
字符串小时、分钟、秒、日、月、年、小时、分钟、秒、时间、日期;
字节秒,分钟,小时,星期日,月日,月,年;
//从DS3231检索数据
读取DS3231时间(秒、分、时、周、月、月、年);
如果(小时对不起,回复迟了!
到目前为止,我的解决方案是:
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));
}
}
}
要设置时钟:
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));
}
你在物联网中有任何时钟信息吗?@BlurrySterk为我迟来的回答感到抱歉。是的,我得到了时钟信息,但只是通过Arduino我无法确定你的问题到底是什么。我会直接在你的物联网中实现I2C通信。你在那里尝试过吗?这正是我遇到的问题。我无法确定正确地将arduino代码翻译成c#for IoT。当我尝试将其实现为IoT时,我收到了无意义或根本没有收到任何信息……上面的代码包括您的努力吗?
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));
}
}
}
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));
}