简体   繁体   English

STM32F103C8 项目 - 我无法让 HAL 在我自己的函数中工作

[英]STM32F103C8 project - I can't get HAL to work in my own functions

This is a strange one for me and I certainly did not expect to get so completely stumped in my first day or so of learning the STM32CubeIDE.这对我来说很奇怪,我当然没想到在学习 STM32CubeIDE 的第一天左右就完全被难住了。 The first order of the day was making the onboard led blink with当天的第一个任务是使板载 LED 闪烁

HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
HAL_Delay(500);

Going through the New Project/pin config and code generator this worked fine inside the while loop provided in the main.c file but when I called my own function inside the loop with the HAL commands it stops working.通过新项目/引脚配置和代码生成器,这在 main.c 文件中提供的 while 循环内运行良好,但是当我使用 HAL 命令在循环内调用我自己的 function 时,它停止工作。 The code loops and calls my function mainLoop() but any HAL commands inside my function don't do anything.代码循环并调用我的 function mainLoop() 但我的 function 中的任何 HAL 命令都不执行任何操作。

Why is this - am I missing some kind of handle that must be passed or an #include to extend the scope of HAL commands within a source file and not just main() itself?为什么会这样 - 我是否缺少某种必须传递的句柄或 #include 来扩展源文件中的 HAL 命令的 scope 而不仅仅是 main() 本身?

      while (1)
      {
          HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
          void mainLoop(void);
    //    HAL_Delay(500);         // works fine when uncommented
      }
    }   // end of main()

    void mainLoop(void)
    {
          HAL_Delay(200);         // this does nothing

}                             // no HAL commands actioned

Yes, you are quite right and thanks for that.是的,你是对的,谢谢。 My stupid question was the result of me actually trying to put mainLoop() in another.cpp file, with appropriate.h header file containing the prototype and call it from the main.c while loop.我的愚蠢问题是我实际上试图将 mainLoop() 放在另一个.cpp 文件中,其中包含原型的适当.h header 文件并从 main.c while 循环中调用它的结果。 Since I got an undefined reference to mainLoop linker error, I moved mainloop() back into main.c and then tried to run the prototype.由于我得到了对 mainLoop linker 错误的未定义引用,我将 mainloop() 移回 main.c 然后尝试运行原型。 Doh!嗬!

So, despite my flawed attempt to simplify the problem, I still cannot call my mainloop() function with its HAL commands residing in another.cpp file.因此,尽管我尝试简化问题存在缺陷,但我仍然无法调用我的 mainloop() function,其 HAL 命令位于 another.cpp 文件中。 Additionally, although I have yet to reach this point, are the HAL commands available in every project source file or will I need to #include a particular header?此外,虽然我还没有达到这一点,但 HAL 命令是否在每个项目源文件中都可用,或者我是否需要#include 特定的 header?

You should call function in the following way.您应该通过以下方式调用 function。

  while (1)
      {
          HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
          mainLoop();
    //    HAL_Delay(500);         // works fine when uncommented
      }
    }   // end of main()

    void mainLoop(void)
    {
          HAL_Delay(200);         // this does nothing

}    

This is not the way to call the function.这不是调用 function 的方法。

void mainLoop(void);  //function declaration (prototype) needed as mainLoop is defined after the call

int main(void)
{
    while (1)
    {
        HAL_GPIO_TogglePin(GPIOC, LED1_Pin);
        mainLoop(); //call to the function
    }
}

void mainLoop(void)  // function definition
{
    HAL_Delay(200);         // this does nothing

}   

As your mainLoop call is before the function definition, you need to provide the function prototype to tell compiler that parameters takes the function and what it returns.由于您的mainLoop调用在 function 定义之前,您需要提供 function 原型来告诉编译器参数接受 function 及其返回的内容。

Then you need to call the function by mainLoop() - not void mainLoop()然后你需要通过mainLoop()调用 function - 不是void mainLoop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM