Estoy intentando simplemente leer el valor del PCO ADC en una placa Nucleo-L476RG. Conecté una fuente de alimentación al PC0 Pin con 2 V y las conexiones a tierra de la placa y la fuente de alimentación. Cuando leo el valor del ADC con el código abajo, obtengo un valor de alrededor de 700. A una tensión de referencia de 3.3 V y 2 V en el PC0 Pin I debería obtener un valor de 2482.
Verifiqué los voltajes en el AVDD / VREF, los pines AGND y el pin PC0 y los niveles de voltaje donde todos deberían estar. Así que creo que debe ser un problema con mi código.
¿Puedes ver lo que está mal con mi código?
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
/* MSI is enabled after System reset, activate PLL with MSI as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLP = 7;
RCC_OscInitStruct.PLL.PLLQ = 4;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
/* Initialization Error */
while(1);
}
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
/* Initialization Error */
while(1);
}
SystemCoreClockUpdate();
if (HAL_GetREVID() == 0x1001)
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
}
HAL_StatusTypeDef adc_init(void)
{
GPIO_InitTypeDef gpioInit;
HAL_StatusTypeDef AdcStatus;
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_ADC_CLK_ENABLE();
gpioInit.Pin = GPIO_PIN_0;
gpioInit.Mode = GPIO_MODE_ANALOG;
gpioInit.Pull = GPIO_PULLDOWN;
gpioInit.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOC, &gpioInit);
HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
ADC_ChannelConfTypeDef adcChannel;
AdcHandle.Instance = ADC1;
AdcStatus = HAL_ADC_DeInit(&AdcHandle);
if (AdcStatus != HAL_OK)
return AdcStatus;
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.ScanConvMode = ADC_SCAN_DISABLE;
AdcHandle.Init.LowPowerAutoWait = DISABLE;
AdcHandle.Init.ContinuousConvMode = ENABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.ExternalTrigConvEdge = ADC_SOFTWARE_START;
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = DISABLE;
AdcHandle.Init.Overrun = ADC_OVR_DATA_PRESERVED;
AdcHandle.Init.OversamplingMode = DISABLE;
AdcStatus = HAL_ADC_Init(&AdcHandle);
if (AdcStatus != HAL_OK)
return AdcStatus;
adcChannel.Channel = ADC_CHANNEL_1;
adcChannel.Rank = ADC_REGULAR_RANK_1;
adcChannel.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
adcChannel.OffsetNumber = ADC_OFFSET_NONE;
adcChannel.Offset = 0;
adcChannel.SingleDiff = ADC_SINGLE_ENDED;
AdcStatus = HAL_ADC_ConfigChannel(&AdcHandle, &adcChannel);
if (AdcStatus != HAL_OK)
return AdcStatus;
AdcStatus = ADC_Enable(&AdcHandle);
if (AdcStatus != HAL_OK)
return AdcStatus;
return HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED);
}
uint32_t adc_get_bits(uint32_t Timeout)
{
uint32_t AdcValue = 0;
if (HAL_ADC_Start(&AdcHandle) != HAL_OK)
return 0;
if (HAL_ADC_PollForConversion(&AdcHandle, Timeout) == HAL_OK)
AdcValue = HAL_ADC_GetValue(&AdcHandle);
else
return 0;
if (HAL_ADC_Stop(&AdcHandle) != HAL_OK)
return 0;
return AdcValue;
}