MCS51 single-chip microcomputer bit operation instructions and programming examples

Publisher:seanxnieLatest update time:2017-10-17 Source: eefocusKeywords:mcs51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the hardware structure of the MCS-51 microcontroller, there is a bit processor (also known as a Boolean processor) that has a set of instructions for bit variable processing . When performing bit processing, CY (the carry bit we mentioned earlier) is called a "bit accumulator". It has its own bit RAM, which is the 16-byte unit from 20H to 2FH of the internal RAM we just mentioned, that is, the 128-bit unit, and its own bit I/O space (i.e. P0.0...P0.7, P1.0...P1.7, P2.0...P2.7, P3.0...P3.7). Of course, in physical entities, they are exactly the same as the original byte-addressed RAM and ports, or in other words, these RAMs and ports can be used in two ways. 

(1) Bit transfer instructions 

MOV C, BIT  The function of this group of instructions

MOV BIT, C 

is to realize data transfer between the bit accumulator (CY) and other bit addresses. 

Example: MOV P1.0,CY; Send the status in CY to the P1.0 pin (if we are doing arithmetic operations, we can know what CY is now by observation). 

MOV P1.0,CY; Send the status of P1.0 to CY. 

(2) Bit correction instructions 

Bit clear instruction 

CLR C; Make CY=0 

CLR bit; Make the bit address of the instruction equal to 0. Example: CLR P1.0; Even if P1.0 becomes 0, 

the position 1 instruction 

SETB C; Make CY=1 

SETB bit; Make the specified bit address equal to 1. Example: SETB P1.0; Make P.0 become 1 

Bit complement instruction 

CPL C; Make CY equal to the opposite value of the original, from 1 to 0, from 0 to 1. 

CPL bit; Make the value of the specified bit equal to the opposite value of the original, from 0 to 1, from 1 to 0. 

Example: CPL P1.0 

Taking the experiment we have done as an example, if the light is on, the light will turn off after executing this instruction. Otherwise, if the light is off, the light will turn on after executing this instruction. 

(3) Bit logic operation instructions 

Bit AND instruction 

ANL C, bit; CY is ANDed with the value of the specified bit address, and the result is sent back to CY 

ANL C, /bit; First take out the value in the specified bit address and then invert it, and then AND it with CY, and the result is sent back to CY, but note that the value in the specified bit address itself does not change. 

Example: ANL C, /P1.0 

Assume that before executing this instruction, CY=1, P1.0 is equal to 1 (light off), then after executing this instruction, CY=0, and P1.0 is also equal to 1. 

The following program can be used for verification: 

ORG 0000H 

AJMP START 

ORG 30H 

START: MOV SP, #5FH 

MOV P1, #0FFH 

SETB C 

ANL C, /P1.0 

MOV P1.1, C; Send the completed result to P1.1. The result should be that the light on P1.1 is on, while the light on P1.0 is still off.  The function of the bit

or instruction 

ORL C, bit 

ORL C, /bit 

is analyzed by yourself, and then compared with the above example, write a verification program to see if you are right? 

(4) Bit conditional transfer instructions 

CY transfer instructions 

JC rel 

JNC rel 

The function of the first instruction is to transfer if CY is equal to 1, and execute in sequence if it is not equal to 1. So where to transfer to? We can understand it this way: JC label, if it is equal to 1, transfer to the label and execute. We have already talked about this instruction in the last class, so we will not repeat it. 

The second instruction is the opposite of the first instruction, that is, if CY=0, it will be transferred, and if it is not equal to 0, it will be executed in sequence. Of course, we also understand: JNC label 

judgment variable transfer instruction 

JB bit,rel 

JNB bit,rel 

The first instruction is to transfer if the value in the specified bit is 1, otherwise it will be executed in sequence. Similarly, we can understand this instruction like this: JB bit, label. 

Please analyze the second instruction by yourself first. Let 

us give an example to illustrate: 

ORG 0000H 

LJMP START 

ORG 30H 

START: MOV SP, #5FH 

MOV P1, #0FFH 

MOV P3, #0FFH 

L1: JNB P3.2,L2; There is a button connected to P3.2. When it is pressed, P3.2=0 

JNB P3.3,L3; There is a button connected to P3.3. When it is pressed, P3.3=0 

LJM P L1 

L2: MOV P1,#00H 

LJMP L1 

L3: MOV P1,#0FFH 

LJMP L1 

END 

Write the above example into the chip and see what happens... 

Press the button connected to P3.2, and all the lights on port P1 will light up. Release it or press it again, and the lights will not go out. Then press the button connected to P3.3, and all the lights will go out. What does this look like? Isn't this the "start" and "stop" function often used in industrial sites? 

How is it done? At the beginning, 0FFH is sent to the P3 port, so that all the leads of P3 are at a high level, and then L1 is executed. If P3.2 is at a high level (the key is not pressed), the JNB P3.3, L3 statement is executed in sequence. Similarly, if P3.3 is at a high level (the key is not pressed), the LJMP L1 statement is executed in sequence. In this way, P3.2 and P3.3 are continuously detected. If the button on P3.2 is pressed once, it will transfer to L2 and execute MOV P1, #00H to make all the lights on, and then transfer to L1 again, and the cycle will continue until P3.3 is detected to be 0, then transfer to L3, execute MOV P1, #0FFH, and all the lights will go out, and then transfer to L1, and the cycle will continue.

Keywords:mcs51 Reference address:MCS51 single-chip microcomputer bit operation instructions and programming examples

Previous article:51 MCU bit transfer instruction MOV
Next article:51 MCU bit addressing area and SFR capable of bit addressing

Recommended ReadingLatest update time:2024-11-16 21:48

What are the components and functions of mcs51 microcontroller?
  MCS51 refers to the general name of a series of single-chip microcomputers produced by the American INTEL company. This series of single-chip microcomputers includes many varieties, such as 8031, 8051, 8751, 8032, 8052, 8752, etc. Among them, 8051 is the earliest and most typical product. Others in this series Micro
[Microcontroller]
What are the components and functions of mcs51 microcontroller?
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号