Add conversion function for resistance to temperature conversion and add it to thept command of the shell
This commit is contained in:
		@@ -43,6 +43,8 @@ CFILES += stm-periph/unique-id.c
 | 
			
		||||
 | 
			
		||||
CFILES += calibration.c
 | 
			
		||||
 | 
			
		||||
CFILES += temp-converter.c
 | 
			
		||||
 | 
			
		||||
DEFINES += -DDEBUGBUILD
 | 
			
		||||
#TODO
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										31
									
								
								stm-firmware/include/reflow-controller/temp-converter.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								stm-firmware/include/reflow-controller/temp-converter.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
/* Reflow Oven Controller
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2020  Mario Hüttel <mario.huettel@gmx.net>
 | 
			
		||||
 *
 | 
			
		||||
 * This file is part of the reflow Oven Controller Project.
 | 
			
		||||
 *
 | 
			
		||||
 * The reflow oven controller is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License version 2 as
 | 
			
		||||
 * published by the Free Software Foundation.
 | 
			
		||||
 *
 | 
			
		||||
 * GDSII-Converter is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef __TEMP_CONVERTER_H__
 | 
			
		||||
#define __TEMP_CONVERTER_H__
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @brief Convert PT1000 resistance to temperature in degrees celsius
 | 
			
		||||
 * @param resistance PT1000 resistance value
 | 
			
		||||
 * @param[out] temp_out Temperature output
 | 
			
		||||
 * @return 0 if ok, -1 if value is below conversion range, 1 if value is above conversion range,-1000 in case of pointer error
 | 
			
		||||
 */
 | 
			
		||||
int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out);
 | 
			
		||||
 | 
			
		||||
#endif /* __TEMP_CONVERTER_H__ */
 | 
			
		||||
@@ -8,6 +8,7 @@
 | 
			
		||||
#include <reflow-controller/systick.h>
 | 
			
		||||
#include <stm-periph/unique-id.h>
 | 
			
		||||
#include <reflow-controller/calibration.h>
 | 
			
		||||
#include <reflow-controller/temp-converter.h>
 | 
			
		||||
 | 
			
		||||
#ifndef GIT_VER
 | 
			
		||||
#define GIT_VER "VERSION NOT SET"
 | 
			
		||||
