We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I am working on STM32F407 discovery board . I am using STMCUBEMX for using the HAL drivers. It is working fine.
I am working on ADC to read the battery voltage . I have used STMCUBEMX for creating the ADC configuration.
The configuration code has generated successfully.
I have changed the main function code as per this to read the battery voltage. I have declared BAT_Output and adcval as global varibles
uint32_t adcval;
float BAT_output;
int main(void) {
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */ SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); /* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */ /* USER CODE BEGIN WHILE */
while (1) {
HAL_ADC_Start(&hadc1); adcval=HAL_ADC_GetValue(&hadc1); BAT_output=(adcval*2.988)/256; HAL_Delay(1000);
} }
The above code is working fine by declaring the BAT_output and adcval as global. The battery value is coming BAT_output=2 with adcval= 185.
ISSUE 1:
But when I use the BAT_output and adcval as local varibles to main() function it is showing garbage value BAT_output=1566577.6 and adcval=134218167
Here is the code snippet of main.c
int main(void) { float BAT_output; uint32_t adcval;
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
What is the real problem. Is it with keil compiler setting or anything code bug with STMCUBEMX.
ISSUE 2:
The same issue comes when I create seperate function for the battery voltage. Here is the seperate file created for battery voltage reading.
#include "main.h" #include "stm32f4xx_hal.h" #include "stm32f4xx.h"
ADC_HandleTypeDef hadc12; extern uint32_t adcval; extern float BAT_output;
float bat(void) { HAL_ADC_Start(&hadc12); adcval=HAL_ADC_GetValue(&hadc12); BAT_output=(adcval*2.988)/256; HAL_Delay(1000);
return BAT_output;
}
Here is the main.c file for issue 2.
float BAT_output; uint32_t adcval; float bat(void); float BAT_output;
while (1) { BAT_output=bat(); //HAL_ADC_Start(&hadc1); //adcval=HAL_ADC_GetValue(&hadc1); //BAT_output=(adcval*2.988)/256; //HAL_Delay(1000);
Can any one sort out this problem.