zzz
iButton.cpp@14:2d0b5e0f0aed, 2020-08-26 (annotated)
- Committer:
- mauuuuul
- Date:
- Wed Aug 26 19:44:50 2020 +0000
- Revision:
- 14:2d0b5e0f0aed
- Parent:
- OneWire.cpp@12:27a1b359b95c
zzzz
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mauuuuul | 14:2d0b5e0f0aed | 1 | #include "iButton.h" |
hudakz | 0:acf75feb0947 | 2 | |
mauuuuul | 14:2d0b5e0f0aed | 3 | extern Serial dbg; |
hudakz | 0:acf75feb0947 | 4 | |
mauuuuul | 14:2d0b5e0f0aed | 5 | iButton::iButton(PinName IO) : io(IO) |
mauuuuul | 14:2d0b5e0f0aed | 6 | { |
mauuuuul | 14:2d0b5e0f0aed | 7 | } |
hudakz | 0:acf75feb0947 | 8 | |
mauuuuul | 14:2d0b5e0f0aed | 9 | iButton::~iButton() |
hudakz | 7:acf3f0ee66d2 | 10 | { |
hudakz | 0:acf75feb0947 | 11 | } |
hudakz | 0:acf75feb0947 | 12 | |
mauuuuul | 14:2d0b5e0f0aed | 13 | void iButton::OneWireReset(void) |
hudakz | 0:acf75feb0947 | 14 | { |
mauuuuul | 14:2d0b5e0f0aed | 15 | io.output(); |
mauuuuul | 14:2d0b5e0f0aed | 16 | io = 0; |
mauuuuul | 14:2d0b5e0f0aed | 17 | wait_us(500); |
mauuuuul | 14:2d0b5e0f0aed | 18 | io.input(); |
mauuuuul | 14:2d0b5e0f0aed | 19 | wait_us(500); |
hudakz | 0:acf75feb0947 | 20 | } |
hudakz | 0:acf75feb0947 | 21 | |
mauuuuul | 14:2d0b5e0f0aed | 22 | void iButton::OneWireOutByte(unsigned char data) |
hudakz | 0:acf75feb0947 | 23 | { |
mauuuuul | 14:2d0b5e0f0aed | 24 | for (int n = 8;n!=0; n--) |
mauuuuul | 14:2d0b5e0f0aed | 25 | { |
mauuuuul | 14:2d0b5e0f0aed | 26 | if ((data & 0x01) == 1) |
mauuuuul | 14:2d0b5e0f0aed | 27 | { |
mauuuuul | 14:2d0b5e0f0aed | 28 | io.output(); |
mauuuuul | 14:2d0b5e0f0aed | 29 | io = 0; |
mauuuuul | 14:2d0b5e0f0aed | 30 | wait_us(5); |
mauuuuul | 14:2d0b5e0f0aed | 31 | io.input(); |
mauuuuul | 14:2d0b5e0f0aed | 32 | wait_us(60); |
mauuuuul | 14:2d0b5e0f0aed | 33 | } |
mauuuuul | 14:2d0b5e0f0aed | 34 | else |
mauuuuul | 14:2d0b5e0f0aed | 35 | { |
mauuuuul | 14:2d0b5e0f0aed | 36 | io.output(); |
mauuuuul | 14:2d0b5e0f0aed | 37 | wait_us(60); |
mauuuuul | 14:2d0b5e0f0aed | 38 | io.input(); |
mauuuuul | 14:2d0b5e0f0aed | 39 | } |
mauuuuul | 14:2d0b5e0f0aed | 40 | data = data >> 1; |
hudakz | 0:acf75feb0947 | 41 | } |
hudakz | 0:acf75feb0947 | 42 | } |
hudakz | 0:acf75feb0947 | 43 | |
mauuuuul | 14:2d0b5e0f0aed | 44 | unsigned char iButton::OneWireReadByte(void) |
hudakz | 0:acf75feb0947 | 45 | { |
mauuuuul | 14:2d0b5e0f0aed | 46 | unsigned char d = 0; |
mauuuuul | 14:2d0b5e0f0aed | 47 | unsigned char b; |
mauuuuul | 14:2d0b5e0f0aed | 48 | for (int n = 0; n<8; n++) |
mauuuuul | 14:2d0b5e0f0aed | 49 | { |
mauuuuul | 14:2d0b5e0f0aed | 50 | io.output(); |
mauuuuul | 14:2d0b5e0f0aed | 51 | io = 0; |
mauuuuul | 14:2d0b5e0f0aed | 52 | wait_us(5); |
mauuuuul | 14:2d0b5e0f0aed | 53 | io.input(); |
mauuuuul | 14:2d0b5e0f0aed | 54 | wait_us(5); |
mauuuuul | 14:2d0b5e0f0aed | 55 | b = io; |
mauuuuul | 14:2d0b5e0f0aed | 56 | wait_us(50); |
mauuuuul | 14:2d0b5e0f0aed | 57 | d = (d >> 1) | (b << 7); |
mauuuuul | 14:2d0b5e0f0aed | 58 | } |
mauuuuul | 14:2d0b5e0f0aed | 59 | return d; |
hudakz | 0:acf75feb0947 | 60 | } |
hudakz | 0:acf75feb0947 | 61 | |
mauuuuul | 14:2d0b5e0f0aed | 62 | void iButton::ResetData() |
hudakz | 0:acf75feb0947 | 63 | { |
mauuuuul | 14:2d0b5e0f0aed | 64 | detect.family = 0; |
mauuuuul | 14:2d0b5e0f0aed | 65 | detect.serial.clear(); |
mauuuuul | 14:2d0b5e0f0aed | 66 | detect.crc = 0; |
mauuuuul | 14:2d0b5e0f0aed | 67 | detect.valid = false; |
hudakz | 0:acf75feb0947 | 68 | } |
hudakz | 0:acf75feb0947 | 69 | |
mauuuuul | 14:2d0b5e0f0aed | 70 | void iButton::DetectiButton(void) |
hudakz | 0:acf75feb0947 | 71 | { |
mauuuuul | 14:2d0b5e0f0aed | 72 | unsigned char crc = 0; |
mauuuuul | 14:2d0b5e0f0aed | 73 | |
mauuuuul | 14:2d0b5e0f0aed | 74 | OneWireReset(); |
mauuuuul | 14:2d0b5e0f0aed | 75 | OneWireOutByte(0x33); |
mauuuuul | 14:2d0b5e0f0aed | 76 | |
mauuuuul | 14:2d0b5e0f0aed | 77 | ResetData(); |
mauuuuul | 14:2d0b5e0f0aed | 78 | |
mauuuuul | 14:2d0b5e0f0aed | 79 | detect.family = OneWireReadByte(); |
mauuuuul | 14:2d0b5e0f0aed | 80 | crc = crc8(crc, detect.family); |
mauuuuul | 14:2d0b5e0f0aed | 81 | if (detect.family == 0x00 || detect.family == 0xFF) |
mauuuuul | 14:2d0b5e0f0aed | 82 | { |
mauuuuul | 14:2d0b5e0f0aed | 83 | detect.valid = false; |
mauuuuul | 14:2d0b5e0f0aed | 84 | return; |
mauuuuul | 14:2d0b5e0f0aed | 85 | } |
mauuuuul | 14:2d0b5e0f0aed | 86 | |
mauuuuul | 14:2d0b5e0f0aed | 87 | for (int i = 0; i <6; i++) |
mauuuuul | 14:2d0b5e0f0aed | 88 | { |
mauuuuul | 14:2d0b5e0f0aed | 89 | // detect.serial[i] = OneWireReadByte(); |
mauuuuul | 14:2d0b5e0f0aed | 90 | detect.serial.push_back(OneWireReadByte()); |
mauuuuul | 14:2d0b5e0f0aed | 91 | crc = crc8(crc, detect.serial[i]); |
mauuuuul | 14:2d0b5e0f0aed | 92 | } |
mauuuuul | 14:2d0b5e0f0aed | 93 | detect.crc = OneWireReadByte(); |
mauuuuul | 14:2d0b5e0f0aed | 94 | if (crc == detect.crc) |
mauuuuul | 14:2d0b5e0f0aed | 95 | { |
mauuuuul | 14:2d0b5e0f0aed | 96 | detect.valid = true; |
mauuuuul | 14:2d0b5e0f0aed | 97 | } |
mauuuuul | 14:2d0b5e0f0aed | 98 | //dbg.printf("[Detect Ibutton]\r\n"); |
mauuuuul | 14:2d0b5e0f0aed | 99 | return; |
hudakz | 0:acf75feb0947 | 100 | } |
hudakz | 0:acf75feb0947 | 101 | |
mauuuuul | 14:2d0b5e0f0aed | 102 | std::string conv(const unsigned char &val) |
hudakz | 0:acf75feb0947 | 103 | { |
mauuuuul | 14:2d0b5e0f0aed | 104 | char st[5]; |
mauuuuul | 14:2d0b5e0f0aed | 105 | sprintf(st,"%02X",val); |
mauuuuul | 14:2d0b5e0f0aed | 106 | return st; |
mauuuuul | 14:2d0b5e0f0aed | 107 | } |
hudakz | 0:acf75feb0947 | 108 | |
mauuuuul | 14:2d0b5e0f0aed | 109 | bool iButton::IsTaping(void) |
mauuuuul | 14:2d0b5e0f0aed | 110 | { |
mauuuuul | 14:2d0b5e0f0aed | 111 | return detect.valid; |
mauuuuul | 14:2d0b5e0f0aed | 112 | } |
hudakz | 0:acf75feb0947 | 113 | |
mauuuuul | 14:2d0b5e0f0aed | 114 | std::string iButton::GetData(void) |
mauuuuul | 14:2d0b5e0f0aed | 115 | { |
mauuuuul | 14:2d0b5e0f0aed | 116 | size_t sz = detect.serial.size(); |
mauuuuul | 14:2d0b5e0f0aed | 117 | string ret = ""; |
mauuuuul | 14:2d0b5e0f0aed | 118 | |
mauuuuul | 14:2d0b5e0f0aed | 119 | for(int i=sz-1; i>=0; i--) |
mauuuuul | 14:2d0b5e0f0aed | 120 | { |
mauuuuul | 14:2d0b5e0f0aed | 121 | ret += conv(detect.serial[i]); |
mauuuuul | 14:2d0b5e0f0aed | 122 | } |
mauuuuul | 14:2d0b5e0f0aed | 123 | return ret; |
mauuuuul | 14:2d0b5e0f0aed | 124 | } |
hudakz | 0:acf75feb0947 | 125 | |
mauuuuul | 14:2d0b5e0f0aed | 126 | unsigned char iButton::crc8(unsigned char crc, unsigned char data) |
mauuuuul | 14:2d0b5e0f0aed | 127 | { |
mauuuuul | 14:2d0b5e0f0aed | 128 | crc = crc ^ data; |
mauuuuul | 14:2d0b5e0f0aed | 129 | for (int i = 0; i < 8; i++) |
mauuuuul | 14:2d0b5e0f0aed | 130 | { |
mauuuuul | 14:2d0b5e0f0aed | 131 | if (crc & 0x01) |
mauuuuul | 14:2d0b5e0f0aed | 132 | { |
mauuuuul | 14:2d0b5e0f0aed | 133 | crc = (crc >> 1) ^ 0x8C; |
mauuuuul | 14:2d0b5e0f0aed | 134 | } |
mauuuuul | 14:2d0b5e0f0aed | 135 | else |
mauuuuul | 14:2d0b5e0f0aed | 136 | { |
mauuuuul | 14:2d0b5e0f0aed | 137 | crc >>= 1; |
hudakz | 8:87fbdaba5535 | 138 | } |
hudakz | 8:87fbdaba5535 | 139 | } |
hudakz | 0:acf75feb0947 | 140 | return crc; |
hudakz | 0:acf75feb0947 | 141 | } |