Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


STM32L476G

I’m trying to read ADC value and display it on the lcd when there is and voltage input.
But it would only give me random number when i run this code on the board.
Is there anything wrong?

  1. include “stm32l4xx.h”
  2. include “stm32l476g_discovery.h”
  3. include “stm32l4xx_hal_adc.h”
  4. include “stm32l4xx_hal_gpio.h”
  5. include “adc.h”


////Internal variables
static GPIO_InitTypeDef pinconf;
static ADC_HandleTypeDef hadc;
static ADC_ChannelConfTypeDef sConfig;

////Internal functions
//Pin initialization.
static void ADC_GPIO_Init(){
__HAL_RCC_GPIOA_CLK_ENABLE();
pinconf.Pin = GPIO_PIN_0;
pinconf.Mode = GPIO_MODE_ANALOG;
pinconf.Pull = GPIO_NOPULL;
pinconf.Speed = GPIO_SPEED_FREQ_HIGH;
pinconf.Alternate = 0;
HAL_GPIO_Init(GPIOA,&pinconf);
}

//Handle initialization.
static void ADCHandle_Init(){
__HAL_RCC_ADC_CLK_ENABLE();
hadc.Instance = ADC2;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.ScanConvMode = DISABLE;
hadc.Init.ContinuousConvMode = ENABLE;
hadc.Init.NbrOfConversion = 1;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.NbrOfDiscConversion = 0;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&hadc);
}

//Channel initialization.
static void ADCChannel_Init(){
sConfig.Channel = ADC_CHANNEL_1;
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
HAL_ADC_ConfigChannel(&hadc,&sConfig);
}

////Exported functions
//ADC initialization.
void ADC_Init(){
ADC_GPIO_Init();
ADCHandle_Init();
ADCChannel_Init();
ADC_Enable(&hadc);
}

//Returns ADC value.
uint32_t getADCValue(void)
{
uint32_t value;
HAL_ADC_Start(&hadc);
while(HAL_ADC_PollForConversion(&hadc, 10)!= HAL_OK);
value = HAL_ADC_GetValue(&hadc);
HAL_ADC_Stop(&hadc);
return value;
}

What the ADC channel connected to? If it is floating, then it wasn’t be surprise when get random value while reading.