IO类限定词 _I _O _IO #define volatile const volatile volatile 描述 只读访问 只写访问 读和写访问 表 5-8 固件库与CMSIS数据类型对比 固件库类型 s32 s16 s8 sc32 sc16 sc8 vs32 vs16 vs8 vsc32 vsc16 vsc8 CMSIS类型 int32_t int16_t int8_t const int32_t const int16_t const int8_t _IO int32_t _IO int16_t _IO int8_t _I int32_t _I int16_t _I int8_t 描述 易挥发只读有符号32位数据 易挥发只读有符号16位数据 易挥发只读有符号8位数据 只读有符号32位数据 只读有符号16位数据 只读有符号8位数据 易挥发读写访问有符号32位数据 易挥发读写访问有符号16位数据 易挥发读写访问有符号8位数据 易挥发只读有符号32位数据 易挥发只读有符号16位数据 易挥发只读有符号8位数据
u32 u16 u8 uc32 uc16 uc8 vu32 vu16 vu8 vuc32 vuc16 vuc8 uint32_t uint16_t uint8_t const uint32_t const uint16_t const uint8_t _IO uint32_t _IO uint16_t _IO uint8_t _I uint32_t _I uint16_t _I uint8_t 无符号32位数据 无符号16位数据 无符号8位数据 只读无符号32位数据 只读无符号16位数据 只读无符号8位数据 易挥发读写访问无符号32位数据 易挥发读写访问无符号16位数据 易挥发读写访问无符号8位数据 易挥发只读无符号32位数据 易挥发只读无符号16位数据 易挥发只读无符号8位数据 文件中还包含了常用的布尔形变量定义,如:
1 typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; 2
3 typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; 4
5 #define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE)) 6
7 typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
不同版本的标准外设库的变量定义略有不同,如版本中就没有之前版本的TRUE和FALSE的定义,用户也可以根据自己的需求按照上面的格式定义自己的布尔形变量。在使用标准外设库进行开发遇到相关的定义问题时应首先找到对应的头文件定义。
4. 使用步骤
前面几个小节已经详细介绍了标准外设库的组成结构以及部分主要文件的功能描述,那么如果在开发中使用标准外设库需要哪些描述呢下面就进行简要的介绍,这儿介绍的使用方法是与开发环境无关的,在不同的开发环境中可能在操作方式上略有不同,但是总体的流程都是一样的,下一小节将介绍在MDK ARM开发环境下使用标准外设库的详细过程。
首先新建一个项目并设置工具链对应的启动文件,可以使用标准外设库中提供的模板,也可以自己根据自己的需求新建。标准外设库中已经提供了不同工具链对应的文件,位于目录下。
其次按照使用产品的具体型号选择具体的启动文件,加入工程。文件主要按照使用产品的容量进行区分,根据产品容量进行选择即可。每个文件的具体含义可以在“”文件中找到对应的说明,摘录如下:
1 #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL) 2 3 /* #define STM32F10X_LD */ /*!< STM32F10X_LD: STM32 Low density devices */ 4
5 /* #define STM32F10X_LD_VL */ /*!< STM32F10X_LD_VL: STM32 Low density Value Line devices */ 6
7 /* #define STM32F10X_MD */ /*!< STM32F10X_MD: STM32 Medium density devices */ 8
9 /* #define STM32F10X_MD_VL */ /*!< STM32F10X_MD_VL: STM32 Medium density Value Line devices */ /* #define STM32F10X_HD */ /*!< STM32F10X_HD: STM32 High density devices */ 10 11 /* #define STM32F10X_HD_VL */ /*!< STM32F10X_HD_VL: STM32 High density value line devices */ 12 13 /* #define STM32F10X_XL */ /*!< STM32F10X_XL: STM32 XL-density devices */
14
15 /* #define STM32F10X_CL */ /*!< STM32F10X_CL: STM32 Connectivity line devices */ 16
17 #endif 18 19 /* Tip: To avoid modifying this file each time you need to switch between these 20
21 devices, you can define the device in your toolchain compiler preprocessor. 22
23 - Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers 24
25 where the Flash memory density ranges between 16 and 32 Kbytes. 26
27 - Low-density value line devices are STM32F100xx microcontrollers where the Flash 28
29 memory density ranges between 16 and 32 Kbytes. 30
31 - Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers 32
33 where the Flash memory density ranges between 64 and 128 Kbytes. 34
35 - Medium-density value line devices are STM32F100xx microcontrollers where the 36
37 Flash memory density ranges between 64 and 128 Kbytes. 38
39 - High-density devices are STM32F101xx and STM32F103xx microcontrollers where 40
41 the Flash memory density ranges between 256 and 512 Kbytes. 42
43 - High-density value line devices are STM32F100xx microcontrollers where the 44
45 Flash memory density ranges between 256 and 512 Kbytes. 46
47 - XL-density devices are STM32F101xx and STM32F103xx microcontrollers where
48
49 the Flash memory density ranges between 512 and 1024 Kbytes. 50
51 - Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers. 52 53 */
“”是整个标准外设库的入口文件,这个文件包含了STM32F10x全系列所有外设寄存器的定义(寄存器的基地址和布局)、位定义、中断向量表、存储空间的地址映射等。为了是这个文件适用于不同系列的产品,程序中是通过宏定义来实现不同产品的匹配的,上面这段程序的注释中已经详细给出了每个启动文件所对应的产品系列,与之对应,也要相应的修改这个入口文件,需要根据所使用的产品系列正确的注释/去掉相应的注释define。在这段程序的下方同样有这样的一个注释程序
/*#define USE_STDPERIPH_DRIVER*/ 用于选择是否使用标准外设库,如果保留这
个注释,则用户开发程序可以基于直接访问“”中定义的外设寄存器,所有的操作均基于寄存器完成,目前不使用固件库的单片机开发,如51、AVR、MSP430等其实都是采用此种方式,通过在对应型号的头文件中进行外设寄存器等方面的定义,从而在程序中对相应的寄存器操作完成相应的功能设计。
如果去掉/*#define USE_STDPERIPH_DRIVER*/的注释,则是使用标准外设库进行开发,用户需要使用在文件“”中,选择要用的外设,外设同样是通过注释/去掉注释的方式来选择。示例程序如下:
1 /* Uncomment the line below to enable peripheral header file inclusion */ 2
3 #include \ 4
5 /* #include \ 6
7 /* #include \ 8
9 /* #include \ 10
11 /* #include \ 12
13 /* #include \ 14