Application of PIC microcontroller OR operation instruction XORWF

Publisher:WhisperingRainLatest update time:2011-02-24 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:PIC Microcontroller Programming Style and Format
Next article:PIC15C5X Basic 8-bit MCU

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号