Hi all,
I'm using LPC55S16-EVK, and I'm trying to add temperature reads using the built in temperature sensor to my application.
I followed lpcxpresso55s16_lpadc_temperature_measurement example.
Here is my code:
ADC initialization
void init_adc_peripheral()
{
const lpadc_config_t config = {
.enableInDozeMode = true,
.conversionAverageMode = kLPADC_ConversionAverage128,
.enableAnalogPreliminary = true,
.powerUpDelay = 0x80UL,
.referenceVoltageSource = ADC_REF,
.powerLevelMode = kLPADC_PowerLevelAlt1,
.triggerPriorityPolicy = kLPADC_TriggerPriorityPreemptImmediately,
.enableConvPause = false,
.convPauseDelay = 0UL,
.FIFO0Watermark = 0UL,
.FIFO1Watermark = 0UL
};
POWER_DisablePD(kPDRUNCFG_PD_LDOGPADC);
LPADC_Init(ADC_BASE, &config);
LPADC_DoOffsetCalibration(ADC_BASE);
LPADC_DoAutoCalibration(ADC_BASE);
}
Command initialization
void init_adc_command()
{
const lpadc_conv_command_config_t config = {
.sampleChannelMode = kLPADC_SampleChannelSingleEndSideA,
.channelNumber = 26,
.chainedNextCommandNumber = 0,
.enableAutoChannelIncrement = false,
.loopCount = 0UL,
.hardwareAverageMode = kLPADC_HardwareAverageCount1,
.sampleTimeMode = kLPADC_SampleTimeADCK3,
.hardwareCompareMode = kLPADC_HardwareCompareDisabled,
.hardwareCompareValueHigh = 0UL,
.hardwareCompareValueLow = 0UL,
.conversionResolutionMode = kLPADC_ConversionResolutionHigh,
.enableWaitTrigger = false
};
LPADC_SetConvCommandConfig(ADC_BASE, 3, &config);
}
Trigger initialization
void init_adc_trigger()
{
const lpadc_conv_trigger_config_t config = {
.targetCommandId = 3,
.delayPower = 0,
.priority = 0,
.channelAFIFOSelect = 0,
.channelBFIFOSelect = 0,
.enableHardwareTrigger = false
};
LPADC_SetConvTriggerConfig(ADC_BASE, 2, &config);
}
Function to read temperature sensor:
uint32_t read_adc()
{
lpadc_conv_result_t config;
/*trigger ADC read*/
LPADC_DoSoftwareTrigger(ADC_BASE, 4);
/*wait for result*/
while (!LPADC_GetConvResult(ADC_BASE, &config, 0U)){};
/*return data*/
return (uint32_t) config.convValue;
}
float calculate_temperature()
{
uint16_t Vbe1 = 0U;
uint16_t Vbe8 = 0U;
uint32_t convResultShift = 3U;
float parameterSlope = DEMO_LPADC_TEMP_PARAMETER_A;
float parameterOffset = DEMO_LPADC_TEMP_PARAMETER_B;
float parameterAlpha = DEMO_LPADC_TEMP_PARAMETER_ALPHA;
float temperature = -273.15f; /* Absolute zero degree as the incorrect return value. */
/* If the temperature sensor need calibration, then read the calibration from the flash. */
uint32_t temperatureSlopeSolidifyValue = (*((volatile uint32_t *)(FSL_FEATURE_FLASH_NMPA_TEMP_SLOPE_ADDRS)));
uint32_t temperatureOffsetSolidifyValue = (*((volatile uint32_t *)(FSL_FEATURE_FLASH_NMPA_TEMP_OFFSET_ADDRS)));
if (((temperatureSlopeSolidifyValue & 0x1UL) != 0UL) && ((temperatureOffsetSolidifyValue & 0x1UL) != 0UL))
{
/* Rejustify slope value and Offset value based on the calibration value. */
parameterSlope = ((float)(uint32_t)(temperatureSlopeSolidifyValue >> 1UL) / 1024.0f);
parameterOffset = ((float)(uint32_t)(temperatureOffsetSolidifyValue >> 1UL) / 1024.0f);
}
Vbe1 = read_adc() >> convResultShift;
Vbe8 = read_adc() >> convResultShift;
/* Final temperature = A*[alpha*(Vbe8-Vbe1)/(Vbe8 + alpha*(Vbe8-Vbe1))] - B. */
temperature = parameterSlope * (parameterAlpha * ((float)Vbe8 - (float)Vbe1) /
((float)Vbe8 + parameterAlpha * ((float)Vbe8 - (float)Vbe1))) -
parameterOffset;
return temperature;
}
The output I get is around the value: 582.
What I'm doing wrong?
Thanks!
已解决! 转到解答。
Hi @PabloAvalos, thanks for your reply.
I found the problem, I had to use different parameters for the ADC command
const lpadc_conv_command_config_t config = {
.sampleChannelMode = kLPADC_SampleChannelDiffBothSide,
.channelNumber = 26,
.chainedNextCommandNumber = 0,
.enableAutoChannelIncrement = false,
.loopCount = FSL_FEATURE_LPADC_TEMP_SENS_BUFFER_SIZE - 1U,
.hardwareAverageMode = kLPADC_HardwareAverageCount128,
.sampleTimeMode = kLPADC_SampleTimeADCK131,
.hardwareCompareMode = kLPADC_HardwareCompareDisabled,
.hardwareCompareValueHigh = 0UL,
.hardwareCompareValueLow = 0UL,
.conversionResolutionMode = kLPADC_ConversionResolutionHigh,
.enableWaitTrigger = false
};
And I had to reset the FIFOs after an ADC read.
Hi @PabloAvalos, thanks for your reply.
I found the problem, I had to use different parameters for the ADC command
const lpadc_conv_command_config_t config = {
.sampleChannelMode = kLPADC_SampleChannelDiffBothSide,
.channelNumber = 26,
.chainedNextCommandNumber = 0,
.enableAutoChannelIncrement = false,
.loopCount = FSL_FEATURE_LPADC_TEMP_SENS_BUFFER_SIZE - 1U,
.hardwareAverageMode = kLPADC_HardwareAverageCount128,
.sampleTimeMode = kLPADC_SampleTimeADCK131,
.hardwareCompareMode = kLPADC_HardwareCompareDisabled,
.hardwareCompareValueHigh = 0UL,
.hardwareCompareValueLow = 0UL,
.conversionResolutionMode = kLPADC_ConversionResolutionHigh,
.enableWaitTrigger = false
};
And I had to reset the FIFOs after an ADC read.
Thank you so much for reaching us in our community.
Hope you are well, I would like to ask, what did you change on the SDK example? and which SDK version you are using for the lpadc_temperature_measurement ? Because the conversion for temperature has to be ok.
I will be here to help you, I really appreciate your reply. Please let me know if you have more concerns.
Best Regards.
Pablo Avalos.