2018-06-21 01:31 AM
Hi All,
Recently, I tried to read temperature sensor from ADC. I setup using STM32CubeMx and below is generated code:
/* ADC3 init function */
void MX_ADC3_Init(void) { ADC_ChannelConfTypeDef sConfig; /**Common config */ hadc3.Instance = ADC3; hadc3.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; hadc3.Init.Resolution = ADC_RESOLUTION_16B; hadc3.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc3.Init.LowPowerAutoWait = DISABLE; hadc3.Init.ContinuousConvMode = DISABLE; hadc3.Init.NbrOfConversion = 1; hadc3.Init.DiscontinuousConvMode = DISABLE; hadc3.Init.NbrOfDiscConversion = 1; hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc3.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; hadc3.Init.Overrun = ADC_OVR_DATA_PRESERVED; hadc3.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; hadc3.Init.BoostMode = ENABLE; hadc3.Init.OversamplingMode = DISABLE; if (HAL_ADC_Init(&hadc3) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } }I read the value like:
adcConVal = HAL_ADC_GetValue(&hadc3);
Fortunately, I got 'wrong value' (concluded after calculating temperature) then i changed
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; to
sConfig.SamplingTime = ADC_SAMPLETIME_64CYCLES_5; and It worked but it raises two points
1) I generated the code using STM32CubeMx and it should work fine with default. There
is no option in STM32CubeMx to change Sampling Time.
2) Secondly, How did it work with new cycling time i.e. ADC_SAMPLETIME_64CYCLES_5 ? I could not reach
to exact reason so kindly help.
Regards,
Manish
#stm32h7 #adc-sampling-time #stm32h7-adcSolved! Go to Solution.
2018-06-27 04:10 AM
As per reference manual (Page-874, RM0433, Section: 25.3.13 Channel-wise programmable sampling time (SMPR1, SMPR2))
Tconv = Sampling Time + 7.5 ADC Clock Cycles
BUT, when i check in the STM32H753xI-datasheet (Page-160)
Tconv = Sampling time + 0.5 + N/2 where N= N-bit resolution.
Is it typo issue because you will get 7.5 only when N=14 ?
2018-06-21 11:34 PM
Hi,
1) Maybe a bug in CubeMX. Witch version you're using? I know for other processors I can select the sampling time in the ADC setting. Not tested yet for your processor type.
2) I think you are using fastest sample time for 16-bit. This is 3.6 MSPS. Now you can calculate the time for one sample: 1 ÷ 3.6MSPS × 1.5 = 0.42 us.
This is too fast for temperature sensor. It needs min. 9 us sample time (datasheet p.169). If you use 64.5 cycles everything is ok: 1 ÷ 3.6MSPS × 64.5 = 17.92 us.
Hope it helps.
Regards,
Tobias
2018-06-22 01:35 AM
1) I have checked it in my CubeMX V4.24.0. I can change the sampling time for temperature channel in the ADC3 config:
2018-06-22 06:42 AM
I am using MxCube.Version=4.26.0.
ADC Clock (Fadc) = 32Mhz and Tconv = Sampling time + 7.5 ADC clock cycles.
Sampling Time = 1.5 so Tconv = 1.5 + 7.5 = 9 Clock Cycle (doesn't work)
Sampling Time = 64.5 so Tconv = 64.5 + 7.5 = 72 Clock Cycle (Its works and i get correct temperature values.)
Fews queries came out after reading your replies:
1) You mentioned for 16-bit. This is 3.6MSPS ????? How ?? I am confused.
2) 1 ÷ 3.6MSPS × 1.5 = 0.42 us - Why is it less ? Do you have any ref against which it is less ?
3) 1 ÷ 3.6MSPS × 64.5 = 17.92 us - Why is it more ? Do you have any ref threshold above which it is valid ?
Regards,
Manish
2018-06-22 01:11 PM
1) read datasheet page 159
2) + 3) yes you're right. My calculations are wrong and I missed the 7.5 clock cycles.
So new calculations for sampling time:
9 ÷ 32MHz = 0.28 us
72 ÷ 32MHz = 2.25 us
The ref threshold of 9us sampling time you find in datasheet page 169 as I mentioned.
In my opinion your sampling time is too fast. Slower the sampling time to get accurate temperature readings.
2018-06-25 11:09 PM
Wonderful. Thanks so much for explanation.
2018-06-26 10:09 PM
My bad and My Apology. It does show 'Sampling Time'. BUT, STM32CubeMX must calculate appropriate sampling time for temperature sensor.
2018-06-27 04:10 AM
As per reference manual (Page-874, RM0433, Section: 25.3.13 Channel-wise programmable sampling time (SMPR1, SMPR2))
Tconv = Sampling Time + 7.5 ADC Clock Cycles
BUT, when i check in the STM32H753xI-datasheet (Page-160)
Tconv = Sampling time + 0.5 + N/2 where N= N-bit resolution.
Is it typo issue because you will get 7.5 only when N=14 ?
2018-07-11 04:01 AM
Hi
manishsharma3134
,As
Wedell.Tobias
said, you can select the sampling time in CubeMX depending on your requirement.Also, about Tconv:
in the Ref Man, Tconv is calculated for 14-bit mode resolution.
the Datasheet shows the general rule to calculate Tconv depending on the sampling time and the ADC resolution. Indeed, it shows just the number of cycles (ts + 0.5 + N/2 ). therefore, The deducted number should be multiplied by ADC clock cycles to get Tconv:
For example, for ts= 1.5 and for 14-bit mode resolution: (ts
+ 0.5 + N/2 )=1.5+0.5+14/2=9 cycles. And In case of ADC clock cycles equal to 24 MHz, Tconv=9/24=0.375 ?s.
Hoping it helps you, Please do not hesitate to contact me for further details.
With regards,
Mohamed