51 MCU tutorial from scratch - 13 MCU logic operation instructions

Publisher:平静的33号Latest update time:2012-02-16 Keywords:MCU  Instruction Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Logical operation on the accumulator A of the microcontroller :

CLR A; clear the value in A to 0, single-cycle single-byte instruction, with the same effect as MOV A, #00H.

CPL A ; reverse the value in A bit by bit

RL A ; logically shift the value in A to the left

RLC A; logically shift left the value in A plus the carry bit

RR A ; logically shift the value in A to the right

RRC A; add the value in A to the carry bit and perform a logical right shift

SWAP A; swap the high and low 4 bits of the value in A.

For example: (A) = 73H, then execute CPL A, as follows:

73H is converted to binary as 01110011.

The bit-by-bit inversion is 10001100, which is 8CH.

RL A sends the 7th bit of the value in (A) to the 0th bit, the 1st bit to the 0th bit, and so on.

For example: The value in A is 68H, and RL A is executed. 68H is converted to binary as 01101000, and the shift is performed as shown above. 01101000 is converted to 11010000, which is D0H.

RLC A shifts the value in (A) with the carry bit (C).

For example: if the value in A is 68H and the value in C is 1, then RLC A is executed.

After 1 01101000, the result is 0 11010001, that is, the value of the carry bit of C becomes 0, and (A) becomes D1H.

I won’t talk much about RR A and RRC A. Please refer to the two examples above and practice on your own.

SWAP A swaps the high and low 4 bits of the value in A.

For example: (A) = 39H, then after executing SWAP A, the value in A is 93H. Why is it swapped like this? Because this is a hexadecimal number, and each hexadecimal digit represents 4 binary bits. Note that if it is like this: (A) = 39, without H behind it, after executing SWAP A, (A) = 93 is not. It needs to be converted into binary and then calculated: 39 is converted into binary 10111, which is 0001. The upper 4 bits of 0111 are 0001, and the lower 4 bits are 0111. After swapping, it is 01110001, which is 71H, or 113.

Exercise, given (A) = 39H, execute the following microcontroller instructions and write out the results of each step

CPL A

RL A

CLR C

RRC A

SETB C

RLC A

SWAP A

Through the previous learning, we have mastered quite a few MCU instructions. You may be a little bored with these boring MCU instructions. Let's relax and do an experiment.

Experiment 5:

ORG 0000H

LJMP START

ORG 30H

START:

MOV SP,#5FH

MOV A,#80H

LOOP:

MOV P1,A

RL A

LCALL DELAY

LJMP LOOP

delay:

mov r7,#255

d1: mov r6,#255

d2: nop

nop

nop

nop

djnz r6,d2

djnz r7,d1

ret

END

Let us first write the program into the chip, install it into the experimental board, and see the phenomenon.

What we see is a phenomenon of dark spots flowing. Let us analyze it.

We will analyze the previous ORG 0000H, LJMP START, ORG 30H later. Starting from START, MOV SP, #5FH, this is to initialize the stack. It doesn't matter whether this sentence exists in this program, but we are slowly starting to get in touch with formal programming, so I will slowly cultivate the habit for everyone.

MOV A, #80H, sends the number 80H to A. What does it do? I don't know. Read on.

MOV P1, A. Send the value in A to the P1 port. At this time, the value in A is 80H, so the value sent out is 80H, so the value of the P1 port is 80H, which is 10000000B. Through the previous analysis, we should know that at this time, the LED connected to P1.7 is not bright, while the other LEDs are bright, so a "dark spot" is formed. Continue to look at RL A, RL A is to shift the value in A to the left. Calculate, what is the result after the shift? Yes, it is 01H, which is 00000001B. In this way, the LED connected to P1.0 should not be bright, while the others are bright. From the phenomenon, the "dark spot" flows to the back. Then call the delay program, which we are very familiar with, to make this "dark spot" "dark" for a while. Then transfer to LOOP (LJMP LOOP). Please calculate which light should be off below. . . . . Yes, it should be connected to P1.1. The light is not on. This cycle forms the phenomenon of "dark spot flow".

question:

How to achieve highlight flow?

How to change the direction of flow?

Answer:

1. Change the initial value in A to 7FH.

2. Change RL A to RR A.

Keywords:MCU  Instruction Reference address:51 MCU tutorial from scratch - 13 MCU logic operation instructions

Previous article:51 MCU Tutorial from Scratch - 12 MCU Arithmetic Instructions
Next article:51 MCU tutorial from scratch - 14 MCU logical AND or XOR instructions detailed explanation

Recommended ReadingLatest update time:2024-11-17 01:27

Extended RAM read and write timing of STC89C52 microcontroller
After all, what you read in books are theoretical things. In practical applications, you will always encounter unexpected or unpredictable situations. Instead of making blind inferences, it is better to do some practical work. The privileged student used the 11.0592MHz STC89C52 for testing, and the C code was a
[Microcontroller]
Extended RAM read and write timing of STC89C52 microcontroller
Multi-task multi-process 51 single-chip C language complete version
*/ /* 1. This program does not use any assembly instructions 2. Timer T0 generates an interrupt to switch the process 3. Due to interruption or calling subroutine, PC stack is needed, so PC can be found with SP as the base address. 4. When an interrupt or subroutine returns, SP must be popped from the stack to the PC,
[Microcontroller]
51 MCU Basics (1) - External Interrupt
Basic Concepts of Interrupts Interrupt The CPU works in parallel with the peripherals. When the peripheral data is ready or some emergency occurs, a request is made to the CPU. The CPU suspends the ongoing work and serves the peripheral (or handles the emergency). After the processing is completed, it returns to the o
[Microcontroller]
51 MCU Basics (1) - External Interrupt
Optimizing method of key "once" processing in single chip microcomputer
When I was working on single-chip microcomputers, I always encountered a problem: when the keyboard input of the single-chip microcomputer was processed once, the original method was usually to wait for the keyboard to be released through "delay", so the single-chip microcomputer would stay here and other programs coul
[Microcontroller]
32-bit microcontroller for energy-efficient automotive drive motor control
Fujitsu Semiconductor (Shanghai) Co., Ltd. recently announced the release of three MB91580 series products, mainly for energy-saving automotive drive motor control. As a member of the FR family of high-performance 32-bit flash-embedded microcontrollers (MCUs), this series of products can be widely used in the drive
[Power Management]
32-bit microcontroller for energy-efficient automotive drive motor control
A fast algorithm for filtering out power frequency interference using digital filter based on AVR microcontroller
Power frequency interference is widely present in various industrial sites, and its main sources include power feeders, lighting equipment, engines, and various electronic instruments and equipment. Generally, power frequency interference can be eliminated through filtering circuits, but this will inevitably increase
[Microcontroller]
A fast algorithm for filtering out power frequency interference using digital filter based on AVR microcontroller
Common causes and solutions for MCU/ARM crashes
A few days ago, when I was writing ARM, the computer often crashed for unknown reasons. Here I collected some better answers and solutions for your reference. Possible Causes: An oscillator stops oscillating It can also be divided into oscillator stopping caused by unstable power supply voltage or strong inter
[Microcontroller]
Design of Temperature Control System of Resistance Furnace Based on Single Chip Microcomputer
1. Introduction Resistance furnaces are widely used in chemical, metallurgical and other industries, so temperature control is of great significance in industrial production and scientific research. Its control system belongs to the first-order pure lag link, with the characteristics of large inertia, pure lag,
[Microcontroller]
Design of Temperature Control System of Resistance Furnace Based on Single Chip Microcomputer
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号