0% found this document useful (0 votes)
54 views21 pages

Micro Control 4th Sem Notes

VTU notes Micro Control 4th sem notes

Uploaded by

Danesh Pujari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views21 pages

Micro Control 4th Sem Notes

VTU notes Micro Control 4th sem notes

Uploaded by

Danesh Pujari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Common Types of SFRs and Their Uses

1. Accumulator (A) - 0xE0


o The Accumulator is used for arithmetic and logic operations. Most instructions use the
Accumulator as one of the operands or as the destination for the result.
2. B Register (B) - 0xF0
o The B register is used mainly during multiplication and division operations. It can also
be used as a general-purpose register.
3. Program Status Word (PSW) - 0xD0
o The PSW register holds status bits, including the carry flag, auxiliary carry flag,
overflow flag, parity flag, and register bank select bits. These flags are affected by the
results of arithmetic and logical operations.
4. Stack Pointer (SP) - 0x81
o The Stack Pointer points to the top of the stack. It is used during subroutine calls and
interrupts to store the return addresses and other data. The stack grows upward in
memory, meaning the SP is incremented when data is pushed onto the stack.
5. Data Pointer (DPTR) - 0x82/0x83
o The Data Pointer is a 16-bit register used for external memory addressing. It consists
of two 8-bit registers: DPH (Data Pointer High) and DPL (Data Pointer Low).
6. Port Registers (P0, P1, P2, P3) - 0x80, 0x90, 0xA0, 0xB0
o These registers correspond to the four I/O ports (P0 to P3) of the 8051
microcontroller. Each port register is 8 bits wide, and each bit corresponds to a pin on
the port. The ports can be configured for input or output.
7. Timer/Counter Registers (TCON, TMOD, TL0, TL1, TH0, TH1)
o TCON (Timer Control) - 0x88: Controls the timers and external interrupts.
o TMOD (Timer Mode) - 0x89: Sets the mode of operation for the timers.
o TL0/TH0 (Timer 0 Low/High) - 0x8A/0x8C: 8-bit registers that hold the low
and high bytes of Timer 0.
o TL1/TH1 (Timer 1 Low/High) - 0x8B/0x8D: 8-bit registers that hold the low and
high bytes of Timer 1.
8. Interrupt Registers (IE, IP)
o IE (Interrupt Enable) - 0xA8: Enables or disables specific interrupts.
o IP (Interrupt Priority) - 0xB8: Sets the priority level of each interrupt.
9. Serial Communication Registers (SCON, SBUF)
o SCON (Serial Control) - 0x98: Controls the serial communication mode, baud
rate, and other serial communication settings.
o SBUF (Serial Buffer) - 0x99: Used for data transmission and reception in serial
communication. Data to be sent via the serial port is written to SBUF, and received
data is read from SBUF.
10. Power Control Register (PCON) - 0x87
o The PCON register controls the power modes of the 8051, such as idle mode, power-
down mode, and serial baud rate doubling.
11. Auxiliary Register (AUXR)
o AUXR - 0x8E: This register is used to control various auxiliary functions of the
microcontroller, such as memory expansion and timer clocking.
1. Pins 1-8 (P1.0 to P1.7)
 Port 1: This is an 8-bit bi-directional I/O port (P1.0 - P1.7). It can be used as input or output
and doesn’t have any dual function like other ports.
2. Pin 9 (RST)
 Reset: Active high reset input. When a high pulse is applied to this pin, it resets the
microcontroller.
3. Pins 10-17 (P3.0 to P3.7)
 Port 3: This is another 8-bit bi-directional
I/O port with dual functions. Some of the
alternate functions include:
o P3.0/RXD (Pin 10): Serial input.
o P3.1/TXD (Pin 11): Serial output.
o P3.2/INT0 (Pin 12): External interrupt 0.
o P3.3/INT1 (Pin 13): External interrupt 1.
o P3.4/T0 (Pin 14): Timer 0 external input.
o P3.5/T1 (Pin 15): Timer 1 external input.
o P3.6/WR (Pin 16): External memory
write.
o P3.7/RD (Pin 17): External memory read.
4. Pins 18-19 (XTAL2, XTAL1)
 Oscillator Input/Output: These pins are
connected to an external crystal oscillator or
resonator, which provides the clock
frequency to the microcontroller.
5. Pin 20 (GND)
 Ground: This is the ground reference for the microcontroller.
6. Pins 21-28 (P2.0 to P2.7)
 Port 2: This is another 8-bit bi-directional I/O port with dual functions. When interfacing
with external memory, it acts as the higher-order address bus.
7. Pin 29 (PSEN)
 Program Store Enable: This is the read strobe for external program memory. It is
activated during external code fetches.
8. Pin 30 (ALE/PROG)
 Address Latch Enable: This is used to latch the lower byte of the address when interfacing
with external memory.
 Programming Enable: During programming mode, this pin acts as the program pulse
input.
9. Pin 31 (EA/VPP)
 External Access Enable: This pin is used to enable or disable external memory. When held
high, the microcontroller uses internal memory, and when low, it accesses external memory.
 Programming Voltage: During programming mode, this pin is used to supply the
programming voltage.
10. Pins 32-39 (P0.0 to P0.7)
 Port 0: This is an 8-bit bi-directional I/O port with dual functions. It can act as a lower-order
address/data bus in external memory interfacing.
11. Pin 40 (VCC)
 Power Supply: This is the main power supply pin for the microcontroller, typically +5V.
1. CPU (Central Processing Unit)
 The CPU is the brain of the microcontroller, responsible for executing instructions from the
program memory.
2. Oscillator and Clock Circuit
 This block generates the clock signal that drives the microcontroller’s operations. The
oscillator circuit is connected to external crystal or resonator pins (XTAL1 and XTAL2).
3. Program Memory (ROM)
 The 8051 has on-chip Read-Only Memory (ROM) for storing the program code. The size of
this ROM varies (e.g., 4 KB in 8051).
4. Data Memory (RAM)
 Internal RAM is used to store data temporarily during program execution. The 8051
typically has 128 bytes of internal RAM.
5. Special Function Registers (SFRs)
 SFRs are used for controlling and monitoring the operation of the 8051. These registers
include the accumulator, B register, PSW, SP, and others.
6. I/O Ports (P0, P1, P2, P3)
 The 8051 has four 8-bit I/O ports (P0, P1, P2, P3) used for interfacing with external devices.
7. Timer/Counters
 The 8051 has two 16-bit timers/counters (Timer 0 and Timer 1). They can be used for
timing events or counting external events.
8. Serial Port
 The 8051 has a serial communication interface, which is used for serial communication with
