void setup_DS1820(void) { onewire_start(); if ( !onewire_search(DS1820_addr)) { onewire_reset_search(); delayMs(250); onewire_search(DS1820_addr); } if ( onewire_crc8( DS1820_addr, 7) != DS1820_addr[7]) { respondstr = "No OneWire devices found"; return; } if ( DS1820_addr[0] == 0x10) { is_DS18B20 = FALSE; } else if ( DS1820_addr[0] == 0x28) { is_DS18B20 = TRUE; } else { respondstr = "No DS1820 found"; return; } start_DS1820(); ds1820_time = getMs(); }
// uint8_t onewire_crc8(const uint8_t *addr, uint8_t len); // Lua: r = ow.crc8( buf ) static int ow_crc8( lua_State *L ) { size_t datalen; const uint8_t *pdata = luaL_checklstring( L, 1, &datalen ); if(datalen > 255) return luaL_error( L, "wrong arg range" ); lua_pushinteger( L, onewire_crc8(pdata, (uint8_t)datalen) ); return 1; }
/** Czyta temperaturę z czujnika @param id indentyfikator czujnika */ void temp_read_temp_from_ds(uint8_t id) { if(onewire_present()==IS_PRESENT) { onewire_match_rom(&(temp_sensors[id].rom[0])); temp_read_scratchpad(&arr[0]); if (onewire_crc8(&arr[0],8)==arr[8] ) { // crc poprawne if (temp_sensors[id].flags & _BV(TEMP_SENSOR_CONVERT)) { temp_var=arr[1]; temp_var=temp_var<<8|arr[0]; temp_sensors[id].temp = temp_var; temp_set_failure(id,OFF); } temp_set_next_convert(id); } else { temp_badreads(id); } } else { temp_set_failure(id,ON); } }
int ICACHE_FLASH_ATTR ds18b20_read(ds18b20_data *read) { byte i; byte present = 0; byte type_s; byte data[12]; byte addr[8]; byte crc; float celsius; //float fahrenheit; DS18B20_DBG("Look for OneWire Devices on PIN =%d", gpioPin); if (!onewire_search(gpioPin, addr)) { DS18B20_DBG("No more addresses."); onewire_reset_search(gpioPin); delay_ms(250); return; } DS18B20_DBG("ADDRESS = %02x %02x %02x %02x %02x %02x %02x %02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); // the first ROM byte indicates which chip switch (addr[0]) { case 0x10: DS18B20_DBG(" Chip = DS18S20"); // or old DS1820 type_s = 1; break; case 0x28: DS18B20_DBG(" Chip = DS18B20"); type_s = 0; break; case 0x22: DS18B20_DBG(" Chip = DS1822"); type_s = 0; break; default: DS18B20_DBG("Device is not a DS18x20 family device."); return; } onewire_reset(gpioPin); onewire_select(gpioPin, addr); onewire_write(gpioPin, 0x44, 1); // start conversion, use ds.write(0x44,1) with parasite power on at the end delay_ms(1000); present = onewire_reset(gpioPin); onewire_select(gpioPin, addr); onewire_write(gpioPin, 0xBE, 1); // Read Scratchpad //DS18B20_DBG(" Present = %d", present); for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = onewire_read(gpioPin); DS18B20_DBG(" DATA: %02x ", data[i]); } crc = onewire_crc8(data, 8); DS18B20_DBG(" CRC=%d ", crc); if (crc != data[8]) { DS18B20_DBG("CRC is not valid!"); return; } // Convert the data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits // even when compiled on a 32 bit processor. int16_t raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; // 9 bit resolution default if (data[7] == 0x10) { // "count remain" gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time } celsius = (float) raw / 16.0; //fahrenheit = celsius * 1.8 + 32.0; read->temp = celsius; DS18B20_DBG("Temperature = "); char *temp_string = (char*) os_zalloc(64); c_sprintf(temp_string, "%f", read->temp); DS18B20_DBG(" %s Celsius", temp_string); os_free(temp_string); //DS18B20_DBG(" %2.2f Fahrenheit", fahrenheit); return 1; }