pcDuino does’t have on-board RTC. It will automatically sync its time when it gets network connection. What if we want to run pcDuino without network connection? If you still want your pcDuino to have the capability to correct date and time, consider to add a RTC to your pcDuino!
In this post, we will show how to interface Linker RTC module to pcDuino, and have pcDuino automatically sync its date/time from Linker RTC.
Linker RTC module uses DS1307 RTC chip, and interfaces to pcDuino using I2C.
Wiring Diagram:
Wiring Instructions:
- GND of Linker RTC module -> GND of pcDuino
- VCC of Linker RTC module -> 5V of pcDuino
- SDA of Linker RTC module -> SDA of pcDuino
- SCL of Linker RTC module -> SCL of pcDuino
Obtain pcDuino’s Arduio-ish C environment:
If you don’t have git installed, please do:
$sudo apt-get install git
Use the following command to get the repo:
#git clone https://github.com/pcduino/c_enviroment
C code to test Linker RTC module:
Under the c_enviroment/sample directory, there is a file named “linker_rtc_test.c”. The executable is located at “c_enviroment/output/test”.
To set the date/time, please using:
a) ./linker_rtc_test [Year] [Month] [Date] [Hour] [Minute] [Second].
b) Check the outputs on you terminal.
Note: [Year] [Month] [Date] [Hour] [Minute] [Second] are optional parameters.
C code to set date/time:
When pcDuino boots, it will automatically run script /etc/rc.local. We call a program which will read the date/time from Linker RTC and set the system date.
The C code to read date/time from Linker RTC module, and set date/time is as following:
dateset_linker_rtc( download here)
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); }
The executable file needs to be added to /etc/rc.local.
We use “sudo vi /etc/rc.local” and inset the following line before “exit 0”.
/home/ubuntu/c_enviroment/otuput/test/dateset_linker_rtc &
We are ready to go! Power cycle pcDuino without network connection, we still get correct date and time.
Leave a Reply
You must be logged in to post a comment.