other devices. It includes a UART for asynchronous communication.
9. Interrupt Control
 The 8051 supports five interrupt sources, including two external interrupts (INT0 and
INT1), two timer interrupts (T0 and T1), and one serial communication interrupt.
10. Bus Control Unit
 This unit controls the data flow between the CPU and external devices or memory.
11. Power Control
 This block manages the power consumption of the microcontroller, including features like
power-down mode and idle mode.
8051 Addressing Modes
. Immediate Addressing Mode
 Description: The operand is specified directly in the instruction itself. This means that the
data to be operated on is provided immediately within the instruction.
 Syntax: MOV A, #25H
 Example: MOV A, #0F3H
o This instruction moves the hexadecimal value 0F3H directly into the accumulator A.
2. Register Addressing Mode
 Description: The operand is stored in one of the registers R0 to R7 of the currently
selected register bank.
 Syntax: MOV A, Rn (where n is from 0 to 7)
 Example: MOV A, R3
o This instruction moves the content of register R3 into the accumulator A.
3. Direct Addressing Mode
 Description: The operand's address is specified directly in the instruction. The operand
can reside in the internal RAM, including the Special Function Registers (SFRs).
 Syntax: MOV A, 30H
 Example: MOV A, 40H
o This instruction moves the content of the internal RAM location 40H into the
accumulator A.
4. Indirect Addressing Mode
 Description: The address of the operand is stored in a register (R0 or R1) or in the Data
Pointer (DPTR). The instruction specifies the register holding the address rather than the
address itself.
 Syntax: MOV A, @Ri (where i is 0 or 1 for registers R0 or R1)
 Example 1: MOV A, @R0
o This instruction moves the content of the internal RAM location whose address is in
R0 into the accumulator A.
 Example 2: MOVX A, @DPTR
o This instruction moves the content of the external memory location pointed to by
DPTR into the accumulator A.
5. Indexed Addressing Mode
 Description: The operand’s address is obtained by adding an offset value (often from the
PC or DPTR) to a base value held in the accumulator or a register.
 Syntax: MOVC A, @A+DPTR or MOVC A, @A+PC
 Example 1: MOVC A, @A+DPTR
o This instruction moves the content of the memory location addressed by DPTR + A
into the accumulator A. Typically used for accessing lookup tables.
 Example 2: MOVC A, @A+PC
o This instruction moves the content of the memory location addressed by PC + A into
the accumulator A.

Time Delay in 8051 C


Time delays are crucial in embedded systems,
especially for tasks like blinking LEDs or
generating pulse signals. In 8051 C, delays can be
generated using loops or timer functions.
Example: Delay using a simple loop
void delay(unsigned int time) {
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 1275; j++); // Approximate delay
}
Blinking an LED with Blinking LEDs on Port ORG 0000H ; Start at address 0
1 MAIN:
------------------------------------------------------ MOV P1, #0FFH ; Set all bits of Port 1
high
ACALL DELAY ; Call delay subroutine
Addition of two numbers and storing the CPL P1 ; Toggle all bits of Port 1
result. SJMP MAIN ; Repeat indefinitely
ORG 0000H ; Start at address 0000H -----DELAY:
MOV A, #25H ; Load 25H into accumulator A MOV R1, #255 ; Outer loop
MOV R1, #34H ; Load 34H into register R1 DLOOP1:
ADD A, R1 ; Add R1 to A MOV R2, #255 ; Inner loop
MOV 40H, A ; Store result in memory DLOOP2:
location 40H DJNZ R2, DLOOP2 ; Decrement R2 until
END ; End of program zero
DJNZ R1, DLOOP1 ; Decrement R1 until
zero
RET ; Return to main program
END
Accessing Code ROM Space in 8051 C write an 8051 c program to convert ascii
The 8051 microcontroller has separate digit 4 and 7 to packed bcd and display
memory spaces for code (ROM) and data them on P1
(RAM). Accessing code ROM space in 8051 C #include <reg51.h>
is necessary when reading stored constants or
lookup tables. void main() {
Example: Accessing data in code space unsigned char ascii_digit1 = '4';
#include <8051.h> unsigned char ascii_digit2 = '7';
unsigned char packed_bcd;
unsigned char code data_table[] = {0x12,
0x34, 0x56, 0x78}; // Convert ASCII '4' to BCD
packed_bcd = ascii_digit1 - 0x30;
void main() { P1 = packed_bcd;
unsigned char value; // Delay for some time (adjust as needed)
_delay_ms(1000);
value = data_table[2]; // Access the third // Convert ASCII '7' to BCD
element in code ROM packed_bcd = ascii_digit2 - 0x30;
P1 = value; // Output the value to Port 1 P1 = packed_bcd;
while(1); while (1); // Infinite loop
} }

Data Conversion Program in 8051 C


Example: BCD to Decimal Conversion
#include <8051.h>
unsigned char bcd_to_decimal(unsigned char bcd);
void main() {
Unsigned char bcd_value = 0x25; // BCD value for 25
Unsigned char decimal_value;
Decimal value = bcd_to_decimal(bcd_value); // Convert BCD to decimal
P1 = decimal value; // Output decimal value to Port 1
while (1);
}
unsigned char bcd_to_decimal(unsigned char bcd)
{
return ((bcd >> 4) * 10) + (bcd & 0x0F);
RS-232 Handshaking Signals
RS-232 is a standard serial communication protocol that uses several control signals for reliable
data transmission. The most common handshaking signals are:
1. Request to Send (RTS): This signal is asserted by the transmitter to indicate that it is
ready to send data.
2. Clear to Send (CTS): This signal is asserted by the receiver to indicate that it is ready to
receive data.
3. Data Terminal Ready (DTR): This signal is asserted by the data terminal equipment
(DTE) to indicate that it is ready to communicate.
4. Data Set Ready (DSR): This signal is asserted by the data circuit-terminating equipment
(DCE) to indicate that it is ready to communicate.
Purpose of MAX232
The MAX232 is an integrated circuit (IC) that converts TTL-level signals (0V to 5V) to RS-232
levels (-12V to +12V) and vice versa. It's essential for interfacing 8051 microcontrollers, which
operate at TTL levels, with RS-232 devices like modems, computers, and other serial devices.
Key functions of MAX232:
 Level Shifting: Converts TTL levels to RS-232 levels and vice versa.
 Driver and Receiver: Provides both transmitter and receiver circuits for serial
communication.
 Power Supply: Operates from a single 5V power supply.
 Handshaking: Supports RS-232 handshaking signals (RTS, CTS, DTR, DSR).

Interfacing a Keyboard with the 8051


Microcontroller
A typical keyboard setup involves a matrix
arrangement of rows and columns. Each key
intersection corresponds to a unique
combination of row and column. By scanning
each row and column, we can identify the
pressed key.
Hardware Connection
 Rows: Connect the rows of the
keyboard to the input pins of the 8051
(e.g., P1.0-P1.7).
 Columns: Connect the columns of
the keyboard to the output pins of the
8051 (e.g., P2.0-P2.7).
Software Implementation
1. Initialization:
o Set the input and output pins of the
8051 accordingly.
o Configure the internal pull-up
resistors on the input pins (P1.0-P1.7) to ensure a stable high level when no key is pressed.
2. Scanning:
o Row-by-Row Scanning:
 Set one row pin to low (0).
 Read the values of all column pins.
 If any column pin is low (0), a key is pressed at the intersection of that row and column.
 Set the row pin back to high (1).
 Repeat for all rows.
3. Identifying the Key:
o Once a key press is detected, you can use the row and column numbers to identify the specific
key pressed.
o For example, if the key press is detected on row 3 and column 2, it might correspond to the
letter 'E' on a standard QWERTY keyboard.
1. Data Transfer Instructions
 MOV destination, source: Transfers data from the source to the destination.
o Example: MOV A, #30H – Move the immediate value 30H into the accumulator.
 MOVC A, @A+DPTR: Moves data from program memory (code memory) to the
accumulator, using the DPTR register and the contents of the accumulator as an offset.
 MOVX A, @DPTR: Transfers data from external memory to the accumulator.
 PUSH direct: Pushes the contents of a direct register onto the stack.
 POP direct: Pops the top of the stack into a direct register.
2. Arithmetic Instructions
 ADD A, source: Adds the source to the accumulator.
o Example: ADD A, R0 – Adds the contents of R0 to the accumulator.
 ADDC A, source: Adds the source and the carry flag to the accumulator.
 SUBB A, source: Subtracts the source and the carry flag from the accumulator.
 MUL AB: Multiplies the contents of the accumulator by the contents of register B. The
result is stored in A (lower byte) and B (higher byte).
 DIV AB: Divides the accumulator by register B. The quotient is stored in A, and the
remainder is stored in B.
 INC destination: Increments the destination by 1.
 DEC destination: Decrements the destination by 1.
3. Logical Instructions
 ANL A, source: Performs a logical AND between the accumulator and the source.
 ORL A, source: Performs a logical OR between the accumulator and the source.
 XRL A, source: Performs a logical XOR between the accumulator and the source.
 CLR A: Clears the accumulator (sets it to 0).
 CPL A: Complements the accumulator (inverts all bits). JMP: Unconditional
 RL A: Rotates the accumulator left. jump.
 RR A: Rotates the accumulator right. JC: Jump if carry flag
 RLC A: Rotates the accumulator left through the carry flag. is set.
 RRC A: Rotates the accumulator right through the carry flag. JNC: Jump if carry
4. Branching Instructions flag is clear.
 SJMP label: Short jump to the specified label (relative to JB: Jump if bit is set.
the current PC). JNB: Jump if bit is
 LJMP address: Long jump to the specified 16-bit address. clear.
 AJMP address: Absolute jump within the same 2KB memory block.
 JZ label: Jumps to the specified label if the accumulator is zero.
 JNZ label: Jumps to the specified label if the accumulator is not zero.
 CJNE A, source, label: Compares the accumulator with the source and jumps to the label
if they are not equal.
 DJNZ reg, label: Decrements the register and jumps to the label if the result is not zero.
 NOP: No operation (used to create delays or for timing).
5. Bit Manipulation Instructions
 SETB bit: Sets the specified bit to 1.
 CLR bit: Clears the specified bit to 0.
 CPL bit: Complements the specified bit.
 ANL C, bit: Performs a logical AND between the carry flag and the specified bit.
 ORL C, bit: Performs a logical OR between the carry flag and the specified bit.
 MOV C, bit: Moves the specified bit into the carry flag.
 MOV bit, C: Moves the carry flag into the specified bit.
6. Control Instructions
 RET: Returns from a subroutine.
 RETI: Returns from an interrupt service routine.
 CALL address: Calls a subroutine located at the specified address.
 ACALL address: Calls a subroutine within the same 2KB memory block.
Block Diagram of 8255
1. Data Bus Buffer (D0-D7)
o Purpose: Buffers the data between the 8255 and the microprocessor's data bus.
o Function: Reads data from or writes data to the data bus.
2. Control Register
o Purpose: Stores the control word that configures the operation of the 8255.
o Function: Configures the modes and directions for Port A, Port B, and Port C.
3. Port A (PA0-PA7)
o Purpose: An 8-bit bidirectional port.
o Function: Can be configured as input or output depending on the control word
settings.
4. Port B (PB0-PB7)
o Purpose: Another 8-bit bidirectional port.
o Function: Can be configured as input or output.
5. Port C (PC0-PC7)
o Purpose: An 8-bit bidirectional port with additional control lines.
o Function: Can be split into upper and lower halves (Port C Upper and Port C Lower)
to provide additional functionality.
6. Port C Upper (PC4-PC7)
o Purpose: The upper nibble of Port C.
o Function: Often used for handshaking or status signals in bidirectional mode.
7. Port C Lower (PC0-PC3)
o Purpose: The lower nibble of Port C.
o Function: Can be used as general-purpose I/O or for handshaking.
8. Address Decoder
o Purpose: Decodes the address lines to select the appropriate register (Port A, Port B,
Port C, or Control Register).
o Function: Ensures that the correct register is accessed based on the address sent by
the microprocessor.
9. Read/Write Control Logic
o Purpose: Manages read and write operations to the 8255.
o Function: Controls the flow of data to and from the 8255’s internal registers based
on the read/write signals from the microprocessor.
10. Chip Select (CS)
o Purpose: Activates the 8255 for communication.
o Function: Ensures that the 8255 responds only when selected by the microprocessor.
Basics of ADC Interfacing
Analog-to-Digital Converters (ADCs) convert an analog input signal into a digital number. To
interface an ADC with the 8051, you'll need to handle several aspects:
 ADC Connections: Connect the ADC to the 8051’s input/output ports.
 Control Signals: Manage control signals like start, clock, and chip select.
 Data Reading: Read the digital value from the ADC.

2. Types of ADCs and Connections


a. Parallel ADC (e.g., ADC0808)
ADC0808 is an 8-bit ADC with multiple channels. It provides an 8-bit parallel data output.
Connections:
 Analog Inputs (A0-A7): Connect to Port 1 (P1.0 - P1.7).
 Address Lines (A, B, C): Connect to Port 0 (P0.0 - P0.2).
 Control Signals (START, EOC): Connect to Port 2 pins.
 Data Lines (D0-D7): Connect to microcontroller data ports for reading the ADC value.

b. Serial ADC (e.g., MAX1112)


MAX1112 is a 12-bit ADC with a serial interface. It uses SPI-like communication.
Connections:
 MOSI (Master Out Slave In): Connect to microcontroller output pin.
 MISO (Master In Slave Out): Connect to microcontroller input pin.
 SCLK (Serial Clock): Connect to microcontroller clock pin.
 CS (Chip Select): Connect to a control pin.

1.Accumulator (ACC) Register


Bit Bit Bit Bit Bit Bit Bit Bit
7 6 5 4 3 2 1 0
D D D D D D D D
 Bits 7-0: Accumulator data bits used for arithmetic and logical operations.
 The Accumulator (ACC) register is a crucial 8-bit register in the 8051 microcontroller. It
serves as a primary workspace for many arithmetic, logical, and data transfer operations.
Key Roles of the Accumulator:
1. Arithmetic Operations:
o The ACC is involved in most arithmetic operations, such as addition, subtraction,
multiplication, and division.
o Intermediate results and final results of these operations are typically stored in the ACC.
2. Logical Operations:
o The ACC is used for logical operations like AND, OR, XOR, and NOT.
o These operations are often performed on data stored in the ACC or other registers.
3. Data Transfer:
o The ACC is the source or destination for many data transfer operations.
o Data can be moved between the ACC and other registers, memory locations, or external
devices.
4. Program Control:
o The ACC can be used to store program counter values for conditional jumps and loops.
o It also plays a role in interrupt handling and other control flow mechanisms.
B Register
Bit Bit Bit Bit Bit Bit Bit Bit
7 6 5 4 3 2 1 0
D D D D D D D D
 The B register is an 8-bit general-purpose register. it plays a specific role in two key
arithmetic operations that is multiplication and division operations.
1. Multiplication (MUL AB):
 The MUL AB instruction multiplies the contents of the ACC with the contents of the B
register.
 The 16-bit result of this multiplication is stored in two registers:
o The lower 8 bits are stored in the ACC.
o The higher 8 bits are stored in the B register.
2. Division (DIV AB):
 The DIV AB instruction divides the 16-bit dividend (stored in the ACC and B registers) by
the 8-bit divisor (stored in the ACC).
 The quotient is stored in the ACC, and the remainder is stored in the B register.

4. Program Status Word (PSW) Register Structure


Bit Bit Bit Bit Bit Bit Bit Bit
7 6 5 4 3 2 1 0
CY AC F0 RS1 RS0 OV UD P
The Program Status Word (PSW) register is an 8-bit register in the 8051 microcontroller that stores
various status flags and control bits. These flags reflect the current state of the processor and
influence the execution of instructions.
1. CY (Carry Flag - Bit 7):
o This flag is set if there is a carry out from the most significant bit (MSB) in an
arithmetic operation or if there is a borrow in subtraction. It's used to detect overflow
in unsigned arithmetic operations.
2. AC (Auxiliary Carry Flag - Bit 6):
o The auxiliary carry flag is set if there is a carry from the lower nibble (bit 3) to the
higher nibble (bit 4) during an arithmetic operation. This flag is used in Binary Coded
Decimal (BCD) operations.
3. F0 (Flag 0 - Bit 5):
o This is a general-purpose flag that can be used by the programmer. It doesn’t affect the
operation of the microcontroller.
4. RS1 and RS0 (Register Bank Select Bits - Bits 4 and 3):
o These bits are used to select one of the four register banks. Each register bank contains
eight 8-bit registers (R0 to R7).
o 00: Register Bank 0
o 01: Register Bank 1
o 10: Register Bank 2
o 11: Register Bank 3
5. OV (Overflow Flag - Bit 2):
o The overflow flag is set if there is an overflow during a signed arithmetic operation,
such as when the result of an addition or subtraction is too large to fit in the 7-bit
signed number format.
6. - (Reserved - Bit 1):
o This bit is not used and is reserved for future use.
7. P (Parity Flag - Bit 0):
o The parity flag is set if the number of 1-bits in the accumulator (A register) is odd. It is
cleared if the number of 1-bits is even. This flag is useful for error detection in data
communication.
3. Data Pointer (DPTR) Register
Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit Bit
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

DPL (Low Byte) DPH (High Byte)

 DPTR IS 16-bit Register. It is made up of two 8-bit registers called DPH and DPL. Separate
addresses are assigned to each of DPH and DPL. These 8-bit registers are used for the
storing the memory addresses that can be used to access internal and external data/code
 DPL (Data Pointer Low): Holds the lower 8 bits of the 16-bit address
 DPH (Data Pointer High): Holds the higher 8 bits of the 16-bit address.

Timer Control (TCON) Register Functions of the TCON Register:


 Starting and Stopping Timers: The TR1 and TR0 bits control the start and stop of
Timer 1 and Timer 0, respectively.
 Enabling and Disabling Interrupts: The IE1 and IE0 bits enable or disable interrupts
for Timer 1 and Timer 0, respectively.
 Selecting Interrupt Types: The IT1 and IT0 bits select the interrupt type for Timer 1 and
Timer 0, respectively.
 Monitoring Timer Overflows: The TF1 and TF0 flags indicate when Timer 1 and Timer 0
overflow, respectively.
Bit Bit Bit Bit Bit Bit Bit Bit  Bit 7: TF1 (Timer 1 Overflow
7 6 5 4 3 2 1 0 Flag) - Set when Timer 1 overflows.
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0  Bit 6: TR1 (Timer 1 Run Control
Bit) - Starts/stops Timer 1.
 Bit 5: TF0 (Timer 0 Overflow Flag) - Set when Timer 0 overflows.
 Bit 4: TR0 (Timer 0 Run Control Bit) - Starts/stops Timer 0.
 Bit 3: IE1 (External Interrupt 1 Edge Flag) - Set on external interrupt 1.
 Bit 2: IT1 (External Interrupt 1 Type Control Bit) - Edge trigger type for interrupt 1.
 Bit 1: IE0 (External Interrupt 0 Edge Flag) - Set on external interrupt 0.
 Bit 0: IT0 (External Interrupt 0 Type Control Bit) - Edge trigger type for interrupt 0.

Serial Control (SCON) Register Functions of the SCON Register


 Selecting Mode 1 or Mode 2: The SM2 bit selects the mode of operation. Mode 1 is used
for asynchronous serial communication, while Mode 2 is used for synchronous serial
communication.
 Enabling or Disabling the Receiver: The REN bit enables or disables the receiver.
 Setting the Data Format: The TB8 and RB8 bits are used to configure the data format
(8-bit or 9-bit).
 Monitoring Transmission and Reception: The TI and RI flags indicate the status of
transmission and reception.
 Initiating Transmission and Reception: The TD and RD bits initiate transmission and
reception, respectively.

Bit 7
Bit Bit Bit Bit Bit Bit Bit
6 5 4 3 2 1 0
SM0 SM1 SM2 REN TB8 RB8 TI RI
 Bit 7: SM0 (Serial Mode Bit 0) - Mode selection.
 Bit 6: SM1 (Serial Mode Bit 1) - Mode selection.
 Bit 5: SM2 (Serial Mode 2 Bit) - Multiprocessor communication.
 Bit 4: REN (Receive Enable Bit) - Enables serial reception.
 Bit 3: TB8 (Transmit Bit 8) - 9th bit of data to transmit.
 Bit 2: RB8 (Receive Bit 8) - 9th bit of received data.
 Bit 1: TI (Transmit Interrupt Flag) - Set when transmission is complete.
 Bit 0: RI (Receive Interrupt Flag) - Set when data is received.
8. Timer Mode (TMOD) Register ; TMOD is an 8-bit register used to set timer mode of timer0
and timer1.

Bit 7 Bit 6
Bit Bit Bit 3 Bit 2 Bit Bit
5 4 1 0
GATE C1/T1 M1 M0 GATE C1/T0 M1 M0
TIM1 TIM0 TOM1 TOM0
Its lower 4 bits are used for Timer0 and the upper 4 bits are used for Timer1
Bit 7,3 – GATE:
1 = Enable Timer/Counter only when the INT0/INT1 pin is high and TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.
Bit 6,2 - C/(Counter/Timer): Timer or Counter select bit
If C/T = 0: The timer operates as a timer, incrementing with the internal clock.
If C/T = 1: The timer operates as a counter, incrementing with external pulses on T0 or T1 pins.
Bit 5:4 & 1:0 - M1:M0: Timer/Counter mode select bit
M1, M0 (Mode Selection): These bits define the mode of the timer. There are four modes:
M1 M0 Mode
0 0 Mode 0: 13 bit mode (seldom used).
0 1 Mode 1: 16-bit mode
1 0 Mode 2: 8-bit mode (with auto-reload feature)
1 1 Mode 3: 8-bit split timer

Interrupt Priority (IP) Register


 Bit 7: PS (Priority of Serial Interrupt) - Priority of serial interrupt.
 Bit 6: PT1 (Priority of Timer 1 Interrupt) - Priority of Timer 1 interrupt.
 Bit 5: PT0 (Priority of Timer 0 Interrupt) - Priority of Timer 0 interrupt.
 Bit 4: PX1 (Priority of External Interrupt 1) - Priority of external interrupt 1.
 Bit 3: PX0 (Priority of External Interrupt 0) - Priority of external interrupt 0.

Default Behavior:
 All interrupts are initially set to low priority (0) by default.
 The 8051 services interrupts based on the natural priority order if all are low-priority:
o External INT0
o Timer 0
Bit Bit Bit Bit Bit Bit Bit Bit
o External INT1
7 6 5 4 3 2 1 0
o Timer 1
o Serial
PS PT1 PT0 PX1 PX0 - - -
Assigning Priorities:
 A bit set to 1 in the IP register assigns a high priority to the corresponding interrupt.
 If multiple interrupts occur:
o High-priority interrupts are serviced first.
o Among interrupts with the same priority level, the natural priority order is followed.
High vs Low Priority:
 High-priority interrupts can interrupt an ISR (Interrupt Service Routine) for a low-
priority interrupt.
 Low-priority interrupts cannot interrupt an ISR of a high-priority interrupt.
Stack Pointer (SP) Register
Bit Bit Bit Bit Bit Bit Bit Bit
7 6 5 4 3 2 1 0
D D D D D D D D
 Bits 7-0: Address of the top of the stack.
Functions of the Stack Pointer (SP)
The Stack Pointer (SP) is a special-purpose register in a microcontroller or processor that
keeps track of the top of the stack in memory. It plays a crucial role in stack operations, which are
fundamental for managing function calls, local variables, and interrupt handling.
Stack Operations:
o Push Operation: When a new item is pushed onto the stack, the SP is decremented (in a
stack that grows downwards) or incremented (in a stack that grows upwards), and the item
is written to the memory location pointed to by the SP.
o Pop Operation: When an item is popped from the stack, it is read from the memory
location pointed to by the SP, and then the SP is incremented (or decremented) to reflect
the removal.
#include <reg51.h> // Function to initialize the LCD
Void lcd_init()
// define LCD control pins {
sbit RS = P2^0; lcd_command(0x02); // Initialize in 4-bit
sbit E = P2^1; mode
sbit D4 = P2^4; lcd_command(0x28); // 2 line, 5x7 matrix
sbit D5 = P2^5; lcd_command(0x0C); // Display on, cursor
sbit D6 = P2^6; off
sbit D7 = P2^7; lcd_command(0x06); // Increment cursor
lcd_command(0x01); // Clear display
// Function to provide delay delay(2);
Void delay (unsigned int time) }
{ // Function to send a string to the LCD
Unsigned int i, j; Void lcd_string(char *str)
For (i = 0; i < time; i++) {
For (j = 0; j < 1275; j++); While (*str) {
} lcd_data(*str++);
// Function to send a command to the }
LCD }
Void lcd_command(unsigned char Void main()
cmd) {
{ lcd_init();
RS = 0; // Command mode // Initialize the LCD
D4 = (cmd & 0x10) >> 4; lcd_command(0x80); // Move the cursor to
D5 = (cmd & 0x20) >> 5; the first line
D6 = (cmd & 0x40) >> 6; lcd_string("Hello, World!");
D7 = (cmd & 0x80) >> 7; lcd_command(0xC0); // Move the cursor to
E = 1; the second line
delay (1); lcd_string("LCD Interface");
E = 0;
while(1); // Infinite loop to keep the display
D4 = cmd & 0x01; on
D5 = (cmd & 0x02) >> 1; }
D6 = (cmd & 0x04) >> 2;
D7 = (cmd & 0x08) >> 3;
E = 1;
delay (1);
E = 0;
}
Components Needed:
1. 8051 Microcontroller Stepper Motor potentiometer (for speed control)
2. ULN2003A or any other suitable motor driver IC
Basic Concept:
 Stepper Motor: A stepper motor moves in discrete steps, with the rotation direction
controlled by the sequence in which the coils are energized.
 ULN2003A Driver: The 8051 microcontroller cannot directly drive a stepper motor due
to its current and voltage limitations, so a driver like ULN2003A is used.
 Speed Control: This can be achieved by adjusting the delay between steps.
 Direction Control: Changing the sequence of the pulses to the motor coils will reverse the
direction.
Connection Setup:
1. Connect the stepper motor to the ULN2003A driver IC.
2. Connect the inputs of the ULN2003A to four output pins of the 8051 (P1.0 to P1.3).
3. Use two buttons for direction control (one for clockwise and another for counterclockwise).
4. Optionally, use a potentiometer connected to an ADC pin for speed control.

 #include <reg51.h> }
  }
 // Define the connections to the
motor driver  // Function to move stepper motor
 sbit IN1 = P1^0; counter-clockwise
 sbit IN2 = P1^1;  void
 sbit IN3 = P1^2; rotate_counter_clockwise(unsigned
 sbit IN4 = P1^3; int speed)
  {
 // Define buttons for direction  IN1 = 0; IN2 = 0; IN3 = 0; IN4 = 1;
control  delay(speed);
 sbit CW_BUTTON = P2^0; //  IN1 = 0; IN2 = 0; IN3 = 1; IN4 = 0;
Clockwise button  delay(speed);
 sbit CCW_BUTTON = P2^1; //  IN1 = 0; IN2 = 1; IN3 = 0; IN4 = 0;
Counter-Clockwise button  delay(speed);
  IN1 = 1; IN2 = 0; IN3 = 0; IN4 = 0;
 // Function to provide delay  delay(speed);
 void delay(unsigned int ms) {  }
 unsigned int i, j;  void main() {
 for(i=0; i<ms; i++)  unsigned int speed = 5; // Adjust this
 for(j=0; j<1275; j++); value for speed control
 } 
 // Function to move stepper motor  while(1) {
clockwise  // Check for direction control
void rotate_clockwise(unsigned  if(CW_BUTTON == 0) { // Button
int speed) pressed for clockwise rotation
{  rotate_clockwise(speed);
 IN1 = 1; IN2 = 0; IN3 = 0; IN4 = 0;  }
 delay(speed);  if(CCW_BUTTON == 0) { // Button
 IN1 = 0; IN2 = 1; IN3 = 0; IN4 = 0; pressed for counter-clockwise rotation
 delay(speed);  rotate_counter_clockwise(speed);
 IN1 = 0; IN2 = 0; IN3 = 1; IN4 = 0;  }
 delay(speed);  }
 IN1 = 0; IN2 = 0; IN3 = 0; IN4 = 1;
Stepper motor interface for direction and
 delay(speed); speed control.
Calculate the memory map of AT89C51 microcontroller with 4 KB of on chip ROM
AT89C51 Memory Map with 4KB On-Chip ROM
The AT89C51 microcontroller has a specific memory map that defines how its memory is
organized. Here's a breakdown of the memory map, considering a 4KB on-chip ROM:
Program Memory (ROM):
 Size: 4KB (4096 bytes)
 Address Range: 0000H to 0FFFh  0000H - 0FFFh: Program Memory
 Purpose: Stores the program instructions. (ROM)
Data Memory (RAM):  00H - 7FH: Data Memory (RAM)
 Size: 128 bytes  80H - FFH: Special Function
 Address Range: 00H to 7FH Registers (SFRs)
 Purpose: Stores temporary data and variables.
Special Function Registers (SFRs):
 Size: 128 bytes
 Address Range: 80H to FFH
 Purpose: Controls the operation of the microcontroller's peripherals, timers, serial port,
and other components.
XTAL=22MHz, calculate the TH1 value for the following baud rates(i)1200 ii)2400
we need to consider the following formula:
TH1 = (256 - (XTAL / 12 / 32 / baud rate))
Where:
 TH1 is the value to be loaded into the TH1 register.
 XTAL is the crystal frequency (22 MHz in this case).
 Baud rate is the desired baud rate.
Calculation for Baud Rate 1200: Calculation for Baud Rate 2400:
TH1 = (256 - (22000000 / 12 / 32 / 1200)) TH1 = (256 - (22000000 / 12 / 32 / 2400))
TH1 = (256 - 120.83) TH1 = (256 - 60.42)
TH1 ≈ 135 TH1 ≈ 195

Microcontroller
 All-in-one: Integrates a CPU, memory (RAM and ROM), input/output ports, and
peripherals onto a single chip.
 Dedicated Tasks: Designed for specific tasks or embedded systems, such as controlling
appliances, automotive systems, and industrial automation.
 Lower Processing Power: Generally less powerful than microprocessors.
 Lower Cost: Due to their simpler architecture and mass production, they are often more
cost-effective.
 Examples: 8051, Arduino, Raspberry Pi Pico
Microprocessor
 CPU Only: Consists primarily of a CPU, requiring external components like memory and
I/O devices.
 General-purpose: More flexible and can be used for a wide range of applications,
including personal computers, servers, and smartphones.
 Higher Processing Power: Offers higher processing power and performance compared
to microcontrollers.
 Higher Cost: More complex architecture and additional components contribute to a
higher cost.
 Examples: Intel Core i7, AMD Ryzen, ARM Cortex-A series
Using DAC interface, write a Draw the interfacing diagram of 0808 ADC with
program to generate staircase microcontroller and write a program to interface
waveform. ADC . Assume Channel 3 to read the analog data.
Assume number of steps to be
5. ; Define port assignments (adjust as needed)
ALE EQU P3.3
SC EQU P3.4
; Initialize P2 as output port EOC EQU P3.5
MOV P2, #00H ADDA EQU P3.0
ADDB EQU P3.1
; Define the step size (255/5 = ADDC EQU P3.2
51)
MOV R0, #51H ; Initialize port directions
; Loop to generate the staircase MOV P3, #00H ; Set P3 as output port
waveform
LOOP: ; Select channel 3 (binary 011)
MOV A, #00H MOV ADDA, #0
MOV R1, #5 ; Number of steps MOV ADDB, #1
MOV ADDC, #1
STEP:
ADD A, R0 ; Start conversion
MOV P2, A MOV ALE, #1 ; Latch address
ACALL DELAY ; Call delay MOV SC, #1 ; Start conversion
subroutine MOV ALE, #0 ; Clear ALE
DJNZ R1, STEP
; Wait for conversion to complete
SJMP LOOP ; Repeat the LOOP:
process MOV C, EOC
JC LOOP ; Jump if conversion is not complete
; Delay subroutine
DELAY: ; Read the result
MOV R2, #250 ; Delay loop MOV P1, #0FF ; Set P1 as input port
counter MOV R0, P1 ; Store the 8-bit result in R0
DLY1:
MOV R3, #250 ; You can now use R0 to process the digital value
DLY2:
DJNZ R3, DLY2
DJNZ R2, DLY1
RET
Circuit of “Interfacing ADC0808
with 8051” is little complex which
contains more connecting wire for
connecting device to each other. In this
circuit we have mainly used AT89s52 as
8051 microcontroller, ADC0808,
Potentiometer and LCD.
*A 16x2 LCD is connected with 89s52
microcontroller in 4-bit mode. Control
pin RS, RW and En are directly
connected to pin P2.0, GND and P2.2.
And data pin D4-D7 is connected to pins
P2.4, P2.5, P2.6 and P2.7 of 89s52.
ADC0808 output pin are directly connected to port P1 of AT89s52. Address line pins ADDA, ADDB,
AADC are connected at P3.0, P3.1, and P3.2.
*ALE (Address latch enable), SC (Start conversion), EOC (End of conversion), OE (Output enable)
and clock pins are connected at P3.3, P3.4, P3.5, P3.6 and P3.7.
*And here we have used three potentiometers connected at pin 26, 27, and 28 of ADC0808.
*A 9 volt battery and a 5 volt voltage regulator namely 7805 are used for powering the circuit.

#include <reg51.h> Calculations:


void init_serial() {
SCON = 0x50; // 8-bit, variable baud rate Baud Rate Setting:
TMOD = 0x20; // Timer 1 in mode 2  Desired baud rate: 9600 bps
TH1 = 0xFD; // Set baud rate to 9600 bps  Timer 1 clock frequency: 11.0592 MHz / 12
(adjust as needed) = 921.6 kHz
TL1 = 0xFD;  Timer 1 counts up to 256.
TR1 = 1; // Start Timer 1  Time period of one count: 1 / 921.6 kHz =
} 1.085 us
Write 8051C program to
void main() {  Time period of 256 counts: 1.085 us * 256
send the message ‘VTU’
init_serial(); = 278.4 us
serially at 9600 baud
while (1) {  To achieve 9600 bps, we need a bit time of
rate and do it
SBUF = 'V'; 1/9600 = 104.17 us.
continuously. Assume 8
while (!TI);  Required timer count: 104.17 us / 1.085 us
bit data with 1 stop and
TI = 0; ≈ 96
1 start bit. Show the
SBUF = 'T';  TH1 = 256 - 96 = 160 ≈ 0FAH
calculations of
while (!TI); SCON Configuration:
TMOD,TH1and SCON
TI = 0;  SCON = 0x50:
SBUF = 'U'; o 8-bit data
while (!TI); o Variable baud rate
TI = 0; o Mode 1 operation (UART mode)
}
}
4-Phase Unipolar Stepper Motor: Construction and Working
A 4-phase unipolar stepper motor consists of a stator and a rotor. The stator has four coils
arranged in pairs, and the rotor has a permanent magnet.
Working:
1. Energizing Coils: By energizing the coils in a specific sequence, a magnetic field is
created, attracting the rotor's permanent magnet.
2. Rotor Movement: As the magnetic field shifts due to the sequential energization of the
coils, the rotor aligns itself with the new magnetic field, resulting in a step-by-step rotation.
3. Step Angle: The step angle, which is the angular displacement of the rotor per step,
depends on the number of stator poles. In a 4-phase unipolar stepper motor with a step
angle of 2°, the rotor moves 2 degrees for each step.
8051 Program to Run the Stepper Motor #include <reg51.h>
62° Clockwise:
void send_char(unsigned char ch);
; Define port assignments void send_string(const char *str);
MOV P1, #00H ; Initialize port 1 as output
void main() {
; Calculate the number of steps required SCON = 0x50; // 8-bit, variable baud
MOV R0, #31 ; 62 degrees / 2 degrees/step = 31 rate
steps TMOD = 0x20; // Timer 1 in mode 2
(8-bit auto-reload)
LOOP: TH1 = 0xFD; // Set baud rate (9600
MOV P1, #01H ; Step 1 for 11.0592 MHz crystal)
ACALL DELAY ; Delay for a specific time TR1 = 1; // Start Timer 1
MOV P1, #02H ; Step 2
ACALL DELAY while (1) {
MOV P1, #04H ; Step 3 send_string("Hello, world!");
ACALL DELAY }
MOV P1, #08H ; Step 4 }
ACALL DELAY
DJNZ R0, LOOP ; Decrement step counter and void send_char(unsigned char ch) {
loop SBUF = ch; // Load character
into serial buffer
; Delay subroutine (adjust delay time as needed) while (!TI); // Wait for
DELAY: transmission to complete
MOV R1, #250 TI = 0; // Clear transmit
DLY1: interrupt flag
MOV R2, #250 }
DLY2:
DJNZ R2, DLY2 void send_string(const char *str) {
DJNZ R1, DLY1 while (*str) { // Loop until null
RET character
send_char(*str); // Send each
character
str++; // Move to next
character
}
}
Write a program to generate two square wave: One of 10KHz frequency at pin P2.1
and another of frequency 20KHz at the pin 2.3. Assume XTAL=11.0592MHz. Use
timer 0 and timer 1 interrupt. Show the calculations of ISR address of timer 0 and
timer 1interrupt, TMOD, IE, TH0 and TH1.
; Crystal Frequency = 11.0592 MHz Calculations:
; Timer 0 for 10 kHz square wave
; Timer 1 for 20 kHz square wave Timer 0 (10 kHz):

ORG 0000H  Desired frequency: 10 kHz


MOV TMOD,#01H ; Timer 0 and 1  Timer 0 clock frequency: 11.0592 MHz / 12 =
in mode 1 921.6 kHz
MOV TH0,#0FDH ; Set timer 0 for  Timer 0 counts up to 256.
10 kHz  Time period of one count: 1 / 921.6 kHz = 1.085
MOV TH1,#0F9H ; Set timer 1 for us
20 kHz  Time period of 256 counts: 1.085 us * 256 = 278.4
MOV IE,#0B8H ; Enable timer 0 us
and timer 1 interrupts  To generate a 10 kHz square wave, the timer
SETB TR0 ; Start timer 0 should overflow every 100 us.
SETB TR1 ; Start timer 1  Required timer count: 100 us / 1.085 us ≈ 92
SJMP $  TH0 = 256 - 92 = 164 ≈ 0FDH

; Timer 0 Interrupt Service Routine Timer 1 (20 kHz):


(ISR)
ORG 0003H  Desired frequency: 20 kHz
CPL P2.1 ; Toggle P2.1  Timer 1 clock frequency: 11.0592 MHz / 12 =
MOV TH0,#0FDH ; Reload timer 0 921.6 kHz
RET  Timer 1 counts up to 256.
 Time period of one count: 1 / 921.6 kHz = 1.085
; Timer 1 Interrupt Service Routine us
(ISR)  Time period of 256 counts: 1.085 us * 256 = 278.4
ORG 000BH us
CPL P2.3 ; Toggle P2.3  To generate a 20 kHz square wave, the timer
MOV TH1,#0F9H ; Reload timer 1 should overflow every 50 us.
RET  Required timer count: 50 us / 1.085 us ≈ 46
-------------------------------------------------  TH1 = 256 - 46 = 210 ≈ 0F9H
---------

Interrupt Service Routine (ISR)


Addresses:

 Timer 0 ISR address: 03H


 Timer 1 ISR address: 0BH

Explain the difference between unsigned and signed char and 𝒊𝒏𝒕 declarations in
8051 C. Give examples.
 Unsigned Char: Use for storing small, non-negative values, such as counters, indexes, or
ASCII characters.
 Signed Char: Use for storing small integer values that can be both positive and negative.
 𝑰𝒏𝒕: Use for storing larger integer values, especially when the range of values exceeds that
of a signed char.
Data Type Range Storage Sign Examples.
unsigned char 0 to 255 8 bits Unsigned unsigned char a = 100;
signed char -128 to 127 8 bits Signed signed char b = -50;
int -32768 to 32767 16 bits Signed int c = 1000;
Interfacing MAX1112 with 8051
The MAX1112 is a serial ADC that offers 8 single-ended input channels. It's a versatile choice for
interfacing with 8051 microcontrollers due to its simplicity and ease of use.
Key Connections:
1. Power Supply: Connect Vcc
and GND pins of both MAX1112
and 8051 to a common 5V supply.
2. Serial Clock (SCLK): Connect
the SCLK pin of MAX1112 to a
suitable output pin of the 8051
(e.g., P1.0).
3. Serial Data Input (DIN):
Connect the DIN pin of MAX1112
to a suitable output pin of the
8051 (e.g., P1.1).
4. Serial Data Output (DOUT):
Connect the DOUT pin of
MAX1112 to a suitable input pin
of the 8051 (e.g., P1.2).
5. Chip Select (CS): Connect the CS pin of MAX1112 to a suitable output pin of the 8051
(e.g., P1.3).

; Define port assignments ; Main program


SCLK EQU P1.0 ORG 0000H
DIN EQU P1.1 MOV P1, #00 ; Set P1 as output port
DOUT EQU P1.2
CS EQU P1.3 ; Initialize MAX1112
MOV A, #01100000B ; Control byte to
; Function to send a bit to MAX1112 select channel 0
SendBit: ACALL SendByte ; Send the control byte
MOV C, A ; Copy the bit to carry flag
CLR DIN ; Set DIN low ; Start conversion
MOV SCLK, #1 ; Pulse SCLK high MOV A, #00000001B ; Start conversion
JC Send1 ; If carry flag is set, set DIN command
high ACALL SendByte
CLR DIN ; Else, keep DIN low
MOV SCLK, #0 ; Pulse SCLK low ; Wait for conversion to complete (you
RET might need a delay or a busy-wait loop)

Send1: ; Read the 8-bit result


SETB DIN MOV R0, #8 ; Loop counter
MOV SCLK, #1 READ_LOOP:
MOV SCLK, #0 ACALL ReadBit
RET RLC A ; Shift the received bit into the
accumulator
; Function to read a bit from MAX1112 DJNZ R0, READ_LOOP
ReadBit:
MOV SCLK, #1 ; Pulse SCLK high ; Now, the 8-bit result is in the
MOV C, DOUT ; Read the bit from accumulator (A)
DOUT ; can process the result as needed
MOV SCLK, #0 ; Pulse SCLK low
RET
Data Conversion Program in 8051 C Write 8051 C program to toggle all the
Data conversion is common in embedded bits of P0 and P1 with 100 mS delay. Use
systems, especially when dealing with sensors,
displays, and communication protocols. EX-OR operator
Example: BCD to Decimal Conversion
#include <8051.h> #include <reg51.h>
unsigned char bcd_to_decimal(unsigned char
bcd); void delay_ms(unsigned int ms) {
void main() { unsigned int i, j;
Unsigned char bcd_value = 0x25; // BCD for(i = 0; i < ms; i++) {
value for 25 for(j = 0; j < 1275; j++);
Unsigned char decimal_value; }
Decimal value = }
bcd_to_decimal(bcd_value); // Convert BCD
to decimal void main() {
P1 = decimal value; // Output decimal value while(1) {
to Port 1 P0 ^= 0xFF;
while (1); P1 ^= 0xFF;
} delay_ms(100);
unsigned char bcd_to_decimal(unsigned char }
bcd) }
{
return ((bcd >> 4) * 10) + (bcd & 0x0F);

Conflict Between Stack and Bank 1 in 8051


In the 8051 microcontroller, the stack and Bank 1 share the same memory space (00H to 7FH).
This can lead to a conflict if the stack pointer (SP) grows too large and overlaps with the data
stored in Bank 1.
How the Conflict Occurs:
 Stack Growth: When a function is called, the 8051 pushes the return address onto the
stack. As more functions are called, the stack pointer moves towards higher memory
addresses.
 Bank 1 Data: If you're using Bank 1 to store variables or data, they will also reside in the
same memory space.
 Conflict: If the stack grows beyond a certain point, it may overwrite the data stored in
Bank 1, leading to unexpected behaviour and potential program crashes.
Here are some strategies avoiding the Conflict:
1. Limit Stack Usage:
o Minimize the number of nested function calls, especially deep recursion.
o Use local variables efficiently to reduce stack usage.
o Consider using static variables instead of local variables when appropriate.
2. Optimize Code:
o Write efficient code to reduce the number of instructions and the amount of stack space
required.
o Use loop unrolling and other optimization techniques to minimize stack usage.
3. Use Bank 0 for Data:
o If possible, store data in Bank 0, which is separate from the stack.
o This can help reduce the chances of stack overflow.
4. Monitor Stack Usage:
o Use a debugger or simulator to monitor the stack pointer and identify potential conflicts.
o Be cautious when using large data structures or arrays that may consume significant stack
space.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy