The project introduced in this article is about the realization of robot control. 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:
list P=16c73
call lefthandup
call righthandup
LO call readinsignal
bdss csl_v,1efthandligbts
call lefthandstop
btfsc csl_v,righthandlight4
goto L0
call righthandstop
L1 call readinsignal
btfsc csl_v,lefthandlight3
goto L1
call lefthandstop
:
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 reached it, the left hand stops, then see if the right hand has reached the hand-raising position, if it has reached it, 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 zeroed in the action sub-function. The program is as follows:
list p=16c73
:
actlONstopflag equ Ox6e ; bit definition
lefthandfla8 cqu 0x0; 0 means stop left hand action
rightbandflag equ Oxl; O means stop the right hand movement
:
movlw Ox03
movWf actionstopflag
call lefthanduo
call righthandup
L2 call readinsignal
btfss csl_v,lefthandhght3
call lefthatldotop
btfss csl_v,righthandlight4
call righthandstop
movlw Ox00
subwf activstopflag,w
btfss status,z
goto L2
Continue with the following procedure
acTIonstopflag represents the action flag parameter, assigns a value to the action flag, and clears the flag value in the action stop function. 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, reaches the stop position, and whether the right hand reaches the hand-raising position, reaches the stop position. 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:
list D=16c76
start:ca11 setcpu
call automatlsn, statel
L3 call readinsignal
btfsc-cs2_v,ultrasonicdetectl
goto L3
goto autonatlsmstate2
:
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.
list D=16c76
call opentimerO
L4 movlw d'30'
subwf t0_v2,W
btfss status,c
goto L4
cau closetimer0
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 the right method 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. This is the author's experience in practice. There are still many deficiencies or issues that have not been considered. I hope to continue to learn the essence of PIC microcontrollers through discussions with everyone.
Previous article:Analysis of the design scheme of PIC microcontroller keyboard system
Next article:Design of Data Acquisition System Based on PIC Microcontroller
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- FPGA Essay - On Deficiencies
- [NUCLEO-L552ZE Review] + ADC-based fluorescence value reading
- TI Live: Detailed talk on "Bidirectional CLLLC Resonant, Dual Active Bridge (DAB) Reference Design", watch and win gifts (64 in total)
- Some considerations for c2000 hardware design
- How does AD software collect company information?
- [GD32L233C-START Review] 11. GD32 ISP software does not support GD32L233 yet
- How can IC products ensure that they comply with RoHS requirements?
- A design of voltage output DAC circuit based on PWM
- Last day! Answer questions to win prizes! Practical sharing | Design a reliable switching power supply from scratch
- How to detect whether the switching power supply transformer is good or bad?