欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
嵌入式c語言調試開關
[打印本頁]
作者:
51黑黑黑
時間:
2016-2-22 23:07
標題:
嵌入式c語言調試開關
本帖最后由 51黑黑黑 于 2016-2-22 23:09 編輯
調試程序時,經常會用到assert和printf之類的函數,我最近做的這個工程里就有幾百個assert,在你自認為程序已經沒有bug的時候,就要除去這些調試代碼,應為系統在正常運行時這些用于調試的信息是無用的,而且會占用時間和空間。怎么刪除呢,俺以前都是用笨方法,一個一個注釋,能用注釋也是經過改進的方法,俺最早都是刪掉之后出了問題再重新寫的,但是這次幾百個一個一個刪除的話可是要了俺的小命了,一首mp3聽完,還不到一百個。以前看過st的函數庫,老外的代碼就是規范,俺現在的代碼好多都是在st和ti那里照搬的,呵呵。
下面給出最簡單的一種方法:
#define DEBUG
#ifdef DEBUG
#define PRINTF(x) printf x
#else
#define PRINTF(x) ((void)0)
#endif
復制代碼
使用時,PRINTF(( "Hello World!\n\r" ));
注意這里是兩個括號,一個會報錯的
不使用時,直接將"#define DEBUG"屏蔽掉
另外一個調試時常用的方法是assert,還是在一個頭文件里,這里用的是STM32函數庫的例子
#ifdef DEBUG 1
/*******************************************************************************
* Macro Name : assert_param
* Description : The assert_param macro is used for function's parameters check.
* It is used only if the library is compiled in DEBUG mode.
* Input : - expr: If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* Return : None
*******************************************************************************/
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(u8* file, u32 line);
#else
#define assert_param(expr) ((void)0)
#endif/* DEBUG */
//assert_failed此函數要自己定義
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 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
復制代碼
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1