質問編集履歴

1

この2つのコードを使ってLCDの日付けと時刻を表示させたいのです。

2021/01/31 06:27

投稿

DMR
DMR

スコア6

test CHANGED
File without changes
test CHANGED
@@ -1 +1,305 @@
1
1
  LCD(SO1602AWWB-UC-WB-U)」を持っています。これを使ってRTC(DS1307)とシリアル通信でLCDに表示させる方法が分かりません。
2
+
3
+
4
+
5
+ ```#include <Wire.h>
6
+
7
+ int DS1307_ADDRESS=0x68;
8
+
9
+ int val=0;
10
+
11
+ byte command;
12
+
13
+
14
+
15
+ void setTime(){
16
+
17
+ byte hour=(Serial.read()<<4);
18
+
19
+ hour=hour+(Serial.read()&0x0F);
20
+
21
+ byte minute=(Serial.read()<<4);
22
+
23
+ minute=minute+(Serial.read()&0x0F);
24
+
25
+ byte sec=(Serial.read()<<4);
26
+
27
+ sec=sec+(Serial.read()&0x0F);
28
+
29
+ Wire.beginTransmission(DS1307_ADDRESS);
30
+
31
+ Wire.write(val);
32
+
33
+ Wire.write(sec);
34
+
35
+ Wire.write(minute);
36
+
37
+ Wire.write(hour);
38
+
39
+ Wire.endTransmission();
40
+
41
+ }
42
+
43
+
44
+
45
+ void setDay(){
46
+
47
+ byte year=(Serial.read()<<4);
48
+
49
+ year=year+(Serial.read()&0x0F);
50
+
51
+ byte month=(Serial.read()<<4);
52
+
53
+ month=month+(Serial.read()&0x0F);
54
+
55
+ byte day=(Serial.read()<<4);
56
+
57
+ day=day+(Serial.read()&0x0F);
58
+
59
+ byte day_of_week=(Serial.read()&0x0F);
60
+
61
+ Wire.beginTransmission(DS1307_ADDRESS);
62
+
63
+ Wire.write(0x03);
64
+
65
+ Wire.write(day_of_week);
66
+
67
+ Wire.write(day);
68
+
69
+ Wire.write(month);
70
+
71
+ Wire.write(year);
72
+
73
+ Wire.endTransmission();
74
+
75
+ }
76
+
77
+ void printTime(){
78
+
79
+ Wire.beginTransmission(DS1307_ADDRESS);
80
+
81
+ Wire.write(val);
82
+
83
+ Wire.endTransmission();
84
+
85
+ Wire.requestFrom(DS1307_ADDRESS,7);
86
+
87
+ byte r_sec=Wire.read();
88
+
89
+ byte r_minute=Wire.read();
90
+
91
+ byte r_hour=Wire.read();
92
+
93
+ byte r_day_of_week=Wire.read();
94
+
95
+ byte r_date=Wire.read();
96
+
97
+ byte r_month=Wire.read();
98
+
99
+ byte r_year=Wire.read();
100
+
101
+ Serial.print(r_year,HEX);
102
+
103
+ Serial.print("/");
104
+
105
+ Serial.print(r_month,HEX);
106
+
107
+ Serial.print("/");
108
+
109
+ Serial.print(r_date,HEX);
110
+
111
+ Serial.print(" ");
112
+
113
+ Serial.print(r_hour,HEX);
114
+
115
+ Serial.print(":");
116
+
117
+ Serial.print( r_minute,HEX);
118
+
119
+ Serial.print(":");
120
+
121
+ Serial.print( r_sec,HEX);
122
+
123
+ Serial.print(" smtwtfs= ");
124
+
125
+ Serial.println(r_day_of_week,HEX);
126
+
127
+
128
+
129
+ }
130
+
131
+
132
+
133
+ void setup() {
134
+
135
+ Serial.begin(9600);
136
+
137
+ Wire.begin();
138
+
139
+ Serial.flush();
140
+
141
+ }
142
+
143
+
144
+
145
+ void loop() {
146
+
147
+ if(Serial.available()){
148
+
149
+ command=Serial.read();
150
+
151
+ if(command==0x74){
152
+
153
+ setTime();
154
+
155
+ }
156
+
157
+ Serial.println(command);
158
+
159
+ if(command==0x64){
160
+
161
+ setDay();
162
+
163
+ }
164
+
165
+ }
166
+
167
+ printTime();
168
+
169
+ delay(2000);
170
+
171
+
172
+
173
+
174
+
175
+ }
176
+
177
+
178
+
179
+
180
+
181
+ ```#include <Wire.h>
182
+
183
+ #define OLED_ADRS 0x3C //SA0=L(SA0=H の場合は 0x3D)
184
+
185
+ int DisplayON = 0x0F,
186
+
187
+ ClearDisplay = 0x01,
188
+
189
+ ReturnHome = 0x02;
190
+
191
+ void setup() {
192
+
193
+ Wire.begin(); //Wire ライブラリを初期化し、I2C マスタとしてバスに接続
194
+
195
+ init_oled();
196
+
197
+ }
198
+
199
+ void loop() {
200
+
201
+ char moji[] = "I2C OLED WHITE12345";
202
+
203
+ for(int i = 0; i < 20; i++) {
204
+
205
+ writeData(moji[i]);
206
+
207
+ }
208
+
209
+
210
+
211
+ writeCommand(0x20+0x80); //2 行目の先頭
212
+
213
+
214
+
215
+ for(int i = 0; i < 20; i++) {
216
+
217
+ writeData(i+0xB1);
218
+
219
+ }
220
+
221
+ contrast_max(); //輝度を最大に設定
222
+
223
+ while(1){}
224
+
225
+ }
226
+
227
+ //----main end----
228
+
229
+ void writeData(byte t_data)
230
+
231
+ {
232
+
233
+ Wire.beginTransmission(OLED_ADRS);
234
+
235
+ Wire.write(0x40);
236
+
237
+ Wire.write(t_data);
238
+
239
+ Wire.endTransmission();
240
+
241
+ delay(1);
242
+
243
+ }
244
+
245
+ void writeCommand(byte t_command)
246
+
247
+ {
248
+
249
+ Wire.beginTransmission(OLED_ADRS);
250
+
251
+ Wire.write(0x00);
252
+
253
+ Wire.write(t_command);
254
+
255
+ Wire.endTransmission();
256
+
257
+ delay(10);
258
+
259
+ }
260
+
261
+ void contrast_max(){
262
+
263
+ writeCommand(0x2a);//RE=1
264
+
265
+ writeCommand(0x79);//SD=1
266
+
267
+ writeCommand(0x81);//コントラストセット
268
+
269
+ writeCommand(0xFF);//輝度MAX
270
+
271
+ writeCommand(0x78);//SD を0にもどす
272
+
273
+ writeCommand(0x28); //2C=高文字 28=ノーマル
274
+
275
+ delay(100);
276
+
277
+ }
278
+
279
+ void init_oled()
280
+
281
+ {
282
+
283
+ delay(100);
284
+
285
+ writeCommand(ClearDisplay); // Clear Display
286
+
287
+ delay(20);
288
+
289
+ writeCommand(ReturnHome); // ReturnHome
290
+
291
+ delay(2);
292
+
293
+ writeCommand(DisplayON); // Send Display on command
294
+
295
+ delay(2);
296
+
297
+ writeCommand(ClearDisplay); // Clear Display
298
+
299
+ delay(20);
300
+
301
+ }
302
+
303
+ コード
304
+
305
+ ```