@@ -106,6 +107,9 @@ static shellmatta_retCode_t shell_cmd_pt1000_res(const shellmatta_handle_t   han
 | 
			
		||||
	int pt1000_status;
 | 
			
		||||
	enum adc_pt1000_error pt1000_flags;
 | 
			
		||||
	char display_status[100];
 | 
			
		||||
	float temp;
 | 
			
		||||
	int temp_conv_status;
 | 
			
		||||
	const char *temp_prefix;
 | 
			
		||||
 | 
			
		||||
	display_status[0] = 0;
 | 
			
		||||
 | 
			
		||||
@@ -125,7 +129,22 @@ static shellmatta_retCode_t shell_cmd_pt1000_res(const shellmatta_handle_t   han
 | 
			
		||||
		strcpy(display_status, "VALID");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	shellmatta_printf(handle, "PT1000 resistance: %.2f Ohm [%s]\r\n", resistance, display_status);
 | 
			
		||||
	temp_conv_status = temp_converter_convert_resistance_to_temp(resistance, &temp);
 | 
			
		||||
	switch (temp_conv_status) {
 | 
			
		||||
	case 1:
 | 
			
		||||
		temp_prefix = ">";
 | 
			
		||||
		break;
 | 
			
		||||
	case -1:
 | 
			
		||||
		temp_prefix = "<";
 | 
			
		||||
		break;
 | 
			
		||||
	case 0:
 | 
			
		||||
		/* Expected fallthrough */
 | 
			
		||||
	default:
 | 
			
		||||
		temp_prefix = "";
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	shellmatta_printf(handle, "PT1000 resistance: %.2f Ohm (%s%.1f °C) [%s]\r\n", resistance, temp_prefix,temp, display_status);
 | 
			
		||||
 | 
			
		||||
	return SHELLMATTA_OK;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										74
									
								
								stm-firmware/temp-converter.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								stm-firmware/temp-converter.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,74 @@
 | 
			
		||||
/* Reflow Oven Controller
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2020  Mario Hüttel <mario.huettel@gmx.net>
 | 
			
		||||
 *
 | 
			
		||||
 * This file is part of the reflow Oven Controller Project.
 | 
			
		||||
 *
 | 
			
		||||
 * The reflow oven controller is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License version 2 as
 | 
			
		||||
 * published by the Free Software Foundation.
 | 
			
		||||
 *
 | 
			
		||||
 * GDSII-Converter is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <reflow-controller/temp-converter.h>
 | 
			
		||||
#include <reflow-controller/temp-converter-data.h>
 | 
			
		||||
#include <helper-macros/helper-macros.h>
 | 
			
		||||
 | 
			
		||||
static const float temp_lookup[(TEMP_CONVERSION_MAX_RES-TEMP_CONVERSION_MIN_RES) / TEMP_CONVERSION_RES_STEP+1] = {TEMP_CONVERSION_ARRAY_DATA};
 | 
			
		||||
 | 
			
		||||
int temp_converter_convert_resistance_to_temp(float resistance, float *temp_out)
 | 
			
		||||
{
 | 
			
		||||
	int ret_val;
 | 
			
		||||
	unsigned int i;
 | 
			
		||||
	unsigned int lower_idx = COUNT_OF(temp_lookup) - 1;
 | 
			
		||||
	float diff_to_low_resistance;
 | 
			
		||||
	float diff_high_low;
 | 
			
		||||
	float low;
 | 
			
		||||
 | 
			
		||||
	/* Check pointer */
 | 
			
		||||
	if (!temp_out)
 | 
			
		||||
		return -1000;
 | 
			
		||||
 | 
			
		||||
	if (resistance < TEMP_CONVERSION_MIN_RES) {
 | 
			
		||||
		*temp_out = temp_lookup[0];
 | 
			
		||||
		ret_val = -1;
 | 
			
		||||
		goto return_ret_val;
 | 
			
		||||
	} else if (resistance > TEMP_CONVERSION_MAX_RES) {
 | 
			
		||||
		*temp_out = temp_lookup[COUNT_OF(temp_lookup)-1];
 | 
			
		||||
		ret_val = 1;
 | 
			
		||||
		goto return_ret_val;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Resistance is in range */
 | 
			
		||||
	ret_val = 0;
 | 
			
		||||
 | 
			
		||||
	/* Calculate lower index for given reistance */
 | 
			
		||||
	for (i = 1; i < COUNT_OF(temp_lookup); i++) {
 | 
			
		||||
		if (resistance <= TEMP_CONVERSION_MIN_RES + TEMP_CONVERSION_RES_STEP * i) {
 | 
			
		||||
			lower_idx = i - 1;
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* Get the lower temperature limit of the current range */
 | 
			
		||||
	low = temp_lookup[lower_idx];
 | 
			
		||||
 | 
			
		||||
	/* Difference to higher limit */
 | 
			
		||||
	diff_high_low = temp_lookup[lower_idx+1] - low;
 | 
			
		||||
 | 
			
		||||
	/* Resistance difference to lower limit's resistance value */
 | 
			
		||||
	diff_to_low_resistance = resistance - (float)(TEMP_CONVERSION_MIN_RES + TEMP_CONVERSION_RES_STEP * lower_idx);
 | 
			
		||||
 | 
			
		||||
	/* Calculate output temperature */
 | 
			
		||||
	*temp_out = (diff_to_low_resistance / TEMP_CONVERSION_RES_STEP) * diff_high_low + low;
 | 
			
		||||
 | 
			
		||||
return_ret_val:
 | 
			
		||||
	return ret_val;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user