This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Global variables and local variables misbehaving in KEIL uvision 5 when using with STMCUBEMX

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 */

/* 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);

}
}

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;

int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* 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) { 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.