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