BACHELOR OF TECHNOLOGY
(COMPUTER SCIENCE & ENGINEERING)
PRACTICAL FILE
For
Course Name: Embedded System Development
Course Code: 130204117
Semester: 4th
Submitted to: Submitted by:
Mr. Pradeep Lamba Ritika sharma 231302100
B Tech CSE(AIML)
Department of Computer Science & Engineering
Faculty of Engineering and Technology
Experiment: 1
Aim: Study of Development tools/environment like KEIL & hardware IC 8051
microcontroller.
Theory:
The study of development tools and environments is crucial for designing, testing, and
optimizing embedded systems efficiently. These tools, such as Keil µVision, Proteus, and
MPLAB, provide a platform for coding, simulation, and debugging, enabling engineers to
develop reliable applications. Understanding these tools ensures seamless integration of
hardware and software, reducing errors and development time. Similarly, the 8051
microcontroller remains a fundamental learning platform for embedded system enthusiasts
due to its simplicity, versatility, and widespread use in industrial applications. Mastering
8051 programming, interfacing techniques, and instruction set architecture helps students and
engineers build a strong foundation in microcontroller-based system design, preparing them
for advanced processors and real-world embedded applications.
USE OF COMPILER AND PROGRAMMER:
1. Compiler: KEIL
2. Programming IC: 8051.
PROCEDURE:
1. Double Click on the icon present on the desktop.
2. The following window will pop up :
3. Go to the “Project” menu and click on “New Project”.
4. Make a folder on desktop & give file name.
5. Now create a file in that folder and hit ‘save’.
6. When you click on the save button, following window opens:
7. Select ATMEL AT89C51.
8. Now, select ‘No’ on the pop-up given below.
9. Make a new file.
10. Write your code there & save it with extension .c/.asm depending on coding language.
11. Go to ‘Target1’, right click ‘Source Group 1’and then click on ‘Add
Existing Files…..’.
12. Select your ‘.asm’ or ‘.c’ file which you want to add.
13. Now, right click the added file under ‘Source Group 1’ and click on ‘Build Target’.
14. It will show you 0 errors & 0 warning on ‘Build Output’ window.
After performing all these steps, the chip will be configured through Flash Magic. Let’s get
our hands on the steps of chip configuration through Flash Magic_ Special Notes: • Make all
the DIP switches in off position before burning the program in the controller.
• Connect the Programming Cable on your Kit (prog. Conn.)And other side of
cable with the COM Port of the Computer.
• Burn the Program in the microcontroller with help of FLASH MAGIC or ECE
FLASH as explained in the next section.
Study of 8051 Pin Diagram:
The 8051 microcontroller is an 8-bit microcontroller introduced by Intel in 1980. It has been
widely used in embedded systems due to its simplicity, reliability, and performance.
Although Intel originally developed the 8051, the architecture has been adopted and modified
by several manufacturers, such as Atmel (now Microchip), Nuvoton, and Silicon Labs.
Key Features of the 8051 Microcontroller:
1. 8-bit Processor: The 8051 is an 8-bit microcontroller, meaning it processes 8 bits of
data at a time.
2. ROM (Read-Only Memory): The 8051 has 4KB of on-chip ROM for storing the program.
3. RAM (Random Access Memory): The microcontroller has 128 bytes of on-chip
RAM, used for temporary data storage during execution.
3. I/O Ports: There are 4 parallel I/O ports (P0, P1, P2, P3), each of which is 8 bits
wide, allowing control of multiple devices.
4. Timers/Counters: It has 2 timers (Timer 0 and Timer 1), each of 16 bits.
5. Interrupts: The 8051 supports 5 interrupt sources and has interrupt priority levels.
6. Serial Communication: Built-in serial communication via UART for communication
with other devices.
7. Clock Speed: Typically operates at a clock speed of 12 MHz, providing a 1 µs
instruction cycle time.
8051 Microcontroller Pin Description
1. VCC (Pin 40)
• Function: This pin provides the positive power supply to the
8051 microcontroller. Typically, +5V is supplied to this pin.
2. GND (Pin 20)
• Function: This is the ground pin of the 8051 microcontroller. It should
be connected to the ground (0V) of the power supply.
3. Port 0 (P0.0 - P0.7, Pins 39 to 32)
• Function: Port 0 is an 8-bit bidirectional I/O port. When connected to
external memory, it acts as the address and data bus. o Special Function:
If external memory is not used, P0 can be used as a general-purpose I/O
port.
o Open Drain: Port 0 pins are open-drain, meaning external pullup
resistors are required when used as general-purpose I/O.
4. Port 1 (P1.0 - P1.7, Pins 1 to 8)
• Function: Port 1 is another 8-bit bidirectional I/O port. It is strongly driven and
does not require external pull-up resistors. o Special Function: Each pin of
Port 1 can serve as an input or output, depending on the control software.
5. Port 2 (P2.0 - P2.7, Pins 21 to 28)
• Function: Port 2 is an 8-bit bidirectional I/O port. o Special Function: It also
functions as the higher byte of the address bus when external
memory is used. If no external memory is connected, it works
as a general-purpose I/O port.
o Port 2 pins are strongly driven, meaning they don't need
external pull-up resistors.
6. Port 3 (P3.0 - P3.7, Pins 10 to 17)
• Function: Port 3 is an 8-bit bidirectional I/O port. It has multiple special
functions: o P3.0 (RXD): Serial input pin for serial communication
(UART receive data).
o P3.1 (TXD): Serial output pin for
serial communication (UART transmit
data).
o P3.2 (INT0): External interrupt 0 input. o
P3.3 (INT1): External interrupt 1 input.
o P3.4 (T0): Timer 0 external input. o P3.5
(T1): Timer 1 external input. o P3.6 (WR):
External memory write signal.
o P3.7 (RD): External memory read signal.
7. RST (Pin 9)
• Function: This is the reset pin. A high signal on this pin initializes the 8051
microcontroller. The reset operation is performed when this pin is held
high for at least two machine cycles.
8. ALE (Address Latch Enable, Pin 30)
• Function: The ALE pin is used to latch the lower byte of the address when the
8051 is accessing external memory. It generates a pulse to enable the
external latch.
9. PSEN (Program Store Enable, Pin 29)
• Function: This pin is used to read program code from external memory
(ROM). It is active low, meaning it outputs a low signal during the read cycle
of external program memory.
10. EA (External Access, Pin 31)
• Function: The EA pin is used to select either internal or external memory
for program code. o EA = 1: Internal memory is used (ROM/EPROM).
o EA = 0: External memory is used.
11. XTAL1 and XTAL2 (Pins 18 and 19)
• Function: These are the pins for connecting an external crystal oscillator to
provide the clock signal to the microcontroller. Typically, a 12 MHz crystal
is used to provide a frequency of 12 MHz.
VIVA VOCE:
1. Define microprocessor, microcontroller & embedded system?
2. Define Flag register in microcontrollers?
3. What was the first system developed in the field of electronics engineering?
4. List different timers/counters supported by the architecture of 8051 microcontroller?
5. Explain port function in 8051?
6. How does assembly language programming differ from high level programming?
7. What is a Harvard architecture? Give example.
8. Differentiate features of 8051, 8052 & 8031 microcontroller.
Experiment: 2
Aim: To perform arithmetic operations (addition, subtraction, multiplication, and division)
on the 8051 microcontroller and analyse the results using the Keil simulator.
Theory:
Arithmetic operations are fundamental in embedded systems programming. The 8051
microcontroller provides various arithmetic instructions to perform operations such as
addition, subtraction, multiplication, and division. These operations are carried out using
registers and memory locations.
The basic arithmetic instructions in 8051 assembly language are:
1. ADD A, Rn – Adds the contents of register Rn to the accumulator.
2. SUBB A, Rn – Subtracts the contents of register Rn from the accumulator
with borrow.
3. MUL AB – Multiplies the contents of registers A and B.
4. DIV AB – Divides the contents of register A by register B.
These operations are useful in real-time applications like digital signal processing,
automation, and robotics.
USE OF COMPILER AND PROGRAMMER:
• Compiler: KEIL µVision
• Programming IC: 8051 PROCEDURE:
1. Open Keil µVision by double-clicking the icon on the desktop.
2. Go to Project → Click on New Project and create a folder.
3. Select ATMEL AT89C51 as the target microcontroller.
4. Create a new file, write the assembly or C program, and save it with .asm or .c
extension.
5. Add the source file to the project by right-clicking on Source Group and selecting
Add Files to Project.
6. Enable the Create HEX file option in the target settings.
7. Click on Build Target to compile the program. Ensure there are 0 errors and
0 warnings.
8. Load the HEX file into the microcontroller using Flash Magic and run the program.
SOURCE CODE:
ORG 0000H ; Set starting address of the program
MOV A, #16H ; Load first number (16H) into accumulator
A MOV B, #14H ; Load second number (14H) into register B
; Perform Addition
ADD A, B ; A = A + B (Result stored in A)
MOV R0, A ; Store addition result in register R0
; Perform Subtraction
MOV A, #16H ; Reload first number (16H) into accumulator
A SUBB A, B ; A = A - B (Result stored in A)
MOV R1, A ; Store subtraction result in register R1
END ; End of program
ORG 0000H ; Set starting address of the program
MOV A, #16H ; Load first number (16H) into accumulator
A MOV B, #14H ; Load second number (14H) into register B
; Perform Multiplication
MUL AB ; Multiply A and B; result: lower byte in A, higher byte in
B MOV R0, A ; Store the lower byte of the result in register R0
MOV R1, B ; Store the higher byte of the result in register R1
; Perform Division
MOV A, #16H ; Reload first number (16H) into accumulator
A MOV B, #14H ; Reload second number (14H) into register B
DIV AB ; Divide A by B; quotient in A, remainder in B
MOV R2, A ; Store the quotient (A) in register R2
MOV R3, B ; Store the remainder (B) in register
R3
END ; End of the program
OBSERVATION & EXPECTED RESULTS:
After execution, the register values should be:
• R0 = 2AH (Addition Result: 16 + 14 = 2AH)
• R1 = 02H (Subtraction Result: 16 - 14 = 02H)
• R0 = B8H (Multiplication Lower Byte: 16 ×14 = 1B8H)
• R1 = 01H (Multiplication Higher Byte: 1B8H)
• R2 = 01H (Division Quotient: 16 ÷ 14 = 1H)
• R3 = 02H (Division Remainder: 16 % 14 = 2H)
Experiment: 3
Aim: Write an assembly language program to generate a square wave with T = 10ms, T(ON)
= T(OFF) = 5ms using Port 2, Pin 3 with Timer 0 in Mode 1, given a crystal frequency of
11.0592 MHz.
Theory:
A square wave is a digital waveform with two distinct voltage levels: HIGH and LOW. The
time spent at each level determines the frequency of the wave. Using the 8051
microcontrollers, a square wave can be generated by toggling an I/O pin at regular intervals
using Timer 0 in Mode 1 (16-bit timer mode).
Timer Calculation:
• Crystal Frequency = 11.0592 MHz
• Machine Cycle Frequency = 11.0592 MHz / 12 = 921.6 kHz
• Machine Cycle Period = 1 / 921.6 kHz = 1.085 µs
• Required Delay (T/2) = 5ms
• Timer Count = 65536 - (5ms / 1.085µs) = 60928 (0xEE00)
By setting up Timer 0 with TH0 = 0xEE and TL0 = 0x00, we can achieve the required delay
of 5ms and toggle P2.3 to generate the desired square wave.
USE OF COMPILER AND PROGRAMMER:
• Compiler: KEIL
• Programming IC: 8051 ALGORITHM:
1. Configure Timer 0 in Mode 1 (16-bit timer mode).
2. Set P2.3 as output.
3. Start the infinite loop:
• Set P2.3 LOW.
• Load TH0 = 0xEE, TL0 = 0x00 for 5ms delay.
• Start Timer 0 and wait for overflow.
• Toggle P2.3 HIGH.
• Reload TH0 and TL0 and
repeat. SOURCE CODE:
ORG 0000H ; Start of program memory
MOV TMOD, #01H ; Set Timer 0 in mode 1 (16-bit mode) RPT:
MOV TH0, #0EEH ; Load high byte of Timer 0 (for desired
delay)
MOV TL0, #00H ; Load low byte of Timer 0 (for desired
delay) SETB TR0 ; Start Timer 0
WAIT:
JNB TF0, WAIT ; Wait for Timer 0 overflow (TF0 set)
CLR TR0 ; Stop Timer 0 after
overflow CLR TF0 ; Clear Timer 0
overflow flag
CPL P2.3 ; Toggle P2.3 to generate square
wave SJMP RPT ; Repeat the process (infinite loop)
END
OBSERVATION & EXPECTED RESULTS:
After execution, proof of being square wave is as follows:
From the screenshot:
• T-ON ≈ 397.8374ms - 397.8351ms = 0.0023ms
• T-OFF ≈ 397.8397ms - 397.8374ms = 0.0023ms
Waveform Parameter:
• T-ON ≈ 0.0023 ms (2.3 µs)
• T-OFF ≈ 0.0023 ms (2.3 µs)
• Total Period ≈ 4.6 µs
o T=TON +TOFF
o T=0.0023 ms+0.0023 ms=0.0046 ms=4.6 µs
• Frequency ≈ 217.39 kHz o f=1/T
o f=14.6×10−6 s=217.39 kHz
Experiment: 4
Aim: To study and observe the effect of Watchdog Timer on infinite loop.
Theory:
A Watchdog Timer (WDT) is a special type of timer used in microcontrollers to reset the
system if it becomes unresponsive or "hangs." Essentially, it monitors the system and ensures
that the microcontroller is running correctly. If the microcontroller fails to perform a required
task within a certain time period (i.e., it "forgets" to reset the watchdog), the watchdog timer
will trigger a reset or some other recovery action.
• The Watchdog Timer is started at the beginning of the program or when the system
is initialized.
• The microcontroller must regularly "feed" or "reset" the watchdog timer within a
specific time frame.
• If the system gets stuck or enters an infinite loop (i.e., fails to feed the watchdog),
the watchdog will time out and perform a reset or other predefined actions.
• Once reset, the microcontroller starts from the beginning (or a known state).
• Some systems might generate an interrupt instead of resetting, which can be used
to perform emergency operations like saving data.
The original 8051 microcontroller does not have a dedicated register or hardware for a
watchdog timer. This means there is no default hardware mechanism that will automatically
reset the system if the program fails to feed the timer within a certain time.
If you still want a watchdog functionality in an original 8051, you will have to implement
your own software-based watchdog, use an external hardware watchdog circuit.
Simple example showing how you can simulate a watchdog timer in AT89C51:
SOURCE CODE:
ORG 0000H ; Reset vector
JMP MAIN ; Jump to main program
ORG 000BH ; Timer0 interrupt
vector
JMP SIMULATED_RESET ; Timer0 overflow simulates reset
MAIN: MOV TMOD, #01H ; Timer0 Mode 1 (16-bit timer)
MOV TH0, #0FCH ; High byte for ~50 ms
MOV TL0, #066H ; Low byte
SETB EA ; Enable global interrupts
SETB ET0 ; Enable Timer0
interrupt SETB TR0 ; Start Timer0
MAIN_LOOP:
NOP
CLR P1.0 ; Dummy task (simulate
working) NOP
SETB P1.0
CALL RESET_WATCHDOG ; Reset Timer0
regularly SJMP MAIN_LOOP ; Loop forever
; Routine to reset the watchdog (Timer0)
RESET_WATCHDOG:
CLR TR0
MOV TH0, #0FCH ; Reload for ~50ms
MOV TL0, #066H
SETB TR0
RET
; Simulate watchdog reset on Timer0 overflow
SIMULATED_RESET:
CLR TR0 ; Stop Timer0
CLR EA ; Disable interrupts (optional)
JMP MAIN ; Simulate system reset
END
OBSERVATION:
WHAT’S HAPPENING?
Section What it Does
RESET_WATCHDOG Reloads Timer0 before it overflows (must be called
regularly)
SIMULATED_RESET Acts like a watchdog reset — jumps back to MAIN
Timer0 Interrupt Triggers SIMULATED_RESET if watchdog
isn’t refreshed in time
• If your MAIN_LOOP gets stuck or CALL RESET_WATCHDOG isn’t reached
in time, the watchdog will trigger a system “reset.”
• You can simulate crashes, infinite loops, or deadlocks to test your software’s
fault recovery.
Experiment: 5
Aim: To observe watch dog timer and reset the count manually.
Theory:
Watchdog Timer (WDT) in AT89S52-
The Watchdog Timer (WDT) is a hardware timer that helps ensure system reliability by
resetting the microcontroller if the software becomes unresponsive — for example, if it enters
an infinite loop or gets stuck due to unexpected behaviour.
It is a critical safety and fault-recovery mechanism used widely in embedded systems to
automatically recover from software failures without human intervention.
In the AT89S52, the Watchdog Timer is not enabled by default and must be reset manually at
regular intervals to prevent a system reset.
To reset the WDT, a specific two-step instruction sequence must be written to the
special function register WDTRST (located at address 0A6H): MOV 0A6H, #1EH
; First part
of the reset sequence
MOV 0A6H, #0E1H ; Second part — completes the reset
This sequence reloads the WDT, preventing it from timing out and resetting the
microcontroller. If this reset sequence is not executed within the WDT timeout period, the
system will automatically reset, assuming the system has become unresponsive.
SOURCE CODE:
ORG 0000H
MAIN:
XRL P0, #0FFH ; Toggle Port 0 pins
ACALL DELAY ; Short delay (optional for visible toggling)
MOV 0A6H, #1EH ; Reset WDT
MOV 0A6H, #0E1H
SJMP MAIN ; Repeat forever
; Simple delay subroutine
DELAY: MOV R2,
#200 DELAY_LOOP1:
MOV R1, #255 DELAY_LOOP2:
DJNZ R1, DELAY_LOOP2
DJNZ R2,
DELAY_LOOP1 RET
OBSERVATION:
EXPLANATION:
• XRL P0, #0FFH
is used to toggle all bits of Port 0.
So, when you XOR a bit with 1, it flips (toggles)
it. 0FFH in binary is: 11111111 This flips all the
bits on P0.
• ACALL DELAY: Calls the DELAY subroutine. This introduces a time delay
so that toggling is visible (e.g., LED blinking or oscilloscope signals).
• MOV 0A6H, #1EH
MOV 0A6H, #0E1H
These are the 2 - byte sequence to reset the Watchdog Timer (WDT). 0A6H is the
address of the WDTRST register.
• SJMP MAIN: Short jump to the label MAIN. Creates an infinite loop such that
µp doesn’t go on executing garbage in the rest of the memory locations.
DELAY: Subroutine
This introduces a time delay using nested loops:
• MOV R2, #200
Outer loop runs 200 times.
• MOV R1, #255
Inner loop runs 255 times.
• DJNZ R1, DELAY_LOOP2
Decrement R1 and jump back to DELAY_LOOP2 until R1 = 0.
• DJNZ R2, DELAY_LOOP1
After inner loop ends, decrement R2 and repeat the inner loop.
• RET
Returns control back to the main program after the delay.
The total delay = 200 × 255 = 51,000 instruction cycles (approx. 51 ms if 1 µs per
instruction).
Experiment: 6
Aim: To write a program for a Traffic Light Control System using 8051 Microcontroller in
assembly language and simulate it on Proteus (SDL format).
Theory:
Traffic lights operate in a sequence: Red → Green → Yellow → Red.
Hardware Interface
• Red LED → P1.0
• Yellow LED → P1.1
• Green LED → P1.2
Each LED is controlled via a microcontroller pin. Delay routines control how long each light
stays ON.
The 8051 microcontroller outputs high or low signals to turn LEDs ON or OFF. In this
experiment, we interface three LEDs (Red, Yellow, Green) with Port 1 (P1.0 to P1.2) of the
8051. The program turns them ON/OFF in sequence with delays to simulate traffic signals.
SOURCE CODE:
ORG 0000H ; Program starts at address 0 START:
; RED ON, others OFF
SETB P1.0 ; Turn on
Red CLR P1.1 ; Yellow
OFF CLR P1.2 ; Green
OFF
ACALL DELAY ; ~1 second delay
; GREEN ON, others OFF
CLR P1.0 ; Red OFF
CLR P1.1 ; Yellow
OFF SETB P1.2 ;
Green ON
ACALL DELAY ; ~1 second delay
; YELLOW ON, others
OFF CLR P1.0 ; Red
OFF SETB P1.1 ;
Yellow ON CLR P1.2 ;
Green OFF
ACALL DELAY ; ~1 second delay
SJMP START ; Repeat forever
; ===== Delay Subroutine =====
; Simple ~1 second delay
DELAY: MOV R2,
#250
D1: MOV R1, #255
D2: DJNZ R1, D2
DJNZ R2, D1
RET
END
• SETB Px.y: Sets pin HIGH (LED ON if active-high).
• CLR Px.y: Sets pin LOW (LED OFF).
• SJMP START: Infinite loop to repeat the sequence.
• DELAY: Roughly 1-second delay (depends on crystal clock).
OBSERVATION:
CONNECTIONS:
For each LED:
• Connect the anode (longer pin) of the LED to:
o P1.0 for Red, o
P1.1 for Yellow,
o P1.2 for Green.
• Connect a resistor (e.g., 330Ω) to the cathode (shorter pin) of each LED.
• Connect the other end of the resistor to
GND. The resistor limits current and protects the
LED.
Experiment: 7
Aim: Write a program to interface DC Motor with 8051 Microcontroller.
Theory:
A DC motor converts electrical energy into mechanical motion. In embedded systems,
controlling the speed and direction of a DC motor is a common task, often required in
robotics and automation projects.
However, a microcontroller like the 8051 cannot directly drive a DC motor because: •
It cannot supply the required current (~250–600 mA),
• It only provides logic-level voltage (0 or 5V).
This is where the L293D motor driver IC comes into
play. The L293D is a dual H-Bridge driver IC that
allows:
• Bidirectional control of two DC motors.
• Handling up to 600 mA per channel.
• Logical control of direction and speed using input pins.
L293D Pin Configuration (1 Motor):
Pin Description
IN1 Control Input 1
IN2 Control Input 2
OUT1
Output to Motor terminal 1
OUT2
Output to Motor terminal 2
EN1 Enable (must be HIGH to run)
VCC1
Logic power (usually 5V)
VCC2
Motor supply (typically 9–12V)
GNDCommon Ground
Control 1 DC Motor using Port 2:
8051 Pin L293D Pin
Description
P2.0 IN1 Motor direction control
P2.1 IN2 Motor direction control
P2.2 EN1 Enable pin
Motor Behaviour:
IN1 IN2 Output (Motor Rotation)
1 0 Forward (Clockwise)
0 1 Reverse (Anti-Clockwise)
0 0 Motor Off
1 1 Motor Off (Brake)
SORCE CODE:
ORG 0000H
MOV P2, #00000101B ; IN1=1, IN2=0, EN=1 ? Forward
ACALL DELAY
MOV P2, #00000011B ; IN1=0, IN2=1, EN=1 ?
Reverse ACALL DELAY
MOV P2, #00000000B ; Motor Off
SJMP $
DELAY: MOV
R2, #255
HERE1: MOV R1, #255
HERE2: DJNZ R1, HERE2
DJNZ R2, HERE1
RET
END
WORKING:
1. The 8051 sends logic signals to the L293D.
2. L293D boosts the current and drives the motor terminals (OUT1/OUT2).
3. Enable pin must be HIGH for the motor to operate.
4. The direction of current determines rotation direction.
OBSERVATION:
CONNECTION:
• Connect the crystal between XTAL1 and XTAL2 of 8051.
• Connect two 22pF capacitors from each crystal pin to GND.
• Connect 10kΩ resistor between RST pin and VCC.
• Connect push-button between RST and GND. • L293D and
Motor Connections:
• P2.0 (8051) → IN1 (L293D)
• P2.1 (8051) → IN2 (L293D)
• P2.2 (8051) → EN1 (L293D)
• OUT1 and OUT2 → DC Motor terminals
• VCC1 (L293D) → 5V (logic)
• VCC2 (L293D) → 9V battery (motor power)
• GND (L293D) → Common Ground with 8051
Be sure VCC1 and VCC2 are not swapped.
:
Experiment: 8
Aim: To interface with stepper motor and rotate clockwise & anticlockwise.
Theory:
A stepper motor is an electromechanical device that converts a series of electrical pulses
into precise mechanical movement. Unlike DC motors, stepper motors move in fixed angular
increments, called steps, which makes them ideal for applications requiring accurate
positioning like printers, CNC machines, and robotics.
1. Each pulse rotates the motor shaft by a fixed angle (e.g., 1.8° per step).
2. The speed of rotation is controlled by the rate of pulses.
3. The direction of rotation depends on the sequence of pulses applied.
The 8051 Microcontroller cannot supply enough current to drive a stepper motor directly.
The ULN2003 is a Darlington transistor array used to amplify current and drive the coils of
the motor.
• It acts as an interface between 8051 and the motor.
• You connect the ULN2003 inputs to 8051 output pins and its outputs to the
motor coils.
For a 4-phase unipolar stepper motor, coils are energized in a specific order to rotate:
Clockwise Sequence:
Anti-Clockwise Sequence:
A stepper motor moves in small steps—each step turns the shaft by a small angle (like 1.8°).
To make it move:
• You energize coils inside the motor in a specific sequence.
• That’s what these values like 09H, 0CH, 06H, and 03H
do: they turn ON/OFF different coils.
Step Hex Binary Coils (P1.3–P1.0)
1 09H 1001 A and D ON
2 0CH 1100 B and A ON
3 06H 0110 B and C ON
4 03H 0011 C and D ON
SOURCE CODE:
ORG 0000H
MOV A, #04H ; Set number of steps =
4 MOV R3, A ; Store step count in R3
CW_LOOP:
MOV A, #09H ; Step 1 (1001)
ACALL OUTPORT ; Output to P1
ACALL DELAY ; Wait
MOV A, #0CH ; Step 2
(1100) ACALL OUTPORT
ACALL DELAY
MOV A, #06H ; Step 3
(0110) ACALL OUTPORT
ACALL DELAY
MOV A, #03H ; Step 4
(0011) ACALL OUTPORT
ACALL DELAY
DJNZ R3, CW_LOOP ; Repeat 4 times
MOV A, #04H
MOV R3, A ; Set step count for
ACW ACW_LOOP:
MOV A, #03H ; Step 1
(0011) ACALL OUTPORT
ACALL DELAY
MOV A, #06H ; Step 2
(0110) ACALL OUTPORT
ACALL DELAY
MOV A, #0CH ; Step 3
(1100) ACALL OUTPORT
ACALL DELAY
MOV A, #09H ; Step 4 (1001)
ACALL OUTPORT
ACALL DELAY
DJNZ R3, ACW_LOOP OUTPORT:
MOV P1, A ; Send step signal to P1 (connected to
ULN2003) RET
DELAY: MOV
R1, #0FFH
D1: MOV R2, #0FFH
D2: DJNZ R2, D2
DJNZ R1, D1
RET
END
EXPLANATION:
• This code rotates a 4-step stepper motor: 4 steps clockwise, then 4 steps anticlockwise
Each step is given a short delay to make the rotation visible and functional.
• Setting value of R3 executes 4 steps in Clockwise direction.
• The pattern 09H → 0CH → 06H → 03H is standard for 4-step clockwise control.
• OUTPORT sends the value to Port 1 (connected to motor through ULN2003).
• DELAY slows the signal so the motor visibly turns.
• DJNZ reduces R3 by 1 and loops until it's 0.
• Repeats the opposite pattern for anticlockwise direction. Sequence: 03H → 06H → 0CH →
09H
• DELAY: Provides a delay loop to make the step visible.
Loops through registers R1 and R2 to waste time (simple software delay).
OBSERVATION:
CONNECTION:
• 8051 to ULN2003
8051 Port ULN2003 INx Description
Pin Pin
P1.0 IN1 Coil A
P1.1 IN2 Coil B
P1.2 IN3 Coil C
P1.3 IN4 Coil D
• ULN2003 to Stepper Motor
ULN2003 OUTx Stepper Motor
Pin Coil
OUT1 Coil A
OUT2 Coil B
OUT3 Coil C
OUT4 Coil D
• ULN2003 Pin 9 (COM): Connect to motor VCC (for flyback diodes).
• ULN2003 Pin 8 (GND): Connect to Ground.
• Motor Power Supply: Use separate 12V or 5V supply (as needed by motor).
• 8051 Power: 5V from regulated supply or USB programmer.
Experiment: 9
Aim: Write a program to interface a matrix keyboard microcontroller 8051.
Theory:
A matrix keypad is a collection of push-button switches arranged in a grid (matrix) of rows
and columns (e.g., 4x4 or 4x3).
Instead of connecting each key separately, rows and columns reduce the number of
microcontroller I/O pins needed.
For a 4x4 keypad, there are:
• 4 Rows → Connected to P1.0-P1.3
• 4 Columns → Connected to P1.4-P1.7
• Output (Pressed Key Code) shown on → Port 2 (P2) (e.g., via LEDs or
logic analyzer)
Pin Type Role Direction
Row pins (R1–R4) Input pins The microcontroller reads these (checks if any key is
pressed)
Column pins (C1– Output The microcontroller sets these HIGH or LOW to scan
C4) pins the keypad
1. Set All Columns HIGH.
2. Pull One Column LOW at a time (others HIGH).
3. Read All Rows:
o If a key is pressed in that column, the row line connected to the key will
become LOW.
o This tells us the row and column → thus which key was pressed.
Example: If C2 is LOW and R3 is read LOW, then the key at Row 3, Column 2 was pressed.
SOURCE CODE:
ORG 0000H ; Start of code
MAIN:
MOV P1, #0FFH ; Set all P1 pins high (input pull-up)
MOV P2, #00H ; Clear Port 2 for output
SCAN:
; Column 1 LOW (P1.4 = 0), others HIGH
MOV A, #0EFH
ACALL CHECK_ROW
; Column 2 LOW (P1.5 = 0), others HIGH
MOV A, #0DFH
ACALL CHECK_ROW
; Column 3 LOW (P1.6 = 0), others HIGH
MOV A, #0BFH
ACALL CHECK_ROW
SJMP SCAN ; Keep scanning
; Subroutine to check rows
CHECK_ROW:
MOV P1, A ; Apply column
pattern MOV R0, A ; Save
column pattern MOV A, P1
ANL A, #0F0H ; Mask out column bits (ensure upper 4 bits are unaffected)
MOV A, P1
ANL A, #0F0H
CJNE A, #0F0H, KEY_PRESSED
RET
KEY_PRESSED:
MOV A, P1
CPL A ; Invert to get active-low logic (0 = pressed ? 1)
ANL A, #0F0H ; Mask only row bits
SWAP A
ANL A, #0F0H ; Adjust nibble
ORL A, R0 ; Combine row and column to form unique pattern
MOV P2, A ; Show key code on P2 (e.g., LEDs)
ACALL DELAY
WAIT_RELEASE:
MOV A, P1
ANL A, #0F0H
CJNE A, #0F0H, WAIT_RELEASE
RET
; Delay subroutine
DELAY: MOV
R2, #200
D1: MOV R1, #255
D2: DJNZ R1, D2
DJNZ R2, D1
RET
END
WORKING:
• P1 is connected to the keypad.
• P2 is used to show output (e.g., on 8 LEDs).
• SCAN: Each line sends a different column LOW.
After each, it calls CHECK_ROW to see if any key in that column is
pressed.
• CHECK-ROW: Applies column pattern to P1.
• Reads the value of P1 again to check if any row line (P1.0–P1.3) has gone
LOW.
If yes (i.e., key is pressed), it jumps to KEY_PRESSED.
• KEY_PRESSED: Determines which row is pressed.
• Combines it with the current column value to get a unique binary
code per key.
• Displays it on P2 — could be used for LEDs or further processing.
• WAIT_RELEASE: Loops here until all rows are HIGH (i.e., no key
is pressed).
Prevents key bouncing or double counting.
• DELAY: Provides a basic delay by
looping. Used to hold the key output
briefly on P2.
OBSERVATION:
CONNECTION:
1. Keypad to Port 1 (P1)
Assuming your keypad has 4 rows and 3 columns (7 pins total):
• Connect Rows to: o R1 → P1.0 o R2
→ P1.1 o R3 → P1.2
o R4 → P1.3
• Connect Columns to: o C1 → P1.4 o C2
→ P1.5 o C3 → P1.6
2. LEDs to Port 2 (P2)
• Connect 8 LEDs (with 330Ω resistors)
to P2.0 to P2.7 P2.0 → Resistor →
LED → GND
• Repeat for P2.1 to P2.7 This way,
each key press will show a unique 8-
bit pattern on the LEDs.
Experiment: 10
Aim: To interface with seven segment and display alphanumeric values.
Theory:
A Seven Segment Display (SSD) is an output device that shows numbers (0–9) and some
letters (A–F) using 7 LEDs arranged like this:
Types of SSD
1. Common Cathode (CC) o All cathodes (negative terminals) are connected
together to GND.
o Send HIGH (1) to a pin to turn ON a segment.
2. Common Anode (CA)
o All anodes (positive terminals) are connected to VCC.
o Send LOW (0) to turn ON a segment.
Each digit/letter is shown by turning ON certain segments.
Character Segments ON
Binary Pattern Hex
0 abcdef 00111111 0x3F
1 bc 00000110 0x06
2 abdeg 01011011 0x5B
A abcefg 01110111 0x77
B cdefg 01111100 0x7C
C adef 00111001 0x39
D bcdeg 01011110 0x5E
E adefg 01111001 0x79
F aefg 01110001 0x71
Patterns are stored in a Lookup Table for easy access.
SOURCE CODE:
ORG 0000H
MOV DPTR, #TABLE ; Point to the lookup table
MOV R0, #16 ; 16 characters to display
MOV R1, #00H ; Index register
LOOP:
MOV A, R1
MOVC A, @A+DPTR ; Get segment
pattern MOV P2, A ; Output to 7-segment
ACALL DELAY ; Wait
INC R1
DJNZ R0, LOOP ; Display all 16 values
SJMP LOOP ; Repeat forever
; Lookup table: 0–9 and A–F for Common Cathode 7-segment
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH ; 0–9
DB 77H,7CH,39H,5EH,79H,71H ; A–F
; Simple delay routine
DELAY: MOV
R2, #255
D1: MOV R3, #255
D2: DJNZ R3, D2
DJNZ R2, D1
RET
END
WORKING:
• DPTR is a 16-bit data pointer.
• It is set to point to the start of the TABLE, which contains the segment patterns for
0– F.
• R0 is the loop counter: 16 iterations for 0–F.
• R1 is the index into the Lookup Table, starting at 0.
• MOV A, R1: Copy index into Accumulator.
• MOVC A, @A+DPTR: Use DPTR + A as address to get a pattern from the
lookup table.
• MOV P2, A: Send the segment pattern to Port 2, which drives the 7-segment display.
• After each character is displayed, there's a small delay.
• INC R1: Move to the next character.
• DJNZ R0, LOOP: Decrease R0 and loop if not zero (runs 16 times). • SJMP
LOOP: Restart the whole display forever.
OBSERVATION:
CONNECTION:
1. Connect Microcontroller to Display
Assuming you're using Port 2 (P2.0 – P2.6) for segments a–g:
SSD Segment Pin Name Connect to Microcontroller
a A P2.0
b B P2.1
c C P2.2
d D P2.3
e E P2.4
f F P2.5
g G P2.6
dp (optional) DP P2.7 or leave unconnected
COM Common cathode GND
Add resistors (220Ω) between each segment pin and P2.0–P2.6 (optional in simulation).
2. Clock and Reset Circuit
• Crystal Oscillator (11.0592 MHz) between XTAL1 and XTAL2
• Two 22pF capacitors from XTAL1/XTAL2 to GND
• Connect Pin 31 (EA) to VCC
• Connect Pin 9 (RST) to:
o 10kΩ resistor → GND o
Push Button → VCC This
acts as a manual RESET
3. Segment Placement
• Place 7SEG-CC
• Rotate and place it near Port 2 (P2.0 to P2.6)
• Connect Common Cathode pins (COM1, COM2) of the display to GND