IUT CACHAN
/
DS18B20
Test DS18B20 sur D8
DS1Wire.cpp@1:7bf2fdef45c9, 2021-06-21 (annotated)
- Committer:
- iut_cachan01
- Date:
- Mon Jun 21 09:55:09 2021 +0000
- Revision:
- 1:7bf2fdef45c9
- Parent:
- 0:03ec282c2908
Test DS18B20 sur D8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feabhas | 0:03ec282c2908 | 1 | #include "DS1Wire.h" |
feabhas | 0:03ec282c2908 | 2 | #include "mbed.h" |
feabhas | 0:03ec282c2908 | 3 | #include <stdint.h> |
feabhas | 0:03ec282c2908 | 4 | |
feabhas | 0:03ec282c2908 | 5 | // Timing delay for 1-wire serial standard option |
feabhas | 0:03ec282c2908 | 6 | enum DELAY { A = 6, B = 64, C = 60, D = 10, E = 9, F = 55, G = 0, H = 480, I = 70, J = 410 }; |
feabhas | 0:03ec282c2908 | 7 | |
feabhas | 0:03ec282c2908 | 8 | |
feabhas | 0:03ec282c2908 | 9 | int Reset(DigitalInOut& pin) { |
feabhas | 0:03ec282c2908 | 10 | pin.output(); |
feabhas | 0:03ec282c2908 | 11 | pin = 0; // drive bus low |
feabhas | 0:03ec282c2908 | 12 | wait_us(H); |
feabhas | 0:03ec282c2908 | 13 | pin.input(); // release bus |
feabhas | 0:03ec282c2908 | 14 | wait_us(I); |
feabhas | 0:03ec282c2908 | 15 | uint32_t result = pin; // read bus value |
feabhas | 0:03ec282c2908 | 16 | wait_us(J); |
feabhas | 0:03ec282c2908 | 17 | return result; |
feabhas | 0:03ec282c2908 | 18 | } |
feabhas | 0:03ec282c2908 | 19 | |
feabhas | 0:03ec282c2908 | 20 | void WriteBit(DigitalInOut& pin, uint32_t bit) { |
feabhas | 0:03ec282c2908 | 21 | pin.output(); |
feabhas | 0:03ec282c2908 | 22 | if (bit) { |
feabhas | 0:03ec282c2908 | 23 | pin = 0; // drive bus low |
feabhas | 0:03ec282c2908 | 24 | wait_us(A); // delay A |
feabhas | 0:03ec282c2908 | 25 | pin.input(); // release bus |
feabhas | 0:03ec282c2908 | 26 | wait_us(B); // delay B |
feabhas | 0:03ec282c2908 | 27 | } else { |
feabhas | 0:03ec282c2908 | 28 | pin = 0; // drive bus low |
feabhas | 0:03ec282c2908 | 29 | wait_us(C); // delay C |
feabhas | 0:03ec282c2908 | 30 | pin.input(); // release bus |
feabhas | 0:03ec282c2908 | 31 | wait_us(D); // delay D |
feabhas | 0:03ec282c2908 | 32 | } |
feabhas | 0:03ec282c2908 | 33 | } |
feabhas | 0:03ec282c2908 | 34 | |
feabhas | 0:03ec282c2908 | 35 | uint32_t ReadBit(DigitalInOut& pin) { |
feabhas | 0:03ec282c2908 | 36 | uint32_t bit_value; |
feabhas | 0:03ec282c2908 | 37 | pin.output(); |
feabhas | 0:03ec282c2908 | 38 | pin = 0; // drive bus low |
feabhas | 0:03ec282c2908 | 39 | wait_us(A); // delay A |
feabhas | 0:03ec282c2908 | 40 | pin.input(); // release bus |
feabhas | 0:03ec282c2908 | 41 | wait_us(E); // delay E |
feabhas | 0:03ec282c2908 | 42 | bit_value = pin; // master sample bus |
feabhas | 0:03ec282c2908 | 43 | wait_us(F); |
feabhas | 0:03ec282c2908 | 44 | return bit_value; |
feabhas | 0:03ec282c2908 | 45 | } |
feabhas | 0:03ec282c2908 | 46 | |
feabhas | 0:03ec282c2908 | 47 | void WriteByte(DigitalInOut& pin, uint32_t byte) { |
feabhas | 0:03ec282c2908 | 48 | for (uint32_t bit = 0; bit < 8; ++bit) { |
feabhas | 0:03ec282c2908 | 49 | WriteBit(pin, byte & 0x01); // lsb to msb |
feabhas | 0:03ec282c2908 | 50 | byte >>= 1; // right shift by 1-bit |
feabhas | 0:03ec282c2908 | 51 | } |
feabhas | 0:03ec282c2908 | 52 | } |
feabhas | 0:03ec282c2908 | 53 | |
feabhas | 0:03ec282c2908 | 54 | uint32_t ReadByte(DigitalInOut& pin) { |
feabhas | 0:03ec282c2908 | 55 | uint32_t byte = 0; |
feabhas | 0:03ec282c2908 | 56 | for (uint32_t bit = 0; bit < 8; ++bit) { |
feabhas | 0:03ec282c2908 | 57 | byte |= (ReadBit(pin) << bit); // Reads lsb to msb |
feabhas | 0:03ec282c2908 | 58 | } |
feabhas | 0:03ec282c2908 | 59 | return byte; |
feabhas | 0:03ec282c2908 | 60 | } |