1. Distinguish between upper and lower case instructions
When writing the source program of the PIC microcontroller, in addition to the strict list instructions at the beginning of the source program, you must also pay attention to the upper and lower case rules of the letters and symbols in the source program, otherwise the program will not be successfully assembled on the PC. The pseudo-instruction INCLUDE is used in the source program. This instruction inserts the microcontroller file specified in the list (in MPLAB) into the source program as part of the source program, so all the registers related to the microcontroller in MPLAB do not need to be assigned with the assignment instruction (EQU) in the source program, which greatly simplifies the established source program.
In addition, due to the pseudo-instruction INCLUDE, according to the format in the MPLAB software, the operands in the source program that involve the register names specified by MPLAB can only be capitalized, not lowercase. The rest of the opcodes and symbol letters can be arbitrarily capitalized, but the X in 0x should be lowercase. Otherwise, the assembly will not succeed. In view of the above reasons, for the convenience of writing, when using the MPLAB software, it is advisable to use uppercase letters for the source program of the PIC microcontroller (except 0x).
The following example is about the realization of robot control. The control part adopts PIC16F7X series microcontroller, which is programmed in assembly language and runs fast, meeting the requirements of the system.
2. Use of action flags
In the whole control, there are many combined actions. When all action positioning is controlled by photoelectric switches, there are some problems in program writing. For example, if the left hand is required to rise to the applause position and the right hand is required to rise to the hand-raising position (the initial position of the hand is at the lowest lower position), the photoelectric switch 0 is valid (that is, it is blocked when it is 0), and it reaches the correct position. With a simple understanding, it can be written as the following program:
list P=16c73
call lefthandup
call righthandup
L0 call readinsignal
bdss csl_v,lefthandligbts
call lefthandstop
bfsc csl_v,righthandlight4
goto L0
call righthandstop
L1 call readinsignal
bfsc csl_v,lefthandlight3
goto L1
call lefthandstop
lefthandlight represents the photoelectric switch, which is used to judge whether it has reached the corresponding position. 1 represents the lowest position of the arm; 2 represents the handshake position of the arm; 3 represents the applause position of the arm; 4 represents the high hand-raising position of the arm. The above program describes the process of the left arm rising to the hand-raising position and the right arm rising to the applause position and stopping. First, determine whether the left hand has reached the position. If it has reached the position, the left hand stops. Then check whether the right hand has reached the hand-raising position. If it has reached the position, it stops. Otherwise, the above detection is repeated until the left hand reaches the applause position and the right hand reaches the hand-raising position.
Note that 3 and 4 here represent the applause position and the hand-raising position. After the cyclic detection, the arm can stop at each position. However, the mechanical action has inertia, and the mechanical stop position may be a little above or below the position, which affects the following actions. The mechanical action may malfunction after several actions, that is, the program cannot run normally. In this case, it is necessary to modify the program writing method and use the flag bit to control the action. If the control flag bit is used, the flag position must be zeroed in the action sub-function.
3. Differentiate between the use occasions of GOTO and CALL instructions
In PIC assembly programs, CALL and GOTO instructions are used frequently and are easy to confuse. Generally, CALL instructions are mostly used between subprograms and main programs; while GOTO instructions are mostly used between state transition modules, that is, entering another state from one state without returning. Since the stack of PIC microcontrollers is limited, GOTO statements cannot be used endlessly in the program, otherwise the stack will overflow and the program will not run normally. The internal loops of each small program occupy a small number of stack levels, so it is feasible to use GOTO instructions, but in large programs, using GOTO cannot return to the next instruction of the program before the call. After the CALL instruction completes the call to the subroutine, it returns to the program before the call. For example, in ultrasonic detection, the program is as follows:
list D=16c76
start:call setcpu
call automaflsmstate1
L3 call readinsignal
bfsc es2_v,ultrasonicdetect1
goto L3
goto automatlsmstate2:
automatlsmstate2:
return
automafismstate1 and automatlsmstate2 represent two states, and ultrasonicdetect1 represents an input ultrasonic detection signal. The above program describes the call to the automatlstmstatei state. After execution, the following detection uhrasonicdetect1 is performed. If there is no trigger, the detection will continue to loop. If triggered, the automatlsmstate2 state will be entered, and the program will not return to the following program after execution.
Since the stack of the PIC microcontroller is limited, the GOTO instruction cannot be used endlessly in the program, otherwise the stack will overflow and the program will not run normally. However, in some cases, such as when the program branches, the GOTO instruction has to be used. For the PIC16F7X series microcontroller, when the program branches, it can only be judged by the z bit or c bit of the STATUS register. In the first case, the GOTO instruction must be used to transfer; otherwise, after the first case is executed, the second case will be executed immediately. Therefore, when using assembly language for programming, the program should be broken down into subroutines at different levels; then calls are made between programs to minimize the range of the GOTO instruction jump.
4. Pay attention to the different usage of status flags Z and C
When judging flags, Z (zero flag) and C (borrow flag) are different. When Z is 1, it means the result above is 0. When Z is 0, the result is not 0. When C is 1, there is a borrow. When C is 0, there is no borrow. When using a timer, the C flag is generally used. This is because when a certain action is completed to check the timer, the time may not have arrived, or it is just right, or it has exceeded the time. As long as the time has arrived or exceeded, the timer must be turned off as required, as described in the following program. If the Z flag is used, it may not be detected when it is equal to 0, and the stop state cannot be determined. Using Z can control the timing well. Z is mostly used for general calculations, such as the previous action flag.
list D=16c76
call openfimer0
L4 movlw d'30'
subwf t0_v2,W
bfsc status,c
goto L4
call dosetimer0
The program checks whether the time has reached 1.5s. If not, it will wait in a loop. If it has reached or the detection time has passed, it will turn off the timer and execute the following program.
In short, using appropriate methods in programming PIC microcontrollers can make the entire program run stably, and the use of program space will also be reduced, avoiding bugs in debugging.
Previous article:Design of triple speed sampling method for serial asynchronous communication based on PIC software
Next article:Temperature measurement system based on temperature sensor with PWM output
- Popular Resources
- Popular amplifiers
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
- Try it, can you access this website of ST?
- I found the fish's partner - [Electronic Components Artwork]
- ESD protection and elimination methods in switching power supply design and production
- 【TI mmWave Radar Review】+3D Profile Test
- I thought it was washing the phone, but it turned out to be washing the phone
- Medium and low pressure MODS pipes
- EEWORLD University Hall----Live Replay: Microchip Security Series 14 - GoodLock with SAM L11 and TrustFLEX ATECC608 Security Devices
- 【ST NUCLEO-G071RB Review】_02_LED Light Experiment
- Help, MPU6050 freezes and no data output after running for a while
- TC58CVG0S3HxAIx Engineering Data Sheet