51 microcontroller uses the serial port to view the data of program execution

51 microcontroller uses the serial port to view the data of program execution

1 Overview

This article introduces the use of the serial port to output program execution data to assist us in debugging the program and improve the efficiency of locating code problems.

2.Hardware circuit principle

Insert image description here

3. Serial port assistant to view program data

The methods of outputting serial port data are divided into CPU query method and interrupt method. They each have their own advantages and disadvantages, and they can be chosen flexibly in actual projects.
CPU query method: The serial port sends and receives data all the time occupying CPU resources.
Interrupt mode: Serial port sending and receiving data triggers an interrupt, and CPU resources will be used only when an interrupt occurs. However, if the interrupt program processing time is long, it will affect the execution of the normal program.

3.1. View serial port data in interrupt mode

1.Procedure

The total interrupt and serial port interrupt are set to be turned on in UART_initthe interrupt initialization function. When we enter information in the STC-ISP software, the UART_Rinterrupt function will be triggered, and the 串口助手input information can be seen in .

/*********************************************************************************************
程序名:    UART串口中断方式程序实例
编写人:    bruce 
编写时间:  2023年11月27日
硬件支持:  STC12C2052AD 外部12MHZ晶振
接口说明:  连接串口ISP下载线  
修改日志:  
  NO.1-								
/*********************************************************************************************
说明:
用Windows系统中的“超级终端”软件,将串口端设置 [ 4800,8,无,1,无 ]
或采用STC-ISP软件中的串口助手功能,将串口端设置 [ 4800,8,无,1,无 ]

向串口发送数据,单片机将数据发还给PC端并显示。

/*********************************************************************************************/

#include<STC12C2052AD.H> //51头文件

/*********************************************************************************************
函数名:UART串口初始化函数
调  用:UART_init();
参  数:无
返回值:无
结  果:启动UART串口接收中断,允许串口接收,启动T/C1产生波特率(占用)
备  注:振荡晶体为12MHz,PC串口端设置 [ 4800,8,无,1,无 ]
/**********************************************************************************************/
void UART_init (void){
    
    
	//打开和关闭中断,打开时使用中断,关闭时使用CPU查询方式。
	EA = 1; //允许总中断(如不使用中断,可用//屏蔽)
	ES = 1; //允许UART串口的中断

	TMOD = 0x20;	//定时器T/C1工作方式2
	SCON = 0x50;	//串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收)
	TH1 = 0xF3;	//定时器初值高8位设置
	TL1 = 0xF3;	//定时器初值低8位设置
	PCON = 0x80;	//波特率倍频(屏蔽本句波特率为2400)
	TR1 = 1;	//定时器启动    
}

/*********************************************************************************************
函数名:UART串口接收中断处理函数
调  用:[SBUF收到数据后中断处理]
参  数:无
返回值:无
结  果:UART串口接收到数据时产生中断,用户对数据进行处理(并发送回去)
备  注:过长的处理程序会影响后面数据的接收
/**********************************************************************************************/
void UART_R (void) interrupt 4  using 1{
    
     //切换寄存器组到1
	unsigned char UART_data; //定义串口接收数据变量
	RI = 0;			//令接收中断标志位为0(软件清零)
	UART_data = SBUF;	//将接收到的数据送入变量 UART_data
		
	SBUF = UART_data;	//将接收的数据发送回去(删除//即生效)
	while(TI == 0);	//检查发送中断标志位
	TI = 0;		//令发送中断标志位为0(软件清零)
}
/*********************************************************************************************
函数名:主函数
调  用:无
参  数:无
返回值:无
结  果:程序开始处,无限循环
备  注:
/**********************************************************************************************/
void main (void){
    
    
	UART_init();
	while(1){
    
    
	}
}
2. View data with serial port assistant

Connect the microcontroller to the computer via USB, open STC-ISPthe software, and burn the above program to the microcontroller.
Note on burning program:

1. Be sure to choose an external crystal oscillator when burning the program, because our circuit has an external 12MHZ crystal oscillator connected to the microcontroller.
2. After the programming is completed, turn off and on the 20-pin VCC and 5V power supply of the microcontroller, thus switching to an external crystal oscillator.
Pay attention to the above two points, otherwise the data sent and received in the serial port assistant will be inconsistent.

Click 串口助手to set the steps as follows

  • 1. Set the HEX mode in the receive buffer area
  • 2. Set the HEX mode in the sending buffer area
  • 3. The serial port selection is the same as the serial port number when burning the program.
  • 4. The baud rate set in the program is 4800, so 4800 must be set here as well.
  • 5. Select None for check digit
  • 6.Stop bit selection 1
  • 7. Open the serial port
  • 8. Enter the content in the sending area and click to send the data
  • 9. The sent data can be viewed in the receiving area

Insert image description here

3.2.CPU query mode to view serial port data

1.Procedure

Remove the interrupt and serial port interrupt codes in UART_initthe interrupt initialization function. When we input information in the STC-ISP software, the CPU will process it in real time, and you 串口助手can see the input information in the STC-ISP software.

The circuit schematic diagram and STC-ISP software operation method are the same as above.

/*********************************************************************************************
程序名:    UART串口中断方式程序实例
编写人:    bruce 
编写时间:  2023年11月27日
硬件支持:  STC12C2052AD 外部12MHZ晶振
接口说明:  连接串口ISP下载线  
修改日志:  
  NO.1-								
/*********************************************************************************************
说明:
用Windows系统中的“超级终端”软件,将串口端设置 [ 4800,8,无,1,无 ]
或采用STC-ISP软件中的串口助手功能,将串口端设置 [ 4800,8,无,1,无 ]

向串口发送数据,单片机将数据发还给PC端并显示。

/*********************************************************************************************/

