希望对KEIL开发的小白的有用,KEIL开发如果高效正确调用Printf 打印输出调试信息。
需要添加红色字体代码
#include <stdio.h>
#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n) (*((volatile unsigned short *)(0xE0000000+4*n)))
#define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n)))
#define DEMCR (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA 0x01000000
struct __FILE{ int handle;};
FILE _stdout;
FILE _stdin;
int fputc(int ch,FILE *f)
{
if(DEMCR & TRCENA)
{
while(ITM_Port32(0) == 0);
ITM_Port8(0)=ch;
}
return ch;
}
设置一下
int main(void)
{
delay_init(); //延时函数初始化
printf("\n=======666=22======\n");
while(1)
{
printf("\n=======调试信息=====\n");
delay_ms(1000);
}
}
编译一下,下载程序到单片机是,开启调试,调出Debug(printf)Viewer窗口
测试ok