As we all know, most registers of PIC microcontrollers, including each storage unit of RAM data memory, can realize a series of complex operation functions such as shift, clear, and bit test. However, in the (most widely used basic and intermediate) reduced instruction sets that coordinate their operation, there is no ready-made instruction to "invert" a certain "bit" of the register. In practical applications, it is very necessary to invert certain "bits". As shown in the figure below. The 8 pins of port RB of the PIC16C622 microcontroller as the output port control 8 different electrical devices respectively. Press button AN once, relay J is energized, and then press J again to release, that is, press AN once, the output state of port RB3 pin changes, while the output of the other 7 pins of port RB remains unchanged. In this case, the best way is to press AN once to "invert" PORTB (3) once - this is extremely easy for MCS-51 microcontrollers. Just write a bit inversion instruction (such as CPL P3.3). The PIC16C622 microcontroller only has the instruction to invert bytes, but no direct instruction to invert bits. The author used the instruction "W and f XOR" to easily solve the bit inversion problem. Example:
MOVLW 0x08; send the constant 0x08 to W
XORWF 6,1; W and F6 XOR The result
is stored in F6
. This achieves the "inversion" operation of F6(3), i.e., RB3 pin. If the state of register F6 is "B01101001" at that time, after the above instruction operation, there is
W before XOR 00001000
F6 01101001
XOR after F6 01100001
The result of register F6 is "B01100001". It can be seen that except for F6(3) being inverted from "1" to "0", the other bits remain unchanged, achieving the expected purpose. This method can "invert" one, two, or more bits of a register at the same time - as long as the corresponding "bit" of the constant is logically "1" when the constant is sent to the working register W.
In addition, when writing programs such as comparing two data, people usually use "subtraction" instructions to complete it. Using the "XOR" instruction can also solve the problem, and it is more concise (it only affects the zero flag "Z" in the status register and has no effect on other bits). For example, when a clock program is established, when the second counter register counts to 60 seconds, the minute counter register should be "advanced by 1". If the second counter register is set to F20 and the minute counter register is F21, the timer interrupt subroutine is:
INTSUB1:
...
INCF 20, 1; second counter register F20; "increase by 1"
MOVLW 0x3C; put the constant D'60' into W
XORWF 20, 0; register F20 count is
; or not equal to 60 seconds
BTFSS STATUS, 2; if the count of F20 is; 60 (zero flag Z = 1)
GOTO INTSUB2; then jump to
INCF 21, 1; minute counter register F21
; "increase by 1"
...
INTSUB2 RETFIE; interrupt return
From the above examples, we can see that the XOR instruction XORWF is the same as other instructions in PIC. As long as its function is used flexibly, it is very powerful.
Reference address:Application of PIC microcontroller OR operation instruction XORWF
MOVLW 0x08; send the constant 0x08 to W
XORWF 6,1; W and F6 XOR The result
is stored in F6
. This achieves the "inversion" operation of F6(3), i.e., RB3 pin. If the state of register F6 is "B01101001" at that time, after the above instruction operation, there is
W before XOR 00001000
F6 01101001
XOR after F6 01100001
The result of register F6 is "B01100001". It can be seen that except for F6(3) being inverted from "1" to "0", the other bits remain unchanged, achieving the expected purpose. This method can "invert" one, two, or more bits of a register at the same time - as long as the corresponding "bit" of the constant is logically "1" when the constant is sent to the working register W.
In addition, when writing programs such as comparing two data, people usually use "subtraction" instructions to complete it. Using the "XOR" instruction can also solve the problem, and it is more concise (it only affects the zero flag "Z" in the status register and has no effect on other bits). For example, when a clock program is established, when the second counter register counts to 60 seconds, the minute counter register should be "advanced by 1". If the second counter register is set to F20 and the minute counter register is F21, the timer interrupt subroutine is:
INTSUB1:
...
INCF 20, 1; second counter register F20; "increase by 1"
MOVLW 0x3C; put the constant D'60' into W
XORWF 20, 0; register F20 count is
; or not equal to 60 seconds
BTFSS STATUS, 2; if the count of F20 is; 60 (zero flag Z = 1)
GOTO INTSUB2; then jump to
INCF 21, 1; minute counter register F21
; "increase by 1"
...
INTSUB2 RETFIE; interrupt return
From the above examples, we can see that the XOR instruction XORWF is the same as other instructions in PIC. As long as its function is used flexibly, it is very powerful.
Previous article:PIC Microcontroller Programming Style and Format
Next article:PIC15C5X Basic 8-bit MCU
- Popular Resources
- Popular amplifiers
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- About C2000 program serial port upgrade
- Moderators and those who want to be moderators, please accept this "Moderator Strategy"
- What are the functions of the three buttons and three indicator lights?
- Teacher He Lei from Zhixin Technology explains the state machine
- Keysight Technologies Live Review: Oscilloscope Basics Training
- Remote control jog problem
- How to solve the problem of field effect tube heating when used as a switch
- How to configure slave device registers via SPI in tms57ls
- [RVB2601 Creative Application Development] 9~11 Game End Restart and Optimize Code, etc.
- Evaluation of domestic FPGA Gaoyun GW1N-4 series development board - stepper motor control (first edition)