Introduce the method of using Keil's software simulation function to realize the 51-chip serial port debugging user program

In the MCU system, the serial port (UART, universal asynchronous transceiver interface) is a very important component. Usually, the serial port of the single-chip microcomputer is connected to the host computer through the RS232/RS485 level conversion chip to perform data exchange, parameter setting, network formation and connection of various external devices of the upper computer and the lower computer. RS232/RS485 serial interface bus has the characteristics of low cost, simple and reliable, easy to use, etc., and its long history, so the application is still very extensive; especially for the occasion that the amount of data is not very large, serial communication is still a good choice. , has a broad use prospects.

In the microcontroller programming, the serial port occupies a very important position. Traditional mode serial program debugging is often the use of a dedicated microcontroller hardware emulator. After writing the program, use the emulator to set breakpoints, observe the variables and the flow of the program, and gradually debug the program to correct the error. Using a hardware emulator is a really effective method, but it also has some drawbacks:

Many emulators can't do full hardware emulation, which will cause the simulation to be normal, but the actual operation will be wrong. There are also cases where the simulation cannot pass, but the actual operation is normal.

For some newer chips or surface mount chips, there is no suitable emulator or emulation header; either the hardware emulator is very expensive and not easy to buy.

Sometimes the emulation head is inconvenient to access due to the limitations of the internal structure of the device.

Some simulators are simple online simulation types, and there are many limitations in simulation. For example, the speed is not high, the real-time or stability is not good, and there are restrictions on the breakpoints, which makes the simulation less convenient.

Introduce the method of using Keil's software simulation function to realize the 51-chip serial port debugging user program

1. Preparation before commissioning

The following describes a method that uses Keil's software simulation function to implement the 51-chip serial port debugging user program. With this approach, no hardware emulators are needed, and even no user boards are needed. All that is needed is:

1 hardware. 1 ordinary computer (requires 2 standard serial ports) and 1 serial cable (both are female), the connection relationship is shown in Figure 1.

2 Serial port software can be dedicated debugging or upper and lower computer communication software, or general serial port software (such as serial port assistant, serial port debugging, etc.), which is mainly used to send and receive data. If you don't have the proper serial debugging software, you can use TurboCom, a free serial port tool written by the author. In addition to the same data transmission and reception functions as other software, it also has two useful functions, such as sending custom data frames and auto-answering (automatically returning corresponding data frames after receiving a specified data frame), which is especially suitable for Aging test. This gadget can be downloaded from the web.

2, basic debugging command introduction

This serial port debugging method mainly utilizes Keil's powerful software simulation function. In the new version (higher than 6.0) of Keil software, the software's simulation capabilities are enhanced, and more microcontroller functions can be simulated using software. Among these functions, one of the most important functions is to use the serial port of the computer to simulate the serial port of the microcontroller (this is different from the stimulus file method used by many softwares in simulation, which can communicate directly with other serial ports, which is more convenient and flexible) . First, we need to introduce two commands that need to be used during simulation: ASSIGN and MODE.

2.1 ASSIGN command

Bind the serial port of the MCU to the serial port of the computer. The basic usage is: ASSIGN channeloutreg

Where: channel represents the serial port of the computer, which can be COM1, COM2, COM3 or COM4; and inreg and outreg represent the serial port of the microcontroller. For ordinary single-chip microcomputers with only one serial port, namely SIN and SOUT; for single-chip microcomputers with two or more serial ports, namely SnIN and SnOUT (n=0, 1, ... is the serial port number of the single-chip microcomputer).

Figure 1 Serial port connection diagram

E.g:

ASSIGN COM1SOUT

Bind the computer's serial port 1 to the serial port of the microcontroller (for a microcontroller with only one serial port).

ASSIGN COM2S0OUT

Bind the serial port 2 of the computer to the serial port 0 of the MCU (for the MCU with multiple serial ports, pay attention to the position of the serial port number).

It should be noted that the parentheses of the parameters cannot be omitted, while the outreg is not bracketed.

2.2 MODE command

Set the parameters of the serial port of the bound computer. The basic usage is:

MODE COMx baudrate, parity, databits, stopbits

Where: COMx(x = 1,2,...) represents the serial port number of the computer; baudrate represents the baud rate of the serial port; parity represents the verification mode; databits represents the data bit length; and stopbits represents the length of the stop bit.

E.g:

MODE COM1 9600, n, 8, 1

Set serial port 1. The baud rate is 9 600, no parity, 8 bits of data, 1 stop bit.

MODE COM2 19200, 1, 8, 1

Set serial port 2. The baud rate is 19 200, odd parity, 8 bits of data, 1 stop bit.

Using the above two commands, you can simulate the serial port of the computer into the serial port of the microcontroller. During software simulation, all data sent to the serial port of the bound computer will be forwarded to the serial port of the Keil analog microcontroller. The user program can receive the data through the interrupt handler or query mode; similarly, the program is sent to the microcontroller program. The data on the serial port of the MCU will also be sent out through the serial port of the bound computer, which can be received by other software. With this feature, it is convenient to simulate and debug the serial port part of the microcontroller. It should be noted that these two commands need to be used together.

