Libary for DS18B20 working in MBED-OS
ds18b20.cpp@0:1abc4873af36, 2020-11-04 (annotated)
- Committer:
- Scheinost_Alexander
- Date:
- Wed Nov 04 07:36:02 2020 +0000
- Revision:
- 0:1abc4873af36
Libary for DS18B20; working in MBED-OS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Scheinost_Alexander | 0:1abc4873af36 | 1 | #include "ds18b20.h" |
Scheinost_Alexander | 0:1abc4873af36 | 2 | |
Scheinost_Alexander | 0:1abc4873af36 | 3 | OWI::OWI(PinName pin):owi_io(pin) |
Scheinost_Alexander | 0:1abc4873af36 | 4 | { |
Scheinost_Alexander | 0:1abc4873af36 | 5 | |
Scheinost_Alexander | 0:1abc4873af36 | 6 | } |
Scheinost_Alexander | 0:1abc4873af36 | 7 | |
Scheinost_Alexander | 0:1abc4873af36 | 8 | void OWI::write0() |
Scheinost_Alexander | 0:1abc4873af36 | 9 | { |
Scheinost_Alexander | 0:1abc4873af36 | 10 | owi_io.output(); // timeslot = 70us |
Scheinost_Alexander | 0:1abc4873af36 | 11 | owi_io.write(0); // owi = 0; |
Scheinost_Alexander | 0:1abc4873af36 | 12 | wait_us(60); |
Scheinost_Alexander | 0:1abc4873af36 | 13 | owi_io.input(); |
Scheinost_Alexander | 0:1abc4873af36 | 14 | wait_us(10); |
Scheinost_Alexander | 0:1abc4873af36 | 15 | } |
Scheinost_Alexander | 0:1abc4873af36 | 16 | |
Scheinost_Alexander | 0:1abc4873af36 | 17 | void OWI::write1() // timeslot = 70us |
Scheinost_Alexander | 0:1abc4873af36 | 18 | { |
Scheinost_Alexander | 0:1abc4873af36 | 19 | owi_io.output(); |
Scheinost_Alexander | 0:1abc4873af36 | 20 | owi_io.write(0); // owi = 0; |
Scheinost_Alexander | 0:1abc4873af36 | 21 | wait_us(6); |
Scheinost_Alexander | 0:1abc4873af36 | 22 | owi_io.input(); |
Scheinost_Alexander | 0:1abc4873af36 | 23 | wait_us(64); |
Scheinost_Alexander | 0:1abc4873af36 | 24 | } |
Scheinost_Alexander | 0:1abc4873af36 | 25 | |
Scheinost_Alexander | 0:1abc4873af36 | 26 | unsigned char OWI::readBit() |
Scheinost_Alexander | 0:1abc4873af36 | 27 | { |
Scheinost_Alexander | 0:1abc4873af36 | 28 | unsigned char readbit; |
Scheinost_Alexander | 0:1abc4873af36 | 29 | |
Scheinost_Alexander | 0:1abc4873af36 | 30 | owi_io.output(); |
Scheinost_Alexander | 0:1abc4873af36 | 31 | owi_io.write(0); // owi = 0; |
Scheinost_Alexander | 0:1abc4873af36 | 32 | wait_us(6); |
Scheinost_Alexander | 0:1abc4873af36 | 33 | owi_io.input(); |
Scheinost_Alexander | 0:1abc4873af36 | 34 | wait_us(9); |
Scheinost_Alexander | 0:1abc4873af36 | 35 | readbit = owi_io.read(); |
Scheinost_Alexander | 0:1abc4873af36 | 36 | wait_us(55); |
Scheinost_Alexander | 0:1abc4873af36 | 37 | |
Scheinost_Alexander | 0:1abc4873af36 | 38 | return readbit; |
Scheinost_Alexander | 0:1abc4873af36 | 39 | } |
Scheinost_Alexander | 0:1abc4873af36 | 40 | |
Scheinost_Alexander | 0:1abc4873af36 | 41 | unsigned char OWI::detectPresence() |
Scheinost_Alexander | 0:1abc4873af36 | 42 | { |
Scheinost_Alexander | 0:1abc4873af36 | 43 | core_util_critical_section_enter(); |
Scheinost_Alexander | 0:1abc4873af36 | 44 | |
Scheinost_Alexander | 0:1abc4873af36 | 45 | unsigned char presencebit; |
Scheinost_Alexander | 0:1abc4873af36 | 46 | |
Scheinost_Alexander | 0:1abc4873af36 | 47 | owi_io.output(); |
Scheinost_Alexander | 0:1abc4873af36 | 48 | owi_io.write(0); // owi = 0; |
Scheinost_Alexander | 0:1abc4873af36 | 49 | wait_us(480); |
Scheinost_Alexander | 0:1abc4873af36 | 50 | owi_io.input(); |
Scheinost_Alexander | 0:1abc4873af36 | 51 | wait_us(70); |
Scheinost_Alexander | 0:1abc4873af36 | 52 | presencebit = !owi_io.read(); |
Scheinost_Alexander | 0:1abc4873af36 | 53 | wait_us(410); |
Scheinost_Alexander | 0:1abc4873af36 | 54 | |
Scheinost_Alexander | 0:1abc4873af36 | 55 | core_util_critical_section_exit(); |
Scheinost_Alexander | 0:1abc4873af36 | 56 | |
Scheinost_Alexander | 0:1abc4873af36 | 57 | return presencebit; |
Scheinost_Alexander | 0:1abc4873af36 | 58 | } |
Scheinost_Alexander | 0:1abc4873af36 | 59 | |
Scheinost_Alexander | 0:1abc4873af36 | 60 | void OWI::sendByte(unsigned char data) |
Scheinost_Alexander | 0:1abc4873af36 | 61 | { |
Scheinost_Alexander | 0:1abc4873af36 | 62 | core_util_critical_section_enter(); |
Scheinost_Alexander | 0:1abc4873af36 | 63 | |
Scheinost_Alexander | 0:1abc4873af36 | 64 | unsigned char temp; |
Scheinost_Alexander | 0:1abc4873af36 | 65 | unsigned char i; |
Scheinost_Alexander | 0:1abc4873af36 | 66 | for (i = 0; i < 8; i++) |
Scheinost_Alexander | 0:1abc4873af36 | 67 | { |
Scheinost_Alexander | 0:1abc4873af36 | 68 | temp = data & 0x01; |
Scheinost_Alexander | 0:1abc4873af36 | 69 | if (temp) |
Scheinost_Alexander | 0:1abc4873af36 | 70 | { |
Scheinost_Alexander | 0:1abc4873af36 | 71 | this->write1(); |
Scheinost_Alexander | 0:1abc4873af36 | 72 | } |
Scheinost_Alexander | 0:1abc4873af36 | 73 | else |
Scheinost_Alexander | 0:1abc4873af36 | 74 | { |
Scheinost_Alexander | 0:1abc4873af36 | 75 | this->write0(); |
Scheinost_Alexander | 0:1abc4873af36 | 76 | } |
Scheinost_Alexander | 0:1abc4873af36 | 77 | data >>= 1; |
Scheinost_Alexander | 0:1abc4873af36 | 78 | } |
Scheinost_Alexander | 0:1abc4873af36 | 79 | |
Scheinost_Alexander | 0:1abc4873af36 | 80 | core_util_critical_section_exit(); |
Scheinost_Alexander | 0:1abc4873af36 | 81 | } |
Scheinost_Alexander | 0:1abc4873af36 | 82 | |
Scheinost_Alexander | 0:1abc4873af36 | 83 | unsigned char OWI::receiveByte() |
Scheinost_Alexander | 0:1abc4873af36 | 84 | { |
Scheinost_Alexander | 0:1abc4873af36 | 85 | core_util_critical_section_enter(); |
Scheinost_Alexander | 0:1abc4873af36 | 86 | |
Scheinost_Alexander | 0:1abc4873af36 | 87 | unsigned char data; |
Scheinost_Alexander | 0:1abc4873af36 | 88 | unsigned char i; |
Scheinost_Alexander | 0:1abc4873af36 | 89 | data = 0x00; |
Scheinost_Alexander | 0:1abc4873af36 | 90 | for (i = 0; i < 8; i++) |
Scheinost_Alexander | 0:1abc4873af36 | 91 | { |
Scheinost_Alexander | 0:1abc4873af36 | 92 | data >>= 1; |
Scheinost_Alexander | 0:1abc4873af36 | 93 | if (this->readBit()) |
Scheinost_Alexander | 0:1abc4873af36 | 94 | { |
Scheinost_Alexander | 0:1abc4873af36 | 95 | data |= 0x80; |
Scheinost_Alexander | 0:1abc4873af36 | 96 | } |
Scheinost_Alexander | 0:1abc4873af36 | 97 | } |
Scheinost_Alexander | 0:1abc4873af36 | 98 | |
Scheinost_Alexander | 0:1abc4873af36 | 99 | core_util_critical_section_exit(); |
Scheinost_Alexander | 0:1abc4873af36 | 100 | return data; |
Scheinost_Alexander | 0:1abc4873af36 | 101 | } |
Scheinost_Alexander | 0:1abc4873af36 | 102 | |
Scheinost_Alexander | 0:1abc4873af36 | 103 | |
Scheinost_Alexander | 0:1abc4873af36 | 104 | DS18B20::DS18B20(PinName pin):DS18B20_OWI(pin) |
Scheinost_Alexander | 0:1abc4873af36 | 105 | { |
Scheinost_Alexander | 0:1abc4873af36 | 106 | |
Scheinost_Alexander | 0:1abc4873af36 | 107 | } |
Scheinost_Alexander | 0:1abc4873af36 | 108 | |
Scheinost_Alexander | 0:1abc4873af36 | 109 | float DS18B20::readTemp() |
Scheinost_Alexander | 0:1abc4873af36 | 110 | { |
Scheinost_Alexander | 0:1abc4873af36 | 111 | unsigned char msb,lsb; |
Scheinost_Alexander | 0:1abc4873af36 | 112 | int itemp; |
Scheinost_Alexander | 0:1abc4873af36 | 113 | float ftemp; |
Scheinost_Alexander | 0:1abc4873af36 | 114 | |
Scheinost_Alexander | 0:1abc4873af36 | 115 | DS18B20_OWI.detectPresence(); |
Scheinost_Alexander | 0:1abc4873af36 | 116 | DS18B20_OWI.sendByte(OWI_SKIP_ROM); |
Scheinost_Alexander | 0:1abc4873af36 | 117 | DS18B20_OWI.sendByte(DS18S20_START); |
Scheinost_Alexander | 0:1abc4873af36 | 118 | |
Scheinost_Alexander | 0:1abc4873af36 | 119 | ThisThread::sleep_for(750); |
Scheinost_Alexander | 0:1abc4873af36 | 120 | |
Scheinost_Alexander | 0:1abc4873af36 | 121 | DS18B20_OWI.detectPresence(); |
Scheinost_Alexander | 0:1abc4873af36 | 122 | DS18B20_OWI.sendByte(OWI_SKIP_ROM); |
Scheinost_Alexander | 0:1abc4873af36 | 123 | DS18B20_OWI.sendByte(DS18S20_READ_SCRATCH_PAD); |
Scheinost_Alexander | 0:1abc4873af36 | 124 | lsb = DS18B20_OWI.receiveByte(); |
Scheinost_Alexander | 0:1abc4873af36 | 125 | msb = DS18B20_OWI.receiveByte(); |
Scheinost_Alexander | 0:1abc4873af36 | 126 | |
Scheinost_Alexander | 0:1abc4873af36 | 127 | itemp = (msb<<8)+lsb; |
Scheinost_Alexander | 0:1abc4873af36 | 128 | ftemp = (float)itemp / 16.0f; |
Scheinost_Alexander | 0:1abc4873af36 | 129 | |
Scheinost_Alexander | 0:1abc4873af36 | 130 | return ftemp; |
Scheinost_Alexander | 0:1abc4873af36 | 131 | } |