diff --git a/max31865.c b/max31865.c --- a/max31865.c +++ b/max31865.c @@ -61,8 +61,6 @@ void max31865_readtemp(SPI_HandleTypeDef // 2-wire RTC or 2-wire (duh) NTC thermistor will be the only options // Need option for resistance of RTD // These options should be stored in the set structure and should be menu-selectable - // TODO: Read RTD msbs (0x01) - // TODO: Read RTD LSBs (0x02) // Assert CS HAL_GPIO_WritePin(MAX_CS, 0); @@ -80,7 +78,7 @@ void max31865_readtemp(SPI_HandleTypeDef HAL_GPIO_WritePin(MAX_CS, 1); // Assemble data array into one var - uint16_t temp_pre = (rxdatah[0]<<8) | rxdatal[0]; + uint16_t adc_count = ((rxdatah[0] & 0x7F) << 8) | rxdatal[0]; if((rxdatah[0] & 0x80) && !set->val.ignore_error) { @@ -89,7 +87,7 @@ void max31865_readtemp(SPI_HandleTypeDef uint8_t reg = MAX31865_REG_FAULTSTATUS; - uint8_t data[1] = {0x11}; + uint8_t data[1] = {0x00}; HAL_SPI_Transmit(hspi1, ®, 1, 100); HAL_SPI_Receive(hspi1, data, 1, 100); @@ -108,31 +106,48 @@ void max31865_readtemp(SPI_HandleTypeDef } else { - // Convert to decimal - //temp_pre = temp_pre >> 1; - uint16_t adc_count = temp_pre & 0x7FFF; //do some scaling? - // Convert to Fahrenheit if(set->val.temp_units == TEMP_UNITS_FAHRENHEIT) { - status->temp = adc_count; - //convert to fahrenheit + //use all fixed point math! + + //convert adc to resistance + //Rrtd = adc / range * Rref + status->temp = adc_count * 9830 * 10; + status->temp /= 32768; + //resistance to temp + //(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1 + status->temp = ((status->temp * 100) - 100000) * (39200 - 3200) / (175860 - 100000) + 3200; + //grab the fraction + status->temp_frac = (status->temp / 10) % 10; + //scale back to degrees + status->temp = status->temp / 100; + //add in the offset status->temp += set->val.temp_offset; } - // Use Celsius values + // Convert to Celsius else { - //convert to celsius - int32_t temp = (((int32_t) adc_count) / 32) - 256; - status->temp = temp; + //use all fixed point math! + + //convert adc to resistance + //Rrtd = adc / range * Rref + status->temp = adc_count * 9830 * 10; + status->temp /= 32768; + //resistance to temp + //(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1 + status->temp = ((status->temp * 10) - 10000) * (20000 - 0) / (17586 - 10000) + 0; + //grab the fraction + status->temp_frac = (status->temp / 10) % 10; + //scale back to degrees + status->temp = status->temp / 100; + //add in the offset status->temp += set->val.temp_offset; } } - - } // vim:softtabstop=4 shiftwidth=4 expandtab