First, let's look at a program:
ittle 'Input.asm' ;Title
This is a simple input experiment
include
COUNT EQU 20H; define the register name 20H as COUNT
ORG 0; The program area will be loaded from 0000H
GOTO START
ORG 4 ;Interrupt entry
GOTO INTEN ; Jump to INTEN when an interrupt occurs
ORG 10 ;START will start from line 10
START
…
MAIN
…
GOTO MAIN
INTSEVER
…
RETFIE ;Interrupt return
END; Program end pseudo instruction, can not be missing!
In fact, the assembler does not have a fixed format, but its parts are arranged in a certain position, usually in the following order:
①Program title: defined by the Title pseudo-instruction, can be defaulted;
②Program comments: Please note that you must use a ";" sign before the program description and comment part, otherwise a compilation error will occur. The program description and comment part can be omitted;
③ The called .inc file: Use the include pseudo-instruction to call. Usually the .inc file corresponds to the microcontroller model, including the definition of some special registers and other contents. The .inc files of various models of PIC microcontrollers can be found in the MPLAB directory, and there is no need to write them yourself. If the called .inc file is lost, a compilation error will occur during compilation;
④General register definition: Defined by EQU pseudo-instruction, the format is "register name EQU register label", and the register name should be written in top space. This is the name given by the programmer to the general register used, corresponding to an actual general register address;
⑤Macro definition: No macro is used in the above program, default;
⑥Program initialization: START part;
⑦Main program: MAIN part;
⑧ Subroutines: such as the INTSEVER part of this program. Sometimes subroutines are placed before the initialization program, such as some table lookup programs;
⑨Program end: END statement, cannot be omitted.
The above parts constitute a complete PIC assembler program. I think the following points should be paid attention to during the program writing process:
1. In programming, we must first avoid using direct addresses to operate registers. Using direct addresses to operate registers will greatly reduce the readability of the program. First, it is easy to confuse with data, and second, it is not easy to change. Take the above program as an example. The direct address of the register "COUNT" is 20H. If during debugging, I suddenly find that the other registers in the subroutine called to COUNT are all in BANK1. It is very inconvenient for me to frequently switch banks to call it, so I have to change its address to 90H. Now I just need to change the definition statement to "COUNT EQU 90H". If I don't define it this way, I can only look for 20H in the program and change it to 90H one by one...
2. Pay attention to the readability of subroutine labels. Although these labels are no different after being compiled (of course, the addresses are still different), they are just marks used by programmers to identify (for example: main is usually used as the main loop label of the program, and start is often used as the label for program initialization. It can be said that the naming of labels has little impact on the compilation and operation of the program. If your main label is changed to loop, the compilation result is the same), but their rationality will affect the debugging of the program. Generally speaking, it is best for the label to be clear at a glance, and even to summarize the content of the program processing. For example, dealy is used for delay programs, intsever is used for interrupt services, and so on. This will increase the readability of the program and make it easier to modify and debug it in the future.
3. The program structure should be reasonable.
Previous article:Bank and PC issues in PIC microcontrollers
Next article:PIC microcontroller data storage problem
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of common problems of touch switches
- What sensor can measure the droplet falling (through the infusion tube)
- Which power chip is BNOF?
- How to connect the communication interface of Topmicro intelligent module (I)
- The 2019 TI Industrial Applications Seminar is coming, and the Ningbo special session is waiting for you to sign up!
- [ST NUCLEO-H743ZI Review] Try to play with DAC2ADC
- 360 was reported to have a massive layoff, and the entire security service team was disbanded on the spot.
- [Anxinke UWB indoor positioning module NodeMCU-BU01] No.002-Hardware test environment construction
- GPS signal acquisition problem under ZSTACK protocol stack
- Powering Smart Homes with the bq25606 Simple Charger