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 12:39 AM
What error do you get? What does not work?
Is the my_fun() called from the main?
What about the Batval/adcval variables? Are they global to be accessible from the my_fun() - they are not declared inside my_fun()...
Etc, etc, etc...
2018-03-18 01:12 AM
2018-03-18 01:28 AM
You put the function definition code inside the main so the program shall not compile.
void myfun(void)
{
HAL_ADC_Start(&hadc1);
adcval=HAL_ADC_GetValue(&hadc1);Batval=(adcval*2.998)/256;HAL_ADC_Stop(&hadc1);HAL_Delay(1000);
}
2018-03-18 01:30 AM
Function definition shall be outside the main().
Beside what do you mean by 'not accessible'. Do you refer to these global vars inside the main? Does the program compile?
2018-03-18 02:13 AM
2018-03-18 03:01 AM
Sorry, I have wrongly, pasted the code. Actually, that function is outside the main function only and the variables adcval and Batval are global. Is something wron with error handlers when I use the funtion outside the main.
2018-03-18 03:07 AM
Yes the program compiles and my code works fine with main but not with the function
2018-03-18 03:11 AM
Could please share what does not work. Be precise.
2018-03-18 03:15 AM
Please share:
1. The whole code which does not work
2. What is expected
3. What goes wrong