STM32F401RC项目中遇到的一些问题和解决方法

好长时间不用C/C++,近期因为工作原因接收了一个单片机项目,记录下遇到的问题和解决方法。


 

#先记录一个基础知识点😅:

在swtich-case语句中,代码写在case或default之外是不执行的,比如下面这样:

 1     switch(PCParm.dzsz[PCTempParm.mode][FlowState.i])
 2     {
 3         loopTimes++;  //这里的代码不会被执行到
 4         case 1:
 5             break;
 6         case 2:
 7             break;
 8         case 3:    
 9             break10         default:
11             break;
12     }

至于为什么,参考大佬的解释:https://blog.csdn.net/u013053075/article/details/106186798/


 

warning: #1-D: last line of file ends without a newline

用keil编译时,每个源文件末尾都需要一行空行,如果没有,就会报上面的warning。


 

warning: #188-D: enumerated type mixed with another type

这个问题是因为传参的时候给枚举类型的形参传了非枚举类型的值导致的,比如下面这个函数:

 1 /**
 2   * @brief  Sets or clears the selected data port bit.
 3   *
 4   * @note   This function uses GPIOx_BSRR register to allow atomic read/modify
 5   *         accesses. In this way, there is no risk of an IRQ occurring between
 6   *         the read and the modify access.
 7   *
 8   * @param  GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
 9   *                      x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
10   * @param  GPIO_Pin specifies the port bit to be written.
11   *          This parameter can be one of GPIO_PIN_x where x can be (0..15).
12   * @param  PinState specifies the value to be written to the selected bit.
13   *          This parameter can be one of the GPIO_PinState enum values:
14   *            @arg GPIO_PIN_RESET: to clear the port pin
15   *            @arg GPIO_PIN_SET: to set the port pin
16   * @retval None
17   */
18 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
19 {
20   /* Check the parameters */
21   assert_param(IS_GPIO_PIN(GPIO_Pin));
22   assert_param(IS_GPIO_PIN_ACTION(PinState));
23 
24   if(PinState != GPIO_PIN_RESET)
25   {
26     GPIOx->BSRR = GPIO_Pin;
27   }
28   else
29   {
30     GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
31   }
32 }
GPIO_PinState是枚举类型,下面是它的定义:
1 typedef enum
2 {
3   GPIO_PIN_RESET = 0,
4   GPIO_PIN_SET
5 }GPIO_PinState;

warning: #550-D: variable "***" was set but never used
这个暂不清除什么原因,但是给变量加上volatile修饰就可以了,这是volatile关键字的说明:
https://www.runoob.com/w3cnote/c-volatile-keyword.html
一句话解释其中一种用途:避免在release时变量被优化导致运行错误。

posted @ 2022-09-02 16:40  晨光不是陈光  阅读(445)  评论(0)    收藏  举报