Movatterモバイル変換


[0]ホーム

URL:


Skip to content
View Course Path

Boolean (bitwise) instructions in 8051 for bit manipulation

   |   Published April 30, 2020   |   Updated May 9, 2020

Most microcontrollers frequently deal with bits of data rather than bytes. There are many occasions where performing a particular operation in a bit of data in a memory location is more convenient than operating on the entire byte. It allows designers to be more resource optimum and reduce unnecessary overhead.

The MCS-51 was one of the first microcontrollers to have a dedicated boolean architecture which was responsible for making it exceptionally fast when it came to processing a single bit.

To support these bit-wise operations the data memory of the 8051 (20H-2FH) is both bit and byte-addressable as we saw in the post on general memory structure of 8051. Moreover, a number ofspecial function registers in the RAM memory space are bit addressable which makes the life of the programmer so much easier.

The CY flag of the PSW register can be considered ground zero for all the bit-wise operations accounting for its extensive use during boolean operations in 8051.

Now that we have seen the rest of the instruction groups in this8051 course, we will specifically single out and enlist all of them that classify underboolean/bit-wise operations and are capable of working on single bits of data.

[C]= Carry flag in PSW register;

OperationMnemonicsDescription
Clear
CLR C[CY]<-0
CLR Address[Address]<-0
Complement
CPL C[CY]<-[CY]’
CPL Address[Address]<-[Address]’
Setting value
SETB C[CY]<-1
SETB Address[Address]<-1
AND
ANL C, Address[CY]<-[CY] AND [Address]
ANL C, /Address[CY]<-[CY] AND [Address]’
OR
ORL C, Address[CY]<-[CY] OR [Address]
ORL C, /Address[CY]<-[CY] OR [Address]’
Move
MOV C, Address[CY]<-[Address]
MOV Address,C[Address]<-[CY]
Jump
JC AddressJump to address if [C]=1
JNC AddressJump to address if [C]=0
JNB Address, AddressJump to destination address if source address =0
JB Address, AddressJump to destination address if source address =1
JBC Address, AddressJump to destination address if source address =1 and sets carry flag to 0

Let’s take a more in-depth look into each of these bit-wise operations.

Contents

Clear operation

The clear operation is used to clear the data bit at a particular address or in the carry flag.

Opcode
Operand
Description
Size
Execution Time
Flags affected
CarryOverflowAuxilary carry
CLR
CClears the data stored in the CY flag of the accumulator1 byte12 clock cyclesset to 0UnaffectedUnaffected
AddressClears the data bit stored at a particular address2 bytes12 clock cyclesUnaffectedUnaffectedUnaffected

An explanation for the size of an instruction

Any instruction in the 8051 microcontroller consists of two parts; an opcode and operand. As the 8051 has an 8-bit architecture each opcode is 8 bit in size (1 byte) but the size of instructions increases due to the size of the operands. In some cases, operands take no space whereas in some they can take up to 2 bytes of space (See the post onaddressing modes in 8051 for more details on the cases where instruction size increases due to operands.)

Let us look at an example to understand the above-mentioned concept.

The instruction CLR C is one byte in size as the opcode itself is enough to describe the instruction. But in the case of CLR 02H, the microcontroller needs two bytes to store the instruction as the opcode takes 1 byte and the address takes another.

In some cases this size increases to 3 bytes, this happens in the case of jump instructions where a relative address is also given to the microcontroller.

Examples

CLR C; Clears the data in the CY flag of the PSW registerCLR P1.7; Clears the bit at port 1.7

Complement instruction

Opcode
Operand
Description
Size
Execution Time
Flags affected
CarryOverflowAuxilary carry
CPL
CComplements the data stored in the CY flag of the accumulator1 byte12 clock cyclesAffectedUnaffectedUnaffected
AddressComplements the data bit stored at a particular address2 bytes12 clock cyclesUnaffectedUnaffectedUnaffected

Examples

CPL C; Complements the value stored in the carry flagCPL TF1; Complements the TF1 flag inTCON register

Set operation

Opcode
Operand
Description
Size
Execution Time
Flags affected
CarryOverflowAuxilary carry
SETB
CSets the data stored in the CY flag of the PSW register to 11 byte12 clock cycles1UnaffectedUnaffected
AddressSets the data bit stored at a particular address to 12 bytes12 clock cyclesUnaffectedUnaffectedUnaffected
SETB C; Sets the value in the carry flag to 1SETB TR1; Sets the value of TR1 flag in TCON register to 1(used to start the timer)

AND Operation

Opcode
Operand
Description
Size
Execution Time
Flags affected
CarryOverflowAuxilary carry
ANL
C, AddressPerforms AND operation between the data stored in the carry flag and the given address2 byte24 clock cyclesAffectedUnaffectedUnaffected
C, /AddressPerforms AND operation between the data stored in the carry flag and the complement of the data at any given address2 bytes24 clock cyclesUnaffectedUnaffectedUnaffected

Examples

ANL C, 09HANL C, /05H

OR Operation

