欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2297|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

嵌入式c語言調(diào)試開關(guān)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:105323 發(fā)表于 2016-2-22 23:07 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 51黑黑黑 于 2016-2-22 23:09 編輯

調(diào)試程序時,經(jīng)常會用到assert和printf之類的函數(shù),我最近做的這個工程里就有幾百個assert,在你自認為程序已經(jīng)沒有bug的時候,就要除去這些調(diào)試代碼,應(yīng)為系統(tǒng)在正常運行時這些用于調(diào)試的信息是無用的,而且會占用時間和空間。怎么刪除呢,俺以前都是用笨方法,一個一個注釋,能用注釋也是經(jīng)過改進的方法,俺最早都是刪掉之后出了問題再重新寫的,但是這次幾百個一個一個刪除的話可是要了俺的小命了,一首mp3聽完,還不到一百個。以前看過st的函數(shù)庫,老外的代碼就是規(guī)范,俺現(xiàn)在的代碼好多都是在st和ti那里照搬的,呵呵。
下面給出最簡單的一種方法:
  1. #define DEBUG
  2. #ifdef DEBUG
  3. #define PRINTF(x) printf x
  4. #else
  5. #define PRINTF(x)         ((void)0)
  6. #endif
復(fù)制代碼
使用時,PRINTF(( "Hello World!\n\r" ));
注意這里是兩個括號,一個會報錯的
不使用時,直接將"#define DEBUG"屏蔽掉
另外一個調(diào)試時常用的方法是assert,還是在一個頭文件里,這里用的是STM32函數(shù)庫的例子
  1. #ifdef DEBUG 1
  2. /*******************************************************************************
  3. * Macro Name : assert_param
  4. * Description : The assert_param macro is used for function's parameters check.
  5. * It is used only if the library is compiled in DEBUG mode.
  6. * Input : - expr: If expr is false, it calls assert_failed function
  7. * which reports the name of the source file and the source
  8. * line number of the call that failed.
  9. * If expr is true, it returns no value.
  10. * Return : None
  11. *******************************************************************************/

  12. #define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LINE__))

  13. /* Exported functions ------------------------------------------------------- */

  14. void assert_failed(u8* file, u32 line);
  15. #else
  16. #define assert_param(expr) ((void)0)
  17. #endif/* DEBUG */

  18. //assert_failed此函數(shù)要自己定義
  19. #ifdef DEBUG
  20. /*******************************************************************************
  21. * Function Name : assert_failed
  22. * Description : Reports the name of the source file and the source line number
  23. * where the assert_param error has occurred.
  24. * Input : - file: pointer to the source file name
  25. * - line: assert_param error line source number
  26. * Output : None
  27. * Return : None
  28. *******************************************************************************/
  29. void assert_failed(u8* file, u32 line)
  30. {

  31. /* User can add his own implementation to report the file name and line number,
  32. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  33. /* Infinite loop */
  34.     while (1){
  35.     }

  36. }
  37. #endif
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表