全新论坛MCU智学网上线,欢迎访问新论坛!稀缺资源、技术干货、参考设计、原厂资料尽在MCU智学网
更新自动建库工具PCB Footprint Expert 2023.13 Pro / Library Expert 破解版

嵌入式c语言调试开关

[复制链接]
4056 1

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
在调试程序时,经常会用到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

举报

回复

1 个评论

yedu8***  新手上路  发表于 2012-3-9 10:16:14  | 显示全部楼层
宏定义嘛?
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

打开支付宝扫一扫,最高立得1212元红包
搜索

图文热点

更多

社区学堂

更多

客服中心

QQ:187196467 服务时间:周一至周日 8:30-20:30

关注我们

关于我们
关于我们
友情链接
联系我们
帮助中心
网友中心
购买须知
支付方式
服务支持
资源下载
售后服务
定制流程
关注我们
官方微博
官方空间
官方微信
快速回复 返回顶部 返回列表