Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void read_temperatures(system_vars_t *vars)
- {
- uint8_t i, j, error;
- // Sensors are having problems and are shutdown for the moment
- if(vars->sensor_reset_counter > 0)
- {
- // Decrement the cooldown counter
- vars->sensor_reset_counter--;
- return;
- }
- for(i = 0; i < NUM_TEMP_SENSORS; i++)
- {
- if(vars->sensor_lines[i] > 0) // sensor enabled
- {
- // sensor_lines: 0 = disabled => subtract one to get the index, multiply by 8
- // to get the correct position (8 bytes long address)
- error = read_sensor(&vars->temperature[i], &vars->onewire_addr[(vars->sensor_lines[i] - 1) * 8]);
- if(error == ERROR_CODE_ONEWIRE_CRC) // XXX debug: error counters
- {
- vars->onewire_crc_errors++;
- continue; // Don't do temperature error handling if the sensor can't be read
- }
- else if(error != 0) /*if(error == ERROR_CODE_ONEWIRE_NO_DEVICE)*/
- {
- vars->onewire_other_errors++;
- continue;
- }
- // Stuck at 0.75 degrees, which is the most common erroneous reading
- if(vars->temperature[i] == 75)
- {
- // Try to check if the 0.75 degrees reading is actually valid.
- // We do this by comparing it to a previous valid (non 0.75 degrees) reading.
- // If the difference is small enough, we accept the reading for a longer grace period.
- // If the difference from the last non 0.75 degrees reading is less than 0.5 degrees
- // (ie. the reading is hovering near 0.75 degrees naturally),
- // the reading is probably valid, so we increase the grace period.
- if(
- (vars->temperature[i] >= vars->temperature_valid[i] && (vars->temperature[i] - vars->temperature_valid[i]) < 50) ||
- (vars->temperature[i] < vars->temperature_valid[i] && (vars->temperature_valid[i] - vars->temperature[i]) < 50)
- )
- {
- // Set the grace period to 60 seconds.
- // If the reading does not fluctuate once in 60 seconds, we assume
- // that the sensor bugged out again.
- vars->sensor_error_limit[i] = 60;
- }
- }
- else if(vars->temperature[i] == 8500)
- {
- // Reset the error limit if we get the 85 degrees reading
- vars->sensor_error_limit[i] = 5;
- }
- // If the sensor is messed up (showing 0.75 or 85.00 degrees constantly),
- // we want to "disconnect" it, by pulling the data line low for X amount
- // of time, so that the sensor can hopefully recover.
- if(vars->temperature[i] == 75 || vars->temperature[i] == 8500)
- {
- // X second grace period
- if(++vars->sensor_error_count[i] >= vars->sensor_error_limit[i])
- {
- // Halt all 1-wire activity for X seconds
- vars->sensor_reset_counter = 1800;
- // Reset the error counters and limits
- for(j = 0; j < NUM_TEMP_SENSORS; j++)
- {
- vars->sensor_error_count[j] = 0;
- vars->sensor_error_limit[j] = 5;
- }
- // Put the sensor(s) into "cooldown mode", ie. pull the data line
- // low so the sensor is unpowered for a while.
- onewire_mosfet_release();
- onewire_low();
- onewire_out();
- return;
- }
- }
- else
- {
- // When the temperature reading is something other than 0.75 or 85, reset the error counter
- vars->sensor_error_count[i] = 0;
- // reset the successive erroneous readings limit: normally we trigger the cooldown
- // after 5 seconds of being stuck, if the hop from the previous value to the erroneous 0.75
- // reading is "big enough", see above
- vars->sensor_error_limit[i] = 5;
- // Store a valid temperature, so that we can calculate the change if we get an erroneous reading the next time
- vars->temperature_valid[i] = vars->temperature[i];
- }
- }
- }
- vars->flags |= FLAG_NEW_TEMPERATURE_DATA;
- onewire_global_conv_temp(); // start next conversion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement