对于我的项目,我需要当前时间和日期。不幸的是,RP2 在关闭时会丢失所有内容。接下来是,我将没有互联网连接来使用 NTP。为此,我需要实现一个 DS3231 RTC 模块。所有设备的通信都通过 I2C (Raspberry <-> Arduino <-> DS3231) 运行。目前我的 Arduino 与模块通信并将日期和时间存储在字符数组中。RP2 与 Arduino 通信以获取日期/时间。这实际上效果很好。但我想直接与模块通信以节省 Arduino 上的资源(它只是一个 Nano)。因此,我想知道是否有人对模块和 Windows IoT 有经验。
您将在下面找到我当前的解决方案:
阿杜诺:
#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);
}
}
物联网:
public async void TestFunction()
{
for (int i = 0; i < 50; i++)
{
for (byte DataToBeSend = 50; DataToBeSend < 52; DataToBeSend++)
{
if (DataToBeSend == 50)
{
byte ReceivedData;
ReceivedData = await Time.WriteRead_OneByte(DataToBeSend);
await Task.Delay(10);
}
if (DataToBeSend == 51)
{
byte ReceivedData;
ReceivedData = await Time.WriteRead_OneByte(DataToBeSend);
await Task.Delay(10);
}
}
await Task.Delay(1000);
}
}
班级:
public class Time
{
private static string AQS;
private static DeviceInformationCollection DIS;
public static float result_fl;
public static async System.Threading.Tasks.Task<byte> WriteRead_OneByte(byte ByteToBeSend)
{
byte[] ReceivedData = new byte[10];
/* Gateway's I2C SLAVE address */
int SlaveAddress = 64; // 0x40
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))
{
/* Send byte to Arduino Nano */
Device.Write(new byte[] { ByteToBeSend });
/* Read byte from Arduino Nano */
Device.Read(ReceivedData);
/* Rewrite Array to avoid empty space in source array */
var i = ReceivedData.Length - 1;
while (ReceivedData[i] == 255)
{
--i;
}
var date_time_arr = new byte[i + 1];
Array.Copy(ReceivedData, date_time_arr, i + 1);
/* Show date or time */
string result = System.Text.Encoding.UTF8.GetString(date_time_arr);
Debug.WriteLine(result);
}
}
catch (Exception)
{
}
return ReceivedData[0];
}
}
如果您需要更多信息,请告诉我。谢谢!