本帖最后由 pcduino 于 2013-5-7 09:39 编辑
pcDuino板子上没有设计RTC,当连接了网络时可以自动同步时间,但是没联网时又想让pcDuino能够时间和日期同步的话就要考虑给pcDuino添加一个实时时钟。
pcDuino通过连接 Linker RTC 模块 可以实现时间日期的自动同步功能,这个模块使用了DS1307芯片,连接pcDuino时使用的是I2C接口。
接线图如下:
GND of Linker RTC -> GND of pcDuino VCC of Linker RTC -> 5V of pcDuino SDA of Linker RTC -> SDA of pcDuino SCL of Linker RTC -> SCL of pcDuino
获取pcDuino的Arduio-ish C 环境:
如果没有安装git:
- $sudo apt-get install git
复制代码用以下命令获取:
- #git clone https://github.com/pcduino/c_enviroment
复制代码用代码测试Link RTC模块: 在目录 c_enviroment/sample下找到 “linker_rtc_test.c”文件,可执行文件位于 “c_enviroment/output/test”。
设置日期/时间:
a ./linker_rtc_test [Year] [Month] [Date] [Hour] [Minute] [Second]. b 在终端检查输出 注意: [Year] [Month] [Date] [Hour] [Minute] [Second] 是可选参数
用代码来设置日期/时间:
当pcDuino启动时,会自动运行 script /etc/rc.local,从 Linker RTC调用读取日期/时间的程序并设置系统日期。 - ubuntu@ubuntu:~/c_enviroment/sample$ more dateset_linker_rtc.c
- /*
- * RTC test program
- */
- #include
- #include "Wire.h"
- #define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
- int command = 0; // This is the command char, in ascii form, sent from the seria
- l port
- int zero = 0;
- //long previousMillis = 0; // will store last time Temp was updated byte second,
- minute, hour, dayOfWeek, dayOfMonth, month, year; byte test;
- //Convert normal decimal numbers to binary coded decimal
- byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- byte test;
- byte decToBcd(byte val)
- {
- return ( (val/10*16) + (val%10) );
- }
- // Convert binary coded decimal to normal decimal numbers
- byte bcdToDec(byte val)
- {
- return ( (val/16*10) + (val%16) );
- }
- void setDateDs1307()
- {
- Wire.beginTransmission(DS1307_I2C_ADDRESS);
- Wire.write(zero);
- Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
- // bit 6 (also need to change readDateDs1307)
- Wire.write(decToBcd(dayOfWeek));
- Wire.write(decToBcd(dayOfMonth));
- Wire.write(decToBcd(month));
- Wire.write(decToBcd(year));
- Wire.endTransmission();
- }
- //Gets the date and time from the ds1307 and prints result
- void getDateDs1307()
- {
- char str_cmd[1000];
- // Reset the register pointer
- Wire.beginTransmission(DS1307_I2C_ADDRESS);
- Wire.write(zero);
- Wire.endTransmission();
- Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
- // A few of these need masks because certain bits are control bits
- second = bcdToDec(Wire.read() & 0x7f);
- minute = bcdToDec(Wire.read());
- hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
- dayOfWeek = bcdToDec(Wire.read());
- dayOfMonth = bcdToDec(Wire.read());
- month = bcdToDec(Wire.read());
- year = bcdToDec(Wire.read());
- sprintf(str_cmd, "sudo date -s %d/%d/%d\n", month, dayOfMonth, year);
- system(str_cmd);
- sprintf(str_cmd, "sudo date -s %d:%d:%d\n", hour, minute, second);
- system(str_cmd);
- }
- void setup()
- {
- Wire.begin();
- getDateDs1307();
- }
- void loop()
- {
- exit(0);
- }
复制代码可执行文件要添加到 /etc/rc.local
使用“sudo vi /etc/rc.local”,在 “exit 0″前插入下面这行
- /home/ubuntu/c_enviroment/otuput/test/dateset_linker_rtc &
复制代码这样就完成了,在不联网时也可以获取正确的日期时间。
|