在Keil IDE
中,如何用printf
向Debug(printf) Viewer
窗口打印结果?如下图所示。
添加一个.c
文件,代码如下。
#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 0X01000000int fputc(int ch, FILE *f){ if(DEMCR & TRCENA) { while(ITM_PORT32(0) == 0); ITM_PORT8(0) = ch; } return ch;}
完成以上2个步骤,就可以在代码中使用printf
函数向Debug(printf) Viewer
窗口输出了。注意,需要开启仿真调试,并调出Debug(printf) Viewer
窗口。
联系客服