root / Ports / examples / rtc_demo / rtc_demo.pde
History | View | Annotate | Download (1.6 KB)
| 1 | 7763 | jcw | //>>> The latest version of this code can be found at https://github.com/jcw/ !! |
|---|---|---|---|
| 2 | 7763 | jcw | |
| 3 | 4727 | jcw | // Hooking up a DS1307 (5V) or DS1340Z (3V) real time clock via I2C. |
| 4 | 5302 | jcw | // see http://jeelabs.org/cp1 |
| 5 | 4727 | jcw | // 2009-09-17 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php |
| 6 | 4727 | jcw | // $Id$ |
| 7 | 4727 | jcw | |
| 8 | 4727 | jcw | // the real-time clock is connected to port 1 in I2C mode (AIO = SCK, dIO = SDA) |
| 9 | 4727 | jcw | |
| 10 | 4727 | jcw | #include <Ports.h> |
| 11 | 4727 | jcw | #include <RF12.h> // needed to avoid a linker error :( |
| 12 | 4727 | jcw | |
| 13 | 4727 | jcw | PortI2C myport (1 /*, PortI2C::KHZ400 */); |
| 14 | 4727 | jcw | DeviceI2C rtc (myport, 0x68); |
| 15 | 4727 | jcw | |
| 16 | 4727 | jcw | static byte bin2bcd (byte val) {
|
| 17 | 4727 | jcw | return val + 6 * (val / 10); |
| 18 | 4727 | jcw | } |
| 19 | 4727 | jcw | |
| 20 | 4727 | jcw | static byte bcd2bin (byte val) {
|
| 21 | 4727 | jcw | return val - 6 * (val >> 4); |
| 22 | 4727 | jcw | } |
| 23 | 4727 | jcw | |
| 24 | 4727 | jcw | static void setDate (byte yy, byte mm, byte dd, byte h, byte m, byte s) {
|
| 25 | 4727 | jcw | rtc.send(); |
| 26 | 4727 | jcw | rtc.write(0); |
| 27 | 4727 | jcw | rtc.write(bin2bcd(s)); |
| 28 | 4727 | jcw | rtc.write(bin2bcd(m)); |
| 29 | 4727 | jcw | rtc.write(bin2bcd(h)); |
| 30 | 4727 | jcw | rtc.write(bin2bcd(0)); |
| 31 | 4727 | jcw | rtc.write(bin2bcd(dd)); |
| 32 | 4727 | jcw | rtc.write(bin2bcd(mm)); |
| 33 | 4727 | jcw | rtc.write(bin2bcd(yy)); |
| 34 | 4727 | jcw | rtc.write(0); |
| 35 | 4727 | jcw | rtc.stop(); |
| 36 | 4727 | jcw | } |
| 37 | 4727 | jcw | |
| 38 | 4727 | jcw | static void getDate (byte* buf) {
|
| 39 | 4727 | jcw | rtc.send(); |
| 40 | 4727 | jcw | rtc.write(0); |
| 41 | 4727 | jcw | rtc.stop(); |
| 42 | 4727 | jcw | |
| 43 | 4727 | jcw | rtc.receive(); |
| 44 | 4727 | jcw | buf[5] = bcd2bin(rtc.read(0)); |
| 45 | 4727 | jcw | buf[4] = bcd2bin(rtc.read(0)); |
| 46 | 4727 | jcw | buf[3] = bcd2bin(rtc.read(0)); |
| 47 | 4727 | jcw | rtc.read(0); |
| 48 | 4727 | jcw | buf[2] = bcd2bin(rtc.read(0)); |
| 49 | 4727 | jcw | buf[1] = bcd2bin(rtc.read(0)); |
| 50 | 4727 | jcw | buf[0] = bcd2bin(rtc.read(1)); |
| 51 | 4727 | jcw | rtc.stop(); |
| 52 | 4727 | jcw | } |
| 53 | 4727 | jcw | |
| 54 | 4727 | jcw | void setup() {
|
| 55 | 4727 | jcw | Serial.begin(57600); |
| 56 | 4727 | jcw | Serial.println("\n[rtc_demo]");
|
| 57 | 4727 | jcw | |
| 58 | 4727 | jcw | // test code: |
| 59 | 4727 | jcw | setDate(9, 9, 17, 13, 18, 0); |
| 60 | 4727 | jcw | } |
| 61 | 4727 | jcw | |
| 62 | 4727 | jcw | void loop() {
|
| 63 | 4727 | jcw | byte now[6]; |
| 64 | 4727 | jcw | getDate(now); |
| 65 | 4727 | jcw | |
| 66 | 4727 | jcw | Serial.print("rtc");
|
| 67 | 4727 | jcw | for (byte i = 0; i < 6; ++i) {
|
| 68 | 4727 | jcw | Serial.print(' ');
|
| 69 | 4727 | jcw | Serial.print((int) now[i]); |
| 70 | 4727 | jcw | } |
| 71 | 4727 | jcw | Serial.println(); |
| 72 | 4727 | jcw | |
| 73 | 4727 | jcw | delay(1000); |
| 74 | 4727 | jcw | } |