2018-03-18 12:30 AM
Hi, I have been using STM32CUBEMX ide for building the code with Keil5 IDE. The issue or bug is the code is working fine when I am placing my code in the main function. But, when I create my own file or outside the main function in it is not affecting the main() function.
Here is the main function that is working. I have highlighted the working lines with bold letters.
But, when I use the same lines of code in a function or outside the file it is not working why?
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(); while (1){HAL_ADC_Start(&hadc1);adcval=HAL_ADC_GetValue(&hadc1);Batval=(adcval*2.998)/256;HAL_ADC_Stop(&hadc1);HAL_Delay(1000);}
void my fun(void)
{
HAL_ADC_Start(&hadc1);
adcval=HAL_ADC_GetValue(&hadc1);Batval=(adcval*2.998)/256;HAL_ADC_Stop(&hadc1);HAL_Delay(1000);}
Can anyone help me out?
2018-03-18 05:58 AM
2018-03-18 06:13 AM
How do you know that the Batval value is wrong? Are you using the debugger for it?
What is the board you are using? Maybe it's discovery o r nucleo so you could print the Batval through the UART (VCOM)?
What is the Keil compiler optimization setting? Set to 'O0' to be able to debug...
2018-03-18 06:16 AM
MAke bat() non-void i.e. float and return the value to the main().
Add 'return Batval' in the body of bat()
It makes sens to call the function, do something, and return the outcome.
Then play with the value you get from bat()
2018-03-19 03:29 AM
When I use HAL for ADC in polling mode I follow this approach to get reliable ADC output:
2018-03-19 05:34 AM
Mr. Golab I have just found the issue.
The problem is not with global or local variables or with configuration. The culprit is HAL_Delay()
But when I keep HAL_Delay(1) the adc value (actual ) is coming and changing to
BAT_output=1566577.6 and adcval=134218167
May I know how to manage this HAL_Delay() in functions and other files
2018-03-19 09:35 AM
Here you can read how to use HAL driver for polling operation. This explain my example provided earlier i.e. HAL_ADC_PollForConversion
Details in stm32f4xx_hal_adc.c
*** Polling mode IO operation ***
================================= [..] Start the ADC peripheral using HAL_ADC_Start() Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage user can specify the value of timeout according to his end application To read the ADC converted values, use the HAL_ADC_GetValue() function. Stop the ADC peripheral using HAL_ADC_Stop()