您现在的位置: 主页 > MCU > 单片机技术应用 > STM32固件库中assert_param的作用 -
本文所属标签:
为本文创立个标签吧:

STM32固件库中assert_param的作用 -

来源: 网络用户发布,如有版权联系网管删除 2018-09-07 

[导读]在学习stm32库函数过程中,笔者遇到大量的assert_param语句。经查明,assert_param的作用就是用来判断传递给函数的参数是否是有效值。以下是从固件库中复制粘贴的:void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Pe

在学习STM32库函数过程中,笔者遇到大量的assert_param语句。经查明,assert_param的作用就是用来判断传递给函数的参数是否是有效值。

本文引用地址: http://www.21ic.com/app/mcu/201808/778994.htm

以下是从固件库中复制粘贴的:

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{

assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
RCC->APB2ENR |= RCC_APB2Periph;
}
else
{
RCC->APB2ENR &= ~RCC_APB2Periph;
}
}

笔者用keil中的鼠标右键“go to definition xxxxxx""

查看assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));语句中IS_RCC_APB2_PERIPH的定义,得到如下结果:


#define RCC_APB2Periph_AFIO((uint32_t)0x00000001)
#define RCC_APB2Periph_GPIOA((uint32_t)0x00000004)
#define RCC_APB2Periph_GPIOB((uint32_t)0x00000008)
#define RCC_APB2Periph_GPIOC((uint32_t)0x00000010)
#define RCC_APB2Periph_GPIOD((uint32_t)0x00000020)
#define RCC_APB2Periph_GPIOE((uint32_t)0x00000040)
#define RCC_APB2Periph_GPIOF((uint32_t)0x00000080)
#define RCC_APB2Periph_GPIOG((uint32_t)0x00000100)
#define RCC_APB2Periph_ADC1((uint32_t)0x00000200)
#define RCC_APB2Periph_ADC2((uint32_t)0x00000400)
#define RCC_APB2Periph_TIM1((uint32_t)0x00000800)
#define RCC_APB2Periph_SPI1((uint32_t)0x00001000)
#define RCC_APB2Periph_TIM8((uint32_t)0x00002000)
#define RCC_APB2Periph_USART1((uint32_t)0x00004000)
#define RCC_APB2Periph_ADC3((uint32_t)0x00008000)
#define RCC_APB2Periph_TIM15((uint32_t)0x00010000)
#define RCC_APB2Periph_TIM16((uint32_t)0x00020000)
#define RCC_APB2Periph_TIM17((uint32_t)0x00040000)
#define RCC_APB2Periph_TIM9((uint32_t)0x00080000)
#define RCC_APB2Periph_TIM10((uint32_t)0x00100000)
#define RCC_APB2Periph_TIM11((uint32_t)0x00200000)

#define IS_RCC_APB2_PERIPH(PERIPH) ((((PERIPH) & 0xFFC00002) == 0x00) && ((PERIPH) != 0x00))

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

以这个函数为例:

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph,

FunctionalState

NewState)的作用就是使能APB2外设时钟,而当我们调用这个函数的时候,所给它的参数必须是以上规定的几个数值中的一个,不可随意填一个未定义的值进去。assert_param()函数有效的解决了这个问题,它在函数运行之初,便判断工程师所给的值是否为这个函数的有效值,以达到纠错报错的功能。同时,当我们不知道这个函数该填入什么样的值的时候,就可以使用keil中提供的右键“go

to definition xxxx"查看assert_param()括号中的定义。




              查看评论 回复



嵌入式交流网主页 > MCU > 单片机技术应用 > STM32固件库中assert_param的作用 -
 

"STM32固件库中assert_param的作用 -"的相关文章

网站地图

围观()