stm32中assert_param的用法說明
首先是要知道條件判斷語句
這個運(yùn)算符分成三部分:(條件)?(條件成立執(zhí)行部分):(條件不成立執(zhí)行部分)就這么簡單
例如:a=(x>y?x:y);當(dāng)x>y為真時,a=x,當(dāng)x>y為假(即y>x)時,a=y。
不少人問在ST官方的STM32的庫函數(shù)里有很多assert_param是什么作用
比如下面的
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_IT(ADC_IT));
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
這樣的函數(shù),幾乎是帶參數(shù)的函數(shù)前面都有調(diào)用assert_param
實(shí)際這是個調(diào)試函數(shù),當(dāng)你在調(diào)試程序時打開DEBUG參數(shù)assert_param才起作用。
assert_param是反映參數(shù)你在調(diào)用庫函數(shù)傳遞的參數(shù)是錯誤的。
assert_param的原型定義在stm32f10x_conf.h 文件里
定義如下:
#ifdefDEBUG
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LINE__))
void assert_failed(u8* file, u32 line);
#else
#define assert_param(expr) ((void)0)
#endif
#endif
可以看到assert_param實(shí)際在DEBUG打開時就是assert_failed,關(guān)閉DEBUG時是空函數(shù)
assert_failed函數(shù)如下
#ifdefDEBUG
void assert_failed(u8* file, u32 line)
{
//用戶可以在這里添加錯誤信息:比如打印出出錯的文件名和行號
while (1)
{
}
}
#endif