volatile const uint32_t TEMP_SENSE0 = DL_FactoryRegion_getTemperatureVoltage();
float internal_temperature_sensor_calc(uint16_t adc_code) {
/*参考MSPM0L系列Technical Reference Manual 2.2.5节*/
float TSc = -1.75e-3; // MSPM0L1306 datasheet 7.13节
float TStrim = 30; // MSPM0L1306 datasheet 7.13节
float Vref = 1.4;
float Vsample = (Vref / 4096) * (adc_code - 0.5);
float Vtrim = (Vref / 4096) * (TEMP_SENSE0 - 0.5);
float Tsample = (1 / TSc) * (Vsample - Vtrim) + TStrim;
return Tsample;
}
TEMP_SENSE0 = DL_FactoryRegion_getTemperatureVoltage();
完整的代码见文末。
/**
* These arguments were used when this file was generated. They will be automatically applied on subsequent loads
* via the GUI or CLI. Run CLI with '--help' for additional information on how to override these arguments.
* @cliArgs --device "MSPM0L130X" --package "VQFN-32(RHB)" --part "Default" --product "mspm0_sdk@1.20.00.06"
* [url=home.php?mod=space&uid=304333]@versions[/url] {"tool":"1.18.0+3266"}
*/
/**
* Import the modules used in this configuration.
*/
const ADC12 = scripting.addModule("/ti/driverlib/ADC12", {}, false);
const ADC121 = ADC12.addInstance();
const Board = scripting.addModule("/ti/driverlib/Board");
const SYSCTL = scripting.addModule("/ti/driverlib/SYSCTL");
const VREF = scripting.addModule("/ti/driverlib/VREF");
/**
* Write custom configuration values to the imported modules.
*/
ADC121.$name = "ADC12_0";
ADC121.sampClkSrc = "DL_ADC12_CLOCK_ULPCLK";
ADC121.sampClkDiv = "DL_ADC12_CLOCK_DIVIDE_8";
ADC121.powerDownMode = "DL_ADC12_POWER_DOWN_MODE_MANUAL";
ADC121.samplingOperationMode = "sequence";
ADC121.adcMem2chansel = "DL_ADC12_INPUT_CHAN_7";
ADC121.adcMem3chansel = "DL_ADC12_INPUT_CHAN_3";
ADC121.sampleTime0 = "40 us";
ADC121.adcMem0chansel = "DL_ADC12_INPUT_CHAN_9";
ADC121.adcMem1chansel = "DL_ADC12_INPUT_CHAN_11";
ADC121.endAdd = 1;
ADC121.enabledInterrupts = ["DL_ADC12_INTERRUPT_MEM0_RESULT_LOADED","DL_ADC12_INTERRUPT_MEM1_RESULT_LOADED"];
ADC121.adcMem1vref = "VREF";
ADC121.peripheral.$assign = "ADC0";
ADC121.adcPin9Config.$name = "ti_driverlib_gpio_GPIOPinGeneric1";
VREF.checkVREFReady = true;
/**
* Pinmux solution for unlocked pins/peripherals. This ensures that minor changes to the automatic solver in a future
* version of the tool will not impact the pinmux you originally saw. These lines can be completely deleted in order to
* re-solve from scratch.
*/
ADC121.peripheral.adcPin9.$suggestSolution = "PA15";
Board.peripheral.$suggestSolution = "DEBUGSS";
Board.peripheral.swclkPin.$suggestSolution = "PA20";
Board.peripheral.swdioPin.$suggestSolution = "PA19";
SYSCTL.peripheral.$suggestSolution = "SYSCTL";
VREF.peripheral.$suggestSolution = "VREF";
#include "ti_msp_dl_config.h"
#include <math.h>
#include <stdio.h>
volatile bool gCheckADC;
volatile uint32_t TEMP_SENSE0; // 内部温度传感器校准值
volatile uint16_t adc_thermistor;
volatile uint16_t adc_internal_temp_sensor;
volatile float t_thermistor;
volatile float t_internal_temp_sensor;
/*热电阻温度换算*/
float VBias = 3.30; // set the VBIAS voltage
// set the number of bits based on you ADC (2^# of ADC Bit Value)
unsigned int ADC_BITS = 4096;
float VTEMP = 0; // set up the variable for the measured voltage
float THRM_TEMP = 0; // setup the variable for the calculated temperature
float thermistor_calc_temperature(uint16_t raw_ADC) {
// THRM calculations via regression
// Copied from TI Thermistor Design Tool Excel Doc
float VTEMP = 0.0;
float THRM_ADC = raw_ADC;
float THRM_A0 = -4.232811E+02;
float THRM_A1 = 4.728797E+02;
float THRM_A2 = -1.988841E+02;
float THRM_A3 = 4.869521E+01;
float THRM_A4 = -1.158754E+00;
VTEMP = (VBias / ADC_BITS) * THRM_ADC; // calculate volts per bit then
// multiply that times the ADV value
THRM_TEMP = (THRM_A4 * powf(VTEMP, 4)) + (THRM_A3 * powf(VTEMP, 3)) +
(THRM_A2 * powf(VTEMP, 2)) + (THRM_A1 * VTEMP) +
THRM_A0; // 4th order regression to get temperature
return THRM_TEMP;
}
float internal_temperature_sensor_calc(uint16_t adc_code) {
/*参考MSPM0L系列Technical Reference Manual 2.2.5节*/
float TSc = -1.75e-3; // MSPM0L1306 datasheet 7.13节
float TStrim = 30; // MSPM0L1306 datasheet 7.13节
float Vref = 1.4;
float Vsample = (Vref / 4096) * (adc_code - 0.5);
float Vtrim = (Vref / 4096) * (TEMP_SENSE0 - 0.5);
float Tsample = (1 / TSc) * (Vsample - Vtrim) + TStrim;
return Tsample;
}
int main(void) {
/* Initialize peripherals and enable interrupts */
SYSCFG_DL_init();
NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);
TEMP_SENSE0 = DL_FactoryRegion_getTemperatureVoltage();
printf("TEMP_SENSE0=%d\n", TEMP_SENSE0);
TEMP_SENSE0 = 1867; //使用自定义的校准值覆盖出厂校准值
gCheckADC = false;
while (1) {
DL_ADC12_startConversion(ADC12_0_INST);
/* Wait until all data channels have been loaded. */
while (gCheckADC == false) {
__WFE();
}
/* ch0:热电阻adc读数,
ch1:内部温度传感器adc读数 */
adc_thermistor = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0);
adc_internal_temp_sensor =
DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_1);
gCheckADC = false;
DL_ADC12_enableConversions(ADC12_0_INST);
printf("adc_thermistor=%d, adc_internal_temp_sensor=%d\n", adc_thermistor,
adc_internal_temp_sensor);
t_thermistor = thermistor_calc_temperature(adc_thermistor);
t_internal_temp_sensor =
internal_temperature_sensor_calc(adc_internal_temp_sensor);
printf("t_thermistor=%g, t_internal_temp_sensor=%g\n", t_thermistor, t_internal_temp_sensor);
printf("\n");
}
}
/* Check for the last result to be loaded then change boolean */
void ADC12_0_INST_IRQHandler(void) {
switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) {
case DL_ADC12_IIDX_MEM1_RESULT_LOADED:
gCheckADC = true;
break;
default:
break;
}
}
from sympy import *
# 已知环境温度为Tsample,ADC采样值为adc_code,求校准值TEMP_SENSE0
Tsample = 24.6
adc_code = 1895
TSc = -1.75e-3
TStrim = 30
Vref = 1.4
TEMP_SENSE0 = Symbol('TEMP_SENSE0')
Vsample = (Vref / 4096) * (adc_code - 0.5)
Vtrim = (Vref / 4096) * (TEMP_SENSE0 - 0.5)
eq = (1 / TSc) * (Vsample - Vtrim) + TStrim - Tsample
print(solve(eq, TEMP_SENSE0))
引用: 吾妻思萌 发表于 2023-11-7 12:40 基准没选对啊。要不就是转换不对吧。华氏和摄氏不会差两百℃啊
基准有做实验对比哦。和单位没关系,就是校准值有问题,手动校准一下就好了