Opcode
Operand
Description
Size
Execution Time
Flags affected
CarryOverflowAuxilary carry
ORL
C, AddressPerforms OR operation between the data stored in the carry flag and the given address2 byte24 clock cyclesAffectedUnaffectedUnaffected
C, /AddressPerforms OR operation between the data stored in the carry flag and the complement of the data at any given address2 bytes24 clock cyclesUnaffectedUnaffectedUnaffected

Examples

ORL C, 09HORL C, /07H

MOV Operation

Opcode
Operand
Description
Size
Execution Time
Flags affected
CarryOverflowAuxilary carry
MOV
C, AddressMoves data from a particular address to the carry flag2 byte12 clock cyclesUnaffectedUnaffectedUnaffected
Address,CMoves data from the carry flag to a particular address2 bytes24 clock cyclesUnaffectedUnaffectedUnaffected

Examples

MOV C, 09HORL 06H, C

Jump Operations

Any computing device performs operations in a sequential manner but in some cases, operations need to be performed in the form of a branch. There are a number of jump instructions which are used to perform this kind of branching, let us have a look at them.

Opcode
Operand
Description
Size
Execution Time
Flags affected
CarryOverflowAuxilary carry
JCAddress/labelThis instruction transfers the control to the specified address/label if the carry flag has the value 1.2 bytes24 clock cyclesUnaffectedUnaffectedUnaffected
JNCAddress/labelThis instruction transfers the control to the specified address/label if the carry flag has the value 0.2 bytes24 clock cyclesUnaffectedUnaffectedUnaffected
JNBAddress, AddressThis instruction transfers the control to the destination address if the source address has the bit 0.3 bytes24 clock cyclesUnaffectedUnaffectedUnaffected
JBAddress, AddressThis instruction transfers the control to the destination address if the source address has the bit 1.3 bytes24 clock cyclesUnaffectedUnaffectedUnaffected
JBCAddress, AddressThis instruction transfers the control to the destination address if the source address has the bit 1. This instruction clears the carry flag as well3 bytes24 clock cyclesUnaffectedUnaffectedUnaffected

Example

        MOV  TMOD , #10H ;The value 10H is 00010000B and selects mode 1 of timer 1        MOV R3 , #200H   ;used as a counter to create a delay of 14 secondsAGAIN : MOVTL1 , #08H    ;sets the value of 08H in the lower bit of timer 1 and the again keyword helps to create a loop.        MOV TH1 , #01H   ;sets the value of 01H in the lower bit of timer 1        SETB TR1         ;turns on the timerBACK:   JNB   TF1, BACK  ;checks the TF1 flag in TCON register continuously to know if overflow has occurred or not        CLR   TR1        ;clears TR1 flag to stop the timer        CLR   TF1        ;clears overflow bit as the timer is resetDJNZ    R3,  AGAIN       ;keeps looping the statements after again keyword till the value in R3 reduces to 0. DJNZ command decreases the value in r3 and checks if it zero

About the author

Related courses to Boolean (bitwise) instructions in 8051 for bit manipulation

Digital Electronics Course

A free course on digital electronics and digital logic design for engineers. Everything is taught from the basics in an easy to understand manner.

8085 Microprocessors Course

A free course on Microprocessors. Start from the basic concepts related to the working of general microprocessors and work upto coding the 8085 and 8086.

Arduino Course

If you have ever thought of making an electronic project but didn't know where to start, this free Arduino course will be perfect for you.

Back to course page 

8051 Microcontroller Course

Course Path

Why do we have to use the 8051? Isn’t it too old?
8051 Architecture – In-depth explanation using old and modern variants
AT89C51 Pins – Ultimate guide to the 8051’s pin configuration
Ports of 8051 – Functions and specifications of the four I/O ports
8051 Memory Organization – ROM and RAM Structure
Special Function Registers of 8051 (SFR)
Addressing modes in 8051 microcontroller
Data Transfer instructions in 8051
Arithmetic instructions in 8051 – with examples
Logical instructions in 8051 – with example codes
Boolean (bitwise) instructions in 8051 for bit manipulation
Branching Instructions in 8051
Interrupts in 8051 microcontroller – With examples
Timers and Counters in 8051
8051 external memory interfacing guide: RAM and ROM
Set up Keil c51 for 8051 microcontroller simulations – A step by step guide
Serial communication with UART in 8051 – Simple in-depth explanation
Interfacing of 8051 with 8255 Programmable Peripheral Interface
Interfacing 8051 with ADC 0808 – Stepwise tutorial
LED interfacing with 8051 – Direct and with 8255
LED and switch interfacing with 8051 – Including switch debouncing
LCD interfacing with 8051 – 8-bit, 4-bit mode, and with 8255 PPI
Seven segment interfacing with 8051 – Single and Quad module
Servo Motor Interfacing with 8051 – Simple tutorial
Stepper Motor Interfacing with 8051 – Simple tutorial
DC motor interfacing with 8051 using L293D and L298N
Interfacing 8051 with relays to drive high power peripherals
Interfacing 4×4 Keypad matrix with 8051 microcontroller
Bluetooth (HC-05) interfacing with 8051 with practical application
8051 – Power Down and Idle mode – Comparative Study
8051 Microcontroller MCQ | Quiz | Interview Questions

Share and Support


[8]ページ先頭

©2009-2025 Movatter.jp