Use STM32 firmware library function operation to control LED lights

    To directly use the register address for STM32 development, you need to look up the chip manual for the address of each register, check the meaning of each bit of each register, and then decide which bits of that address are set. This kind of programming is inefficient, the portability of the program is poor, and the readability of the program is poor.

    STM32 provides functional functions for peripheral register operations. You only need to call the corresponding library functions for register operations. Generally, the combination of function names and words is closely related to the function, which enhances the readability of the program.

1. Based on the CMSIS firmware library project, add a "Lib" folder under the project folder, add "inc" and "src" folders under the "Lib" folder, and copy in the "inc" folder "stm32f10x_gpio.h" and "stm32f10x_rcc.h" two header files, copy "stm32f10x_gpio.c" and "stm32f10x_rcc.c" two library function program files in the "src" folder, which respectively contain RCC and GPIO operation related libraries function.

2. Add the "Lib" group in the "Project Item Management" dialog box, and add two library function program files to the group.

3. Modify "Include Paths" and add ".\Lib\inc" path

4. Rewrite the main function in main.c

① Include the required header files: "stm32f10x_gpio.h" and "stm32f10x_rcc.h"

② Enable GPIOC clock

    The RCC_APB2PeriphClockCmd function is defined in the RCC library function, and the function prototype is:

void RCC_APB2PeriphClockCmd ( uint32_t  RCC_APB2Periph, FunctionalState  NewState )

The value of the parameter RCC_APB2Periph is the predefined value in "stm32f10x_rcc.h". The operation of GPIOC here is RCC_APB2Periph_GPIOC, which is defined as follows:

This value is consistent with the value obtained by the expression "0x1<<4" in the previous program.

    The second parameter of the function, NewState, takes the value of the FunctionState type. The FunctionState type is an enumeration type defined in the "stm32f10x_rcc.c" file. It has two values, DISABLE and ENABLE, which are defined as follows:

    Now to enable GPIOC, call the function as follows:

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );

③ Configure GPIOC_0 as push-pull output, 50MHz speed.

    The configuration of GPIO pins is implemented by calling the library function GPIO_Init in "stm32f10x_gpio.c". The function prototype is:

void GPIO_Init ( GPIO_TypeDef *  GPIOx, GPIO_InitTypeDef * GPIO_InitStruct  )

The value of the first parameter GPIOx is defined in "stm32f10x.h", and GPIOC is used here; the second parameter GPIO_InitStruct is the GPIO_InitTypeDef pointer type, and the GPIO_InitTypeDef type is defined in the "stm32f10x_gpio.h" file:

GPIO_Pin in this structure is the pin number of the IO port, here is the first LED light, then take 0;

GPIO_Speed ​​is of GPIOSpeed_TypeDef type, and the GPIOSpeed_TypeDef type is defined in the "stm32f10x_gpio.h" file:

As an enumeration type, there are three enumeration values. Here take GPIO_Speed_50MHz;

GPIO_Mode is of the GPIOMode_TypeDef type, and the GPIOMode_TypeDef type is defined in the "stm32f10x_gpio.h" file:

    These enumeration values ​​are:

(1) GPIO_Mode_AIN analog input

(2) GPIO_Mode_IN_FLOATING floating input

(3) GPIO_Mode_IPD pull-down input

(4) GPIO_Mode_IPU pull-up input

(5) GPIO_Mode_Out_OD open-drain output

(6) GPIO_Mode_Out_PP push-pull output

(7) GPIO_Mode_AF_OD multiplexed open-drain output

(8) GPIO_Mode_AF_PP multiplexing push-pull output

    Here select GPIO_Mode_Out_PP.

To configure the working mode of GPIOC_0, first define a variable with the GPIO_InitTypeDef type, then assign values ​​to the members of this structure variable, and finally call the GPIO_Init function to complete the configuration:

GPIO_InitTypeDef GPIOC_0_mode;

GPIOC_0_mode.GPIO_Pin = GPIO_Pin_0;

GPIOC_0_mode.GPIO_Speed = GPIO_Speed_50MHz;

GPIOC_0_mode.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOC, &GPIOC_0_mode);

④ Control the reset and setting of LED1 through the GPIO_ResetBits and GPIO_SetBits functions. The function prototype is as follows:

void GPIO_ResetBits ( GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin ) //Reset function

void GPIO_SetBits ( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin ) //Setting function

The parameters of the function are which GPIO port and which pin are respectively.

If GPIOC_0 is reset, then: GPIO_ResetBits( GPIOC, GPIO_Pin_0) ;

If GPIOC_0 is set, then: GPIO_SetBits( GPIOC, GPIO_Pin_0) ;

The final main.c program is as follows:

#include "stm32f10x.h"

#include "stm32f10x_rcc.h"

#include "stm32f10x_gpio.h"

void delay(int t)

{

     int i;

     for( ;t>0; t--)

         for(i=0;i<1000;i++);

}

intmain()

{

     GPIO_InitTypeDef GPIOC_0_mode;

     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE ); //Enable GPIOC clock

     GPIOC_0_mode.GPIO_Pin = GPIO_Pin_0;

     GPIOC_0_mode.GPIO_Speed = GPIO_Speed_50MHz;

     GPIOC_0_mode.GPIO_Mode = GPIO_Mode_Out_PP;

     GPIO_Init(GPIOC, &GPIOC_0_mode); //Configure GPIOC_0 pin as push-pull output, 50MHz speed

     while(1)

     {

         GPIO_ResetBits( GPIOC, GPIO_Pin_0); //Reset GPIOC_0

         delay(1000);

         GPIO_SetBits( GPIOC, GPIO_Pin_0); //Set GPIOC_0

         delay(1000);

     }

}

5. Connection errors and their solutions.

In the above project, after the project configuration and programming are finally completed, the following error occurs when compiling and linking:

出现这个错误是由于STM32外设库函数的开发中用到了断言机制。正常使用库函数时,需要包含"stm32f10x_conf.h"头文件,在该文件中有如下定义

/* Exported macro ------------------------------------------------------------*/

#ifdef  USE_FULL_ASSERT

/**

  * @brief  The assert_param macro is used for function's parameters check.

  * @param  expr: If expr is false, it calls assert_failed function which reports

  *         the name of the source file and the source line number of the call

  *         that failed. If expr is true, it returns no value.

  * @retval None

  */

  #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))

/* Exported functions ------------------------------------------------------- */

  void assert_failed(uint8_t* file, uint32_t line);

#else

  #define assert_param(expr) ((void)0)

#endif /* USE_FULL_ASSERT */

在该头文件中定义了assert_param宏,根据项目配置,如果设置了"USE_FULL_ASSERT"参数,则对函数参数进行检查,检查不成功转到"assert_failed"函数处理;如果没有设置使用断言,则相当于不对参数进行检测。

解决方法:将这一段宏定义直接复制到"stm32f10x.h"文件中,因为所有的外设库函数文件都直接或间接地包含了该头文件。

6、编译连接项目,下载程序,开发板的LED1闪烁。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325775915&siteId=291194637