source: rtos_arduino/trunk/arduino_lib/hardware/arduino/samd/cores/validation/validation_I2C_RTC/test.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 1.8 KB
Line 
1#include "Arduino.h"
2#include <Wire.h>
3
4const uint8_t address = 0x68;
5
6uint8_t decToBcd(byte val)
7{
8 return ( (val/10*16) + (val%10) );
9}
10
11// Convert binary coded decimal to normal decimal numbers
12uint8_t bcdToDec(byte val)
13{
14 return ( (val/16*10) + (val%16) );
15}
16
17void setup()
18{
19 Serial5.begin(9600);
20 Serial5.println("Wire init");
21 Wire.begin();
22
23 // Setting the date
24 /*
25 Wire.beginTransmission(address);
26 Wire.write((uint8_t)0x00); // Init register
27
28 Wire.write(decToBcd(0)); // Second
29 Wire.write(decToBcd(39)); // Minute
30 Wire.write(decToBcd(11)); // Hours
31
32 Wire.write(decToBcd(4)); // Day of week
33
34 Wire.write(decToBcd(21)); // Day of Month
35 Wire.write(decToBcd(5)); // Month
36 Wire.write(decToBcd(14)); // Year
37 Wire.endTransmission();
38 */
39}
40
41void loop()
42{
43
44 Wire.beginTransmission(address);
45 Wire.write((uint8_t)0x3F);
46 Wire.endTransmission();
47
48 delay(10);
49
50 Wire.requestFrom(address, 7);
51/* while(Wire.available())
52 {
53 Serial5.print(bcdToDec(Wire.read()));
54 Serial5.print(" ");
55 }
56 Serial5.println();*/
57
58
59 int second = bcdToDec(Wire.read() & 0x7f);
60 int minute = bcdToDec(Wire.read());
61 int hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
62 int dayOfWeek = bcdToDec(Wire.read());
63 int dayOfMonth = bcdToDec(Wire.read());
64 int month = bcdToDec(Wire.read());
65 int year = bcdToDec(Wire.read());
66
67 Serial5.print(hour, DEC);
68 Serial5.print(":");
69 Serial5.print(minute, DEC);
70 Serial5.print(":");
71 Serial5.print(second, DEC);
72 Serial5.print(" ");
73 Serial5.print(month, DEC);
74 Serial5.print("/");
75 Serial5.print(dayOfMonth, DEC);
76 Serial5.print("/");
77 Serial5.print(year,DEC);
78 Serial5.print(" ");
79 Serial5.println();
80
81 delay(990);
82}
Note: See TracBrowser for help on using the repository browser.