Workbook 8051 Microcontroller - N
Workbook 8051 Microcontroller - N
Choice Based Credit System (CBCS) Syllabus Under National Education Policy
(NEP)
To be implemented from Academic Year 2024-2025
Title of the Course: B.C.A./B.Sc.(CA)
Work Book
Name:
College Name:
Academic Year :
Editor:
Prof. Dr. Deepak S. Kumbhar
PES Modern College of Arts, Science & Commerce, Ganeshkhind, Pune 411016
1
F.Y.B.C.A
Semester-II
CA- 154 - P Lab course on CA- 153 - T
CERTIFICATE
This is to certify that
Mr./Ms. _________________________________________ has successfully
completed the lab book on 8051 Microcontroller Programming CA- 154 - P
Lab course on CA- 153 - T course work and has scored _______marks out
of 15.
Instructor H.O.D./Coordinator
2
List of Assignments
3
Assignment Completion Sheet
4
Assignment No. 1
Study of 8051 Microcontroller Chip and Keil μVision-5
Aim:
To study the architecture, features, and applications of the 8051 microcontroller and familiarize with the
Keil μVision-5 IDE for developing and simulating embedded system programs.
Objectives :
1. Understand the architecture, features, and pin configuration of the 8051 microcontroller.
2. Learn to use Keil μVision-5 IDE for writing, compiling, and debugging programs.
3. Write and simulate simple programs in embedded C or assembly for 8051.
4. Familiarize with the process of generating a .HEX file for hardware interfacing.
5. Build a foundation for interfacing peripherals and embedded system applications.
Introduction
The 8051 microcontroller, introduced by Intel, is an 8-bit microcontroller widely used in embedded
systems. It is based on Harvard architecture and supports a range of peripherals, making it ideal for small-
scale to medium-scale applications.
Features
1. 8-bit Data Bus: Processes 8 bits of data at a time.
2. 16-bit Address Bus: Accesses up to 64 KB of memory.
3. On-chip Memory:
o 4 KB of ROM for program storage.
o 128 bytes of RAM for data storage.
4. I/O Ports:
o Four 8-bit parallel ports (P0, P1, P2, P3).
5. Timers:
o Two 16-bit timers (Timer 0 and Timer 1) for time-related operations.
6. Serial Communication:
o Full-duplex UART for communication.
7. Interrupt System:
o 5 interrupt sources for handling real-time events.
8. Oscillator Frequency:
o Typically operates at 12 MHz.
5
Pin Diagram
The pin configuration of the 8051 microcontroller:
6
Pin 21-28: Port 2 If there is no intention to use external memory then these port pins
are configured as general inputs/outputs. In case external memory is used, the
higher address byte, i.e. addresses A8-A15 will appear on this port. Even though
memory with capacity of 64Kb is not used, which means that not all eight port bits
are used for its addressing, the rest of them are not available as inputs/outputs.
Pin 29: PSEN If external ROM is used for storing program then a logic zero (0)
appears on it every time the microcontroller reads a byte from memory.
Pin 30: ALE Prior to reading from external memory, the microcontroller puts the
lower address byte (A0-A7) on P0 and activates the ALE output. After receiving
signal from the ALE pin, the external register (usually 74HCT373 or 74HCT375
add-on chip) memorizes the state of P0 and uses it as a memory chip address.
Immediately after that, the ALU pin is returned its previous logic state and P0 is now
used as a Data Bus. As seen, port data multiplexing is performed by means of only
one additional (and cheap) integrated circuit. In other words, this port is used for both
data and address transmission.
Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and
address transmission with no regard to whether there is internal memory or not. It
means that even there is a program written to the microcontroller, it will not be
executed. Instead, the program written to external ROM will be executed. By
applying logic one to the EA pin, the microcontroller will use both memories, first
internal then external (if exists).
Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins can be
used as general inputs/outputs. Otherwise, P0 is configured as address output (A0-
A7) when the ALE pin is driven high (1) or as data output (Data Bus) when the ALE
pin is driven low (0).
Pin 40: VCC +5V power supply.
7
Block Diagram:
Types of instructions:
Depending on operation they perform, all instructions are divided in several groups:
Arithmetic Instructions
Branch Instructions
Data Transfer Instructions
Logic Instructions
Bit-oriented Instructions
Arithmetic instructions:
Arithmetic instructions perform several basic operations such as addition, subtraction, division,
multiplication etc. After execution, the result is stored in the first operand. For example:
ADD A, R1 - The result of addition (A+R1) will be stored in the accumulator
. Mnemonic Description
ADD A,Rn Adds the register to the accumulator
ADD A,direct Adds the direct byte to the accumulator
ADD A,@Ri Adds the indirect RAM to the
accumulator
ADD A,#data Adds the immediate data to the
accumulator
ADDC A,Rn Adds the register to the accumulator with
a carry flag
ADDC A,direct Adds the direct byte to the accumulator
with a carry flag
ADDC A,@Ri Adds the indirect RAM to the
accumulator with a carry flag
8
ADDC A,#data Adds the immediate data to the
accumulator with a carry flag
SUBB A,Rn Subtracts the register from the
accumulator with a borrow
SUBB A,direct Subtracts the direct byte from the
accumulator with a borrow
SUBB A,@Ri Subtracts the indirect RAM from the
accumulator with a borrow
SUBB A,#data Subtracts the immediate data from the
accumulator with a borrow
INC A Increments the accumulator by 1
INC Rn Increments the register by 1
INC Rx Increments the direct byte by 1
INC @Ri Increments the indirect RAM by 1
DEC A Decrements the accumulator by 1
DEC Rn Decrements the register by 1
DEC Rx Decrements the direct byte by 1
DEC @Ri Decrements the indirect RAM by 1
INC DPTR Increments the Data Pointer by 1
MUL AB Multiplies A and B
DIV AB Divides A by B
DA A Decimal adjustment of the accumulator
according to BCD code
Branch Instructions:
There are two kinds of branch instructions:
Unconditional jump instructions: upon their execution a jump to a new location from
where the program continues execution is executed.
Conditional jump instructions: a jump to a new program location is executed only if a
specified condition is met. Otherwise, the program normally proceeds with the next
instruction.
Mnemonic Description
ACALL addr11 Absolute subroutine call
LCALL addr16 Long subroutine call
RET Returns from subroutine
RETI Returns from interrupt subroutine
AJMP addr11 Absolute jump
LJMP addr16 Long jump
SJMP rel Short jump (from –128 to +127 locations relative to the
following instruction)
JC rel Jump if carry flag is set. Short jump.
JNC rel Jump if carry flag is not set. Short jump.
JB bit,rel Jump if direct bit is set. Short jump.
JBC bit,rel Jump if direct bit is set and clears bit. Short jump.
JMP @A+DPTR Jump indirect relative to the DPTR
JZ rel Jump if the accumulator is zero. Short jump.
JNZ rel Jump if the accumulator is not zero. Short jump.
CJNE A,direct,rel Compares direct byte to the accumulator and jumps if not
equal. Short jump.
CJNE A,#data,rel Compares immediate data to the accumulator and jumps
if not equal. Short jump.
CJNE Rn,#data,rel Compares immediate data to the register and jumps if not
9
equal. Short jump.
CJNE @Ri,#data,rel Compares immediate data to indirect register and jumps
if not equal. Short jump.
DJNZ Rn,rel Decrements register and jumps if not 0. Short jump.
DJNZ Rx,rel Decrements direct byte and jump if not 0. Short jump.
NOP No operation
Data transfer instructions move the content of one register to another. The register the content of
which is moved remains unchanged. If they have the suffix “X” (MOVX), the data is exchanged
with external memory.
Mnemonic Description
10
MOVC A,@A+PC Moves the code byte relative to the PC to the accumulator
(address=A+PC)
MOVX A,@Ri Moves the external RAM (8-bit address) to the accumulator
MOVX @DPTR,A Moves the accumulator to the external RAM (16-bit address)
XCHD A,@Ri Exchanges the low-order nibble indirect RAM with the
accumulator
Logic Instructions:
Logic instructions perform logic operations upon corresponding bits of two registers. After execution,
the result is stored in the first operand.
Mnemonic Description
11
ORL direct,#data OR immediate data to direct byte
Bit-oriented Instructions
Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that
these are performed upon single bits.
Mnemonic Description
Direct - is any 8-bit address register of RAM. It can be any general-purpose register or a
SFR (I/O port, control register etc.);
@Ri - is indirect internal or external RAM location addressed by register R0 or R1;
#data - is an 8-bit constant included in instruction (0-255);
#data16 - is a 16-bit constant included as bytes 2 and 3 in instruction (0-65535);
addr16 - is a 16-bit address. May be anywhere within 64KB of program memory;
addr11 - is an 11-bit address. May be within the same 2KB page of program memory as the
first byte of the following instruction;
Rel - is the address of a close memory location (from -128 to +127 relative to the first byte of
the following instruction). On the basis of it, assembler computes the value to add or subtract
from the number currently stored in the program counter;
bit - is any bit-addressable I/O pin, control or status bit; and
C - is carry flag of the status register (register PSW).
8051 Addressing modes:
The way of specifying the address of the operand is called as addressing mode. The 8051
microcontroller is having four addressing modes for accessing data.
13
2. Register Addressing mode:
Register addressing mode involves the use of registers to hold the data to be manipulated. In this
addressing mode register which is having the data is part of the instruction.
MOV A, R0 ;copy the contents of RO into A
MOV R2,A ; copy the contents of A into R2
ADD A, R5 ; add the contents of R5 to contents of A
ADD A, R7 ; add the contents of R7 to contents of A
MOV R6, A ; save accumulator in R6
MOV A, @R0 ; move contents of RAM location whose ;address is held by RO into A
MOV @R1, B ; move contents of B into RAM location ; whose address is held by R1
Applications
Home appliances (e.g., washing machines).
Robotics and automation.
Automotive systems.
Industrial control systems.
14
Part 2: Keil μVision-5
Introduction
Keil μVision-5 is a professional IDE for embedded systems development. It supports programming and
simulation for a variety of microcontrollers, including the 8051 family.
Features
1. Code Editor: Provides syntax highlighting and error detection.
2. Compiler: Converts high-level language (C) to machine code (HEX files).
3. Simulator: Allows debugging and testing without hardware.
4. Peripheral Support: Simulates ports, timers, serial interfaces, etc.
5. Integration: Works with real hardware using programmers/debuggers.
Step 1: Give a double click on μvision4 icon on the desktop; it will generate a window as shown below:
15
Step 3: Select a drive where you would like to create your project.
Step 4: Create a new folder and name it with your project name.
Step 5: Open that project folder and give a name of your project executable file and save it.
Step 6: After saving, it will show some window there select your microcontroller company i.e. Atmel.
Step 8: After selecting chip click on OK then it will display some window asking to add STARTUP file.
Select YES.
Step 9: A target is created and start up file is added to your project window and is shown below.
16
Step 10: To write your project code select a new file from FILE menu bar.
Step 11: It will display some text editor, to save that file select SAVE option from FILE menu bar.
Step 12: Save file name with .c/ .asm extension depending on the language using to write the programs.
Step 13: Write the code of your project and save it.
Step 14: To add c/asm file to target, give a right click on Source Group, choose “ADD files to Group” option.
17
Step 15: It will displays some window there select the file you have to add and click on ADD option.
Step 16: The file will be added to target and it is shown in the project window.
Step 17: Now give a right click on target in the project window and select “Options for Target”.
18
Step 18: It will show some window like below, in that go to target option and type crystal oscillator frequency
Xtal (MHz) as 11.0592
Step 19: It will come back to the previous window and click OK in that window.
Step 20: Now go to project and click on Translate as shown in below figure it shows the errors and warnings in
Build output.
Step 21: Clear all the syntax errors and make sure 0 errors and 0 warnings.
Step 22: Now go to project and click on Build as shown in below figure it shows the errors and warnings in Build
output and shows target file is created.
19
Step 23: Go to debug tab and click on start/stop debug it opens the below window and click OK.
20
Step 24: It opens the following window and click run in the debug tab and the observe the output in
interfacing board
Assignment Evaluation
0: Not Done [ ] 1: Incomplete [ ] 2: Late Complete [ ]
3: Needs Improvement [ ] 4: Complete [ ] 5: Well Done [ ]
Lab Incharge Signature
21
Assignment No. 2
Study of Proteus Simulator for 8051 Simulation
Aim
To study and understand the use of the Proteus Simulator for designing, simulating, and testing
8051 microcontroller-based circuits in a virtual environment.
Objectives
1. Learn the interface and features of the Proteus simulator.
2. Understand how to add and configure components like 8051 microcontroller, LEDs,
switches, sensors, motors, and displays.
3. Simulate 8051 programs using .HEX files generated from Keil μVision-5.
4. Verify and debug microcontroller-based circuits virtually without physical hardware.
5. Develop skills to design and test embedded systems projects efficiently.
.
Apparatus Required
1. Proteus Design Suite software.(Use Student version or License Version only)
2. A PC with Windows OS.
3. Knowledge of 8051 programming (HEX files).
Theory
Proteus is an electronic circuit design and simulation software widely used to test and validate
embedded system designs, including microcontroller-based projects. It provides a virtual
platform to:
22
PROTEUS Environment
PROTEUS
Basically PROTEUS is also a simulating software but it helps you attach many components with
the 8051. Like resistors, capacitors, LEDs, LCDs, keypads, ICs etc. and these are just few that I
have named in general. It has a complete library and you will find everything that you will ever
need. You can design your complete circuit and then simulate it to view the final output. This
means that after perfecting your project on the programming side in KEIL, you'll need to
simulate it on PROTEUS to determine the output of the hardware components and change it if
need be. This will completely ensure your project's success.
DOWNLOAD PROTEUS
It is a paid application but you can download the free demo version of PROTEUS from their
official website here. It is fully functional except that it won't allow you to save your designs.
USING PROTEUS
PROTEUS is designed to be user-friendly and you will get the hold of it instantly. There is no
need to worry about some complex configuration / settings prior to simulation. Here are the basic
steps.
PLACING COMPONENTS
1. Click the "Pick from library (P)" button as shown in the figure
2. Select any category
3. Select item from the list
4. Click OK
23
After selecting component, click anywhere in the design area to select it and then click
again to place it
CONNECTING COMPONENTS
1. Place all the required components
2. Connect the desired nodes by clicking at starting and ending points
24
LOAD HEX FILE
1. Double click the 8051 component to open its properties
2. Browse for the HEX file as shown and select it
And don't worry, in PROTEUS, there is no need to provide the RESET circuit or crystal
oscillator to the microcontroller. It will work just fine even without it. The frequency can be
adjusted in the properties window as well.
25
The above picture is the complete circuitry for testing an LED on P2.0 like toggling (ON / OFF)
through programming but we will get to that part later on. At this point, you will just see the
LED glow if you have programmed it to be always ON.
Assignment Evaluation
0: Not Done [ ] 1: Incomplete [ ] 2: Late Complete [ ]
3: Needs Improvement [ ] 4: Complete [ ] 5: Well Done [ ]
Lab Incharge Signature
26
Assignment No. 3
Program to perform data transfer/exchange between specified memories
locations.
Aim
To implement data transfer and exchange between specified memory locations using the 8051
microcontroller in Assembly language and simulate the program in Keil μVision.
Objectives
1. To learn data transfer techniques between different memory locations in the 8051.
2. To write Assembly programs for memory data exchange.
3. To simulate and debug the program using Keil μVision.
Theory
Data Transfer: Involves copying data from a source memory location to a destination
memory location.
Data Exchange: Swaps the data between two memory locations without losing any
information.
Programs
1. Data Transfer from Source to Destination
This program transfers data from memory location 30H to 40H.
ORG 00H ; Program starts at address 0
MOV R0, #30H ; Load source address into R0
MOV R1, #40H ; Load destination address into R1
MOV A, @R0 ; Move data from source address to accumulator
MOV @R1, A ; Move data from accumulator to destination address
SJMP $ ; Stop execution
Observations
1. For Data Transfer:
Result
28
Conclusion
Assignment Evaluation
29
Assignment No. 4
Program to perform Arithmetic, logical & code conversion problems
Aim
To perform arithmetic, logical, and code conversion operations using the 8051 microcontroller in
Assembly language and simulate the program in Keil μVision.
Objectives
1. To implement basic arithmetic and logical operations.
2. To perform code conversions (binary, BCD, hexadecimal).
3. To write and simulate programs in Keil μVision.
Theory
1. Arithmetic Operations:
o Basic operations like addition, subtraction, multiplication, and division.
o Handled using the 8051 instruction set.
2. Logical Operations:
o Perform AND, OR, XOR, and NOT operations on 8-bit data.
o Useful for bit manipulation and decision-making.
3. Code Conversion:
o Conversion between different data formats (e.g., Binary to BCD, ASCII to Hex).
Assembly Programs
1. Arithmetic Operations
Addition of Two Numbers
ORG 00H ; Program starts at address 0
MOV A, #25H ; Load first number into accumulator
ADD A, #15H ; Add second number to accumulator
MOV 30H, A ; Store result at memory location 30H
SJMP $ ; Stop execution
Multiplication
ORG 00H
MOV A, #25H ; Load multiplicand into A
MOV B, #15H ; Load multiplier into B
MUL AB ; Perform multiplication (result in A and B)
MOV 30H, A ; Store lower byte at 30H
MOV 31H, B ; Store higher byte at 31H
SJMP $ ; Stop execution
30
2. Logical Operations
AND Operation
ORG 00H
MOV A, #0F0H ; Load first operand into A
ANL A, #0A0H ; Perform AND with second operand
MOV 30H, A ; Store result at memory location 30H
SJMP $ ; Stop execution
OR Operation
ORG 00H
MOV A, #0F0H ; Load first operand into A
ORL A, #0A0H ; Perform OR with second operand
MOV 30H, A ; Store result at memory location 30H
SJMP $ ; Stop execution
XOR Operation
ORG 00H
MOV A, #0F0H ; Load first operand into A
XRL A, #0A0H ; Perform XOR with second operand
MOV 30H, A ; Store result at memory location 30H
SJMP $ ; Stop execution
NOT Operation
ORG 00H
MOV A, #0F0H ; Load operand into A
CPL A ; Complement (NOT) the accumulator
MOV 30H, A ; Store result at memory location 30H
SJMP $ ; Stop execution
3. Code Conversion
Binary to BCD Conversion
ORG 00H
MOV A, #27H ; Load binary number into A (e.g., 39 decimal)
DA A ; Decimal adjust to BCD format
MOV 30H, A ; Store BCD result at memory location 30H
SJMP $ ; Stop execution
31
ASCII to Hexadecimal Conversion
ORG 00H
MOV A, #39H ; Load ASCII character '9' into A
SUBB A, #30H ; Subtract offset to convert to Hexadecimal
MOV 30H, A ; Store hexadecimal result at memory location 30H
SJMP $ ; Stop execution
Observations
32
Result
Addition of Two Numbers
AND Operation:
33
Binary to BCD:
Conclusion
Assignment Evaluation
34
Assignment No. 5
Program to Perform Addition, Subtraction, Multiplication, and Division of
8/16-Bit Data
Aim
To perform arithmetic operations (addition, subtraction, multiplication, and division) on 8-bit
and 16-bit data using 8051 microcontroller and simulate the program in Keil μVision.
Objective
To write and simulate an assembly language program for the 8051 microcontroller using Keil
μVision for arithmetic operations on 8-bit and 16-bit data.
Theory
Addition: Adds two numbers, may result in a carry.
Subtraction: Subtracts one number from another using 2's complement.
Multiplication: Multiplies two numbers, result is stored in the A and B registers.
Division: Divides two numbers, quotient is stored in A and remainder in B.
Program Structure
The program is divided into segments to perform each operation, with separate routines for 8-bit
and 16-bit arithmetic.
Assembly Programs:
1. Addition of 8-Bit Numbers
35
4. Division of 8-Bit Numbers
CARRY:
INC A ; Increment higher byte to handle carry
STORE_HIGH:
MOV DPTR, #3005H ; Store higher byte of result at address 3005H
MOVX @DPTR, A ; Store result's higher byte
36
6. Multiplication of 16-Bit Numbers
For 16-bit multiplication, use repeated addition or advanced algorithms. 8051 does not
directly support 16-bit multiplication.
Observations
37
Result
1. 8-BIT ADDITION
2. 8-BIT MULTIPLICATION
38
Conclusion
Assignment Evaluation
39
Assignment No. 6
Program to find Largest/smallest from a series.
Aim:
To write an assembly language program for the 8051 microcontroller to find the largest and
smallest numbers from a given series of data.
Objectives:
1. Learn to handle arrays in assembly language.
2. Implement comparison logic to find the largest or smallest number.
3. Use 8051 registers (Accumulator and general-purpose registers).
4. Practice 8051 assembly syntax and instructions.
5. Test and verify the program through simulation.
Apparatus Required:
1. Keil μVision IDE.
2. 8051 microcontroller (AT89S51/52 or similar) for simulation.
3. Basic understanding of assembly programming.
Theory:
The program uses a loop to compare elements in an array stored in memory locations. Depending
on the requirement (largest or smallest), the accumulator holds the current result as comparisons
proceed.
Program Algorithm:
1. Load the first number of the series into the accumulator.
2. Point to the memory address of the next number using a pointer register (e.g., R0).
3. Compare the current number with the accumulator value:
o If the current number is larger (or smaller), update the accumulator.
o Otherwise, continue with the next number.
4. Repeat the comparison for all numbers in the series.
5. Store the result (largest or smallest number) in memory.
Assembly Program:
Finding the Largest Number
J1: MOV 40H, A ; Store the largest number at memory location 40H
SJMP $ ; Stop the program
Address Value
30H 34
31H 78
32H 12
33H 56
34H 89
Observations
41
Result
Conclusion
Assignment Evaluation
42
Assignment No. 7
Interfacing of LED/LEDs to 8051 microcontroller
Aim
To interface LEDs with the 8051 microcontroller and control on-off action of an using
Embedded C programming .
Objectives
1. To write a simple program in Embedded C to turn an LED on and off using the 8051
microcontroller.
2. To control the LED through a microcontroller's I/O port.
3. To demonstrate basic digital output control with the 8051.
4. To test the functionality using simulation software (Keil μVision) and physical hardware
(circuit board).
Theory
1. LED Basics:
o LEDs emit light when current flows through them.
o They are connected in either active-high or active-low configuration.
2. Interfacing LEDs with 8051:
o LEDs are connected to the microcontroller’s I/O port through current-limiting
resistors.
o An I/O pin configured as output can control the LED's state.
3. Circuit Design:
o Connect LEDs to Port 1 of the 8051 microcontroller.
o Use 330Ω resistors in series with each LED to limit the current.
Circuit Diagram
Port 1 pins (P1.0 to P1.7) are connected to LEDs.
Each LED has a series resistor connected to ground (active-high configuration).
43
Program in Embedded C
1. Blinking a Single LED
void main() {
while (1) {
LED = 1; // Turn ON LED
delay_ms(500); // Delay of 500 ms
LED = 0; // Turn OFF LED
delay_ms(500); // Delay of 500 ms
}
}
void main() {
unsigned char LED_pattern = 0x01; // Initial LED pattern
while (1) {
P1 = ~LED_pattern; // Send pattern to Port 1 (active-low)
delay_ms(500); // Delay of 500 ms
LED_pattern = LED_pattern << 1; // Shift the LED pattern to the left
if (LED_pattern == 0x00) { // Reset the pattern after 8 shifts
LED_pattern = 0x01;
}
}
}
44
Steps to Simulate in Keil μVision
1. Create a New Project:
o Open Keil μVision and create a new project for the 8051 microcontroller.
2. Write the Program:
o Copy the code into a .c file.
3. Configure the Project:
o Add the file to the project.
o Set the target device and include the reg51.h header.
4. Compile the Code:
o Build the project to generate the .HEX file.
5. Simulate the Program:
o Use the Keil debugger or Proteus simulator to verify the LED behavior.
Observations
Result
Conclusion
Assignment Evaluation
45
Assignment No. 8
Interfacing of Switch & LED to 8051 Microcontroller
Objective
To interface a switch and an LED with the 8051 microcontroller and control the LED based on
the switch's state using Embedded C.
Theory
1. Switch Basics:
o A switch can be used as an input device to the microcontroller.
o It is connected to a microcontroller pin, usually through a pull-up or pull-down
resistor, to ensure a stable logic level.
2. LED Basics:
o An LED is connected to the microcontroller as an output device.
o It can be turned ON or OFF based on the logic level sent by the microcontroller.
3. Working:
o When the switch is pressed, it changes the input logic at the microcontroller pin.
o The microcontroller reads the switch's state and turns the LED ON or OFF
accordingly.
Circuit Diagram
1. Switch: Connected to pin P2.0 with a pull-up resistor.
2. LED: Connected to pin P1.1 through a 330Ω resistor (active-high configuration).
46
Program in Embedded C
void main() {
SWITCH = 1; // Configure P1.0 as input (high by default due to pull-up)
LED = 0; // Turn OFF LED initially
while (1) {
if (SWITCH == 0) { // Check if switch is pressed (active-low)
LED = 1; // Turn ON LED
} else {
LED = 0; // Turn OFF LED
}
}
}
Explanation
1. Switch:
The switch is active-low; when pressed, it connects the input pin to ground (logic 0).
The if condition checks for this state.
2. LED:
The LED is controlled by writing logic 1 (ON) or 0 (OFF) to its pin.
Observations
47
Result
Conclusion
Assignment Evaluation
48
Assignment No. 9
Traffic Light Controller Using 8051 Microcontroller
Objective
To design and simulate a traffic light controller system using the 8051 microcontroller for:
1. Simple one-way traffic control.
2. Two-way or four-way traffic control.
Theory
Traffic light controllers manage traffic flow at intersections by using Red, Yellow, and Green
lights. A microcontroller like the 8051 is used to automate this process efficiently.
Program
#include <reg51.h>
void main() {
while (1) {
RED = 1; // Turn ON Red LED
YELLOW = 0; // Turn OFF Yellow LED
GREEN = 0; // Turn OFF Green LED
delay(5000); // 5-second delay
#include <reg51.h>
51
Observations
Result
Conclusion
Assignment Evaluation
52
Assignment No. 11
Interfacing LCD to 8051 Microcontroller
Objective
To interface a 16x2 LCD with the 8051 microcontroller in 8-bit mode and display a message.
Theory
In 8-bit mode, all 8 data pins (D0-D7) of the LCD are used for sending data and commands. This
mode provides faster communication compared to 4-bit mode as the full byte is sent at once.
Pin Configuration
1. Control Pins:
o RS: Register Select (0 for Command, 1 for Data).
o RW: Read/Write (0 for Write, 1 for Read).
o E: Enable (High to Low transition triggers data).
2. Data Pins:
o D0-D7: Data lines connected to the microcontroller's port for 8-bit
communication.
Circuit Design
1. Connections:
o Control Lines:
RS -> P2.0
RW -> P2.1
E -> P2.2
o Data Lines:
D0-D7 -> P1.0 to P1.7
o Adjust LCD contrast using a 10kΩ potentiometer connected to the VEE pin.
o Power pins (VCC and GND) connected to the power supply.
53
Program (8-Bit Mode)
#include <reg51.h>
void main() {
LCD_Init(); // Initialize LCD
LCD_String("Hello, World!"); // Display message
LCD_Command(0xC0); // Move to second line
LCD_String("8051 Micro!"); // Display message
while (1);
}
Observations
Result
Conclusion
Assignment Evaluation
56
Program
#include <reg51.h>
void main() {
LCD_Init(); // Initialize LCD
while (1) {
if (IR_SENSOR == 1) { // Object detected
LCD_Command(0x80); // Move to the first line
LCD_String("Object Detected");
} else { // No object detected
LCD_Command(0x80); // Move to the first line
LCD_String("No Object "); // Clear remaining characters
}
delay_ms(500);
}
}
58
Result
Conclusion
Assignment Evaluation
59
Assignment No. 13
ADC Interfacing to 8051 Microcontroller
Objective
To interface an Analog-to-Digital Converter (ADC) with the 8051 microcontroller and display
the converted digital value on a 16x2 LCD.
Theory
1. Analog-to-Digital Converter (ADC):
o Converts an analog input signal into a corresponding digital value.
o Common ADC used: ADC0804.
8-bit resolution.
Takes analog input (0-5V) and converts it into an 8-bit digital value (0-
255).
2. Pin Configuration of ADC0804:
o VCC and GND: Power supply.
o VREF: Reference voltage (5V for full range).
o IN+ and IN-: Analog input pins.
o WR (Write): Starts the conversion process.
o RD (Read): Enables the digital output.
o INTR: Interrupt pin, goes low when conversion is complete.
o D0-D7: Digital output pins for 8-bit data.
3. LCD Basics:
o Used to display the converted digital value.
Pin Connections
1. ADC0804:
o WR -> P0.0
o RD -> P0.1
o INTR -> P0.2
o D0-D7 -> P3.0-P3.7 (digital output to microcontroller).
o IN+ -> Analog input (e.g., potentiometer).
o VREF -> 5V.
2. LCD:
o Control Pins:
RS -> P2.0
RW -> P2.1
E -> P2.2
o Data Pins:
D0-D7 -> P1.0-P1.7.
Circuit Design
1. Connect ADC0804 pins to the microcontroller as described.
2. Connect LCD in 8-bit mode to the microcontroller.
3. Use a potentiometer to provide an adjustable analog input to the ADC.
60
Program
#include <reg51.h>
void main() {
unsigned char adc_value;
char str[4];
while (1) {
adc_value = ADC_Read(); // Read ADC value
IntToStr(adc_value, str); // Convert to string
LCD_Command(0xC0); // Move to second line
LCD_String(str); // Display ADC value
delay_ms(500);
}
}
Observations
Result
63
Conclusion
Assignment Evaluation
64
Assignment No. 9
Waveform Generation Using DAC Interface to 8051 Microcontroller
Objective
To generate analog waveforms (e.g., sine wave, triangular wave, square wave) using the 8051
microcontroller and a Digital-to-Analog Converter (DAC).
Theory
1. Digital-to-Analog Converter (DAC):
o Converts digital signals from the microcontroller into analog signals.
o Common DACs, like the DAC0808, accept an 8-bit digital input and provide an
equivalent analog output.
2. Waveform Generation:
o Square Wave: Alternating between high and low digital values at regular
intervals.
o Triangular Wave: Gradually increasing and decreasing digital values to form a
triangular shape.
o Sine Wave: Approximated by sending precomputed sine values from a lookup
table to the DAC.
3. 8051-DAC Interface:
o DAC data pins are connected to an 8-bit port of the microcontroller (e.g., Port 1).
o The output of the DAC is the analog waveform.
Circuit Diagram
1. Connect the DAC’s data input pins (D0-D7) to Port 1 (P1.0 to P1.7) of the 8051
microcontroller.
2. Connect the DAC output to an oscilloscope or analog device to visualize the waveform.
65
Programs
1. Square Wave Generation
#include <reg51.h>
void main() {
while (1) {
P1 = 0xFF; // Output maximum value (high)
delay(500); // Delay for half a period
P1 = 0x00; // Output minimum value (low)
delay(500); // Delay for half a period
}
}
#include <reg51.h>
void main() {
unsigned char value;
while (1) {
// Ascending values for triangular wave
for (value = 0; value < 255; value++) {
P1 = value; // Send value to DAC
delay(2); // Delay for smooth transition
}
// Descending values for triangular wave
for (value = 255; value > 0; value--) {
P1 = value; // Send value to DAC
delay(2); // Delay for smooth transition
}
}
}
66
3. Sine Wave Generation
#include <reg51.h>
void main() {
unsigned char i;
while (1) {
for (i = 0; i < 32; i++) {
P1 = sine_table[i]; // Send sine value to DAC
delay(2); // Adjust delay for frequency
}
}
}
67
Observations
Result
Conclusion
Assignment Evaluation
68
Assignment No. 14
DC Motor Interfacing to 8051 Microcontroller
Objective
To interface a DC motor with the 8051 microcontroller using an H-Bridge motor driver
(L293D) to control its direction and speed.
Theory
1. DC Motor Basics:
o A DC motor converts electrical energy into mechanical energy.
o Requires an external driver circuit due to higher current and voltage requirements.
2. H-Bridge Motor Driver (L293D):
o Acts as an interface between the microcontroller and the DC motor.
o Controls motor direction by varying the polarity of the voltage applied to the
motor terminals.
o Features:
Input Pins (IN1, IN2): Control motor direction.
Enable Pin (EN): Enables or disables the motor.
Output Pins (OUT1, OUT2): Connect to the motor terminals.
3. Control Logic:
o IN1 = 1, IN2 = 0: Clockwise rotation.
o IN1 = 0, IN2 = 1: Counterclockwise rotation.
o IN1 = IN2 = 0: Motor off.
Pin Connections
1. L293D to 8051:
o IN1 -> P1.0
o IN2 -> P1.1
o EN1 (Enable) -> P1.2
2. Motor:
o Connect the motor terminals to OUT1 and OUT2 of L293D.
3. Power Supply:
o VCC1 (Logic): 5V.
o VCC2 (Motor): 12V (depending on motor rating).
o GND: Common ground.
Circuit Design
1. Connect the L293D motor driver IC as described.
2. Use a suitable DC motor with the driver outputs.
3. Use external power supplies for the motor and microcontroller.
69
Program
#include <reg51.h>
void Motor_Clockwise() {
EN = 1; // Enable motor
IN1 = 1; // Set direction to clockwise
IN2 = 0;
}
void Motor_Counterclockwise() {
EN = 1; // Enable motor
IN1 = 0; // Set direction to counterclockwise
IN2 = 1;
}
70
void Motor_Stop() {
EN = 0; // Disable motor
IN1 = 0; // Stop motor
IN2 = 0;
}
void main() {
while (1) {
Motor_Clockwise(); // Rotate motor clockwise
delay_ms(5000); // Run for 5 seconds
Motor_Stop(); // Stop motor
delay_ms(2000); // Wait for 2 seconds
Motor_Counterclockwise(); // Rotate motor counterclockwise
delay_ms(5000); // Run for 5 seconds
Motor_Stop(); // Stop motor
delay_ms(2000); // Wait for 2 seconds
}
}
Observations
Result
Conclusion
71
Assignment Evaluation
72
Assignment No. 15
Stepper Motor Interfacing to 8051 Microcontroller
Objective
To interface a Stepper Motor with the 8051 microcontroller and control its rotation direction
(clockwise and counterclockwise) and steps.
Theory
1. Stepper Motor Basics:
o A stepper motor rotates in discrete steps, providing precise control over position.
o Types:
Unipolar (easier control, multiple windings with a common center tap).
Bipolar (requires an H-bridge, no center tap, stronger torque).
o Each step corresponds to energizing a specific sequence of coils.
2. Control Logic:
o Full-Step Mode:
Two coils are energized simultaneously for each step.
o Half-Step Mode:
Coils are energized alternately in single and double configurations.
o Wave Drive Mode:
Only one coil is energized at a time.
3. ULN2003 Driver IC:
o Used to drive the stepper motor.
o Acts as an interface between the microcontroller and motor by amplifying current.
o Input pins control corresponding output pins to the motor.
Pin Connections
1. ULN2003 to 8051:
o IN1 -> P1.0
o IN2 -> P1.1
o IN3 -> P1.2
o IN4 -> P1.3
2. Stepper Motor:
o Connect stepper motor windings to the corresponding outputs of the ULN2003.
3. Power Supply:
o Provide sufficient power to the stepper motor (12V or as per motor
specifications).
Circuit Design
1. Connect the ULN2003 driver to the microcontroller and stepper motor.
2. Ensure proper grounding and power supply connections.
73
Program
#include <reg51.h>
void main() {
unsigned int steps;
while (1) {
// Rotate clockwise for 100 steps
for (steps = 0; steps < 100; steps++) {
Step_CW();
}
delay_ms(1000); // Wait for 1 second
Observations
75
Result
Conclusion
Assignment Evaluation
76
Programming the 89S52 Microcontroller Using ProISP 1.72
Software Requirement:
ProISP 1.72 software (freely available for download from trusted sources).
Hardware Requirements:
Procedure
77
Erase the flash memory.
Write the HEX file to the microcontroller.
Verify the written data.
78
[Sample Practical Slip]
A. To find the largest and smallest number from a given series __,__,__,__,__,__,__
OR
B. To perform addition, subtraction, multiplication, or division of ___-bit data.
____ _____
Q2. Draw the block diagram of given interface and write 8051 C Program (20 Marks)
79