Diltech RM Consultant
/
Double_I2C_Sample
Sample code with BluePill STM32F103C8 and Oled SSD1306 and LCD1602 on same connect on DS18B20.
ds18b20-single/DS18B20.cpp@1:27d0c6ee0e55, 2022-05-29 (annotated)
- Committer:
- diltech
- Date:
- Sun May 29 18:40:06 2022 -0400
- Revision:
- 1:27d0c6ee0e55
- Parent:
- 0:47b4bbc994df
Etape1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
diltech | 0:47b4bbc994df | 1 | /* MIT License |
diltech | 0:47b4bbc994df | 2 | * |
diltech | 0:47b4bbc994df | 3 | * Copyright (c) 2020 Lukas Gessner |
diltech | 0:47b4bbc994df | 4 | * |
diltech | 0:47b4bbc994df | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
diltech | 0:47b4bbc994df | 6 | * of this software and associated documentation files (the "Software"), to deal |
diltech | 0:47b4bbc994df | 7 | * in the Software without restriction, including without limitation the rights |
diltech | 0:47b4bbc994df | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
diltech | 0:47b4bbc994df | 9 | * copies of the Software, and to permit persons to whom the Software is |
diltech | 0:47b4bbc994df | 10 | * furnished to do so, subject to the following conditions: |
diltech | 0:47b4bbc994df | 11 | * |
diltech | 0:47b4bbc994df | 12 | * The above copyright notice and this permission notice shall be included in all |
diltech | 0:47b4bbc994df | 13 | * copies or substantial portions of the Software. |
diltech | 0:47b4bbc994df | 14 | * |
diltech | 0:47b4bbc994df | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
diltech | 0:47b4bbc994df | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
diltech | 0:47b4bbc994df | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
diltech | 0:47b4bbc994df | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
diltech | 0:47b4bbc994df | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
diltech | 0:47b4bbc994df | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
diltech | 0:47b4bbc994df | 21 | * SOFTWARE. |
diltech | 0:47b4bbc994df | 22 | */ |
diltech | 0:47b4bbc994df | 23 | |
diltech | 0:47b4bbc994df | 24 | #include "DS18B20.h" |
diltech | 0:47b4bbc994df | 25 | |
diltech | 0:47b4bbc994df | 26 | OWI::OWI(PinName pin):owi_io(pin) |
diltech | 0:47b4bbc994df | 27 | { |
diltech | 0:47b4bbc994df | 28 | |
diltech | 0:47b4bbc994df | 29 | } |
diltech | 0:47b4bbc994df | 30 | |
diltech | 0:47b4bbc994df | 31 | void OWI::write0() |
diltech | 0:47b4bbc994df | 32 | { |
diltech | 0:47b4bbc994df | 33 | owi_io.output(); // timeslot = 70us |
diltech | 0:47b4bbc994df | 34 | owi_io.write(0); // owi = 0; |
diltech | 0:47b4bbc994df | 35 | wait_us(60); |
diltech | 0:47b4bbc994df | 36 | owi_io.input(); |
diltech | 0:47b4bbc994df | 37 | wait_us(10); |
diltech | 0:47b4bbc994df | 38 | } |
diltech | 0:47b4bbc994df | 39 | |
diltech | 0:47b4bbc994df | 40 | void OWI::write1() // timeslot = 70us |
diltech | 0:47b4bbc994df | 41 | { |
diltech | 0:47b4bbc994df | 42 | owi_io.output(); |
diltech | 0:47b4bbc994df | 43 | owi_io.write(0); // owi = 0; |
diltech | 0:47b4bbc994df | 44 | wait_us(6); |
diltech | 0:47b4bbc994df | 45 | owi_io.input(); |
diltech | 0:47b4bbc994df | 46 | wait_us(64); |
diltech | 0:47b4bbc994df | 47 | } |
diltech | 0:47b4bbc994df | 48 | |
diltech | 0:47b4bbc994df | 49 | unsigned char OWI::readBit() |
diltech | 0:47b4bbc994df | 50 | { |
diltech | 0:47b4bbc994df | 51 | unsigned char readbit; |
diltech | 0:47b4bbc994df | 52 | |
diltech | 0:47b4bbc994df | 53 | owi_io.output(); |
diltech | 0:47b4bbc994df | 54 | owi_io.write(0); // owi = 0; |
diltech | 0:47b4bbc994df | 55 | wait_us(6); |
diltech | 0:47b4bbc994df | 56 | owi_io.input(); |
diltech | 0:47b4bbc994df | 57 | wait_us(9); |
diltech | 0:47b4bbc994df | 58 | readbit = owi_io.read(); |
diltech | 0:47b4bbc994df | 59 | wait_us(55); |
diltech | 0:47b4bbc994df | 60 | |
diltech | 0:47b4bbc994df | 61 | return readbit; |
diltech | 0:47b4bbc994df | 62 | } |
diltech | 0:47b4bbc994df | 63 | |
diltech | 0:47b4bbc994df | 64 | unsigned char OWI::detectPresence() |
diltech | 0:47b4bbc994df | 65 | { |
diltech | 0:47b4bbc994df | 66 | core_util_critical_section_enter(); |
diltech | 0:47b4bbc994df | 67 | |
diltech | 0:47b4bbc994df | 68 | unsigned char presencebit; |
diltech | 0:47b4bbc994df | 69 | |
diltech | 0:47b4bbc994df | 70 | owi_io.output(); |
diltech | 0:47b4bbc994df | 71 | owi_io.write(0); // owi = 0; |
diltech | 0:47b4bbc994df | 72 | wait_us(480); |
diltech | 0:47b4bbc994df | 73 | owi_io.input(); |
diltech | 0:47b4bbc994df | 74 | wait_us(70); |
diltech | 0:47b4bbc994df | 75 | presencebit = !owi_io.read(); |
diltech | 0:47b4bbc994df | 76 | wait_us(410); |
diltech | 0:47b4bbc994df | 77 | |
diltech | 0:47b4bbc994df | 78 | core_util_critical_section_exit(); |
diltech | 0:47b4bbc994df | 79 | |
diltech | 0:47b4bbc994df | 80 | return presencebit; |
diltech | 0:47b4bbc994df | 81 | } |
diltech | 0:47b4bbc994df | 82 | |
diltech | 0:47b4bbc994df | 83 | void OWI::sendByte(unsigned char data) |
diltech | 0:47b4bbc994df | 84 | { |
diltech | 0:47b4bbc994df | 85 | core_util_critical_section_enter(); |
diltech | 0:47b4bbc994df | 86 | |
diltech | 0:47b4bbc994df | 87 | unsigned char temp; |
diltech | 0:47b4bbc994df | 88 | unsigned char i; |
diltech | 0:47b4bbc994df | 89 | for (i = 0; i < 8; i++) |
diltech | 0:47b4bbc994df | 90 | { |
diltech | 0:47b4bbc994df | 91 | temp = data & 0x01; |
diltech | 0:47b4bbc994df | 92 | if (temp) |
diltech | 0:47b4bbc994df | 93 | { |
diltech | 0:47b4bbc994df | 94 | this->write1(); |
diltech | 0:47b4bbc994df | 95 | } |
diltech | 0:47b4bbc994df | 96 | else |
diltech | 0:47b4bbc994df | 97 | { |
diltech | 0:47b4bbc994df | 98 | this->write0(); |
diltech | 0:47b4bbc994df | 99 | } |
diltech | 0:47b4bbc994df | 100 | data >>= 1; |
diltech | 0:47b4bbc994df | 101 | } |
diltech | 0:47b4bbc994df | 102 | |
diltech | 0:47b4bbc994df | 103 | core_util_critical_section_exit(); |
diltech | 0:47b4bbc994df | 104 | } |
diltech | 0:47b4bbc994df | 105 | |
diltech | 0:47b4bbc994df | 106 | unsigned char OWI::receiveByte() |
diltech | 0:47b4bbc994df | 107 | { |
diltech | 0:47b4bbc994df | 108 | core_util_critical_section_enter(); |
diltech | 0:47b4bbc994df | 109 | |
diltech | 0:47b4bbc994df | 110 | unsigned char data; |
diltech | 0:47b4bbc994df | 111 | unsigned char i; |
diltech | 0:47b4bbc994df | 112 | data = 0x00; |
diltech | 0:47b4bbc994df | 113 | for (i = 0; i < 8; i++) |
diltech | 0:47b4bbc994df | 114 | { |
diltech | 0:47b4bbc994df | 115 | data >>= 1; |
diltech | 0:47b4bbc994df | 116 | if (this->readBit()) |
diltech | 0:47b4bbc994df | 117 | { |
diltech | 0:47b4bbc994df | 118 | data |= 0x80; |
diltech | 0:47b4bbc994df | 119 | } |
diltech | 0:47b4bbc994df | 120 | } |
diltech | 0:47b4bbc994df | 121 | |
diltech | 0:47b4bbc994df | 122 | core_util_critical_section_exit(); |
diltech | 0:47b4bbc994df | 123 | return data; |
diltech | 0:47b4bbc994df | 124 | } |
diltech | 0:47b4bbc994df | 125 | |
diltech | 0:47b4bbc994df | 126 | |
diltech | 0:47b4bbc994df | 127 | DS18B20::DS18B20(PinName pin):DS18B20_OWI(pin) |
diltech | 0:47b4bbc994df | 128 | { |
diltech | 0:47b4bbc994df | 129 | |
diltech | 0:47b4bbc994df | 130 | } |
diltech | 0:47b4bbc994df | 131 | |
diltech | 0:47b4bbc994df | 132 | float DS18B20::readTemp() |
diltech | 0:47b4bbc994df | 133 | { |
diltech | 0:47b4bbc994df | 134 | unsigned char msb,lsb; |
diltech | 0:47b4bbc994df | 135 | int itemp; |
diltech | 0:47b4bbc994df | 136 | float ftemp; |
diltech | 0:47b4bbc994df | 137 | |
diltech | 0:47b4bbc994df | 138 | DS18B20_OWI.detectPresence(); |
diltech | 0:47b4bbc994df | 139 | DS18B20_OWI.sendByte(OWI_SKIP_ROM); |
diltech | 0:47b4bbc994df | 140 | DS18B20_OWI.sendByte(DS18B20_START); |
diltech | 0:47b4bbc994df | 141 | |
diltech | 0:47b4bbc994df | 142 | #if MBED_MAJOR_VERSION >= 6 |
diltech | 0:47b4bbc994df | 143 | ThisThread::sleep_for(750ms); |
diltech | 0:47b4bbc994df | 144 | #elif MBED_MAJOR_VERSION < 5 |
diltech | 0:47b4bbc994df | 145 | wait(0.75); |
diltech | 0:47b4bbc994df | 146 | #else |
diltech | 0:47b4bbc994df | 147 | ThisThread::sleep_for(750); |
diltech | 0:47b4bbc994df | 148 | #endif |
diltech | 0:47b4bbc994df | 149 | |
diltech | 0:47b4bbc994df | 150 | DS18B20_OWI.detectPresence(); |
diltech | 0:47b4bbc994df | 151 | DS18B20_OWI.sendByte(OWI_SKIP_ROM); |
diltech | 0:47b4bbc994df | 152 | DS18B20_OWI.sendByte(DS18B20_READ_SCRATCH_PAD); |
diltech | 0:47b4bbc994df | 153 | lsb = DS18B20_OWI.receiveByte(); |
diltech | 0:47b4bbc994df | 154 | msb = DS18B20_OWI.receiveByte(); |
diltech | 0:47b4bbc994df | 155 | |
diltech | 0:47b4bbc994df | 156 | itemp = (msb<<8)+lsb; |
diltech | 0:47b4bbc994df | 157 | ftemp = (float)itemp / 16.0f; |
diltech | 0:47b4bbc994df | 158 | |
diltech | 0:47b4bbc994df | 159 | return ftemp; |
diltech | 0:47b4bbc994df | 160 | } |
diltech | 0:47b4bbc994df | 161 | |
diltech | 0:47b4bbc994df | 162 | |
diltech | 0:47b4bbc994df | 163 |