DS1Wire simple Library

Committer:
feabhas
Date:
Fri Dec 30 19:34:53 2011 +0000
Revision:
0:bfa1afd9ae98

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feabhas 0:bfa1afd9ae98 1 #include "DS1Wire.h"
feabhas 0:bfa1afd9ae98 2 #include "mbed.h"
feabhas 0:bfa1afd9ae98 3 #include <stdint.h>
feabhas 0:bfa1afd9ae98 4
feabhas 0:bfa1afd9ae98 5 // Timing delay for 1-wire serial standard option
feabhas 0:bfa1afd9ae98 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:bfa1afd9ae98 7
feabhas 0:bfa1afd9ae98 8
feabhas 0:bfa1afd9ae98 9 int Reset(DigitalInOut& pin) {
feabhas 0:bfa1afd9ae98 10 pin.output();
feabhas 0:bfa1afd9ae98 11 pin = 0; // drive bus low
feabhas 0:bfa1afd9ae98 12 wait_us(H);
feabhas 0:bfa1afd9ae98 13 pin.input(); // release bus
feabhas 0:bfa1afd9ae98 14 wait_us(I);
feabhas 0:bfa1afd9ae98 15 uint32_t result = pin; // read bus value
feabhas 0:bfa1afd9ae98 16 wait_us(J);
feabhas 0:bfa1afd9ae98 17 return result;
feabhas 0:bfa1afd9ae98 18 }
feabhas 0:bfa1afd9ae98 19
feabhas 0:bfa1afd9ae98 20 void WriteBit(DigitalInOut& pin, uint32_t bit) {
feabhas 0:bfa1afd9ae98 21 pin.output();
feabhas 0:bfa1afd9ae98 22 if (bit) {
feabhas 0:bfa1afd9ae98 23 pin = 0; // drive bus low
feabhas 0:bfa1afd9ae98 24 wait_us(A); // delay A
feabhas 0:bfa1afd9ae98 25 pin.input(); // release bus
feabhas 0:bfa1afd9ae98 26 wait_us(B); // delay B
feabhas 0:bfa1afd9ae98 27 } else {
feabhas 0:bfa1afd9ae98 28 pin = 0; // drive bus low
feabhas 0:bfa1afd9ae98 29 wait_us(C); // delay C
feabhas 0:bfa1afd9ae98 30 pin.input(); // release bus
feabhas 0:bfa1afd9ae98 31 wait_us(D); // delay D
feabhas 0:bfa1afd9ae98 32 }
feabhas 0:bfa1afd9ae98 33 }
feabhas 0:bfa1afd9ae98 34
feabhas 0:bfa1afd9ae98 35 uint32_t ReadBit(DigitalInOut& pin) {
feabhas 0:bfa1afd9ae98 36 uint32_t bit_value;
feabhas 0:bfa1afd9ae98 37 pin.output();
feabhas 0:bfa1afd9ae98 38 pin = 0; // drive bus low
feabhas 0:bfa1afd9ae98 39 wait_us(A); // delay A
feabhas 0:bfa1afd9ae98 40 pin.input(); // release bus
feabhas 0:bfa1afd9ae98 41 wait_us(E); // delay E
feabhas 0:bfa1afd9ae98 42 bit_value = pin; // master sample bus
feabhas 0:bfa1afd9ae98 43 wait_us(F);
feabhas 0:bfa1afd9ae98 44 return bit_value;
feabhas 0:bfa1afd9ae98 45 }
feabhas 0:bfa1afd9ae98 46
feabhas 0:bfa1afd9ae98 47 void WriteByte(DigitalInOut& pin, uint32_t byte) {
feabhas 0:bfa1afd9ae98 48 for (uint32_t bit = 0; bit < 8; ++bit) {
feabhas 0:bfa1afd9ae98 49 WriteBit(pin, byte & 0x01); // lsb to msb
feabhas 0:bfa1afd9ae98 50 byte >>= 1; // right shift by 1-bit
feabhas 0:bfa1afd9ae98 51 }
feabhas 0:bfa1afd9ae98 52 }
feabhas 0:bfa1afd9ae98 53
feabhas 0:bfa1afd9ae98 54 uint32_t ReadByte(DigitalInOut& pin) {
feabhas 0:bfa1afd9ae98 55 uint32_t byte = 0;
feabhas 0:bfa1afd9ae98 56 for (uint32_t bit = 0; bit < 8; ++bit) {
feabhas 0:bfa1afd9ae98 57 byte |= (ReadBit(pin) << bit); // Reads lsb to msb
feabhas 0:bfa1afd9ae98 58 }
feabhas 0:bfa1afd9ae98 59 return byte;
feabhas 0:bfa1afd9ae98 60 }