|
7 | 7 | The following example assumes the ground of your DS18x20 is connected to |
8 | 8 | Y11, vcc is connected to Y9 and the data pin is connected to Y10. |
9 | 9 |
|
10 | | ->>> gnd = Pin('Y11') |
11 | | ->>> gnd.init(Pin.OUT_PP) |
| 10 | +>>> from pyb import Pin |
| 11 | +>>> gnd = Pin('Y11', Pin.OUT_PP) |
12 | 12 | >>> gnd.low() |
13 | | - |
14 | | ->>> vcc = Pin('Y9') |
15 | | ->>> vcc.init(Pin.OUT_PP) |
| 13 | +>>> vcc = Pin('Y9', Pin.OUT_PP) |
16 | 14 | >>> vcc.high() |
17 | 15 |
|
| 16 | +>>> from ds18x20 import DS18X20 |
18 | 17 | >>> d = DS18X20(Pin('Y10')) |
19 | 18 |
|
20 | 19 | Call read_temps to read all sensors: |
@@ -47,27 +46,22 @@ def __init__(self, pin): |
47 | 46 | # correct # first byte in their rom for a DS18x20 device. |
48 | 47 | self.roms = [rom for rom in self.ow.scan() if rom[0] == 0x10 or rom[0] == 0x28] |
49 | 48 |
|
50 | | - def _select_rom(self, rom): |
51 | | - if rom: |
52 | | - self.ow.select_rom(rom) |
53 | | - else: |
54 | | - self.ow.skip_rom() |
55 | | - |
56 | 49 | def read_temp(self, rom=None): |
57 | 50 | """ |
58 | 51 | Read and return the temperature of one DS18x20 device. |
59 | 52 | Pass the 8-byte bytes object with the ROM of the specific device you want to read. |
60 | 53 | If only one DS18x20 device is attached to the bus you may omit the rom parameter. |
61 | 54 | """ |
| 55 | + rom = rom or self.roms[0] |
62 | 56 | ow = self.ow |
63 | 57 | ow.reset() |
64 | | - self._select_rom(rom) |
| 58 | + ow.select_rom(rom) |
65 | 59 | ow.write_byte(0x44) # Convert Temp |
66 | 60 | while True: |
67 | 61 | if ow.read_bit(): |
68 | 62 | break |
69 | 63 | ow.reset() |
70 | | - self._select_rom(rom) |
| 64 | + ow.select_rom(rom) |
71 | 65 | ow.write_byte(0xbe) # Read scratch |
72 | 66 | data = ow.read_bytes(9) |
73 | 67 | return self.convert_temp(rom[0], data) |
|
0 commit comments