STM32学习(一)—STM32固件库的使用

来源:互联网 发布:碣石正车行淘宝店 编辑:程序博客网 时间:2024/05/16 11:07

STM32学习(一)-------------------------2013年07月16日

和我一起学习STM32——STM32固件库的使用

在这里我就不讨论在开发STM32的过程中到底是使用固件库好,还是直接上寄存器好。个人觉得STM32固件库编程思想是很不错的。在利用固件库开发的过程中一方面可以在很大的程度上缩短项目的开发周期,同时我们可以借鉴STM32固件库的编程方式试着为别的单片机写自己的固件库。好了,废话不多说了,下面我们来看看STM32的固件库:

(1)      STM32固件库结构:

STM32学习(一)-------------------------2013年07月16日

STM32学习(一)-------------------------2013年07月16日

 至于每个文件夹都有什么,我想再也没有STM32固件库的帮助文档中写的更详细了的吧。

(2)      STM32固件库命名规则:

    我认为编程必须要有一个规范,要有一个良好的编程风格。当我看到我们班有的同学码代码的时候,什么变量名,函数名啦都还用拼音,我巨汗的不行了。当他们把这样的代码拿给我看的时候,那真叫个蛋疼啊,,,,,所以一定要有一个良好的编程风格。至于用什么样的风格,这个没有具体的规定,没有说哪种方式特别好。我一直参考的是林锐的《高质量C编程指南》我把它共享在了我的网盘上了,这里是下载地址:http://vdisk.weibo.com/s/KfoXD  。下面我们来看看STM32固件库的命名规则:

Naming conventions

  • PPPrefers to any peripheral acronym,for exampleADC.
  • System andsource/header file names are preceded by the prefix‘stm32f10x_’.
  • Constants used in onefile are defined within this file. A constant used in more than onefile is defined in a header file. All constants are written inupper case.
  • Registers areconsidered as constants. Their names are in upper case. In mostcases, the same acronyms as in the STM32F10x reference manualdocument are used.
  • Names of peripheral’sfunctions are preceded by the corresponding peripheral acronym inupper case followed by an underscore. The first letter in each wordis in upper case, for exampleUSART_SendData. Onlyone underscore is allowed in a function name to separate theperipheral acronym from the rest of the function name.
  • Functions used toinitialize the PPP peripheral according to parameters specified inPPP_InitTypeDef are namedPPP_Init, forexampleTIM_Init.
  • Functions used to resetthe PPP peripheral registers to their default values are namedPPP_DeInit, for exampleTIM_DeInit.
  • Functions used to fillthe PPP_InitTypeDef structure with the reset values of each memberare namedPPP_StructInit, for exampleUSART_StructInit.
  • Functions used toenable or disable the specified PPP peripheral are namedPPP_Cmd, for exampleUSART_Cmd.
  • Functions used toenable or disable an interrupt source of the specified PPPperipheral are namedPPP_ITConfig, for exampleRCC_ITConfig.
  • Functions used toenable or disable the DMA interface of the specified PPP peripheralare namedPPP_DMAConfig, for exampleTIM_DMAConfig.
  • Functions used toconfigure a peripheral function always end with the string‘Config’, for exampleGPIO_PinRemapConfig.
  • Functions used to checkwhether the specified PPP flag is set or reset are namedPPP_GetFlagStatus, for exampleI2C_GetFlagStatus.
  • Functions used to cleara PPP flag are namedPPP_ClearFlag, for exampleI2C_ClearFlag.
  • Functions used to checkwhether the specified PPP interrupt has occurred or not are namedPPP_GetITStatus, for exampleI2C_GetITStatus.
  • Functions used to cleara PPP interrupt pending bit are namedPPP_ClearITPendingBit, for exampleI2C_ClearITPendingBit.

  • RegistersstructurePeripheraldeclarationegistersbitsMappingformulaExampleof implementation等的命名规则具体见STM32固件库帮助文档中的命名规则。

    在这里顺便说一下STM32固件库代码的注释规则,它使用的DOXYGEN注释规则,本人平时码代码也用的是这个工具生成代码的HTML结构。至于下载地址及使用方法,我已经打包放在了我的微盘里了,这里是下载地址:http://vdisk.weibo.com/s/KfopH

    (1)      STM32固件库外设驱动程序:

    STM32学习(一)-------------------------2013年07月16日

    STM32F10xStandard Peripheral's Drivers里是STM32固件库提供的所有的外设驱动程序,平时写代码时主要也就是查看这些驱动程序。

    在这里我们来看一下其中关于STM32GPIO,在stm32f10x_gpio.c文件下,包含相关寄存器的宏定义和所有相关的函数。并且每个函数下都有该函数的介绍。例:

    void 

    GPIO_AFIODeInit (void)

     

    Deinitializes the Alternate Functions (remap, eventcontrol and EXTI configuration) registers to their default resetvalues.

     

    stm32f10x_gpio.h头文件中包含相关寄存器的宏定义及相关结构体,枚举及相关函数的声明。

    (2)      STM32固件库中的例子程序:

    固件库中对每个外设都提供了相关的例子程序及样板程序,这些程序对我们在做项目开发中是有很大的帮助的,我们可以从中找到许多相关问题的解答。

    而且固件库中说明了不同开发平台的移植方法,具体见:

    STM32学习(一)-------------------------2013年07月16日

    结束语:本人觉得还是官方提供的帮助文档所包含的信息量最大,其实好多问题都可以在相关的官方帮助文档中找到,可是因为都是英文的所以大部分的人都不想去读它,其实我想说的是,你可以不喜欢英语,但是最简单的你要有阅读英文文档的能力。我也是一位学生,刚刚入手的STM32,所以写的这些东西都只是我的个人想法,所以难免会有很多的错误。希望大家可以指出。不过我想我会坚持写下去的,我会在我的微博上和大家分享我学习STM32的经历。

                                                                                                                                             @Vampieyifeng

    2013716日星期二