0% found this document useful (0 votes)
30 views101 pages

Embedded Report 043

The report details three experiments using the AT89C51 microcontroller, focusing on LED control, a digital calculator, and seven-segment display interfacing. Each experiment includes objectives, materials, circuit design, code implementation, and results, confirming successful operation of the circuits. The findings emphasize key concepts in microcontroller programming and embedded system design, applicable to real-world scenarios.

Uploaded by

saifiurrehman43
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)
30 views101 pages

Embedded Report 043

The report details three experiments using the AT89C51 microcontroller, focusing on LED control, a digital calculator, and seven-segment display interfacing. Each experiment includes objectives, materials, circuit design, code implementation, and results, confirming successful operation of the circuits. The findings emphasize key concepts in microcontroller programming and embedded system design, applicable to real-world scenarios.

Uploaded by

saifiurrehman43
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/ 101

MIRPUR UNIVERSITY OF SCIENCE AND TECHNOLOGY

EMBEDDED SYSTEMS REPORT


SUBMITTED TO:

ENGR M UMAIR

SUBMITTED BY:

SAIF UR REHMAN

FA22-BSE-043

SECTION (a)

Pg. 1
Table of Contents

1. Experiment 1: LED Blinking Automatically


o Objectives…………………………………………………………….3
o Materials and Tools…………………………………………………..3 o
Phase 1: Circuit Deployment (Proteus)……………………………….4 o
Phase 2: Code IDE Setup (Keil µVision)……………………………..5 o
Phase 3: Implementation Logic…………………………………….....9
o Code
Explanation……………………………………………………..10 o
Output and Observations……………………………………………...10
o Use Cases and
Notes………………………………………………….11

2. Experiment 2: Implementation and Analysis of a Seven-Segment Display


o Objectives…………………………………………………………….13
o Materials and Tools…………………………………………….…….14
o Phase 1: Circuit Deployment (Proteus)…………………………...…..14
o Phase 2: Code IDE Setup (Keil
µVision)…………………………..…15 o Phase 3: Implementation
Logic…………………………………….....20 o Code
Explanation……………………………………………………...21 o
Output and
Observations………………………………………………22 o Use
Cases and Notes…………………………………………………..25

3. Experiment 3: Design of a Digital Calculator


o Objectives……………………………………………………………27
o Materials and Tools……………………………………………..……27
o Phase 1: Circuit Deployment (Proteus)……………………………….27
o Phase 2: Code IDE Setup (Keil
µVision)…………………………......28 o Phase 3: Implementation
Logic……………………………………….31 o Code
Explanation…………………………………………………..…38 o
Output and Observations……………………………………………...38

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:

• Implement a C program to control two LEDs blinking alternately.

• Demonstrate interfacing LEDs with the AT89C51 microcontroller.

• Simulate the designed circuit in Proteus software.

MATERIALS:

Primary:

1. AT89C51 Microcontroller Secondary:

• 1 Green LED

• 1 Red LED

Pg. 3
TOOLS:

SimulationEnvironment: Proteus Software


Code Development and Compilation: Keil µVision

PHASE 1: CIRCUIT DEPLOYMENT (PROTEUS)

MicrocontrollerSetup:

• Place the AT89C51 microcontroller in the Proteus workspace.

• Connect GND and VCC pins to their respective power sources.

Circuit Design:

• Connect the two LEDs to the microcontroller as follows:

o Green LED: P1.0

o Red LED: P1.1

• Use appropriate resistors (e.g., 220Ω) in series with the LEDs to limit current.

Pg. 4 Fig 1.1 LED Circuit


PHASE 2: CODE IDE SETUP (KEIL µVISION)

ProjectInitialization:

1. Launch Keil µVision and create a new project.

2. Save the project with an appropriate name.

TargetConfiguration:

• Select the AT89C51 microcontroller as the target device.

• "Target1" will appear in the Projectwindow

Pg. 5
."

• Execute a right-click on "Target1" and select "Optionsfor Target 'Target1'

• Enable HEX file creation in the output setting

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.

