Changeset - 6d43230d5986
[Not reviewed]
tip rtd
0 3 0
matthewreed - 6 years ago 2018-11-25 21:08:06

Untested changes for RTDs
3 files changed with 30 insertions and 14 deletions:
display.c
16
3
max31865.c
13
10
pid.c
1
1
0 comments (0 inline, 0 general)
display.c
Show inline comments
 
@@ -22,13 +22,13 @@ static uint8_t sw_right_last = 0;
 

	
 

	
 
// States
 
static uint8_t trigger_drawsetpoint = 1;
 
static int16_t last_temp = 21245;
 
static int16_t last_temp_frac = 21245;
 
static int16_t last_state = STATE_IDLE;
 
static int16_t last_state = STATE_RESET;
 
static uint8_t goto_mode = MODE_HEAT;
 
static uint8_t reset_mode = RESET_REBOOT;
 

	
 

	
 

	
 
// Display state machine
 
@@ -406,22 +406,35 @@ void display_process(therm_settings_t* s
 
            ssd1306_drawstring("Error:              ", 0, 0);
 

	
 
            char tempstr[6];
 
            itoa(status->error_code, tempstr, 10);
 
            ssd1306_drawstring(tempstr, 0, 57);
 

	
 
            //TODO: add RTD error codes
 

	
 
			#ifdef MAX31865_RTD_SENSOR
 
            // RTD error codes
 
            if(status->error_code & 0b00000100)
 
                ssd1306_drawstring(" RTD Over/Undervolt", 1, 0);
 
            else if(status->error_code & 0b00001000)
 
                ssd1306_drawstring("    RTD FORCE- Open", 1, 0);
 
            else if(status->error_code & 0b00010000)
 
                ssd1306_drawstring("RTD REF FORCE- Open", 1, 0);
 
            else if(status->error_code & 0b00100000)
 
                ssd1306_drawstring("    RTD REFIN- High", 1, 0);
 
            else
 
                ssd1306_drawstring("#?, Unknown Error", 1, 0);
 
			#else
 
            // TC error codes
 
            if(status->error_code == 1)
 
                ssd1306_drawstring("    TC Open Circuit", 1, 0);
 
            else if(status->error_code == 4)
 
                ssd1306_drawstring("    TC Short to GND", 1, 0);
 
            else if(status->error_code == 8)
 
                ssd1306_drawstring("    TC Short to VCC", 1, 0);
 
            else
 
                ssd1306_drawstring("#?, Unknown Error", 1, 0);
 
			#endif
 
            ssd1306_drawstring("                    ", 2, 0);
 

	
 
            ssd1306_drawstring("-> to ignore all or", 2, 0);
 
            ssd1306_drawstring("press to continue", 3, 0);
 

	
 
            // Button handler
max31865.c
Show inline comments
 
@@ -86,31 +86,34 @@ void max31865_readtemp(SPI_HandleTypeDef
 
        HAL_SPI_Transmit(hspi1, &reg, 1, 100);
 
        HAL_SPI_Receive(hspi1, data, 1, 100);
 

	
 
        // Release CS
 
        HAL_GPIO_WritePin(MAX_CS, 1);
 

	
 
        status->error_code = data[0];
 

	
 
        HAL_Delay(400); // FIXME: remove?
 
        status->state_resume = status->state;
 
        status->state = STATE_TC_ERROR;
 
        status->temp = 0;
 
        status->temp_frac = 0;
 
        
 
        // check to see if it's an error we care about
 
        if(data[0] & 0b00111100) {
 
			status->error_code = data[0];
 
			status->state_resume = status->state;
 
			status->state = STATE_TC_ERROR;
 
			status->temp = 0;
 
			status->temp_frac = 0;
 
        }
 
        else {
 
        	max31865_clear_errors(spi_get());
 
        }
 
    }
 
    else 
 
    {
 
        // Convert to Fahrenheit
 
        if(set->val.temp_units == TEMP_UNITS_FAHRENHEIT)
 
        {
 
        	//use all fixed point math!
 

	
 
        	//convert adc to resistance
 
        	//Rrtd = adc / range * Rref
 
        	status->temp = (int32_t) adc_count * 48600L;
 
        	status->temp = (int32_t) adc_count * 40200L;
 
        	status->temp /= 32768L;
 
        	//resistance to temp
 
        	//(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1
 
        	status->temp = ((status->temp) - 10000L) * (39200L - 3200L) / (17586L - 10000L) + 3200L;
 
        	//grab the fraction
 
        	status->temp_frac = (status->temp / 10) % 10L;
 
@@ -124,13 +127,13 @@ void max31865_readtemp(SPI_HandleTypeDef
 
        else
 
        {
 
        	//use all fixed point math!
 

	
 
        	//convert adc to resistance
 
        	//Rrtd = adc / range * Rref
 
        	status->temp = (int32_t) adc_count * 48600L;
 
        	status->temp = (int32_t) adc_count * 40200L;
 
        	status->temp /= 32768L;
 
        	//resistance to temp
 
        	//(x - in1) * (cal2 - cal1) / (in2 - in1) + cal1
 
        	status->temp = (((status->temp) - 10000L) * (20000L- 0L)) / (17586L - 10000L) + 0L;
 
        	//grab the fraction
 
        	status->temp_frac = (status->temp / 10) % 100L;
pid.c
Show inline comments
 
@@ -15,13 +15,13 @@ int16_t pid_update(therm_settings_t* set
 
  // Convert temperature to fixed point number with 1/10th resolution
 
  int8_t temp_frac = status->temp_frac > 9 ? status->temp_frac / 10 : status->temp_frac;
 
  temp_frac = status->temp > 0 ? temp_frac : temp_frac * -1;
 
  int32_t temp = (status->temp * 10) + temp_frac;
 

	
 
  // Calculate instantaneous error
 
  int16_t error = status->setpoint * 10 - temp; // TODO: Use fixed point fraction
 
  int16_t error = status->setpoint * 10 - temp;
 

	
 
  // Proportional component
 
  int32_t p_term = set->val.k_p * error;
 

	
 
  // Error accumulator (integrator)
 
  state->i_state += error;
0 comments (0 inline, 0 general)