#include<STC12C2052AD.H> //51头文件

/*********************************************************************************************
函数名:UART串口初始化函数
调  用:UART_init();
参  数:无
返回值:无
结  果:启动UART串口接收中断,允许串口接收,启动T/C1产生波特率(占用)
备  注:振荡晶体为12MHz,PC串口端设置 [ 4800,8,无,1,无 ]
/**********************************************************************************************/
void UART_init (void){
    
    
	//打开和关闭中断,打开时使用中断,关闭时使用CPU查询方式。
	//EA = 1; //允许总中断(如不使用中断,可用//屏蔽)
	//ES = 1; //允许UART串口的中断

	TMOD = 0x20;	//定时器T/C1工作方式2
	SCON = 0x50;	//串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收)
	TH1 = 0xF3;	//定时器初值高8位设置
	TL1 = 0xF3;	//定时器初值低8位设置
	PCON = 0x80;	//波特率倍频(屏蔽本句波特率为2400)
	TR1 = 1;	//定时器启动    
}

/*********************************************************************************************
函数名:主函数
调  用:无
参  数:无
返回值:无
结  果:程序开始处,无限循环
备  注:
/**********************************************************************************************/
void main (void){
    
    
	unsigned char UART_data; //定义串口接收数据变量
	UART_init();
	while(1){
    
    
		if (RI == 1){
    
    		//接收中断标志位为1时
			UART_data = SBUF;	//接收数据 SBUF 为单片机的接收发送缓冲寄存器
			RI = 0;			//令接收中断标志位为0(软件清零)

			SBUF = UART_data;	//将接收的数据发送回去(删除//即生效)
			while(TI == 0);	//检查发送中断标志位,为1表示在发送数据,0为发送结束。
			TI = 0;		//令发送中断标志位为0(软件清零)
		}
	}
}

3.3.Send letters and Chinese characters

The above two examples send and receive in hexadecimal (HEX) and cannot display letters and Chinese characters. The following describes how to display letters and Chinese characters.

1.Procedure
  • Added #include <string.h>a header file at the beginning of the code to display Chinese characters and letters
  • UART_TThe function is used to receive input data and then 串口助手output it in
/*********************************************************************************************
程序名:    UART串口中断方式程序实例
编写人:    bruce 
编写时间:  2023年11月27日
硬件支持:  STC12C2052AD 外部12MHZ晶振
接口说明:  连接串口ISP下载线  
修改日志:  
  NO.1-								
/*********************************************************************************************
说明:
用Windows系统中的“超级终端”软件,将串口端设置 [ 4800,8,无,1,无 ]
或采用STC-ISP软件中的串口助手功能,将串口端设置 [ 4800,8,无,1,无 ]

向串口发送数据,单片机将数据发还给PC端并显示。

/*********************************************************************************************/

#include<STC12C2052AD.H> //51头文件
#include <string.h>

/*********************************************************************************************
函数名:UART串口初始化函数
调  用:UART_init();
参  数:无
返回值:无
结  果:启动UART串口接收中断,允许串口接收,启动T/C1产生波特率(占用)
备  注:振荡晶体为12MHz,PC串口端设置 [ 4800,8,无,1,无 ]
/**********************************************************************************************/
void UART_init (void){
    
    
	//打开和关闭中断,打开时使用中断,关闭时使用CPU查询方式。
	//EA = 1; //允许总中断(如不使用中断,可用//屏蔽)
	//ES = 1; //允许UART串口的中断

	TMOD = 0x20;	//定时器T/C1工作方式2
	SCON = 0x50;	//串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收)
	TH1 = 0xF3;	//定时器初值高8位设置
	TL1 = 0xF3;	//定时器初值低8位设置
	PCON = 0x80;	//波特率倍频(屏蔽本句波特率为2400)
	TR1 = 1;	//定时器启动    
}

/*********************************************************************************************
函数名:UART串口发送函数
调  用:UART_T (?);
参  数:需要UART串口发送的数据(8位/1字节)
返回值:无 
结  果:将参数中的数据发送给UART串口,确认发送完成后退出
备  注:
/**********************************************************************************************/
void UART_T (unsigned char UART_data){
    
     //定义串口发送数据变量
	SBUF = UART_data;	//将接收的数据发送回去
	while(TI == 0);		//检查发送中断标志位
	TI = 0;			//令发送中断标志位为0(软件清零)
}

/*********************************************************************************************
函数名:UART串口发送字符串函数
调  用:UART_TC (?);
参  数:需要UART串口发送的数据(8位/1字节)
返回值:无 
结  果:向串口发送一个字符串,长度不限。
备  注:例:UART_TC("d9887321$"); 此函数需要#include <string.h>头文件支持。
/**********************************************************************************************/
void UART_TC (unsigned char *str){
    
    
	while(*str != '\0'){
    
    
		UART_T(*str);
		*str++;
	}
	*str = 0;
}
/*********************************************************************************************
函数名:主函数
调  用:无
参  数:无
返回值:无
结  果:程序开始处,无限循环
备  注:
/**********************************************************************************************/
void main (void){
    
    
	unsigned char UART_data;
	UART_init();
	while(1){
    
    
		UART_TC("value:");
		UART_T(0X30+5);
	}
}
2. Serial port assistant

When the serial port assistant views character type data, set the receiving area to文本模式
Insert image description here

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/134663690
Recommended