2019-10-20 08:16 PM
hi guys, i'm stm freshman, I am trying to read the adc value by using the adc example of stm32f4, I added bold code to get uhADCxConvertedValue value, but uhADCxConvertedValue value always get 0 Whether I connect adc pin to grounded or vcc.
the led4 and led3 is bright.
example:
STM32 DISCO board's LEDs can be used to monitor the transfer status:
- LED4 is ON when the conversion is complete.
- LED5 is ON when there are an error in initialization.
code:
int main(void)
{
ADC_ChannelConfTypeDef sConfig;
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 4
- Global MSP (MCU Support Package) initialization
*/
HAL_Init();
/* Configure the system clock to 144 MHz */
SystemClock_Config();
/* Configure LED4 and LED5 */
BSP_LED_Init(LED4);
BSP_LED_Init(LED5);
BSP_LED_Init(LED3);
BSP_LED_Init(LED6);
/*##-1- Configure the ADC peripheral #######################################*/
AdcHandle.Instance = ADCx;
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.ContinuousConvMode = ENABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DMAContinuousRequests = ENABLE;
AdcHandle.Init.EOCSelection = DISABLE;
if(HAL_ADC_Init(&AdcHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure ADC regular channel ######################################*/
/* Note: Considering IT occurring after each number of size of */
/* "uhADCxConvertedValue" ADC conversions (IT by DMA end */
/* of transfer), select sampling time and ADC clock with sufficient */
/* duration to not create an overhead situation in IRQHandler. */
sConfig.Channel = ADCx_CHANNEL;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
sConfig.Offset = 0;
if(HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
{
/* Channel Configuration Error */
Error_Handler();
}
/*##-3- Start the conversion process and enable interrupt ##################*/
/* Note: Considering IT occurring after each number of ADC conversions */
/* (IT by DMA end of transfer), select sampling time and ADC clock */
/* with sufficient duration to not create an overhead situation in */
/* IRQHandler. */
if(HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&uhADCxConvertedValue, 1) != HAL_OK)
{
/* Start Conversation Error */
Error_Handler();
}
uint32_t Value1 = 0;
Value1=uhADCxConvertedValue;
if(Value1 ==0){
BSP_LED_On(LED3);
}
else{
BSP_LED_On(LED6);
}
/* Infinite loop */
while (1)
{
}
}
2019-10-21 01:05 AM
Hi. Try to put you led control code to ADC Conversion Comlete Callback.
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
uint32_t Value1 = 0;
Value1=uhADCxConvertedValue;
if(Value1 ==0){
BSP_LED_On(LED3);
}
else {
BSP_LED_On(LED6);
}
}
2019-10-21 11:18 PM
thank you oleksandr.karbivsky, I try to put to ADC Conversion Comlete Callback, and it's work now.