2.3 Simulation steps

First, connect the two serial ports of the computer with a serial cable (or two serial ports on two computers). One of these two serial ports is used to simulate the serial port of the microcontroller, and the other is used by the debugger. This is assigned by the user himself and has no special requirements.

Second, write the user program and compile it.

Then, set the relevant parameters of the project file (Project), as shown in Figure 2 and Figure 3. Mainly choose the software simulation mode (Use Simulator) and crystal parameters.

Figure 2 Simulation parameter settings

In order to not need to enter the serial port parameter setting command every time after entering the simulation state, an initialization file can be created. The initialization file is a plain text file. The content is the command required for simulation. Enter one line in order. As shown in Figure 2, an initialization file for debug.ini is created. Thus, each time you enter the emulation debug state, Keil will automatically load the contents of debug.ini for initialization.

In order to correctly simulate the serial port, in the software simulation debugging, in the properties of the user's Keil project file, you also need to set the actual crystal frequency. This parameter is very important, directly affects the baud rate of the communication, and can be set according to the parameters actually used. Note that the unit of this parameter is MHz.

Once the parameters are set, the simulation can be performed. Click the icon of the toolbar to enter the Debug state in the new window. Enter the command described above in the command text box (usually in the lower left corner) in the Output window window. For example, set the serial port 1 of the PC to the serial port of the MCU:

Mode com1 9600,0,8,1

Assign com1 Sout

Then set a breakpoint, usually in a key place or in a place associated with the serial port. Click the icon to run the user program to make the user program work (otherwise it will not receive serial data). Then use the serial debugging software or user debugging software to send communication commands or data packets to see if the user program enters the breakpoint and whether the relevant variables are correct. You can also intentionally send a packet with erroneous data to see if the exception handling part of the user program is normal. Once you find an error in the program, you can stop the simulation debugging immediately, modify the code immediately, and then repeat the above steps to simulate again. Because there is no need to connect to the user target board or download the code to the user board, the speed is very high. These steps are basically the same as using a hardware emulator, except that software emulation is now used.

It should be noted that the actual baud rate of the serial port of the MCU is specified by the MODE command during simulation. The parameters such as TMOD and SCON in the MCU program do not affect the serial port emulation state (that is, these parameters do not affect the baud rate of the simulation. Even if they are wrong). However, the interrupt enable bits (such as ES, EA, etc.) still work. If the ES or EA is disabled, then the serial port interrupt will not be entered.

Because this method uses the serial port of the computer to simulate the serial port of the single-chip microcomputer, and the simulation uses the Keil software to convert the data on the serial port, instead of directly forwarding the data, in actual simulation, the processing speed will be slightly lower than the actual single-chip operation. a little. For example, 1 s can only send/receive 10 data frames in the simulation state, but it can receive/send 50 data frames in 1 s when running on the microcontroller hardware. This is related to the speed of the computer used, but it has no effect on the simulation.

For a multi-serial MCU, in theory, you can bind multiple serial ports at once, as long as the computer has enough serial ports. Basically, the number of serial ports that need to occupy the computer using this method is twice that of the serial port bound to the microcontroller. A serial port is occupied by Keil and used to simulate the serial port of the MCU. Another serial port is occupied by the computer and used to send and receive data to the serial port of the MCU.

3, summary

The methods presented here are suitable for both C51 and assembly language. Its biggest advantage is that it is simple, convenient, easy to use, does not require any circuit, and has no special requirements; even the program of the serial port can be written and debugged before the hardware circuit is made. I have been using this method for a long time, and it turns out that this method is really effective. In fact, for 51 single-chip microcomputers, Keil's simulation function is too powerful. As long as you fully grasp its characteristics and be able to use it skillfully, you can solve most problems in your work. A lot of work can be done using software emulation, no hardware emulator is needed at all; only some new external device timings, interface debugging, may need to use a hardware emulator. At present, there are very few reference books on Keil software simulation. Some of them are still used in the old version, but it doesn't matter. Keil's help files are written in very detailed and clear, and will be used as long as they are carefully understood. After using the proficiency, you will find that Keil is quite powerful.

For serial programming, 51 single-chip microcomputer has Keil, a powerful development software, which brings us great convenience; while in the development of other single-chip software, there is no such powerful development tool and convenient debugging means. Here is a workaround, that is, you can first write and debug the serial port program in Keil, and then transplant the program to other microcontroller platforms (I used this method in the development of PIC18 microcontroller, and received good results. Of course, this means that assembly language is not portable when developing a microcontroller program using C language. As for how to reduce the workload of program migration, the program has better versatility, can be smoothly transplanted to other microcontroller platforms at a minimum cost, is also a problem worthy of discussion.

Air Bar

Air Bar,Disposable Vape Air Bar,Air Bar Disposable Vape Pod,Air Bar Light Edition

Shenzhen Zpal Technology Co.,Ltd , https://www.zpal-vape.com