ibutton read with modified ds18b20 libraries
Due to the lack of programs for ibutton interfacing with mbed, i built this one (still needs a lot of development), it's in a very basic state, but i'm working on it...
DS1Wire.cpp@0:4f399e4b64a9, 2013-08-23 (annotated)
- Committer:
- Renato
- Date:
- Fri Aug 23 08:36:59 2013 +0000
- Revision:
- 0:4f399e4b64a9
Function to read ibutton serial & family
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Renato | 0:4f399e4b64a9 | 1 | #include "DS1Wire.h" |
Renato | 0:4f399e4b64a9 | 2 | #include "mbed.h" |
Renato | 0:4f399e4b64a9 | 3 | #include <stdint.h> |
Renato | 0:4f399e4b64a9 | 4 | |
Renato | 0:4f399e4b64a9 | 5 | // Timing delay for 1-wire serial standard option |
Renato | 0:4f399e4b64a9 | 6 | enum DELAY { A = 6, B = 64, C = 60, D = 10, E = 9, F = 55, G = 0, H = 480, I = 70, J = 410 }; |
Renato | 0:4f399e4b64a9 | 7 | |
Renato | 0:4f399e4b64a9 | 8 | |
Renato | 0:4f399e4b64a9 | 9 | int Reset(DigitalInOut& pin) { |
Renato | 0:4f399e4b64a9 | 10 | pin.output(); |
Renato | 0:4f399e4b64a9 | 11 | pin = 0; // drive bus low |
Renato | 0:4f399e4b64a9 | 12 | wait_us(H); |
Renato | 0:4f399e4b64a9 | 13 | pin.input(); // release bus |
Renato | 0:4f399e4b64a9 | 14 | wait_us(I); |
Renato | 0:4f399e4b64a9 | 15 | uint32_t result = pin; // read bus value |
Renato | 0:4f399e4b64a9 | 16 | wait_us(J); |
Renato | 0:4f399e4b64a9 | 17 | return result; |
Renato | 0:4f399e4b64a9 | 18 | } |
Renato | 0:4f399e4b64a9 | 19 | |
Renato | 0:4f399e4b64a9 | 20 | void WriteBit(DigitalInOut& pin, uint32_t bit) { |
Renato | 0:4f399e4b64a9 | 21 | pin.output(); |
Renato | 0:4f399e4b64a9 | 22 | if (bit) { |
Renato | 0:4f399e4b64a9 | 23 | pin = 0; // drive bus low |
Renato | 0:4f399e4b64a9 | 24 | wait_us(A); // delay A |
Renato | 0:4f399e4b64a9 | 25 | pin.input(); // release bus |
Renato | 0:4f399e4b64a9 | 26 | wait_us(B); // delay B |
Renato | 0:4f399e4b64a9 | 27 | } else { |
Renato | 0:4f399e4b64a9 | 28 | pin = 0; // drive bus low |
Renato | 0:4f399e4b64a9 | 29 | wait_us(C); // delay C |
Renato | 0:4f399e4b64a9 | 30 | pin.input(); // release bus |
Renato | 0:4f399e4b64a9 | 31 | wait_us(D); // delay D |
Renato | 0:4f399e4b64a9 | 32 | } |
Renato | 0:4f399e4b64a9 | 33 | } |
Renato | 0:4f399e4b64a9 | 34 | |
Renato | 0:4f399e4b64a9 | 35 | uint32_t ReadBit(DigitalInOut& pin) { |
Renato | 0:4f399e4b64a9 | 36 | uint32_t bit_value; |
Renato | 0:4f399e4b64a9 | 37 | pin.output(); |
Renato | 0:4f399e4b64a9 | 38 | pin = 0; // drive bus low |
Renato | 0:4f399e4b64a9 | 39 | wait_us(A); // delay A |
Renato | 0:4f399e4b64a9 | 40 | pin.input(); // release bus |
Renato | 0:4f399e4b64a9 | 41 | wait_us(E); // delay E |
Renato | 0:4f399e4b64a9 | 42 | bit_value = pin; // master sample bus |
Renato | 0:4f399e4b64a9 | 43 | wait_us(F); |
Renato | 0:4f399e4b64a9 | 44 | return bit_value; |
Renato | 0:4f399e4b64a9 | 45 | } |
Renato | 0:4f399e4b64a9 | 46 | |
Renato | 0:4f399e4b64a9 | 47 | void WriteByte(DigitalInOut& pin, uint32_t byte) { |
Renato | 0:4f399e4b64a9 | 48 | for (uint32_t bit = 0; bit < 8; ++bit) { |
Renato | 0:4f399e4b64a9 | 49 | WriteBit(pin, byte & 0x01); // lsb to msb |
Renato | 0:4f399e4b64a9 | 50 | byte >>= 1; // right shift by 1-bit |
Renato | 0:4f399e4b64a9 | 51 | } |
Renato | 0:4f399e4b64a9 | 52 | } |
Renato | 0:4f399e4b64a9 | 53 | |
Renato | 0:4f399e4b64a9 | 54 | uint32_t ReadByte(DigitalInOut& pin) { |
Renato | 0:4f399e4b64a9 | 55 | uint32_t byte = 0; |
Renato | 0:4f399e4b64a9 | 56 | for (uint32_t bit = 0; bit < 8; ++bit) { |
Renato | 0:4f399e4b64a9 | 57 | byte |= (ReadBit(pin) << bit); // Reads lsb to msb |
Renato | 0:4f399e4b64a9 | 58 | } |
Renato | 0:4f399e4b64a9 | 59 | return byte; |
Renato | 0:4f399e4b64a9 | 60 | } |