Skip to main content
Kumar B
Associate III
March 18, 2018
Question

STM32CUBE MX and KEIL5 IDE

  • March 18, 2018
  • 5 replies
  • 2230 views
Posted on March 18, 2018 at 08:30

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?

    This topic has been closed for replies.

    5 replies

    Bogdan Golab
    Lead
    March 18, 2018
    Posted on March 18, 2018 at 08:39

    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...

    Kumar B
    Kumar BAuthor
    Associate III
    March 18, 2018
    Posted on March 18, 2018 at 09:13

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6fV&d=%2Fa%2F0X0000000bso%2FiXtnzNbNCzJuNgH97_C1EYk4fc3uJKXLtkVo17kq3VE&asPdf=false
    Kumar B
    Kumar BAuthor
    Associate III
    March 18, 2018
    Posted on March 18, 2018 at 09:12

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6uj&d=%2Fa%2F0X0000000bxu%2FpU2exFZUGzkAU20BFV7Tn2_a37JrWCR9SX9hnjZfdgs&asPdf=false
    Bogdan Golab
    Lead
    March 18, 2018
    Posted on March 18, 2018 at 09:28

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

     

    }

    Bogdan Golab
    Lead
    March 18, 2018
    Posted on March 18, 2018 at 09:30

    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?

    Kumar B
    Kumar BAuthor
    Associate III
    March 18, 2018
    Posted on March 18, 2018 at 10:01

    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. 

    Bogdan Golab
    Lead
    March 19, 2018
    Posted on March 19, 2018 at 11:29

    When I use HAL for ADC in polling mode I follow this approach to get reliable ADC output:

    0690X0000060A4IQAU.png
    Kumar B
    Kumar BAuthor
    Associate III
    March 19, 2018
    Posted on March 19, 2018 at 12:34

    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

    Bogdan Golab
    Lead
    March 19, 2018
    Posted on March 19, 2018 at 16:35

    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()