打印
[牛人杂谈]

新唐M051头文件的艺术

[复制链接]
1650|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
好多人不知道怎么学编程,怎么学C语言,其实学习这些大牛厂商的头文件和库函数的风格,就可以了

/** Define GPIO Pin Data Input/Output. It could be used to control each I/O pin by pin address mapping.
*  Example 1:
*
*      P00 = 1;
*
*  It is used to set P0.0 to high;
*
*  Example 2:
*
*      if (P00)
*          P00 = 0;
*
*  If P0.0 pin status is high, then set P0.0 data output to low.
*/
#define GPIO_PIN_ADDR(port, pin)    (*((volatile uint32_t *)((GPIO_PIN_DATA_BASE+(0x20*(port))) + ((pin)<<2))))
#define P00             GPIO_PIN_ADDR(0, 0) /*!< Specify P00 Pin Data Input/Output */
#define P01             GPIO_PIN_ADDR(0, 1) /*!< Specify P01 Pin Data Input/Output */
#define P02             GPIO_PIN_ADDR(0, 2) /*!< Specify P02 Pin Data Input/Output */
#define P03             GPIO_PIN_ADDR(0, 3) /*!< Specify P03 Pin Data Input/Output */
#define P04             GPIO_PIN_ADDR(0, 4) /*!< Specify P04 Pin Data Input/Output */
#define P05             GPIO_PIN_ADDR(0, 5) /*!< Specify P05 Pin Data Input/Output */
#define P06             GPIO_PIN_ADDR(0, 6) /*!< Specify P06 Pin Data Input/Output */
#define P07             GPIO_PIN_ADDR(0, 7) /*!< Specify P07 Pin Data Input/Output */
#define P10             GPIO_PIN_ADDR(1, 0) /*!< Specify P10 Pin Data Input/Output */
#define P11             GPIO_PIN_ADDR(1, 1) /*!< Specify P11 Pin Data Input/Output */

---------------------------------------------------------------------------------------------------------------------
我们看到这注释也是很简单易懂的,先告诉你怎么用,然后再给您看具体这个是怎么实现的。
沙发
玛尼玛尼哄|  楼主 | 2016-5-13 22:35 | 只看该作者
#define GPIO_PIN_ADDR(port, pin)    (*((volatile uint32_t *)((GPIO_PIN_DATA_BASE+(0x20*(port))) + ((pin)<<2))))
#define P00             GPIO_PIN_ADDR(0, 0) /*!< Specify P00 Pin Data Input/Output */

#define P10             GPIO_PIN_ADDR(1, 0) /*!< Specify P10 Pin Data Input/Output */
定义一个宏,然后后面再定义新的宏的时候就可以直接利用前面定义好的宏了,方便好用,
这里第一条就是通过端口和管脚的位置号来使用端口对应的管脚,我们看到每个端口有8个管脚,通过这么简单的宏处理后,我们再使用就可以直接用Pxy的形式来使用这个管脚了。这样就跟我们习惯的51单片机一样的方便了。

使用特权

评论回复
板凳
玛尼玛尼哄|  楼主 | 2016-5-13 22:46 | 只看该作者
/** @addtogroup M051_GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions
  @{
*/

/**
* @brief       Clear GPIO Pin Interrupt Flag
*
* @param[in]   port        GPIO port. It could be P0, P1, P2, P3 or P4.
* @param[in]   u32PinMask  The single or multiple pins of specified GPIO port.
*                          It could be BIT0 ~ BIT7.
*
* @return      None
*
* @Details     Clear the interrupt status of specified GPIO pin.
*/
#define GPIO_CLR_INT_FLAG(port, u32PinMask)         ((port)->ISRC = (u32PinMask))

我们再来看这个,这个是用来清零GPIO管脚的中断标志位的。
我们看到后面的通过近期操作的也是很简单的,但是你能看出来是神马意思吗?你不能,而通过宏替换的方式,我们可以替换成易于理解的句子,而通过参数来决定是哪个端口的哪个位置。如果你不清楚这个端口和管脚号是什么形式的,就看说明,说明里讲端口是P0、P1等,管脚是BIT0,BIT1等,我们看到实际上这个BIT0也是一个宏替换。
那么这个在哪儿呢,既然不在这个文件里,肯定就是在这个芯片的头文件里了,另外不得不说的是这些库函数的头文件中引用到的寄存器都是在芯片的头文件里的。

