Rainer Raul
/
OneWireDrv
Test 1-wire , working wtih parasite power and few sensors with mixed power supply.
onewire.cpp@1:f8aa0ff8d04a, 2022-05-18 (annotated)
- Committer:
- rainerraul
- Date:
- Wed May 18 10:09:56 2022 +0000
- Revision:
- 1:f8aa0ff8d04a
- Parent:
- 0:1197076b78f4
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
macraj | 0:1197076b78f4 | 1 | #include "mbed.h" |
macraj | 0:1197076b78f4 | 2 | #include "onewire.h" |
macraj | 0:1197076b78f4 | 3 | #include "crc8.h" |
macraj | 0:1197076b78f4 | 4 | |
macraj | 0:1197076b78f4 | 5 | |
macraj | 0:1197076b78f4 | 6 | |
macraj | 0:1197076b78f4 | 7 | // DS18B20 converted to run on mbed |
macraj | 0:1197076b78f4 | 8 | |
macraj | 0:1197076b78f4 | 9 | |
macraj | 0:1197076b78f4 | 10 | |
macraj | 0:1197076b78f4 | 11 | |
macraj | 0:1197076b78f4 | 12 | |
rainerraul | 1:f8aa0ff8d04a | 13 | DigitalInOut ow_pin(PB_2); |
macraj | 0:1197076b78f4 | 14 | |
macraj | 0:1197076b78f4 | 15 | |
macraj | 0:1197076b78f4 | 16 | |
macraj | 0:1197076b78f4 | 17 | BYTE ow_reset(void) { // reset. Should improve to act as a presence pulse |
macraj | 0:1197076b78f4 | 18 | BYTE err; |
macraj | 0:1197076b78f4 | 19 | |
macraj | 0:1197076b78f4 | 20 | ow_pin.output(); |
macraj | 0:1197076b78f4 | 21 | ow_pin = 0; // bring low for 500 us |
macraj | 0:1197076b78f4 | 22 | wait_us(500); |
macraj | 0:1197076b78f4 | 23 | ow_pin.input(); |
macraj | 0:1197076b78f4 | 24 | wait_us(60); |
macraj | 0:1197076b78f4 | 25 | err = ow_pin; |
macraj | 0:1197076b78f4 | 26 | wait_us(240); |
macraj | 0:1197076b78f4 | 27 | if ( ow_pin == 0 ) { // short circuit |
macraj | 0:1197076b78f4 | 28 | err = OW_SHORT_CIRCUIT; |
macraj | 0:1197076b78f4 | 29 | #ifdef DEBUG |
macraj | 0:1197076b78f4 | 30 | printf("Error. Short circut!!!\n"); |
macraj | 0:1197076b78f4 | 31 | #endif |
macraj | 0:1197076b78f4 | 32 | } |
macraj | 0:1197076b78f4 | 33 | return err; |
macraj | 0:1197076b78f4 | 34 | } |
macraj | 0:1197076b78f4 | 35 | |
macraj | 0:1197076b78f4 | 36 | BYTE ow_bit_io( BYTE b ) { |
macraj | 0:1197076b78f4 | 37 | |
macraj | 0:1197076b78f4 | 38 | ow_pin.output(); // drive bus low |
macraj | 0:1197076b78f4 | 39 | ow_pin = 0; |
macraj | 0:1197076b78f4 | 40 | wait_us(1); // Recovery-Time wuffwuff was 1 |
macraj | 0:1197076b78f4 | 41 | //ow_pin.input(); |
macraj | 0:1197076b78f4 | 42 | if ( b ) ow_pin.input(); // if bit is 1 set bus high (by ext. pull-up) |
macraj | 0:1197076b78f4 | 43 | |
macraj | 0:1197076b78f4 | 44 | // delay was 15uS-1 see comment above |
macraj | 0:1197076b78f4 | 45 | wait_us(15-1); |
macraj | 0:1197076b78f4 | 46 | // ???ow_pin.input(); |
macraj | 0:1197076b78f4 | 47 | if ( ow_pin == 0 ) b = 0; // sample at end of read-timeslot |
macraj | 0:1197076b78f4 | 48 | |
macraj | 0:1197076b78f4 | 49 | wait_us(60-15); |
macraj | 0:1197076b78f4 | 50 | ow_pin.input(); |
macraj | 0:1197076b78f4 | 51 | |
macraj | 0:1197076b78f4 | 52 | return b; |
macraj | 0:1197076b78f4 | 53 | } |
macraj | 0:1197076b78f4 | 54 | |
macraj | 0:1197076b78f4 | 55 | BYTE ow_byte_wr( uint8_t b ) { |
macraj | 0:1197076b78f4 | 56 | uint8_t i = 8, j; |
macraj | 0:1197076b78f4 | 57 | |
macraj | 0:1197076b78f4 | 58 | do { |
macraj | 0:1197076b78f4 | 59 | j = ow_bit_io( b & 1 ); |
macraj | 0:1197076b78f4 | 60 | b >>= 1; |
macraj | 0:1197076b78f4 | 61 | if ( j ) b |= 0x80; |
macraj | 0:1197076b78f4 | 62 | } while ( --i ); |
macraj | 0:1197076b78f4 | 63 | |
macraj | 0:1197076b78f4 | 64 | return b; |
macraj | 0:1197076b78f4 | 65 | } |
macraj | 0:1197076b78f4 | 66 | |
macraj | 0:1197076b78f4 | 67 | |
macraj | 0:1197076b78f4 | 68 | uint8_t ow_byte_rd( void ) { |
macraj | 0:1197076b78f4 | 69 | // read by sending 0xff (a dontcare?) |
macraj | 0:1197076b78f4 | 70 | return ow_byte_wr( 0xFF ); |
macraj | 0:1197076b78f4 | 71 | } |
macraj | 0:1197076b78f4 | 72 | |
macraj | 0:1197076b78f4 | 73 | |
macraj | 0:1197076b78f4 | 74 | |
macraj | 0:1197076b78f4 | 75 | BYTE ow_rom_search( BYTE diff, BYTE *id ) { |
macraj | 0:1197076b78f4 | 76 | BYTE i, j, next_diff; |
macraj | 0:1197076b78f4 | 77 | BYTE b; |
macraj | 0:1197076b78f4 | 78 | |
macraj | 0:1197076b78f4 | 79 | if ( ow_reset() ) return OW_PRESENCE_ERR; // error, no device found |
macraj | 0:1197076b78f4 | 80 | |
macraj | 0:1197076b78f4 | 81 | ow_byte_wr( OW_SEARCH_ROM ); // ROM search command |
macraj | 0:1197076b78f4 | 82 | next_diff = OW_LAST_DEVICE; // unchanged on last device |
macraj | 0:1197076b78f4 | 83 | |
macraj | 0:1197076b78f4 | 84 | i = OW_ROMCODE_SIZE * 8; // 8 bytes |
macraj | 0:1197076b78f4 | 85 | |
macraj | 0:1197076b78f4 | 86 | do { |
macraj | 0:1197076b78f4 | 87 | j = 8; // 8 bits |
macraj | 0:1197076b78f4 | 88 | do { |
macraj | 0:1197076b78f4 | 89 | b = ow_bit_io( 1 ); // read bit |
macraj | 0:1197076b78f4 | 90 | if ( ow_bit_io( 1 ) ) { // read complement bit |
macraj | 0:1197076b78f4 | 91 | if ( b ) // 11 |
macraj | 0:1197076b78f4 | 92 | return OW_DATA_ERR; // data error |
macraj | 0:1197076b78f4 | 93 | } else { |
macraj | 0:1197076b78f4 | 94 | if ( !b ) { // 00 = 2 devices |
macraj | 0:1197076b78f4 | 95 | if ( diff > i || ((*id & 1) && diff != i) ) { |
macraj | 0:1197076b78f4 | 96 | b = 1; // now 1 |
macraj | 0:1197076b78f4 | 97 | next_diff = i; // next pass 0 |
macraj | 0:1197076b78f4 | 98 | } |
macraj | 0:1197076b78f4 | 99 | } |
macraj | 0:1197076b78f4 | 100 | } |
macraj | 0:1197076b78f4 | 101 | ow_bit_io( b ); // write bit |
macraj | 0:1197076b78f4 | 102 | *id >>= 1; |
macraj | 0:1197076b78f4 | 103 | if ( b ) *id |= 0x80; // store bit |
macraj | 0:1197076b78f4 | 104 | |
macraj | 0:1197076b78f4 | 105 | i--; |
macraj | 0:1197076b78f4 | 106 | |
macraj | 0:1197076b78f4 | 107 | } while ( --j ); |
macraj | 0:1197076b78f4 | 108 | |
macraj | 0:1197076b78f4 | 109 | id++; // next byte |
macraj | 0:1197076b78f4 | 110 | |
macraj | 0:1197076b78f4 | 111 | } while ( i ); |
macraj | 0:1197076b78f4 | 112 | |
macraj | 0:1197076b78f4 | 113 | return next_diff; // to continue search |
macraj | 0:1197076b78f4 | 114 | } |
macraj | 0:1197076b78f4 | 115 | |
macraj | 0:1197076b78f4 | 116 | void ow_command( BYTE command, BYTE *id ) { |
macraj | 0:1197076b78f4 | 117 | BYTE i; |
macraj | 0:1197076b78f4 | 118 | |
macraj | 0:1197076b78f4 | 119 | ow_reset(); |
macraj | 0:1197076b78f4 | 120 | |
macraj | 0:1197076b78f4 | 121 | if ( id ) { |
macraj | 0:1197076b78f4 | 122 | ow_byte_wr( OW_MATCH_ROM ); // to a single device |
macraj | 0:1197076b78f4 | 123 | i = OW_ROMCODE_SIZE; |
macraj | 0:1197076b78f4 | 124 | do { |
macraj | 0:1197076b78f4 | 125 | ow_byte_wr( *id ); |
macraj | 0:1197076b78f4 | 126 | id++; |
macraj | 0:1197076b78f4 | 127 | } while ( --i ); |
macraj | 0:1197076b78f4 | 128 | } else { |
macraj | 0:1197076b78f4 | 129 | ow_byte_wr( OW_SKIP_ROM ); // to all devices |
macraj | 0:1197076b78f4 | 130 | } |
macraj | 0:1197076b78f4 | 131 | |
macraj | 0:1197076b78f4 | 132 | ow_byte_wr( command ); |
macraj | 0:1197076b78f4 | 133 | } |
macraj | 0:1197076b78f4 | 134 | |
macraj | 0:1197076b78f4 | 135 | void ow_parasite_enable(void) { |
macraj | 0:1197076b78f4 | 136 | ow_pin.output(); |
macraj | 0:1197076b78f4 | 137 | ow_pin = 1; |
macraj | 0:1197076b78f4 | 138 | } |
macraj | 0:1197076b78f4 | 139 | |
macraj | 0:1197076b78f4 | 140 | void ow_parasite_disable(void) { |
macraj | 0:1197076b78f4 | 141 | |
macraj | 0:1197076b78f4 | 142 | ow_pin.input(); |
macraj | 0:1197076b78f4 | 143 | } |
macraj | 0:1197076b78f4 | 144 | |
macraj | 0:1197076b78f4 | 145 | |
macraj | 0:1197076b78f4 | 146 | /* find DS18X20 Sensors on 1-Wire-Bus |
macraj | 0:1197076b78f4 | 147 | input/ouput: diff is the result of the last rom-search |
macraj | 0:1197076b78f4 | 148 | output: id is the rom-code of the sensor found */ |
macraj | 0:1197076b78f4 | 149 | void DS18X20_find_sensor(BYTE *diff, BYTE id[]) { |
macraj | 0:1197076b78f4 | 150 | for (;;) { |
macraj | 0:1197076b78f4 | 151 | *diff = ow_rom_search( *diff, &id[0] ); |
macraj | 0:1197076b78f4 | 152 | if ( *diff==OW_PRESENCE_ERR || *diff==OW_DATA_ERR || |
macraj | 0:1197076b78f4 | 153 | *diff == OW_LAST_DEVICE ) return; |
macraj | 0:1197076b78f4 | 154 | if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) return; |
macraj | 0:1197076b78f4 | 155 | } |
macraj | 0:1197076b78f4 | 156 | } |
macraj | 0:1197076b78f4 | 157 | |
macraj | 0:1197076b78f4 | 158 | /* get power status of DS18x20 |
macraj | 0:1197076b78f4 | 159 | input : id = rom_code |
macraj | 0:1197076b78f4 | 160 | returns: DS18X20_POWER_EXTERN or DS18X20_POWER_PARASITE |
macraj | 0:1197076b78f4 | 161 | */ |
macraj | 0:1197076b78f4 | 162 | BYTE DS18X20_get_power_status(uint8_t id[]) { |
macraj | 0:1197076b78f4 | 163 | uint8_t pstat; |
macraj | 0:1197076b78f4 | 164 | ow_reset(); |
macraj | 0:1197076b78f4 | 165 | ow_command(DS18X20_READ_POWER_SUPPLY, id); |
macraj | 0:1197076b78f4 | 166 | pstat=ow_bit_io(1); // pstat 0=is parasite/ !=0 ext. powered |
macraj | 0:1197076b78f4 | 167 | ow_reset(); |
macraj | 0:1197076b78f4 | 168 | return (pstat) ? DS18X20_POWER_EXTERN:DS18X20_POWER_PARASITE; |
macraj | 0:1197076b78f4 | 169 | } |
macraj | 0:1197076b78f4 | 170 | |
macraj | 0:1197076b78f4 | 171 | void DS18X20_show_id_uart( BYTE *id, size_t n ) { |
macraj | 0:1197076b78f4 | 172 | size_t i; |
macraj | 0:1197076b78f4 | 173 | for ( i = 0; i < n; i++ ) { |
macraj | 0:1197076b78f4 | 174 | if ( i == 0 ) printf( "FC: " ); |
macraj | 0:1197076b78f4 | 175 | else if ( i == n-1 ) printf( "CRC: " ); |
macraj | 0:1197076b78f4 | 176 | if ( i == 1 ) printf( " SN: " ); |
macraj | 0:1197076b78f4 | 177 | printf("%X ",id[i]); |
macraj | 0:1197076b78f4 | 178 | if ( i == 0 ) { |
macraj | 0:1197076b78f4 | 179 | if ( id[0] == DS18S20_ID ) printf("(18S)"); |
macraj | 0:1197076b78f4 | 180 | else if ( id[0] == DS18B20_ID ) printf("(18B)"); |
macraj | 0:1197076b78f4 | 181 | else printf("( ? )"); |
macraj | 0:1197076b78f4 | 182 | } |
macraj | 0:1197076b78f4 | 183 | } |
macraj | 0:1197076b78f4 | 184 | if ( crc8( id, OW_ROMCODE_SIZE) ) |
macraj | 0:1197076b78f4 | 185 | printf( " CRC FAIL\n " ); |
macraj | 0:1197076b78f4 | 186 | else |
macraj | 0:1197076b78f4 | 187 | printf( " CRC O.K.\n" ); |
macraj | 0:1197076b78f4 | 188 | } |
macraj | 0:1197076b78f4 | 189 | |
macraj | 0:1197076b78f4 | 190 | /* start measurement (CONVERT_T) for all sensors if input id==NULL |
macraj | 0:1197076b78f4 | 191 | or for single sensor. then id is the rom-code */ |
macraj | 0:1197076b78f4 | 192 | uint8_t DS18X20_start_meas( uint8_t with_power_extern, uint8_t id[]) { |
macraj | 0:1197076b78f4 | 193 | ow_reset(); //** |
macraj | 0:1197076b78f4 | 194 | if ( ow_pin ) { // only send if bus is "idle" = high |
macraj | 0:1197076b78f4 | 195 | ow_command( DS18X20_CONVERT_T, id ); |
macraj | 0:1197076b78f4 | 196 | if (with_power_extern != DS18X20_POWER_EXTERN) |
macraj | 0:1197076b78f4 | 197 | ow_parasite_enable(); |
macraj | 0:1197076b78f4 | 198 | return DS18X20_OK; |
macraj | 0:1197076b78f4 | 199 | } else { |
macraj | 0:1197076b78f4 | 200 | #ifdef DEBUG |
macraj | 0:1197076b78f4 | 201 | printf( "DS18X20_start_meas: Short Circuit !\n" ); |
macraj | 0:1197076b78f4 | 202 | #endif |
macraj | 0:1197076b78f4 | 203 | return DS18X20_START_FAIL; |
macraj | 0:1197076b78f4 | 204 | } |
macraj | 0:1197076b78f4 | 205 | } |
macraj | 0:1197076b78f4 | 206 | |
macraj | 0:1197076b78f4 | 207 | /* reads temperature (scratchpad) of sensor with rom-code id |
macraj | 0:1197076b78f4 | 208 | output: subzero==1 if temp.<0, cel: full celsius, mcel: frac |
macraj | 0:1197076b78f4 | 209 | in millicelsius*0.1 |
rainerraul | 1:f8aa0ff8d04a | 210 | i.e.: subzero=1, cel=18, millicel=5000 = -18,5000�C */ |
macraj | 0:1197076b78f4 | 211 | uint8_t DS18X20_read_meas(uint8_t id[], uint8_t *subzero, |
macraj | 0:1197076b78f4 | 212 | uint8_t *cel, uint8_t *cel_frac_bits) { |
macraj | 0:1197076b78f4 | 213 | uint8_t i; |
macraj | 0:1197076b78f4 | 214 | uint8_t sp[DS18X20_SP_SIZE]; |
macraj | 0:1197076b78f4 | 215 | |
macraj | 0:1197076b78f4 | 216 | ow_reset(); //** |
macraj | 0:1197076b78f4 | 217 | ow_command(DS18X20_READ, id); |
macraj | 0:1197076b78f4 | 218 | for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) sp[i]=ow_byte_rd(); |
macraj | 0:1197076b78f4 | 219 | if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) |
macraj | 0:1197076b78f4 | 220 | return DS18X20_ERROR_CRC; |
macraj | 0:1197076b78f4 | 221 | DS18X20_meas_to_cel(id[0], sp, subzero, cel, cel_frac_bits); |
macraj | 0:1197076b78f4 | 222 | return DS18X20_OK; |
macraj | 0:1197076b78f4 | 223 | } |
macraj | 0:1197076b78f4 | 224 | |
macraj | 0:1197076b78f4 | 225 | /* |
macraj | 0:1197076b78f4 | 226 | convert raw value from DS18x20 to Celsius |
macraj | 0:1197076b78f4 | 227 | input is: |
macraj | 0:1197076b78f4 | 228 | - familycode fc (0x10/0x28 see header) |
macraj | 0:1197076b78f4 | 229 | - scratchpad-buffer |
macraj | 0:1197076b78f4 | 230 | output is: |
macraj | 0:1197076b78f4 | 231 | - cel full celsius |
macraj | 0:1197076b78f4 | 232 | - fractions of celsius in millicelsius*(10^-1)/625 (the 4 LS-Bits) |
macraj | 0:1197076b78f4 | 233 | - subzero =0 positiv / 1 negativ |
macraj | 0:1197076b78f4 | 234 | always returns DS18X20_OK |
macraj | 0:1197076b78f4 | 235 | TODO invalid-values detection (but should be covered by CRC) |
macraj | 0:1197076b78f4 | 236 | */ |
macraj | 0:1197076b78f4 | 237 | uint8_t DS18X20_meas_to_cel( uint8_t fc, uint8_t *sp, |
macraj | 0:1197076b78f4 | 238 | uint8_t* subzero, uint8_t* cel, uint8_t* cel_frac_bits) { |
macraj | 0:1197076b78f4 | 239 | uint16_t meas; |
macraj | 0:1197076b78f4 | 240 | uint8_t i; |
macraj | 0:1197076b78f4 | 241 | |
macraj | 0:1197076b78f4 | 242 | meas = sp[0]; // LSB |
macraj | 0:1197076b78f4 | 243 | meas |= ((uint16_t)sp[1])<<8; // MSB |
macraj | 0:1197076b78f4 | 244 | //meas = 0xff5e; meas = 0xfe6f; |
macraj | 0:1197076b78f4 | 245 | |
macraj | 0:1197076b78f4 | 246 | // only work on 12bit-base |
macraj | 0:1197076b78f4 | 247 | if ( fc == DS18S20_ID ) { // 9 -> 12 bit if 18S20 |
macraj | 0:1197076b78f4 | 248 | /* Extended measurements for DS18S20 contributed by Carsten Foss */ |
macraj | 0:1197076b78f4 | 249 | meas &= (uint16_t) 0xfffe; // Discard LSB , needed for later extended precicion calc |
macraj | 0:1197076b78f4 | 250 | meas <<= 3; // Convert to 12-bit , now degrees are in 1/16 degrees units |
macraj | 0:1197076b78f4 | 251 | meas += (16 - sp[6]) - 4; // Add the compensation , and remember to subtract 0.25 degree (4/16) |
macraj | 0:1197076b78f4 | 252 | } |
macraj | 0:1197076b78f4 | 253 | |
macraj | 0:1197076b78f4 | 254 | // check for negative |
macraj | 0:1197076b78f4 | 255 | if ( meas & 0x8000 ) { |
macraj | 0:1197076b78f4 | 256 | *subzero=1; // mark negative |
macraj | 0:1197076b78f4 | 257 | meas ^= 0xffff; // convert to positive => (twos complement)++ |
macraj | 0:1197076b78f4 | 258 | meas++; |
macraj | 0:1197076b78f4 | 259 | } else *subzero=0; |
macraj | 0:1197076b78f4 | 260 | |
macraj | 0:1197076b78f4 | 261 | // clear undefined bits for B != 12bit |
macraj | 0:1197076b78f4 | 262 | if ( fc == DS18B20_ID ) { // check resolution 18B20 |
macraj | 0:1197076b78f4 | 263 | i = sp[DS18B20_CONF_REG]; |
macraj | 0:1197076b78f4 | 264 | if ( (i & DS18B20_12_BIT) == DS18B20_12_BIT ) ; |
macraj | 0:1197076b78f4 | 265 | else if ( (i & DS18B20_11_BIT) == DS18B20_11_BIT ) |
macraj | 0:1197076b78f4 | 266 | meas &= ~(DS18B20_11_BIT_UNDF); |
macraj | 0:1197076b78f4 | 267 | else if ( (i & DS18B20_10_BIT) == DS18B20_10_BIT ) |
macraj | 0:1197076b78f4 | 268 | meas &= ~(DS18B20_10_BIT_UNDF); |
macraj | 0:1197076b78f4 | 269 | else { // if ( (i & DS18B20_9_BIT) == DS18B20_9_BIT ) { |
macraj | 0:1197076b78f4 | 270 | meas &= ~(DS18B20_9_BIT_UNDF); |
macraj | 0:1197076b78f4 | 271 | } |
macraj | 0:1197076b78f4 | 272 | } |
macraj | 0:1197076b78f4 | 273 | |
macraj | 0:1197076b78f4 | 274 | *cel = (uint8_t)(meas >> 4); |
macraj | 0:1197076b78f4 | 275 | *cel_frac_bits = (uint8_t)(meas & 0x000F); |
macraj | 0:1197076b78f4 | 276 | |
macraj | 0:1197076b78f4 | 277 | return DS18X20_OK; |
macraj | 0:1197076b78f4 | 278 | } |
macraj | 0:1197076b78f4 | 279 | |
macraj | 0:1197076b78f4 | 280 | /* converts to decicelsius |
macraj | 0:1197076b78f4 | 281 | input is ouput from meas_to_cel |
macraj | 0:1197076b78f4 | 282 | returns absolute value of temperatur in decicelsius |
rainerraul | 1:f8aa0ff8d04a | 283 | i.e.: sz=0, c=28, frac=15 returns 289 (=28.9�C) |
macraj | 0:1197076b78f4 | 284 | 0 0 0 |
macraj | 0:1197076b78f4 | 285 | 1 625 625 1 |
macraj | 0:1197076b78f4 | 286 | 2 1250 250 |
macraj | 0:1197076b78f4 | 287 | 3 1875 875 3 |
macraj | 0:1197076b78f4 | 288 | 4 2500 500 4 |
macraj | 0:1197076b78f4 | 289 | 5 3125 125 |
macraj | 0:1197076b78f4 | 290 | 6 3750 750 6 |
macraj | 0:1197076b78f4 | 291 | 7 4375 375 |
macraj | 0:1197076b78f4 | 292 | 8 5000 0 |
macraj | 0:1197076b78f4 | 293 | 9 5625 625 9 |
macraj | 0:1197076b78f4 | 294 | 10 6250 250 |
macraj | 0:1197076b78f4 | 295 | 11 6875 875 11 |
macraj | 0:1197076b78f4 | 296 | 12 7500 500 12 |
macraj | 0:1197076b78f4 | 297 | 13 8125 125 |
macraj | 0:1197076b78f4 | 298 | 14 8750 750 14 |
macraj | 0:1197076b78f4 | 299 | 15 9375 375 */ |
macraj | 0:1197076b78f4 | 300 | uint16_t DS18X20_temp_to_decicel(uint8_t subzero, uint8_t cel, |
macraj | 0:1197076b78f4 | 301 | uint8_t cel_frac_bits) { |
macraj | 0:1197076b78f4 | 302 | uint16_t h; |
macraj | 0:1197076b78f4 | 303 | uint8_t i; |
macraj | 0:1197076b78f4 | 304 | uint8_t need_rounding[] = { 1, 3, 4, 6, 9, 11, 12, 14 }; |
macraj | 0:1197076b78f4 | 305 | |
macraj | 0:1197076b78f4 | 306 | h = cel_frac_bits*DS18X20_FRACCONV/1000; |
macraj | 0:1197076b78f4 | 307 | h += cel*10; |
macraj | 0:1197076b78f4 | 308 | if (!subzero) { |
macraj | 0:1197076b78f4 | 309 | for (i=0; i<sizeof(need_rounding); i++) { |
macraj | 0:1197076b78f4 | 310 | if ( cel_frac_bits == need_rounding[i] ) { |
macraj | 0:1197076b78f4 | 311 | h++; |
macraj | 0:1197076b78f4 | 312 | break; |
macraj | 0:1197076b78f4 | 313 | } |
macraj | 0:1197076b78f4 | 314 | } |
macraj | 0:1197076b78f4 | 315 | } |
macraj | 0:1197076b78f4 | 316 | return h; |
macraj | 0:1197076b78f4 | 317 | } |
macraj | 0:1197076b78f4 | 318 | |
macraj | 0:1197076b78f4 | 319 | /* compare temperature values (full celsius only) |
macraj | 0:1197076b78f4 | 320 | returns -1 if param-pair1 < param-pair2 |
macraj | 0:1197076b78f4 | 321 | 0 if == |
macraj | 0:1197076b78f4 | 322 | 1 if > */ |
macraj | 0:1197076b78f4 | 323 | int8_t DS18X20_temp_cmp(uint8_t subzero1, uint16_t cel1, |
macraj | 0:1197076b78f4 | 324 | uint8_t subzero2, uint16_t cel2) { |
macraj | 0:1197076b78f4 | 325 | int16_t t1 = (subzero1) ? (cel1*(-1)) : (cel1); |
macraj | 0:1197076b78f4 | 326 | int16_t t2 = (subzero2) ? (cel2*(-1)) : (cel2); |
macraj | 0:1197076b78f4 | 327 | |
macraj | 0:1197076b78f4 | 328 | if (t1<t2) return -1; |
macraj | 0:1197076b78f4 | 329 | if (t1>t2) return 1; |
macraj | 0:1197076b78f4 | 330 | return 0; |
macraj | 0:1197076b78f4 | 331 | } |
macraj | 0:1197076b78f4 | 332 | |
macraj | 0:1197076b78f4 | 333 | void OneWireOutByte(unsigned char d) { // output byte d (least sig bit first). |
macraj | 0:1197076b78f4 | 334 | for (int n=8; n!=0; n--) { |
macraj | 0:1197076b78f4 | 335 | if ((d & 0x01) == 1) { // test least sig bit |
macraj | 0:1197076b78f4 | 336 | ow_pin.output(); |
macraj | 0:1197076b78f4 | 337 | ow_pin = 0; |
macraj | 0:1197076b78f4 | 338 | wait_us(5); |
macraj | 0:1197076b78f4 | 339 | ow_pin.input(); |
macraj | 0:1197076b78f4 | 340 | wait_us(80); |
macraj | 0:1197076b78f4 | 341 | } else { |
macraj | 0:1197076b78f4 | 342 | ow_pin.output(); |
macraj | 0:1197076b78f4 | 343 | ow_pin = 0; |
macraj | 0:1197076b78f4 | 344 | wait_us(80); |
macraj | 0:1197076b78f4 | 345 | ow_pin.input(); |
macraj | 0:1197076b78f4 | 346 | } |
macraj | 0:1197076b78f4 | 347 | |
macraj | 0:1197076b78f4 | 348 | d=d>>1; // now the next bit is in the least sig bit position. |
macraj | 0:1197076b78f4 | 349 | } |
macraj | 0:1197076b78f4 | 350 | |
macraj | 0:1197076b78f4 | 351 | } |
macraj | 0:1197076b78f4 | 352 | |
macraj | 0:1197076b78f4 | 353 | unsigned char OneWireInByte() { // read byte, least sig byte first |
macraj | 0:1197076b78f4 | 354 | unsigned char d = 0, b; |
macraj | 0:1197076b78f4 | 355 | for (int n=0; n<8; n++) { |
macraj | 0:1197076b78f4 | 356 | ow_pin.output(); |
macraj | 0:1197076b78f4 | 357 | ow_pin = 0; |
macraj | 0:1197076b78f4 | 358 | wait_us(5); |
macraj | 0:1197076b78f4 | 359 | ow_pin.input(); |
macraj | 0:1197076b78f4 | 360 | wait_us(5); |
macraj | 0:1197076b78f4 | 361 | b =ow_pin; |
macraj | 0:1197076b78f4 | 362 | wait_us(50); |
macraj | 0:1197076b78f4 | 363 | d = (d >> 1) | (b << 7); // shift d to right and insert b in most sig bit position |
macraj | 0:1197076b78f4 | 364 | } |
macraj | 0:1197076b78f4 | 365 | return d; |
macraj | 0:1197076b78f4 | 366 | } |
macraj | 0:1197076b78f4 | 367 |