keil中串口的虚拟调试信息在通过View-serial windows-#usart1/2/3/4/debug(printf)可以看到。当然也可以通过虚拟串口VSPD+串口调试助手在外部实现,方法如下:
虚拟 串口使用:步骤
1 下载虚拟串口软件,虚拟2个连接的串口COMA/COMB,这两个串口与PC机的真实物理串口没关系。两边的设置相同
2 打开串口通讯助手,将A分配给串口通讯助手,则B就分配给下面的COMx
2 在MDK中输入命令行或者将下面的做成debug.ini文件加载
MODE COMB baudrate, parity, databits, stopbits (注意:逗号必须是英文的逗号:settings PC's COM ports)
ASSIGN COMB < SnIN > SnOUT //COMB就是keil uvision的串口,根据单片机 的串口号来设置n(如果只有一个串口则n可省略)。
注意:这个在硬件仿真时不能用。
- The ASSIGN command cannot be used when debugging on target hardware.
- DIR VTREG列出可用的定义符号
The PC serial port (COM port) supports only up to 8 data bits. Therefore, it is impossible to generate more then 8 data bits, as it is possible on some microcontroller UART interfaces
2 printf的实现方法一(函数重定向):重写fputc ()函数,用单片机的一个串口实现:
2.1 在mdk->option中选择 Use MicroLIB
21.2 包含标准库头文件#include <stdio.h>,提前把单片机的串口程序调试好
2.3 重写fput函数
/*
* 函数名: fputc
* 描述 :重定向 c 库函数 printf 到 USART1
* 调用 :由 printf 调用
*/
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);
// while (!(USART1->SR & USART_FLAG_TXE));
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
return (ch);
}
2.4用printf进行调用。
MDK中软仿真下Debug (printf) Viewer
https://wenku.baidu.com/view/15f99562fc4ffe473268ab0a.html
工程中包含以下Simulation_debugPrinfView.c文件
/************************Simulation_debugPrinfView.c for M3*************************************/
#define DBG_PRINT 1
#if DBG_PRINT
#define Debug_Printf printf
#else
#define Debug_Printf
#endif
#include <string.h>
//#include <stdio.h>
typedef struct __FILE FILE; //与#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; /* Add whatever needed */ };
typedef struct __FILE FILE;
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
if (DEMCR & TRCENA)
{
while (ITM_Port32(0) == 0);
ITM_Port8(0) = ch;
}
return(ch);
}
三 利用SWO+硬件调试器实现
MDK的debug(printf)使用:
1 仿真器设置:Debug页面,选择仿真器为Cortex-M/R J-Link/J-Trace, 并点击仿真器选项边上的setting选项,打开具体的设置窗口。
在打开的窗口中,切换到Trace页面,选中Enable,并且设置Core Clock为14MHz,分频选项为Core Clock/16。详情如下: