Logical Operations in 8051 Microcontroller (Common Introduction)
• Logic operations are fundamental to the functionality of any digital system, including
microcontrollers.
• Allows for bit manipulation, decision-making, and control of hardware peripherals.
• Particularly important in embedded systems, where efficient handling of data and control
signals is essential.
• They can be performed directly on the accumulator (A), registers, and Special Function
Registers (SFRs), making them versatile tools in embedded programming.
• Crucial for tasks like masking bits, setting and clearing individual bits, toggling outputs, and
performing arithmetic operations conditionally.
Types of Logical Functions:
BITWISE LOGICAL OPERATIONS:
These are essential for manipulating individual bits or groups of bits in variables (e.g., registers,
ports).
• AND (&):
o Performs a bitwise AND between two variables.
o Example:
• OR (|):
o Performs a bitwise OR between two variables.
o Example:
• XOR (^):
o Performs a bitwise XOR (exclusive OR) between two variables.
o Example:
• NOT (~):
o Computes the bitwise complement of a variable (1's complement).
o Example:
LOGICAL (BOOLEAN) OPERATIONS:
These are used for control flow and decision-making based on truth values (true or false).
• Logical AND (&&):
o Evaluates to true if both operands are non-zero.
o Example:
• Logical OR (||):
o Evaluates to true if at least one operand is non-zero.
o Example:
• Logical NOT (!):
o Negates the truth value of an expression.
o Example:
SHIFT OPERATIONS:
These are used for efficiently moving bits within a variable.
• Left Shift (<<):
o Shifts all bits to the left, filling vacated bits with zeros.
o Example:
• Right Shift (>>):
o Shifts all bits to the right. For unsigned types, vacated bits are filled with zeros.
o Example:
1. Analyze how logic operations in the 8051 can be used to implement basic logical gates
like AND, OR, XOR, and NOT. (COVERED IN INTRO) Provide examples of how these logic
gates can be implemented in software.
• PORT DATA MANIPULATION
#include <reg51.h>
void main(void)
{
P0=0x35 & 0x0F; //ANDing
P1=0x04 | 0x68; //ORing
P2=0x54 ^ 0x78; //XORing
P0=~0x55; //inverting
P1=0x9A >> 3; //shifting right 3
P2=0x77 >> 4; //shifting right 4
P0=0x6 << 4; //shifting left 4
• BIT MANIPULATION
(SAME AS CONTROLLING I/O PINS IN 2ND QUESTION)
2. Inspect the use of logic operations in controlling peripheral devices (e.g., I/O ports,
timers, serial communication) in the 8051 microcontroller. Provide examples of how
logic operations are used in conjunction with SFRs.
WRITE LOGICAL OPERATIONS INTRO AND TYPES
Logic operations are crucial in interacting with peripheral devices of the 8051 microcontroller. By
manipulating bits and using Special Function Registers (SFRs), you can control I/O ports, timers, and
serial communication effectively.
1. Logic Operations in Controlling I/O Ports
I/O ports in the 8051 are controlled using logic operations for tasks like setting, clearing, or toggling
specific pins. The SFRs for ports (e.g., P0, P1, P2, P3) allow direct access to these pins.
Examples in Embedded C:
• Setting a Pin High:
P1 |= 0x01; // Set P1.0 to HIGH (1), leaving other pins unchanged
• Clearing a Pin:
P1 &= ~0x02; // Set P1.1 to LOW (0), leaving other pins unchanged
• Toggling a Pin:
P1 ^= 0x04; // Toggle P1.2 (invert its state)
• Checking a Pin's Status:
if (P1 & 0x08) {
// Executes if P1.3 is HIGH
• Masking Multiple Bits:
unsigned char masked_value = P1 & 0x0F; // Read only lower 4 bits of P1
2. Logic Operations in Timer Control
Timers in the 8051 are controlled via SFRs like TCON (Timer Control) and TMOD (Timer Mode). Logic
operations help configure timer modes, start/stop timers, and check their status.
Examples in Embedded C:
• Configuring Timer 0 in Mode 1 (16-bit Timer):
TMOD &= 0xF0; // Clear lower 4 bits of TMOD
TMOD |= 0x01; // Set Timer 0 to Mode 1
• Starting Timer 0:
TCON |= 0x10; // Set TR0 (Timer 0 Run) bit
• Stopping Timer 0:
TCON &= ~0x10; // Clear TR0 bit
• Checking Timer 0 Overflow:
if (TCON & 0x20) {
// Executes when Timer 0 overflow flag (TF0) is set
TCON &= ~0x20; // Clear TF0 flag
3. Logic Operations in Serial Communication
Serial communication in the 8051 is managed via SFRs like SCON (Serial Control) and SBUF (Serial
Buffer). Logic operations are used to configure communication modes, check transmission/reception
flags, and control data flow.
Examples in Embedded C:
• Configuring Serial Communication Mode 1 (8-bit UART):
SCON = 0x50; // Set SCON: Mode 1, REN enabled, clear transmit/receive flags
• Starting Transmission:
SBUF = 0x41; // Load character 'A' into SBUF for transmission
while (!(SCON & 0x02)); // Wait for TI (Transmit Interrupt) flag
SCON &= ~0x02; // Clear TI flag
• Receiving Data:
Copy code
while (!(SCON & 0x01)); // Wait for RI (Receive Interrupt) flag
unsigned char received_data = SBUF; // Read received data from SBUF
SCON &= ~0x01; // Clear RI flag
• Enabling/Disabling Reception:
Copy code
SCON |= 0x10; // Enable reception by setting REN bit
SCON &= ~0x10; // Disable reception by clearing REN bit
3.Examine the logic operations combined with conditional branching in the 8051 microcontroller to
implement decision-making in embedded systems? Provide examples of using logic operations in
control flow.
LOGICAL OPERATIONS INTRO AND TYPES
Conditional branching allows the program to choose between different paths of execution based on
specific conditions. Common constructs include:
if Statement
• Executes a block of code if a condition is true.
• Example: Checking if a button is pressed.
if-else Statement
• Executes one block if the condition is true, another block if false.
• Example: Controlling an LED based on a switch state.
else if Ladder
• Handles multiple conditions in a hierarchical manner.
• Example: Detecting specific bit patterns from sensor inputs.
switch Statement
• Used for decision-making among multiple discrete cases.
• Example: Performing different actions based on user input.
EXAMPLES:
• if Statements
• if else Statements
• else if Ladder
• switch statements