source: rtos_arduino/trunk/arduino_lib/hardware/arduino/samd/cores/validation/validation_I2C_multi/test.cpp

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

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

File size: 2.4 KB
Line 
1#include "Arduino.h"
2#include <Wire.h>
3#include <LiquidCrystal_I2C.h>
4
5
6const uint8_t addressTemp = 0x4Ful;
7const uint8_t addressLCD = 0x20ul;
8const uint8_t addressRTC = 0x68ul;
9uint16_t valueTemp = 0;
10uint8_t a, b;
11
12LiquidCrystal_I2C lcd(addressLCD,16,2); // set the LCD address to 0x20(Cooperate with 3 short circuit caps) for a 16 chars and 2 line display
13
14struct timeRTC
15{
16 uint8_t hour;
17 uint8_t minute;
18 uint8_t second;
19
20 uint8_t dayMonth;
21 uint8_t dayWeek;
22 uint8_t month;
23 uint8_t year;
24} timeRtc;
25
26// Convert normal decimal to numbers binary coded decimal
27uint8_t decToBcd(byte val)
28{
29 return ( (val/10*16) + (val%10) );
30}
31
32// Convert binary coded decimal to normal decimal numbers
33uint8_t bcdToDec(byte val)
34{
35 return ( (val/16*10) + (val%16) );
36}
37
38void updateTime()
39{
40 Wire.beginTransmission(addressRTC);
41 //Wire.write((uint8_t)0x3F);
42 Wire.write((uint8_t)0x00);
43 Wire.endTransmission();
44
45 delay(10);
46
47 Wire.requestFrom(addressRTC, 7);
48
49 timeRtc.second = bcdToDec(Wire.read());
50 timeRtc.minute = bcdToDec(Wire.read());
51 timeRtc.hour = bcdToDec(Wire.read());
52
53 timeRtc.dayWeek = bcdToDec(Wire.read());
54 timeRtc.dayMonth = bcdToDec(Wire.read());
55 timeRtc.month = bcdToDec(Wire.read());
56 timeRtc.year = bcdToDec(Wire.read());
57}
58
59void setup()
60{
61 Serial5.begin( 115200 );
62 Serial5.println("Wire init");
63 Wire.begin();
64
65 lcd.init();
66 lcd.backlight();
67 lcd.home();
68
69 pinMode(2, INPUT_PULLUP);
70}
71
72void LCDSpecialPrint(uint8_t value)
73{
74 if(value < 10)
75 {
76 lcd.print('0');
77 }
78
79 lcd.print(value);
80}
81
82void loop()
83{
84 Wire.beginTransmission(addressTemp);
85 Wire.write((uint8_t) 0x00);
86 Wire.endTransmission();
87
88 delay(10);
89
90 Wire.requestFrom(addressTemp, 2);
91 Serial5.print((char)13); // Erase current line
92 Serial5.print("Temperature : ");
93
94 a = Wire.read();
95 b = Wire.read();
96
97 valueTemp = a << 7;
98 valueTemp |= b;
99 valueTemp >>= 7;
100
101 Serial5.print(a);
102 Serial5.print(" | ");
103 Serial5.print(b);
104
105 updateTime();
106 lcd.setCursor(0, 0);
107 lcd.print(" ");
108 LCDSpecialPrint(valueTemp);
109 lcd.print((char)0xDF);
110 lcd.print("C ");
111
112 LCDSpecialPrint(timeRtc.month);
113 lcd.print("/");
114 LCDSpecialPrint(timeRtc.dayMonth);
115 lcd.print("/");
116 LCDSpecialPrint(timeRtc.year);
117 lcd.print(" ");
118
119 lcd.setCursor(0, 1);
120 lcd.print(" ");
121 LCDSpecialPrint(timeRtc.hour);
122 lcd.print(":");
123 LCDSpecialPrint(timeRtc.minute);
124 lcd.print(":");
125 LCDSpecialPrint(timeRtc.second);
126}
Note: See TracBrowser for help on using the repository browser.