PHASE 3: IMPLEMENTATION LOGIC

CODE

#include <reg51.h> // Header file

sbit LED1 = P1^0; // Green LED connected to P1.0 sbit LED2 = P1^1; //

Red LED connected to P1.1 void delay(unsigned int time) {


unsigned
int i, j;
for (i =
0; i <
time;
i++) {
for (j = 0; j < 1200; j++); //
Approximate delay } void main() { while (1) {
LED1 = 1; // Turn ON Green

LED LED2 = 0; // Turn OFF


Red LED delay(300); // Delay
for 300ms

LED1 = 0; // Turn OFF Green LED LED2 = 1;


// Turn ON Red LED delay(300); //

Delay for 300ms


}
}

Pg. 9
EXPLANATION

★ #include <reg51.h>: Provides definitions for the AT86C51


microcontroller's registers, allowing access to port and pin
configurations.
★ sbit: Defines LED1 (Green LED) at P1.0 and LED2 (Red
LED) at P1.1 for easier pin control.
★ delay(unsigned int time): Introduces a delay between state
changes using nested loops to create visible blinking effects.
★ void main(): The entry point of the program where
execution begins.
★ while(1): Creates an infinite loop to continuously
alternate the states of the LEDs.
★ LED control: The program alternates LED1 and LED2
between ON (1) and OFF (0), ensuring only one LED is ON at
a time.
★ Timing: Each state lasts for 300ms before switching to the
next.

LED Blinking Alternate Logic:

• The program toggles the states of two LEDs connected


to P1.0 and P1.1.

• A delay is introduced after each toggle to allow the


LEDs to blink alternately.

• The sbit directive is used to assign descriptive names to


the specific pins.

EXAMPLE USE CASES:

• Visual indicators for alternating signals.

• Basic demonstration of microcontroller programming


and LED interfacing.

NOTE:
• The circuit can be extended to include additional LEDs
or other devices.

• Adjust the delay function values to modify the blinking


speed.

OUTPUT

Pg. G
Fig 1.2 LED Circuit Output

Fig 1.3 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:

• Understand the concept and working of a seven-segment display.


• Interface a seven-segment display with the AT89C51 microcontroller.
• Write a program to display numbers or characters on the seven-segment
display.
• Simulate the circuit to analyze the functionality and ensure correct operation.

MATERIALS:

1. Primary Components:

• AT89C51 Microcontroller

• 7-SEG-CAT-COM-GREEN (Seven-Segment Display)

2. Tools/Software:
• LogicState for circuit simulation

Pg. 13
Environment: Proteus Software
TOOLS: and Compilation: Keil µVision
Simulation
Code Development

CIRCUIT DEPLOYMENT (PROTEUS)

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

PHASE 2: CODE IDE SETUP (KEIL µVISION)

ProjectInitialization:

3. Launch Keil µVision and create a new project.

4. Save the project with an appropriate name.

TargetConfiguration:

• Select the AT89C51 microcontroller as the target device.

Pg. 16
• "Target1" will appear in the Projectwindow

."

• Execute a right-click on "Target1" and select "Optionsfor Target 'Target1'

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.

PHASE 3: IMPLEMENTATION LOGIC

CODE

#include <reg51.h> // Include 8051 microcontroller header file

// Define control pin


sbit CONTROL_PIN = P2^0; // Pin P2.0 is used as the control logic state

// Seven-segment display values for


digits 0-9 unsigned char seven_seg[10]
={

0x3F, // 0 -> 0b00111111


0x06, // 1 -> 0b00000110

0x5B, // 2 -> 0b01011011


0x4F, // 3 -> 0b01001111

0x66, // 4 -> 0b01100110


0x6D, // 5 -> 0b01101101

0x7D, // 6 -> 0b01111101


0x07, // 7 -> 0b00000111
0x7F, // 8 -> 0b01111111
0x6F // 9 -> 0b01101111
};
void delay(unsigned int time) {

Pg. 20
unsigned int i, j; for (i = 0; i < time;
i++) for (j = 0; j < 100; j++); // Delay
loop
}

void main() { unsigned char digit = 0; // Variable to store the


current digit

while (1) {

if (CONTROL_PIN == 1) { // Check if control pin


is HIGH Port 1
P1 = seven_seg[digit]; // Send digit pattern
to
count speed
delay(500); // Delay for visible

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:

o Continuously checks the state of


CONTROL_PIN.

o If HIGH, the corresponding digit pattern is


output to Port 1 (P1), with a delay to ensure
visibility.

o If LOW, the display is turned off by setting


P1 to 0.

o The digits cycle from 0 to 9, resetting after


9.

• Delay Function: Introduces a brief pause to slow


down the counting, ensuring clear visibility of each
digit.
OUTPUT

Pg. 22
Fig 2.2 Seven Segment Display Circuit Output

Fig 2.3 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.

• Expected Result: Numbers (0–9) appear sequentially on the display.

Use Case 2: Displaying Custom Characters or Symbols


Description: Program the microcontroller to show letters (A–F) or symbols.

• Actors: AT89C51 microcontroller, seven-segment display.


• Preconditions:
o Hardware is configured and verified.
o Hexadecimal patterns for custom characters are defined.
• Steps:
1. Define data patterns for the required symbols.
2. Send patterns to the seven-segment display.
3. Verify output.
• Expected Result: Custom characters appear as defined.
Use Case 3: Error Indication Using the Display
Description: Use the display to show error codes or alerts.
Pg. 25
Pg. 36
• Actors: AT89C51 microcontroller, seven-segment display.
• Preconditions:
o Error codes and logic are predefined.
o The system detects errors based on inputs.
• Steps:
1. Detect the error condition.
2. Trigger the corresponding hexadecimal pattern for the error code.
3. Display the code.
• Expected Result: Error codes are displayed in case of system failures.

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

• Design and implement a digital calculator that


performs basic arithmetic and scientific
operations.

• Interface the AT86C51 microcontroller with an


LCD display (LM016L) and a keypad for user
input.

• Write a program to handle the input, perform


calculations, and display the results on the LCD.

• Ensure the design is user-friendly and the program


logic handles different types of calculations
correctly.

MATERIALS

1. Primary Components:

• AT86C51 Microcontroller

• KEYPAD-CALCULATOR

• LM016L LCD Display

2. Tools/Software:

• Keil µVision for programming and simulation

TOOLS:

Simulation Environment: Proteus Software


Code Development and Compilation: Keil µVision
PHASE 1: CIRCUIT DEPLOYMENT (PROTEUS)

1. Microcontroller Setup
•Place the AT89C51 microcontroller in the simulation
workspace.

Pg. 27

Pg. 40
6. Save the project with an appropriate name.

TargetConfiguration:

• Select the AT89C51 microcontroller as the target device.

• "Target1" will appear in the Projectwindow

Pg. 42
."

• Execute a right-click on "Target1" and select "Optionsfor Target 'Target1'

• Enable HEX file creation in the output setting

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.

PHASE 3: IMPLEMENTATION LOGIC

CODE

#include <reg51.h>
#include <math.h> // Include math library

// LCD Control Pins


sbit LCD_RS = P3^0; //
Register Select sbit
LCD_RW = P3^1; //

Read/Write
sbit LCD_EN = P3^2; // Enable

// Keypad Rows (connected to P0^0

to P0^3) sbit ROW1 = P0^0; // Row

sbit
ROW2
=
P0^1;
// Row
2 sbit
ROW3
=
P0^2;
// Row
3 sbit
ROW4
=
P0^3;
// Row
4

// Keypad Columns (connected to P1^0 to P1^5) sbit


COL1 = P1^0;

// 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

time (in ms) void


delay_ms(unsigned int ms) {
unsigned int i, j;

for (i = 0; i <
ms; i++)
{ for (j =
0; j <
123;
j++);
}
}

// LCD functions

void lcd_command(unsigned
char cmd) { P2 = cmd; //

Send command to LCD


LCD_RS = 0; //
Command mode
LCD_RW = 0; // Write
mode

LCD_EN = 1; // Enable
pulse LCD_EN = 0;

// Disable pulse
delay_ms(1);

void lcd_data(unsigned char


data_val) { P2 = data_val; //
Send data to LCD
LCD_RS = 1; // Data
mode

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);
}

void lcd_display(char *str) { while (*str) {


lcd_data(*str++);

}
}

// Keypad Functions

unsigned char keypad_read() { while (1) {


ROW1 = 0; ROW2 = ROW3 = ROW4 = 1; //
Set row 1 low,
others high

if (!COL1) return 'O'; if (!COL2) return '7'; if


(!COL3) return '8'; if (!COL4)
return '9';
if (!COL5) return '*'; if (!COL6) return '/';
ROW1 = 1; ROW2 = 0; ROW3 = ROW4 = 1;
// Set row 2 low,
others high
if (!COL1) return "+/-"; if (!COL2) return '4';
if
(!COL3) return '5'; if (!COL4) return '6';
if (!COL5) return '-'; if (!COL6) return
"MRC";

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-";

ROW1 = 1; ROW2 = 1; ROW3 = 1; ROW4 = 0; // Set row 4


low, others high

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
}
}

sprintf(result_string, "%.2f", result); // Convert result to


string lcd_command(0x01); // Clear LCD
lcd_display("Result: "); lcd_display(result_string); //
Display result delay_ms(3000);

// Main Program
void main() {

Pg. 35
double first_operand = 0,
second_operand = 0; char

current_operator = 0;
char key_input;

lcd_initialize(); // Initialize LCD


lcd_display("Press ON");

// Wait for the user to press

ON to start while (1) {

key_input =

keypad_read(); if

(key_input
== 'O') {
lcd_command(0x01);
// Clear LCD
delay_ms(1000);
break;
}
}

// Main loop to perform calculations while (1) {


// Read first operand

first_operand = keypad_read() - '0'; //


Convert char to number
lcd_command(0xC0); // Move cursor to
second line lcd_data(first_operand + '0');
// Display first
operand
delay_ms(500);

// Read operator
current_operator = keypad_read();

lcd_command(0xC0); // Move cursor to


second line

Pg. 36
lcd_data(current_operator); // Display operator delay_ms(500);

// Read second operand


second_operand = keypad_read() - '0'; // Convert char to number
lcd_command(0xC0); // Move cursor to second line
lcd_data(second_operand + '0'); // Display second

delay_ms(500);

operand lcd_command(0x01); // Clear LCD lcd_display("Press ="); // Prompt to press


"="

// Wait for the user to press "=" while (1) { key_input

= keypad_read(); if (key_input
== '=') {
break;
}
}

lcd_command(0x01); // Clear LCD

lcd_display("Calculating..."); delay_ms(1000);

perform_calculation(first_operand, second_operand, current_operator); // Perform

lcd_command(0x01); // Clear LCD

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

Fig 3.2 Calculator Circuit Output

Fig 3.3 Calculator Circuit Output

Pg. 38
Fig 3.4 Calculator Circuit Output

Fig 3.5 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:

• Ensure resistors are used to protect LEDs from excess current.


• Test the simulation in Proteus before hardware implementation.
• Confirm the clock frequency and power connections in the Proteus setup.

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:

• Implement a C program to simulate a dice roll using the AT89C51 microcontroller.


• Interface a 7-segment display and a trigger button with the microcontroller.
• Simulate the designed circuit in Proteus software.

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:

• Simulation Environment: Proteus Software


• Code Development and Compilation: Keil µVision

Pg. 3G
PHASE 1: CIRCUIT DEPLOYMENT (PROTEUS)

Microcontroller Setup:

• Place the AT89C51 microcontroller in the Proteus workspace.


• Connect GND and VCC pins to their respective power sources.

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.

Fig 4.0 Dice Simulator Circuit

Pg. 3G
PHASE 2: CODE IDE SETUP (KEIL µVISION)

Project Initialization:

1. Launch Keil µVision and create a new project.


2. Save the project with an appropriate name.

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 for dice simulation.

PHASE 3: IMPLEMENTATION LOGIC

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;

timer_init(); // Initialize and start Timer 0

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:

1. Pin Definitions: o trigger: The push button input connected to


P1.0.
o seg_code[]: Array storing 7-segment codes for digits 1–6.

2. Random Number Generation: o The low byte of Timer 0 (TL0) is used


to generate a random value.
o The modulo operation limits the value to a range of 1–6.

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.

5. Debounce Logic: o A delay ensures the button press is stable before


processing input.

PHASE 4: TESTING IN PROTEUS SIMULATION

1. Load the compiled HEX file into the Proteus simulation.


2. Run the simulation and press the button to observe the dice roll.
3. The displayed number should randomly vary between 1 and 6.

EXAMPLE USE CASES:


• Simulating dice rolls for games.
• Demonstrating random number generation in embedded systems.

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

Fig 4.2 Dice Simulator Output


Pg. 3G
Fig 4.3 Dice Simulator Output

Pg. 3G
Experiment 5 - Home Automation System Using AT89C51 Microcontroller

OBJECTIVES:

• Develop a home automation system using the AT89C51 microcontroller in Proteus.


• Interface sensors, relays, and other components to control home appliances. • Simulate the circuit for automation
in Proteus software.

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

PHASE 1: CIRCUIT DEPLOYMENT (PROTEUS)

Microcontroller Setup:

• Place the AT89C51 microcontroller in the Proteus workspace. • Connect GND and VCC pins to their respective
power sources.

Circuit Design:

1. Temperature-Controlled Fan: o Connect the LM35 temperature sensor to


the ADC0804 for analog-to-digital conversion.
o Use the output from ADC0804 to control the DC fan via the L293D motor driver.
2. Motion-Controlled Lighting: o Interface PIR sensors with the
microcontroller to detect motion.
o Connect LED lights to be activated when motion is detected.
3. Password-Based Door Lock: o Attach the keypad to the microcontroller to
take password input.
o Use a relay driven by ULN2003A to control the lock mechanism.
4. Relay Controls: o Interface relays to control multiple appliances, such
as fans and lights. o Use transistors and diodes for relay protection and
smooth operation.

Pg. 3G
Fig 5.1 Home automation system cirruit

PHASE 2: CODE IDE SETUP (KEIL µVISION)

Project Initialization:

1. Launch Keil µVision and create a new project.


2. Save the project with an appropriate name, such as "HomeAutomationSystem."

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.

Pg. 3G 3: IMPLEMENTATION LOGIC


PHASE
Code:
#include <REGX51.H>

// ADC0804 Control Pins

#define RD P2_0

#define WR P2_1

#define INTR P2_2

// Output Pins

#define MOTOR_CONTROL P3_0 // Motor connected to P3.0

#define LED_CONTROL P3_1 // LED connected to P3.1

// PIR Sensor Input

#define PIR_SENSOR P3_2 // PIR sensor output connected to P3.2

// Function Prototypes

unsigned char ADC_Read(); // Function to read ADC value

void delay(unsigned int ms);

void main() {

unsigned char adc_value;

float temperature;

// Initialize pins

P1 = 0xFF; // ADC Data pins (P1.0 - P1.7) as input

RD = 1; // ADC Read pin high

WR = 1; // ADC Write pin high

INTR = 1; // ADC Interrupt pin high

MOTOR_CONTROL = 0; // Motor OFF initially

LED_CONTROL = 0; // LED OFF initially

while (1) {

// Step 1: Read temperature from ADC0804

adc_value = ADC_Read();

temperature = (adc_value * 5.0 / 255.0) * 100.0; // Convert ADC value to temperature

// Step 2: Motor Control based on temperature

if (temperature > 30.0) {

MOTOR_CONTROL = 1; // Turn ON Motor

Pg. 3G
} else {

MOTOR_CONTROL = 0; // Turn OFF Motor

// Step 3: LED Control based on PIR sensor

if (PIR_SENSOR == 1) {

LED_CONTROL = 1; // Turn ON LED if motion detected

} else { LED_CONTROL = 0; // Turn OFF

LED if no motion

// Function to read ADC value from ADC0804

unsigned char ADC_Read() {

unsigned char adc_value; WR

= 0; // Start ADC conversion

delay(1);

WR = 1;

while (INTR == 1); // Wait for ADC conversion to complete

RD = 0; // Enable ADC output

delay(1);

adc_value = P1; // Read ADC data

RD = 1; // Disable ADC output

return adc_value; }

// Delay function

void delay(unsigned int ms) {

unsigned int i, j;

for (i = 0; i < ms; i++) {

Pg. 3G
for (j = 0; j < 1275; j++);

PHASE 4: TESTING IN PROTEUS SIMULATION

1. Load the compiled HEX file into the Proteus simulation.


2. Test individual components:
o Verify that the fan turns on/off based on temperature changes.
o Test the motion sensors to ensure lights activate upon detecting motion.
o Simulate keypad input for the password-based door lock.
3. Debug any issues using the Proteus oscilloscope or virtual terminal.

OUTPUT:

Fig 5.2 Home automation system output

Pg. 3G
EXAMPLE USE CASES:

• Automating home lighting based on motion.


• Maintaining optimal room temperature with temperature-controlled fans.
• Securing entry points with a password-based door lock system.
• Providing centralized control for multiple appliances.

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.

Experiment 6 - Name Display Program

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:

• Simulation Environment: Proteus Software


• Code Development and Compilation: Keil µVision

PHASE 1: CIRCUIT DEPLOYMENT (PROTEUS)


Microcontroller
Setup:
• Place the AT89C51 microcontroller in the Proteus workspace.
• Connect GND and VCC pins to their respective power sources.

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

PHASE 2: CODE IDE SETUP (KEIL µVISION)

Project Initialization:

1. Launch Keil µVision and create a new project.


2. Save the project with an appropriate name, such as "NameDisplayer."

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 display the name on the LCD.

Pg. 3G
PHASE 3: IMPLEMENTATION LOGIC

Code:
#include <reg51.h>

#define LCD_PORT P2 // Define LCD data port


sbit RS = P3^0; // Register Select pin
sbit RW = P3^1; // Read/Write pin sbit
EN = P3^2; // Enable pin
void delay_ms(unsigned int ms); void
lcd_command(unsigned char command); void
lcd_data(unsigned char character); void
lcd_init(void);

void main(void) {
lcd_init(); // Initialize LCD

// Display "SHOAIB ALI" on LCD


lcd_command(0x80); // Set cursor to the first line
lcd_data('S'); lcd_data('H'); lcd_data('O');
lcd_data('A'); lcd_data('I'); lcd_data('B');
lcd_data(' ');
lcd_data('A');
lcd_data('L');
lcd_data('I');

while (1); // Infinite loop to keep the display static


}
void delay_ms(unsigned int ms) {
unsigned int i, j; for (i =
0; i < ms; i++) {
for (j = 0; j < 1275; j++); // Approx 1 ms delay for 11.0592 MHz clock
}
}
void lcd_command(unsigned char command) {
LCD_PORT = command; // Load command to data port
RS = 0; // Command mode
RW = 0; // Write mode
EN = 1; // Generate a high-to-low pulse
delay_ms(2); // Short delay
EN = 0;
}
void lcd_data(unsigned char character) {
LCD_PORT = character; // Load data to data port
RS = 1; // Data mode
RW = 0; // Write mode
EN = 1; // Generate a high-to-low pulse
delay_ms(20); // Short delay
EN = 0;
}
void lcd_init(void) {
delay_ms(20); // Wait for LCD to power up
lcd_command(0x38); // 8-bit mode, 2 lines, 5x7 matrix
delay_ms(5); // Wait for the command to execute
lcd_command(0x0C); // Display ON, Cursor OFF
delay_ms(5); // Wait for the command to execute
lcd_command(0x01); // Clear display
delay_ms(20); // Wait for the command to execute
lcd_command(0x06); // Increment cursor delay_ms(5);
// Wait for the command to execute

Pg. 3G
lcd_command(0x80); // Force cursor to the first line
}

PHASE 4: TESTING IN PROTEUS SIMULATION

1. Load the compiled HEX file into the Proteus simulation.


2. Power on the circuit and observe the LCD display.
3. Verify that the first line displays "Hello," and the second line displays your name.

OUPUT:

Fig 6.2 Name Display Ouput

EXAMPLE USE CASES:

• Name display in identity systems.


• Basic introductory projects for embedded systems.
• Creating personalized greeting devices.

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)

Tools and Software:


• Simulation Environment: Proteus Software
• Code Development and Compilation: Keil µVision

PHASE 1: CIRCUIT DEPLOYMENT (PROTEUS)

Microcontroller Setup:
1. Place the AT89C51 microcontroller in the Proteus workspace.
2. Connect GND and VCC pins to their respective power sources.

Circuit Design:

1. Logic State Inputs:


• Connect buttons or DIP switches to P0.0–P0.4 to represent logic states for controlling motor directions.

2. Motor Driver Connections:


• Connect the L293D motor driver inputs to the microcontroller as follows:
o Motor 1:
▪ Input 1 (IN1) → P3.0
▪ Input 2 (IN2) → P3.1 o Motor 2:
▪ Input 3 (IN3) → P3.2
▪ Input 4 (IN4) → P3.3
• Connect the L293D output pins to the DC motors.

3. Power Supply for Motors:


• Provide the necessary power supply (VCC and GND) to the L293D and the motors.

Pg. 3G
Fig 7.0 Car Prototype Circuit

PHASE 2: CODE IDE SETUP (KEIL µVISION)

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.

Source File Integration:


1. Create a new C source file under the "Source Group" and write the motor control logic.
2. Save the program and compile it to generate the HEX file.

PHASE 3: IMPLEMENTATION LOGIC

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

void controlMotors(unsigned char state) {


switch (state) {
case 1: // Button 1: Motor 1 rotates CW, Motor 2 rotates CCW
Motor1_Input1 = 1; Motor1_Input2 = 0;
Motor2_Input1 = 0; Motor2_Input2 = 1; break;
case 2: // Button 2: Motor 1 rotates CCW, Motor 2 rotates CW
Motor1_Input1 = 0; Motor1_Input2 = 1;
Motor2_Input1 = 1; Motor2_Input2 = 0; break;
case 3: // Button 3: Both Motors rotate CW
Motor1_Input1 = 1; Motor1_Input2 = 0;
Motor2_Input1 = 1; Motor2_Input2 = 0; break;
case 4: // Button 4: Both Motors rotate CCW
Motor1_Input1 = 0; Motor1_Input2 = 1;
Motor2_Input1 = 0; Motor2_Input2 = 1; break;
case 5: // Button 5: Both Motors OFF
default: // Default case: Stop both motors
Motor1_Input1 = 0; Motor1_Input2 = 0;
Motor2_Input1 = 0; Motor2_Input2 = 0;
break;
}
} void
main() {
unsigned char state = 0; // Variable to hold the active state
while (1) {
// Determine which button is pressed and assign the corresponding
state
if (Button1) state = 1;
else if (Button2) state = 2;
else if (Button3) state = 3;
else if (Button4) state = 4;
else if (Button5) state = 5;
else state = 0;

// Control motors based on the current state

controlMotors(state);
}
}

PHASE 4: TESTING IN PROTEUS SIMULATION


1. Import the compiled HEX file into the Proteus microcontroller.
2. 3G
Pg. Simulate the circuit to test the motor behavior based on button presses.
3. Verify that the motors respond to each logic state as programmed.

OUTPUT:

Fig 7.1 Car Prototype Output

Pg. 3G
Fig 7.2 Car Prototype Output

Fig 7.3 Car Prototype Output

Pg. 3G
Fig 7.4 Car Prototype Output

Fig 7.5 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

EXAMPLE USE CASES:


• Car prototype models for educational purposes.
• Demonstration of microcontroller-controlled motor operations.
• Basic embedded systems projects with motor drivers.

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

You might also like