質問編集履歴
1
プログラムの内容を<code>を使っていれるようにしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,6 +14,8 @@
|
|
14
14
|
|
15
15
|
DS18B20a.pyがLCDに表示できません
|
16
16
|
|
17
|
+
|
18
|
+
|
17
19
|
2019.9.23
|
18
20
|
|
19
21
|
yama286です
|
@@ -30,7 +32,9 @@
|
|
30
32
|
|
31
33
|
以上はRaspberry Pi Beginner Kitで購入したものです
|
32
34
|
|
35
|
+
|
36
|
+
|
33
|
-
1. 不具合内容
|
37
|
+
**1. 不具合内容**
|
34
38
|
|
35
39
|
後述のサンプルプログラムDS18B20a.pyをターミナルで動作させると、ターミナルには測定結果が出ますが、標準開発環境Thonny Python IDEではエラーになります。
|
36
40
|
|
@@ -44,11 +48,15 @@
|
|
44
48
|
|
45
49
|
|
46
50
|
|
47
|
-
2. DS18B20単独動作確認
|
51
|
+
**2. DS18B20単独動作確認**
|
48
52
|
|
49
53
|
連続温度測定は下記リスト(webより入手)で確認済みです。
|
50
54
|
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
51
|
-
|
59
|
+
```ここに言語を入力
|
52
60
|
|
53
61
|
#/usr/bin/ruby
|
54
62
|
|
@@ -92,231 +100,243 @@
|
|
92
100
|
|
93
101
|
main()
|
94
102
|
|
103
|
+
|
104
|
+
|
105
|
+
```**実行結果**
|
106
|
+
|
107
|
+
時刻と温度がパソコン画面に表示されます。この結果は正常です。
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
**3.LCD:12 LCD Display単独動作確認**
|
116
|
+
|
117
|
+
動作は下記リスト(webより入手)で確認済みです。
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```ここに言語を入力
|
122
|
+
|
123
|
+
import smbus
|
124
|
+
|
125
|
+
import time
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
# Define some device parameters
|
130
|
+
|
131
|
+
I2C_ADDR = 0x27 # I2C device address, if any error, change this address to 0x27
|
132
|
+
|
133
|
+
LCD_WIDTH = 16 # Maximum characters per line
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
# Define some device constants
|
138
|
+
|
139
|
+
LCD_CHR = 1 # Mode - Sending data
|
140
|
+
|
141
|
+
LCD_CMD = 0 # Mode - Sending command
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
|
146
|
+
|
147
|
+
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
|
148
|
+
|
149
|
+
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
|
150
|
+
|
151
|
+
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
LCD_BACKLIGHT = 0x08 # On
|
156
|
+
|
157
|
+
#LCD_BACKLIGHT = 0x00 # Off
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
ENABLE = 0b00000100 # Enable bit
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
# Timing constants
|
166
|
+
|
167
|
+
E_PULSE = 0.0005
|
168
|
+
|
169
|
+
E_DELAY = 0.0005
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
#Open I2C interface
|
174
|
+
|
175
|
+
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
|
176
|
+
|
177
|
+
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def lcd_init():
|
182
|
+
|
183
|
+
# Initialise display
|
184
|
+
|
185
|
+
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
|
186
|
+
|
187
|
+
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
|
188
|
+
|
189
|
+
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
|
190
|
+
|
191
|
+
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
|
192
|
+
|
193
|
+
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
|
194
|
+
|
195
|
+
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
|
196
|
+
|
197
|
+
time.sleep(E_DELAY)
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
def lcd_byte(bits, mode):
|
202
|
+
|
203
|
+
# Send byte to data pins
|
204
|
+
|
205
|
+
# bits = the data
|
206
|
+
|
207
|
+
# mode = 1 for data
|
208
|
+
|
209
|
+
# 0 for command
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
|
214
|
+
|
215
|
+
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
# High bits
|
220
|
+
|
221
|
+
bus.write_byte(I2C_ADDR, bits_high)
|
222
|
+
|
223
|
+
lcd_toggle_enable(bits_high)
|
224
|
+
|
225
|
+
# Low bits
|
226
|
+
|
227
|
+
bus.write_byte(I2C_ADDR, bits_low)
|
228
|
+
|
229
|
+
lcd_toggle_enable(bits_low)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
def lcd_toggle_enable(bits):
|
234
|
+
|
235
|
+
# Toggle enable
|
236
|
+
|
237
|
+
time.sleep(E_DELAY)
|
238
|
+
|
239
|
+
bus.write_byte(I2C_ADDR, (bits | ENABLE))
|
240
|
+
|
241
|
+
time.sleep(E_PULSE)
|
242
|
+
|
243
|
+
bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
|
244
|
+
|
245
|
+
time.sleep(E_DELAY)
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
def lcd_string(message,line):
|
250
|
+
|
251
|
+
# Send string to display
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
message = message.ljust(LCD_WIDTH," ")
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
lcd_byte(line, LCD_CMD)
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
for i in range(LCD_WIDTH):
|
264
|
+
|
265
|
+
lcd_byte(ord(message[i]),LCD_CHR)
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
def main():
|
270
|
+
|
271
|
+
# Main program block
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
# Initialise display
|
276
|
+
|
277
|
+
lcd_init()
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
while True:
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
# Send some test
|
286
|
+
|
287
|
+
lcd_string("Created by <",LCD_LINE_1)
|
288
|
+
|
289
|
+
lcd_string("Osoyoo.com <",LCD_LINE_2)
|
290
|
+
|
291
|
+
time.sleep(3) # Send some more text
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
lcd_string("> Tutorial Url:",LCD_LINE_1)
|
296
|
+
|
297
|
+
lcd_string("> http://osoyoo.com",LCD_LINE_2)
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
time.sleep(3)
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
if __name__ == '__main__':
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
try:
|
310
|
+
|
311
|
+
main()
|
312
|
+
|
313
|
+
except KeyboardInterrupt:
|
314
|
+
|
315
|
+
pass
|
316
|
+
|
317
|
+
finally:
|
318
|
+
|
319
|
+
lcd_byte(0x01, LCD_CMD)
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
```
|
324
|
+
|
325
|
+
|
326
|
+
|
95
327
|
**実行結果**
|
96
328
|
|
97
|
-
時刻と温度がパソコン画面に表示されます。
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
3.LCD:12 LCD Display単独動作確認
|
106
|
-
|
107
|
-
動作は下記リスト(webより入手)で確認済みです。
|
108
|
-
|
109
|
-
********************************************************************************
|
110
|
-
|
111
|
-
import smbus
|
112
|
-
|
113
|
-
import time
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
# Define some device parameters
|
118
|
-
|
119
|
-
I2C_ADDR = 0x27 # I2C device address, if any error, change this address to 0x27
|
120
|
-
|
121
|
-
LCD_WIDTH = 16 # Maximum characters per line
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
# Define some device constants
|
126
|
-
|
127
|
-
LCD_CHR = 1 # Mode - Sending data
|
128
|
-
|
129
|
-
LCD_CMD = 0 # Mode - Sending command
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
|
134
|
-
|
135
|
-
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
|
136
|
-
|
137
|
-
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
|
138
|
-
|
139
|
-
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
LCD_BACKLIGHT = 0x08 # On
|
144
|
-
|
145
|
-
#LCD_BACKLIGHT = 0x00 # Off
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
ENABLE = 0b00000100 # Enable bit
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
# Timing constants
|
154
|
-
|
155
|
-
E_PULSE = 0.0005
|
156
|
-
|
157
|
-
E_DELAY = 0.0005
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
#Open I2C interface
|
162
|
-
|
163
|
-
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
|
164
|
-
|
165
|
-
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
def lcd_init():
|
170
|
-
|
171
|
-
# Initialise display
|
172
|
-
|
173
|
-
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
|
174
|
-
|
175
|
-
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
|
176
|
-
|
177
|
-
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
|
178
|
-
|
179
|
-
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
|
180
|
-
|
181
|
-
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
|
182
|
-
|
183
|
-
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
|
184
|
-
|
185
|
-
time.sleep(E_DELAY)
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
def lcd_byte(bits, mode):
|
190
|
-
|
191
|
-
# Send byte to data pins
|
192
|
-
|
193
|
-
# bits = the data
|
194
|
-
|
195
|
-
# mode = 1 for data
|
196
|
-
|
197
|
-
# 0 for command
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
|
202
|
-
|
203
|
-
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
# High bits
|
208
|
-
|
209
|
-
bus.write_byte(I2C_ADDR, bits_high)
|
210
|
-
|
211
|
-
lcd_toggle_enable(bits_high)
|
212
|
-
|
213
|
-
# Low bits
|
214
|
-
|
215
|
-
bus.write_byte(I2C_ADDR, bits_low)
|
216
|
-
|
217
|
-
lcd_toggle_enable(bits_low)
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
def lcd_toggle_enable(bits):
|
222
|
-
|
223
|
-
# Toggle enable
|
224
|
-
|
225
|
-
time.sleep(E_DELAY)
|
226
|
-
|
227
|
-
bus.write_byte(I2C_ADDR, (bits | ENABLE))
|
228
|
-
|
229
|
-
time.sleep(E_PULSE)
|
230
|
-
|
231
|
-
bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
|
232
|
-
|
233
|
-
time.sleep(E_DELAY)
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
def lcd_string(message,line):
|
238
|
-
|
239
|
-
# Send string to display
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
message = message.ljust(LCD_WIDTH," ")
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
lcd_byte(line, LCD_CMD)
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
for i in range(LCD_WIDTH):
|
252
|
-
|
253
|
-
lcd_byte(ord(message[i]),LCD_CHR)
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
def main():
|
258
|
-
|
259
|
-
# Main program block
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
# Initialise display
|
264
|
-
|
265
|
-
lcd_init()
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
while True:
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
# Send some test
|
274
|
-
|
275
|
-
lcd_string("Created by <",LCD_LINE_1)
|
276
|
-
|
277
|
-
lcd_string("Osoyoo.com <",LCD_LINE_2)
|
278
|
-
|
279
|
-
time.sleep(3) # Send some more text
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
lcd_string("> Tutorial Url:",LCD_LINE_1)
|
284
|
-
|
285
|
-
lcd_string("> http://osoyoo.com",LCD_LINE_2)
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
time.sleep(3)
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
if __name__ == '__main__':
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
try:
|
298
|
-
|
299
|
-
main()
|
300
|
-
|
301
|
-
except KeyboardInterrupt:
|
302
|
-
|
303
|
-
pass
|
304
|
-
|
305
|
-
finally:
|
306
|
-
|
307
|
-
lcd_byte(0x01, LCD_CMD)
|
308
|
-
|
309
|
-
**実行結果**
|
310
|
-
|
311
|
-
LCDに“Created by <” と "> Tutorial Url:"
|
329
|
+
LCDに“Created by <” と "> Tutorial Url:"
|
312
|
-
|
330
|
+
|
313
|
-
"Osoyoo.com <" "> http://osoyoo.com"
|
331
|
+
"Osoyoo.com <" "> http://osoyoo.com"
|
314
332
|
|
315
333
|
が交互に表示されます。
|
316
334
|
|
317
|
-
|
335
|
+
これも正常表示です。
|
318
|
-
|
336
|
+
|
337
|
+
|
338
|
+
|
319
|
-
4. DS18B20の測定データをLCD:12 LCD Displayに表示させたい
|
339
|
+
**4. DS18B20の測定データをLCD:12 LCD Displayに表示させたい**
|
320
340
|
|
321
341
|
1)参考資料 https://www.denshi.club/pc/raspi/5raspberry-pi-zeroiot161.html
|
322
342
|
|
@@ -334,7 +354,7 @@
|
|
334
354
|
|
335
355
|
正しく認識されています。(DS18B20は1-wireのためアドレスは出ません)
|
336
356
|
|
337
|
-
3)プログラム作成 1
|
357
|
+
3)プログラム作成 1)参考資料urlのプログラム参照ください
|
338
358
|
|
339
359
|
この資料の“プログラム”にある47ステップをコーディングしました。
|
340
360
|
|