First, let's look at a program:
Title '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:PIC microcontroller read/modify/write problems and solutions
Next article:Analysis of bank and PC errors in PIC microcontrollers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Award-winning live broadcast: You can watch 3D movies without glasses, learn about the German-quality naked-eye 3D technology
- Analysis of basic knowledge of microcontroller reset circuit
- Popular power supply design
- MAX232cse chip introduction
- [Fudan Micro FM33LG0 Series Development Board Review] Driving SPI TFT Display
- Come to Infineon flagship store to find good things in autumn! Get coupons and win a 50-yuan JD card
- [2022 Digi-Key Innovation Design Competition] Material unboxing ESP32-S3 SHT30 LMG5200 and other devices
- Help, power circuit analysis
- If there is a bug in the third-party code, how can everyone adapt it to the original version after it is fixed?
- Share a general good book Digital Signal Processing C Language Program Collection-DSP Algorithm Encyclopedia C Language Version Original Book