使用特权

评论回复
地板
玛尼玛尼哄|  楼主 | 2016-5-13 22:49 | 只看该作者
本帖最后由 玛尼玛尼哄 于 2016-5-13 22:51 编辑

/**
* @brief       Disable Pin De-bounce Function
*
* @param[in]   port        GPIO port. It could be P0, P1, P2, P3 or P4.
* @param[in]   u32PinMask  The single or multiple pins of specified GPIO port.
*                          It could be BIT0 ~ BIT7.
*
* @return      None
*
* @Details     Disable the interrupt de-bounce function of specified GPIO pin.
*/
#define GPIO_DISABLE_DEBOUNCE(port, u32PinMask)     ((port)->DBEN &= ~(u32PinMask))


/**
* @brief       Enable Pin De-bounce Function
*
* @param[in]   port        GPIO port. It could be P0, P1, P2, P3 or P4.
* @param[in]   u32PinMask  The single or multiple pins of specified GPIO port.
*                          It could be BIT0 ~ BIT7.
* @return      None
*
* @details     Enable the interrupt de-bounce function of specified GPIO pin.
*/
#define GPIO_ENABLE_DEBOUNCE(port, u32PinMask)      ((port)->DBEN |= (u32PinMask))

启用指定的GPIO引脚的中断功能去抖。

使用特权

评论回复
5
玛尼玛尼哄|  楼主 | 2016-5-13 22:55 | 只看该作者
/**
* @brief       Get GPIO Pin Interrupt Flag
*
* @param[in]   port        GPIO port. It could be P0, P1, P2, P3 or P4.
* @param[in]   u32PinMask  The single or multiple pins of specified GPIO port.
*                          It could be BIT0 ~ BIT7.
*
* @retval      0           No interrupt at specified GPIO pin
* @retval      1           The specified GPIO pin generate an interrupt
*
* @details     Get the interrupt status of specified GPIO pin.
*/
#define GPIO_GET_INT_FLAG(port, u32PinMask)     ((port)->ISRC & (u32PinMask))

获取指定端口的中断标志状态,带有值的宏,如果是0,就是没有中断发生,如果是1就是发生了中断。

使用特权

评论回复
6
玛尼玛尼哄|  楼主 | 2016-5-13 23:15 | 只看该作者


/**
* @brief       Get GPIO Port IN Data
*
* @param[in]   port        GPIO port. It could be P0, P1, P2, P3 or P4.
*
* @retval      The specified port data
*
* @details     Get the PIN register of specified GPIO port.
*/
#define GPIO_GET_IN_DATA(port)  ((port)->PIN)


这个比较特别,就一个参数,也就是获取端口的数据,那么port好理解,PIN是什么呢?

这就是,就是一个简单的变量,可以获取该端口的整个数值。

使用特权

评论回复
7
598330983| | 2016-5-15 07:34 | 只看该作者
好多厂家都是这么写,

使用特权

评论回复
8
玛尼玛尼哄|  楼主 | 2016-5-24 09:35 | 只看该作者
这么写有个好处统一,兼容性好。

使用特权

评论回复
9
yiyigirl2014| | 2016-5-24 22:52 | 只看该作者
这是大多数都是这么写的,基本上是个规范了

使用特权

评论回复
10
killer2014| | 2016-5-25 10:34 | 只看该作者
规范是无法兼容所有人的胃口的, 所以, 大家在使用厂家的BSP包或者是文件的时候, 尽量遵守厂家的风格, 但是如果自己增加代码, 可以用自己的风格。

使用特权

评论回复
11
orangebanana| | 2016-5-25 14:37 | 只看该作者
所有都朝着人性化方向发展,这样更容易学习

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

158

主题

3008

帖子

2

粉丝