The control part uses PICl6F7X series microcontrollers, which are programmed in assembly language. The running speed is fast and can meet the system requirements. Most of the control used here is digital signal control, and the motor control has only two states: open and closed. The coordinated actions of the two arms, body, head, feet, etc. are required during the action. The entire control system is relatively complex, so some issues need to be paid attention to in PIC program writing and space allocation.
1. Use of action flags
In the whole control, there are many combined actions. When all the action positioning is controlled by photoelectric switches, there are some problems in programming. 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 effective (that is, it is blocked when it is 0), and reaches the correct position. With simple understanding, it can be written as the following program:
listP=16c73
calllefthandup
callrighthandup
LOcallreadinsignal
bdsscsl_v,1efthandlIGBTs
calllefthandstop
btfsccsl_v,righthandlight4
gotoL0
callrighthandstop
L1callreadinsignal
btfsccsl_v,lefthandlight3
gotoL1
calllefthandstop
:
Lefthandlight represents the photoelectric switch, which is used to determine 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 it, if it has, the left hand stops, then check whether the right hand has reached the hand-raising position, if it has, stop, otherwise loop the above detection 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 loop detection, the arm can stop at each position, but 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 set to zero in the action sub-function. The program is as follows:
listp=16c73
:
actlonstopflagequOx6e; bit definition
lefthandfla8cqu0x0; 0 means stop the left hand action
rightbandflagequOxl; O means stop the right hand movement
:
MOV lwOx03
MOV Wfactionstopflag
calllefthanduo
callrighthandup
L2callreadinsignal
btfsscsl_v,lefthandhght3
calllefthatldotop
btfsscsl_v,righthandlight4
callrighthandstop
MOV lwOx00
subwfactionstopflag,w
btfssstatus,z
gotoL2
Continue with the following procedure
Activstopflag represents the action flag parameter, which assigns a value to the action flag. The action stop function will clear the flag value. The above program has the same function as the previous program, realizing the action of the two arms. The above program describes whether the left hand reaches the applause position and stops, and whether the right hand reaches the hand-raising position and stops. Check whether the action flag is zero. If not, keep looping and checking; if yes, execute the following program.
2. Different uses of GOTO and CALL instructions
In the PIC assembly program, CALL and GOT0 instructions are used in different occasions. Generally speaking, the CALL instruction is mostly used between the subprogram and the main program; and the GOTO instruction is mostly used between the state transition modules, that is, entering another state from this state without returning. Since the stack of the PIC microcontroller is limited, the GOTO statement cannot be used endlessly in the program, which will cause the stack to overflow and the program cannot run normally. The internal loops of each small program occupy a small number of stack levels, so it is feasible to use the GOTO instruction, but in a large program, 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. The program is as follows:
listD=16c76
start:ca11setcpu
callautomatlsn, statel
L3callreadinsignal
btfsccs2_v,ultrasonicdetectl
gotoL3
gotoautonatlsmstate2
:
automatlsmstate2:
return
auatomaTIsmstate1 and automatismstate2 represent two states, and uhrasonicdetect1 represents an input ultrasonic detection signal. The above program describes calling the automatismmstate1 state, and after execution, the following detection ultrasonicdetect is performed. If there is no trigger, the detection will be looped. If triggered, it will enter the automatismstate2 state, and it will not return to the following program after execution.
3. Different usage of status flags Z and C
When judging the flag, Z (zero flag) and C (borrow flag) are different. When Z is 1, it means the result above is 0, and when Z is 0, the result is not 0. When C is 1, there is a borrow, and 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 may be just right, or it may have 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, while using z can control the timing well. Z is mostly used for general calculations, such as the previous action flag.
listD=16c76
callopentimerO
L4 MOV lwd '30'
subwft0_v2,W
btfssstatus,c
gotoL4
cauclosetimer0
The program checks whether the time reaches 1.5s. If not, it waits in a loop. If it reaches 1.5s or the detection time has passed, it turns off the timer and executes 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 be reduced, avoiding bugs in debugging.
Previous article:Target Detection and Tracking System Based on Embedded PIC32 Microcontroller
Next article:PIC microcontroller realizes the design of 8 digital tubes to realize circular display
- Popular Resources
- Popular amplifiers
- 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)
- Learn ARM development (6)
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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Low-cost DIY nano-level lithography machine! This post-95s boy is popular...
- Type-C interface display solution
- Is there any module similar to Yiweilian?
- How to choose the right operational amplifier
- How to use a switching boost chip to solder a circuit to achieve 5-24V output
- Here is a basic CAD shortcut key
- [GD32L233C-START Review] 1. Unboxing + Environment Setup
- Maxim Basic Analog IC APP download helps you innovate analog design!
- How is the internal low-pass filter of Bh1415 designed? How is the 150pf connected to pin 3 determined?
- Its automobile inner wheel difference safety warning system