Embedded Report 043
Embedded Report 043
ENGR M UMAIR
SUBMITTED BY:
SAIF UR REHMAN
FA22-BSE-043
SECTION (a)
Pg. 1
Table of Contents
Pg. 2
Abstract
This report outlines a series of three experiments utilizing the AT89C51 microcontroller, implemented through
Keil and Proteus simulation platforms. The first experiment focuses on LED control programming, showcasing
alternating LED patterns to simulate a blinking effect. The second experiment involves the design and
implementation of a multifunctional digital calculator capable of performing both basic arithmetic and advanced
scientific operations, integrating LCD and keypad interfaces for input and output. The third experiment
investigates seven-segment display interfacing, emphasizing design techniques and control logic. The results
confirm the successful execution of all experiments, with the calculator delivering precise calculations and the
LED and seven-segment circuits operating reliably. These studies highlight essential concepts in microcontroller
programming, peripheral interfacing, and embedded system design, providing foundational knowledge for real-
world applications.
Experiment 1 – LED Blinking Automatically
Write a program in C using Keil and design the circuit in Proteus where two LEDs blink
alternately (one OFF while the other is ON).
OBJECTIVES:
MATERIALS:
Primary:
• 1 Green LED
• 1 Red LED
Pg. 3
TOOLS:
MicrocontrollerSetup:
Circuit Design:
• Use appropriate resistors (e.g., 220Ω) in series with the LEDs to limit current.
ProjectInitialization:
TargetConfiguration:
Pg. 5
."
Pg. 6
Source File Integration:
1. Create a new C source file by right clicking on source group and clicking add new item to
group as shown in the fig below.
Pg. 7
• Selecting a C file and giving it an appropriate name.
Pg. 8
This program demonstrates basic microcontroller LED interfacing and
control logic with alternating blinking.
CODE
sbit LED1 = P1^0; // Green LED connected to P1.0 sbit LED2 = P1^1; //
Pg. 9
EXPLANATION
NOTE:
• The circuit can be extended to include additional LEDs
or other devices.
OUTPUT
Pg. G
Fig 1.2 LED Circuit Output
Pg. 10
USE CASES
Use Case 1: LED Status Indicator
• Actors: User, Microcontroller (AT89C51)
• Purpose: To demonstrate alternating LED blinking behavior, suitable for visual
signaling systems.
• Preconditions:
1. Microcontroller properly configured and programmed.
2. Circuit connections are accurate as per the schematic.
• Main Flow:
1. Power on the microcontroller.
2. Observe LED 1 turning ON while LED 2 remains OFF.
3. After a delay, LED 1 turns OFF, and LED 2 turns ON.
4. The cycle repeats indefinitely.
• Postconditions: LEDs continue blinking alternately without errors.
• Exceptions:
o LED connection failure.
o Software bugs leading to irregular blinking patterns.
Use Case 2: Debugging Output Signals
• Actors: Engineer, Debugger Tools, Microcontroller
• Purpose: Verify signal output behavior at microcontroller ports.
• Preconditions:
1. Circuit loaded into Proteus simulation.
2. Code compiled and uploaded via Keil µVision.
• Main Flow:
1. Open Proteus and run the simulation.
2. Monitor the LED behavior and debug any anomalies.
• Postconditions: All signals behave as expected.
• Exceptions:
o Compilation errors.
o Simulation mismatches due to configuration errors.
NOTES
Pg. 11
Phase 1: Circuit Design and Deployment in Proteus
• Ensure proper placement of the AT89C51 microcontroller in the workspace.
• Double-check the connections:
o Green LED connected to Pin P1.0. o Red LED connected to
Pin P1.1. o Resistors (220Ω) added in series to limit
current and prevent damage.
Phase 2: Coding and Compilation in Keil µVision
• Configure the microcontroller frequency and mode.
• Write a simple loop-based delay function for alternating LED control.
• Verify that the program initializes Port 1 pins as outputs.
Phase 3: Testing in Proteus Simulation
• Load the compiled HEX file into the Proteus microcontroller.
• Run the simulation and observe LED behavior.
• Debug errors, if any, in code or circuit design.
Troubleshooting Tips:
• Check power connections (VCC and GND).
• Ensure proper resistor values to avoid excessive current.
• Use oscilloscope probes in Proteus to monitor port outputs if the LEDs fail.
• Verify the HEX file upload process to ensure correct programming.
Pg. 12
Experiment 2 – Implementation of a Seven-Segment
Display
OBJECTIVES:
MATERIALS:
1. Primary Components:
• AT89C51 Microcontroller
2. Tools/Software:
• LogicState for circuit simulation
Pg. 13
Environment: Proteus Software
TOOLS: and Compilation: Keil µVision
Simulation
Code Development
Setup:
PHASE 1:
AT89C51 microcontroller in the Proteus workspace.
Microcontroller
GND and VCC pins to their respective power sources.
• Place the
Display Connection the seven-segment display (7SEG-CAT-COM-
Connect
•
GREEN) to the output pins of
Seven-Segment
the microcontroller.
• Connect
specific port pins (e.g., P2) for segment control:
Segment a → P2.0
• Assign
Segment b →
ooo
P2.1 Segment
ooo
c → P2.2
o
Segment d →
P2.3
Segment e → P2.4
Segment f → P2.5
Segment g → P2.6
Pg. 14
Pg. 15
Fig 2.1 Seven Segment Display Circuit
ProjectInitialization:
TargetConfiguration:
Pg. 16
• "Target1" will appear in the Projectwindow
."
Pg. 17
• Enable HEX file creation in the output setting
Pg. 18
Source File Integration:
2. Create a new C source file by right clicking on source group and clicking add new item to
group as shown in the fig below.
Pg. 19
• Selecting a C file and giving it an appropriate name.
Pg.
1G
This program demonstrates basic microcontroller LED interfacing and control logic
with alternating blinking.
CODE
Pg. 20
unsigned int i, j; for (i = 0; i < time;
i++) for (j = 0; j < 100; j++); // Delay
loop
}
while (1) {
digit++; //
Increment the digit
if (digit > 9) {
// Reset digit after 9
digit = 0;
} else {
}
P1 = 0x00; // Turn off the seven-segment display
}
}
}
EXPLANATION
• Header File: #include <reg51.h> provides access to the AT89C51
microcontroller's registers and functionalities.
• Control Pin: CONTROL_PIN (P2.0) is used to toggle the display's ON/OFF state.
Pg. 21
• Seven-Segment Array: seven_seg contains
predefined patterns for displaying digits 0-9, with
each bit controlling a segment.
• Logic:
Pg. 22
Fig 2.2 Seven Segment Display Circuit Output
Pg. 23
Fig 2.4 Seven Segment Display Circuit Output
Pg. 24
USE CASES:
Use Case 1: Displaying Numbers on the Seven-Segment Display
Description: Display numbers (0–9) sequentially on the seven-segment display.
• Actors: AT89C51 microcontroller, seven-segment display.
• Preconditions:
o The microcontroller and seven-segment display are connected correctly.
o Power supply is available.
• Steps:
1. Initialize ports for output.
2. Load hexadecimal values corresponding to each digit into the display.
3. Provide delays between number transitions.
NOTES:
1. Pin Configuration:
o Connect seven-segment display pins (A–G) to the microcontroller port
(P2.0–P2.6). o Connect the common cathode (COM) to GND. o Use
current-limiting resistors (220Ω) in series with each segment to prevent
damage.
2. Programming Logic:
o Define a lookup table for hexadecimal values corresponding to digits (0–9)
and characters (A–F).
o Implement delays using timers to create visibility for each output.
3. Simulation in Proteus:
o Import the hex file generated from Keil µVision into Proteus.
o Test the circuit by observing the outputs on the seven-segment display.
4. Troubleshooting Tips:
o Verify connections between microcontroller and display. o Check for
correct hexadecimal values corresponding to the desired output.
o Ensure delays are appropriately timed for visibility.
Pg. 38
Experiment 3 - Design of a Digital
Calculator Using a
Microcontroller, LCD,
and Keypad
OBJECTIVES
MATERIALS
1. Primary Components:
• AT86C51 Microcontroller
• KEYPAD-CALCULATOR
2. Tools/Software:
TOOLS:
1. Microcontroller Setup
•Place the AT89C51 microcontroller in the simulation
workspace.
Pg. 27
Pg. 40
6. Save the project with an appropriate name.
TargetConfiguration:
Pg. 42
."
Pg.
Source File Integration:
3. Create a new C source file by right clicking on source group and clicking add new item to
group as shown in the fig below.
Pg. 44
2G
• Selecting a C file and giving it an appropriate name.
Pg.
30
This program demonstrates basic microcontroller LED
interfacing and control logic with alternating blinking.
CODE
#include <reg51.h>
#include <math.h> // Include math library
Read/Write
sbit LCD_EN = P3^2; // Enable
sbit
ROW2
=
P0^1;
// Row
2 sbit
ROW3
=
P0^2;
// Row
3 sbit
ROW4
=
P0^3;
// Row
4
// Column 1
sbit COL2
= P1^1; //
Column 2
sbit COL3
= P1^2; //
Column 3
sbit COL4
= P1^3; //
Column 4
sbit COL5
= P1^4; //
Column 5
sbit COL6
= P1^5; //
Column 6
Pg. 31
// Function to delay for some
for (i = 0; i <
ms; i++)
{ for (j =
0; j <
123;
j++);
}
}
// LCD functions
void lcd_command(unsigned
char cmd) { P2 = cmd; //
LCD_EN = 1; // Enable
pulse LCD_EN = 0;
// Disable pulse
delay_ms(1);
LCD_RW
= 0; // Write
mode LCD_EN
= 1; // Enable
pulse LCD_EN
= 0; // Disable
pulse
delay_ms(1); }
Pg. 32
void lcd_initialize() {
lcd_command(0x38);
// Initialize 2-line
mode
lcd_command(0x0C);
// Display ON, Cursor
OFF
lcd_command(0x06);
// Auto increment
cursor
lcd_command(0x01);
// Clear display
delay_ms(1);
}
}
}
// Keypad Functions
Pg. 33
ROW1 = 1; ROW2 = 1; ROW3 = 0; ROW4 = 1; // Set row 3
low, others high
if (!COL1) return '%'; if (!COL2) return '1'; if (!COL3) return '2'; if (!COL4)
return '3';
if (!COL5) return '+'; if (!COL6) return "M-";
if (!COL1) return 'S'; if (!COL2) return '0'; if (!COL3) return '.'; if (!COL4)
return '=';
if (!COL5) return '+'; if (!COL6) return "M+";
}
}
// Perform Calculation
void perform_calculation(double operand1, double operand2, char operator) {
double result = 0;
char result_string[16];
if (operator == '+') {
result =
operand1 +
operand2;
}
else if (operator == '-') { result =
operand1 - operand2;
}
else if (operator == '*') { result =
operand1 * operand2;
}
else if (operator == '/') { if
(operand2 != 0) {
Pg. 34
result = operand1 / operand2;
} else {
lcd_command(0x01); // Clear LCD
lcd_display("Error: Div by 0");
delay_ms(2000); return;
}
}
else if (operator == '%') { result =
(operand1 * operand2) / 100;
}
else if (operator == 'S') { if (operand1 >=
0) { result =
sqrt(operand1);
} else {
result = -1; // Invalid input for square root
}
}
// Main Program
void main() {
Pg. 35
double first_operand = 0,
second_operand = 0; char
current_operator = 0;
char key_input;
key_input =
keypad_read(); if
(key_input
== 'O') {
lcd_command(0x01);
// Clear LCD
delay_ms(1000);
break;
}
}
// Read operator
current_operator = keypad_read();
Pg. 36
lcd_data(current_operator); // Display operator delay_ms(500);
delay_ms(500);
= keypad_read(); if (key_input
== '=') {
break;
}
}
lcd_display("Calculating..."); delay_ms(1000);
the calculation
Pg. 37
}
EXPLANATION
• The program interfaces the AT89C51 microcontroller with an LCD and keypad.
• The keypad is used for input, and the LCD displays the output for basic
arithmetic operations such as addition, subtraction, multiplication, and
division.
• The program logic is designed to handle scientific operations like square root
calculation.
OUTPUT
Pg. 38
Fig 3.4 Calculator Circuit Output
Pg. 3G
Fig 3.6 Calculator Circuit Output
USE CASES:
1. Testing LED Connectivity: o Verify LED connections and their operation by observing
alternate blinking.
2. Timing Adjustments: o Modify delay intervals for custom blinking patterns.
3. Troubleshooting:
o Detect issues in circuit connections or code logic during simulation.
4. Embedded Systems Learning:
o Understand GPIO pin manipulation and timing controls in microcontroller programming.
NOTES:
Pg. 3G
Experiment - 4 Dice Simulation Using AT89C51 Microcontroller
Write a program in C using Keil and design the circuit in Proteus to simulate a dice roll. The random
number generated (1–6) is displayed on a 7-segment display when a trigger button is pressed.
OBJECTIVES:
MATERIALS:
Primary:
1. AT89C51 Microcontroller
Secondary:
• 1 7-Segment Display (Common Cathode)
• 1 Push Button (Trigger)
• Resistors (330Ω for the 7-segment display)
• 10kΩ Resistor for the trigger button
• 10µF Capacitor (for button debounce)
Pg. 3G
TOOLS:
Pg. 3G
PHASE 1: CIRCUIT DEPLOYMENT (PROTEUS)
Microcontroller Setup:
Circuit Design:
1. 7-Segment Display:
o Connect the 7-segment display to Port 2 (P2.0 to P2.7) of the microcontroller.
o Add 330Ω resistors to each segment for current limiting.
2. Trigger Button:
o Connect the push button to Pin P1.0 with a 10kΩ pull-down resistor.
o Add a 10µF capacitor for debounce.
Pg. 3G
PHASE 2: CODE IDE SETUP (KEIL µVISION)
Project Initialization:
Target Configuration:
1. Create a new C source file by right-clicking on "Source Group" and selecting "Add New Item to Group."
2. Write and save the program for dice simulation.
Code:
#include <reg51.h>
// Define pins
sbit trigger = P1^0; // Trigger pin (logic state)
// 7-segment display codes for numbers 1 to 6
unsigned char seg_code[] = {0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D};
void delay(unsigned int ms) {
unsigned int i, j; for (i = 0;
i < ms; i++) for (j = 0; j
< 123; j++);
}
void timer_init() {
TMOD = 0x01; // Timer 0 in Mode 1 (16-bit timer)
TH0 = 0x00; // Initial high byte
TL0 = 0x00; // Initial low byte
TR0 = 1; // Start Timer 0
}
void main() {
unsigned char random_number;
while (1) {
if (trigger == 1) { // Check if trigger is active
delay(200); // Debounce delay
if (trigger == 1) { // Check again for stable input
random_number = (TL0 % 6) + 1; // Generate a random number between 1 and 6
P2 = seg_code[random_number - 1]; // Display number on
7segment
delay(500); // Short display delay
}
} else {
P2 = 0x00; // Turn off display when trigger is low
}
}
}
Pg. 3G
EXPLANATION:
3. Timer Initialization:
o Timer 0 is configured in Mode 1 (16-bit timer).
o TR0 = 1 starts the timer.
4. Display Control:
o The random number is displayed on the 7-segment display using the corresponding segment
code.
NOTES:
Troubleshooting Tips:
• Verify proper connections:
o 7-segment display connected to Port 2.
o Trigger button connected to Pin P1.0.
• Ensure correct resistor values for current limiting.
• Use Proteus oscilloscope to debug port outputs if the display fails.
OUTPUT
• Expected Behavior: When the button is pressed, the 7-segment display will show a random number
between 1 and 6.
• Observations:
o Accurate number generation and display. o Stable button press detection due to debounce.
Pg. 3G
Fig 4.1 Dice Simulator Output
Pg. 3G
Experiment 5 - Home Automation System Using AT89C51 Microcontroller
OBJECTIVES:
MATERIALS:
Primary:
1. AT89C51 Microcontroller
Secondary Components:
• 1N4004 Diode
• 2N2369 Transistor
• 10W Resistor (1kΩ)
• 555 Timer IC
• ADC0804 (Analog-to-Digital Converter)
• L293D Motor Driver IC
• PIR Sensors (3 units)
• LM35 Temperature Sensor
• ULN2003A Relay Driver
• DC Fan
• Keypad (for password input)
• LEDs (Green and Red)
• Switches (SPDT and general switches) TOOLS:
• Simulation Environment: Proteus Software
• Code Development and Compilation: Keil µVision
Microcontroller Setup:
• Place the AT89C51 microcontroller in the Proteus workspace. • Connect GND and VCC pins to their respective
power sources.
Circuit Design:
Pg. 3G
Fig 5.1 Home automation system cirruit
Project Initialization:
Target Configuration:
• Select the AT89C51 microcontroller as the target device.
• Configure output settings to enable HEX file creation.
Source File Integration:
1. Create a new C source file by right-clicking on "Source Group" and selecting "Add New Item to Group."
2. Write and save the program to handle temperature, motion detection, and keypad inputs.
#define RD P2_0
#define WR P2_1
// Output Pins
// Function Prototypes
void main() {
float temperature;
// Initialize pins
while (1) {
adc_value = ADC_Read();
Pg. 3G
} else {
if (PIR_SENSOR == 1) {
LED if no motion
delay(1);
WR = 1;
delay(1);
return adc_value; }
// Delay function
unsigned int i, j;
Pg. 3G
for (j = 0; j < 1275; j++);
OUTPUT:
Pg. 3G
EXAMPLE USE CASES:
NOTES:
Troubleshooting Tips:
• Verify sensor connections and ensure they are correctly interfaced with the microcontroller.
• Use current-limiting resistors for LEDs and other components to prevent damage.
• Debug faulty relay operations by checking transistor and diode configurations.
Output Observations:
• Motion-controlled lights activate only when movement is detected. • Fan speed or on/off operation corresponds
accurately to temperature input.
• Secure and reliable door lock mechanism with keypad functionality.
OBJECTIVES:
Pg. 3G
• Develop a program to display a name on a 16x2 LCD (LM016L) using the AT89C51 microcontroller.
• Interface the LCD with the microcontroller and display text dynamically.
• Simulate the designed circuit in Proteus software.
MATERIALS:
Primary:
1. AT89C51 Microcontroller Secondary Components:
• LM016L 16x2 LCD
• Resistors (10kΩ for pull-up)
• Potentiometer (10kΩ for LCD contrast adjustment)
• Capacitors (10µF, 33pF)
• Crystal Oscillator (12 MHz)
TOOLS:
Circuit Design:
1. LCD Connections:
o Connect the data pins (D4–D7) of the LM016L to Port 2 (P2.4–P2.7) of the AT89C51. o
Connect RS (Register Select) to P1.0, RW (Read/Write) to GND, and E
(Enable) to P1.1.
o Adjust contrast using a 10kΩ potentiometer connected to pin VEE of the LCD.
2. Clock and Reset Circuit:
o Connect a 12 MHz crystal oscillator to XTAL1 and XTAL2. o Add two 33pF capacitors
to stabilize the clock signal.
o Use a 10µF capacitor and a resistor for the reset circuit.
Pg. 3G
Fig 6.1 Name Display cirruit
Project Initialization:
Target Configuration :
• Select the AT89C51 microcontroller as the target device.
• Configure output settings to enable HEX file creation.
1. Create a new C source file by right-clicking on "Source Group" and selecting "Add New Item to Group."
2. Write and save the program to display the name on the LCD.
Pg. 3G
PHASE 3: IMPLEMENTATION LOGIC
Code:
#include <reg51.h>
void main(void) {
lcd_init(); // Initialize LCD
Pg. 3G
lcd_command(0x80); // Force cursor to the first line
}
OUPUT:
NOTES:
Troubleshooting Tips:
• Ensure correct connections for the LCD data and control pins.
• Use a potentiometer to adjust the LCD contrast if the text is not visible.
• Verify resistor and capacitor values for the clock and reset circuits.
Pg. 3G
Experiment 7 - Car Prototype Control
OBJECTIVES:
• Design and implement a car prototype controlled using the AT89C51 microcontroller.
• Interface L293D motor driver, logic states, and motors with the microcontroller for directional control.
• Simulate the designed circuit in Proteus software to validate functionality.
MATERIALS:
Primary Components:
1. AT89C51 Microcontroller
Secondary Components:
• L293D Motor Driver IC
• Logic States (Push Buttons or DIP Switches)
• Motors (DC Motors for the prototype wheels)
Microcontroller Setup:
1. Place the AT89C51 microcontroller in the Proteus workspace.
2. Connect GND and VCC pins to their respective power sources.
Circuit Design:
Pg. 3G
Fig 7.0 Car Prototype Circuit
Project Initialization:
1. Launch Keil µVision and create a new project.
2. Save the project with a name, such as "CarPrototypeControl."
Target Configuration:
• Select the AT89C51 microcontroller as the target device.
• Configure output settings to enable HEX file creation.
Code:
#include <reg51.h>
// Pin Configurations
sbit Button1 = P0^0; // Button 1 connected to P0.0
sbit Button2 = P0^1; // Button 2 connected to P0.1
sbit Button3 = P0^2; // Button 3 connected to P0.2
sbit Button4 = P0^3; // Button 4 connected to P0.3
sbit Button5 = P0^4; // Button 5 connected to P0.4
Pg. 3G
sbit Motor1_Input1 = P3^0; // Motor 1 Input 1 connected to P3.0
sbit Motor1_Input2 = P3^1; // Motor 1 Input 2 connected to P3.1
sbit Motor2_Input1 = P3^2; // Motor 2 Input 1 connected to P3.2
sbit Motor2_Input2 = P3^3; // Motor 2 Input 2 connected to P3.3
controlMotors(state);
}
}
OUTPUT:
Pg. 3G
Fig 7.2 Car Prototype Output
Pg. 3G
Fig 7.4 Car Prototype Output
Expected Behavior:
• Button 1: Motor 1 (CW), Motor 2 (CCW)
Pg.
• 3G
Button 2: Motor 1 (CCW), Motor 2 (CW)
• Button 3: Both motors (CW)
• Button 4: Both motors (CCW)
• Button 5: Motors OFF
NOTES:
Troubleshooting Tips:
• Double-check the motor driver and microcontroller connections.
• Ensure that the power supply is sufficient for both the microcontroller and motors.
• Use pull-down resistors on logic state pins to avoid floating inputs.
Pg. 3G