【C语言】assert_param的意义与一个应用举例

用于判断一个初始化值,是否符合平台设置。举个例子,其他的类似的。

比如stm32中的时钟格式,时钟格式为12或者24,并且事先用宏定义12和24。

//头文件,默认定义宏

#define RTC_HOURFORMAT_24              0x00000000U
#define RTC_HOURFORMAT_12              0x00000040U 

//用于判断预设变量是否正确,正确返回1

#define IS_RTC_HOUR_FORMAT(FORMAT)        ((FORMAT) == RTC_HOURFORMAT_12) ||         \
                                                                                        ((FORMAT) == RTC_HOURFORMAT_24)

//实际中使用

assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat))

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

//如果是默认值则为1,就什么都不做。否则调用asser_failed函数

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

//assert函数是main.c里的一个自定义函数,提示example可以是用printf打印

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值