Fybca Electronics Practical Updated
Fybca Electronics Practical Updated
Choice Based Credit System (CBCS) Syllabus Under National Education Policy
(NEP)
Work Book
Name:
College Name:
Academic Year:
1
F.Y.B.C.A
Semester-II
CERTIFICATE
This is to certify that
Mr./Ms. has successfully
completed the lab book on 8051 Microcontroller Programming CA- 154 -
P Lab course on CA- 153 - T course work and has scored marks
out of 15.
Instructor H.O.D./Coordinator
2
List of Assignments
3
Assignment Completion Sheet
4
Assignment No. 1
Study of 8051 Microcontroller Chip and Keil μVision-5
Aim: Study of 8051 microcontroller chip, Keil μvision-5.
Requirements: System / PC required min. 04 GB RAM / Installed Keil uvision-5 software.
USB Drivers to be installed and Program Burner with Willar Programmer installed.
Objectives:
1. Understand the architecture, features, and pin configuration of the 8051
microcontrollers.
2. Learn to use Keil μVision-5 IDE for writing, compiling, and debugging programs.
3. Write and simulate simple programs in embedded C or assembly for 8051.
4. Familiarize with the process of generating a .HEX file for hardware interfacing.
5. Build a foundation for interfacing peripherals and embedded system applications.
Circuit Diagram:
The microcontroller is a Dual Inline Package (DIP) integrated circuit and to has a total of 40
pins. It has a total of four input/output ports and each port has 8 pins. So, among the 40 pins,
for ports cover the total of 32 pins other pins are used for other functions such as interrupt,
power supply, oscillation, etc.
5
Special Function Register:
A special function register (SFR) is a memory area in a microcontroller that controls or
monitors the microcontroller's functions. SFRs are used to:
• Control timers, counters, serial I/Os, and port I/Os
• Monitor the processor status, such as if it is servicing an interrupt or running in
protected mode
• Monitor condition codes, which are the result of previous comparisons
• Control the stack pointer, which indicates the next data to be accessed
6
Port1 (Pin1 to Pin8): Port1 includes pin1.0 to pin1.7 and these pins can be configured as
input or output pins.
Pin 9 (RST): Reset pin is used to Reset 8051 Microcontroller by giving a positive pulse to
this Pin.
7
Port3 (Pin 10 to 17): The Port3 Pins are like port1 pins and can be used as universal Input
or output pins. These pins dual-function Pins and the function of each Pin is given as:
Pin 10 (RXD): RXD pin is a Serial Asynchronous Communication Input or Serial
synchronous Communication Output.
Pin 11 (TXD): Serial Asynchronous Communication Output or Serial Synchronous
Communication Clock Output.
Pin 12 (INT0): Input of Interrupt 0
Pin 13 (INT1): Input of Interrupt 1
Pin 14 (T0): Input of Counter 0 clock
Pin 15 (T1): Input of Counter 1 clock
Pin 16 (WR): Writing Signal to write content on external RAM.
Pin 17 (RD): Reading Signal to read contents of external RAM.
Pin 18 and 19 (XTAL2, XTAL1): X2 and X1 pins are input output pins for the oscillator.
These pins are used to connect an internal oscillator to the microcontroller.
Pin 20 (GND): Pin 20 is a ground pin.
Port2 (Pin 21 to Pin28): Port2 includes pin21 to pin28 which can be configured as Input
Output Pins. But this is only possible when we don’t use any external memory. If we use
external memory, then these pins will work as high order address bus (A8 to A15).
Pin 29 (PSEN): This pin is used to enable external program memory. If we use an external
ROM for storing the program, then logic 0 appears on it, which indicates Micro controller to
read data from the memory.
Pin 30 (ALE): Address Latch Enable pin is an active high-output signal. If we use multiple
memory chips, then this pin is used to distinguish between them. This Pin also gives program
pulse input during programming of EPROM.
Pin 31 (EA): If we have to use multiple memories then the application of logic 1 to this pin
instructs the Microcontroller to read data from both memories: first internal and then external.
Port 0 (Pin 32 to 39): Similar to the port 2 and 3 pins, these pins can be used as input output
pins when we don’t use any external memory. When ALE or Pin 30 is at 1, then this port is
used as data bus: when the ALE pin is at 0, then this port is used as a lower order address bus
(A0 to A7)
Pin40 (VCC): This VCC pin is used for power supply.
8
Types of instructions:
Depending on operation they perform, all instructions are divided in several groups:
➢ Arithmetic Instructions
➢ Branch Instructions
➢ Data Transfer Instructions
➢ Logic Instructions
➢ Bit-oriented Instructions
Arithmetic instructions:
Arithmetic instructions perform several basic operations such as addition, subtraction, division,
multiplication etc. After execution, the result is stored in the first operand. For example:
ADD A, R1 - The result of addition (A+R1) will be stored in the accumulator
. Mnemonic Description
ADD A,Rn Adds the register to the accumulator
ADD A,direct Adds the direct byte to the accumulator
ADD A,@Ri Adds the indirect RAM to the
accumulator
ADD A,#data Adds the immediate data to the
accumulator
ADDC A,Rn Adds the register to the accumulator with
a carry flag
ADDC A,direct Adds the direct byte to the accumulator
with a carry flag
ADDC A,@Ri Adds the indirect RAM to the
accumulator with a carry flag
9
Branch Instructions:
There are two kinds of branch instructions:
Unconditional jump instructions: upon their execution a jump to a new location from
where the program continues execution is executed.
Conditional jump instructions: a jump to a new program location is executed only if a
specified condition is met. Otherwise, the program normally proceeds with the next
instruction.
Mnemonic Description
ACALL addr11 Absolute subroutine call
LCALL addr16 Long subroutine call
RET Returns from subroutine
RETI Returns from interrupt subroutine
AJMP addr11 Absolute jump
LJMP addr16 Long jump
SJMP rel Short jump (from –128 to +127 locations relative to the
following instruction)
JC rel Jump if carry flag is set. Short jump.
JNC rel Jump if carry flag is not set. Short jump.
JB bit,rel Jump if direct bit is set. Short jump.
JBC bit,rel Jump if direct bit is set and clears bit. Short jump.
JMP @A+DPTR Jump indirect relative to the DPTR
JZ rel Jump if the accumulator is zero. Short jump.
JNZ rel Jump if the accumulator is not zero. Short jump.
CJNE A,direct,rel Compares direct byte to the accumulator and jumps if not
equal. Short jump.
CJNE A,#data,rel Compares immediate data to the accumulator and jumps
if not equal. Short jump.
CJNE Rn,#data,rel Compares immediate data to the register and jumps if not
Data transfer instructions move the content of one register to another. The register the content of
which is moved remains unchanged. If they have the suffix “X” (MOVX), the data is exchanged with
external memory.
Mnemonic Description
10
MOV A,#data Moves the immediate data to the accumulator
MOVC A,@A+PC Moves the code byte relative to the PC to the accumulator
(address=A+PC)
MOVX A,@Ri Moves the external RAM (8-bit address) to the accumulator
MOVX @DPTR,A Moves the accumulator to the external RAM (16-bit address)
11
XCH A,@Ri Exchanges the indirect RAM with the accumulator
XCHD A,@Ri Exchanges the low-order nibble indirect RAM with the
accumulator
Logic Instructions:
Logic instructions perform logic operations upon corresponding bits of two registers. After execution,
the result is stored in the first operand.
Mnemonic Description
12
SWAP A Swaps nibbles within the accumulator
Bit-oriented Instructions
Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that
these are performed upon single bits.
Mnemonic Description
The way of specifying the address of the operand is called as addressing mode. The 8051
microcontroller is having four addressing modes for accessing data.
14
2. Register Addressing mode:
Register addressing mode involves the use of registers to hold the data to be manipulated. In
this addressing mode register which is having the data is part of the instruction.
MOV A, R0 ;copy the contents of RO into A
MOV R2,A
; copy the contents of A into R2
ADD A, R5
ADD A, R7 ; add the contents of R5 to contents
MOV R6, A of A
Applications
• Home appliances (e.g., washing machines).
• Robotics and automation.
15 | P a g e
• Automotive systems.
• Industrial control systems.
Keil – 5 Software:
16 | P a g e
Next step is to run setup file C51V954A.EXE and then we’ll get pop-up box, hit on Next and Proceed
Installation.
Read license agreement, check I agree to all the terms…., and click Next
Select Destination folder where you want to install Keil, or default destination is already there. And
hit on Next. Wait for installation completes and hit on Next. Tick on show release notes, deselect
remaining (as per your choice) and click on Finish.
17 | P a g e
The Arm® Keil Microcontroller Tools include C/C++ compilers, integrated
development environments, RTOS, middleware, as well as debug adapters and
evaluation boards for Arm Cortex®-M based devices
Embedded C is the most popular programming language in the software field for
developing electronic gadgets.
Features of Keil uvision-5:
Cross-platform compiler:
Supports multiple microcontroller architectures, mainly focusing on ARM Cortex-
M series, allowing you to develop code for various devices using the same IDE
Integrated development environment (IDE):
Provides a user-friendly interface with code editor, project management tools, build
system, and debugger all within one window
Real-time operating system (RTOS):
Keil RTX, a lightweight RTOS, can be integrated into projects for managing real-
time tasks with deterministic behavior.
Advanced debugging:
Includes features like real-time variable monitoring, breakpoint setting, single-step
execution, and hardware breakpoint support for in-depth code analysis
How to Execute Assembly Language Programs in Keil Micro-vision?
Keil Microvision software is an open-source software, or an IDE used for executing assembly
language programs and embedded C programs. Let’s see the steps to execute the assembly
language program in Keil Microvision software.
Executing assembly language programs in Keil Microvision Software
Follow the following steps to execute assembly language programs in Keil Microvision software:
18 | P a g e
Step 1: Open Keil Microvision software then click on the project then click on the new
Microvision project.
Step 2: Save your file with .asm extension for example (addition of numbers.asm) and save it.
19 | P a g e
Step 4: Now click on target 1 and then double click on source group 1, select the file
that you saved previously with extension .asm add it to the source group, and close
it.
Step 5: Now click on the + sign shown before source group 1, then double click on
the file that comes after clicking on +sign. Then write assembly language code in
the plane text area that appears.
20 | P a g e
Step 6: Click on build and rebuild to check errors then click on debug and start
debug session.
Step 7: Then click on run you can run directly, or you can run step by step by clicking
every time on the step or pressing F11 and then check the output here the output is
visible in blue color.
By following the above-mentioned steps, you can execute any type of assembly language
statements or instructions and assembler directives in Keil Microvision software.
Assignment Evaluation
Signature of Instructor
21 | P a g e
Assignment No. 3
Program to find Largest/smallest from a series.
Aim: To write an assembly language program for the 8051 microcontroller to find the
largest and smallest numbers from a given series of data.
Objectives:
1. Learn to handle arrays in assembly language.
2. Implement comparison logic to find the largest or smallest number.
3. Use 8051 registers (Accumulator and general-purpose registers).
4. Practice 8051 assembly syntax and instructions.
5. Test and verify the program through simulation.
22 | P a g e
Algorithm:
1. Load the 56H location with 00H which is considered as the largest number
2. Initialize a counter based on no. of numbers given
3. Move the first number into the accumulator
4. Compare the content of accumulator with 00H in 56H
5. If there is no carry, which means (A) is the largest so, move the content of accumulator to the
56H
6. If there is a carry leave the content of accumulator as it is.
7. Repeat the loop until all the numbers are compared and the counter value becomes zero
8. The largest number will be in the 56H location.
Code 1: To find out the Largest Number from the given Array: -
Output:
Check the Output in Register Window in Keil Vision-5.
23 | P a g e
Code 2: To find the smallest number of the given set of numbers:
Algorithm:
1. Load the 70H location with 0FFH which is considered as the smallest no. initially
2. Initialize a counter based on no. of numbers given
3. Move the first number into the accumulator
4. Compare the content of accumulator with 0FFH in 2H
5. If there is carry, which means (A) is the smallest so, move the content of accumulator to the 70H
6. If there is no carry leave the content of accumulator as it is.
7. Repeat the loop until all the numbers are compared and the counter value becomes zero
8. The smallest number will be in the 2H location.
Program:
24 | P a g e
OUTPUT:
25 | P a g e
Assignment No. 4
Program to Perform Addition, Subtraction, Multiplication, and
Division of 8/16-Bit Data
Aim: To perform arithmetic, logical, and code conversion operations using the 8051
microcontroller in Assembly language and simulate the program in Keil μVision.
Objectives
1. To implement basic arithmetic and logical operations.
2. To perform code conversions (binary, BCD, hexadecimal).
3. To write and simulate programs in Keil μVision.
26 | P a g e
Program to perform Addition / subtraction / /multiplication/division
of 8/16 bit data
1. ADDITION OF TWO 8-BIT NUMBERS
AIM: To Perform addition of two 8-bit numbers with 8051 microcontroller and Keil uvision
Software
Experiment Required: Keil uvision Software
ALGORITHM:
1. Load two RAM locations with two operands
2. Move the 1st operand to the accumulator
3. Add the accumulator content with the next location
4. Check the carry flag
5. If the carry is set indicate it by moving 1 in a location
6. Move the result to a RAM location
Program:
27 | P a g e
Output:
ALGORITHM:
1. Load the two 8-bit operands into the locations 40H and 41H
2. Load the larger into the accumulator
3. Subtract the content of accumulator with the smaller number in 40H location.
4. Store the result in 60H location
5. To subtract from smaller to larger number, load the smaller number into the accumulator
6. Subtract the larger number from the smaller number.
7. Now doing this carry flag will set so take the 2’s complement of the result
8. Store the result in 62H location
9. Load the R0 with 1 to indicate negative sign
28 | P a g e
Program:
Output:
29 | P a g e
3. MULTIPLICATION OF TWO 8-BIT NUMBERS
ALGORITHM:
1. Load the accumulator with an immediate data
2. Load ‘B’ register with an immediate data
3. Multiply (A)*(B) using “MUL AB “instruction
4. The lower byte is in accumulator
5. The higher byte is in the ‘B’ register
6. Move the result to the appropriate location.
Program:
Output:
30 | P a g e
4. DIVISION OF TWO 8-BIT NUMBERS
Algorithm:
1. Get the Dividend in the accumulator
2. Get the Divisor in the Register B
3. Divide A by B
4. Store the Quotient and Remainder in the memory.
Program:
OUTPUT:
31 | P a g e
Assignment 5
Aim: Program to perform Arithmetic, logical & code conversion problems
Aim
To perform arithmetic, logical, and code conversion operations using the 8051 microcontroller
in Assembly language and simulate the program in Keil μVision.
Objectives
1. To implement basic arithmetic and logical operations.
2. To perform code conversions (binary, BCD, hexadecimal).
3. To write and simulate programs in Keil μVision.
Experiment Required: Keil µvision software
Block Diagram:
32 | P a g e
1. Addition:
Algorithm:
1. Load the value 10H (16 in decimal) into register A.
2. Load the value 08H (8 in decimal) into register B.
3. Add the values in registers A and B using the ADD instruction.
4. The result of the addition is stored in register A.
5. Move the result from register A into register R1.
6. End the Program:
2. Subtraction:
Alogirithm :
33 | P a g e
3. Division:
Algorithm:
1. Load the value 25H (37 in decimal) into the accumulator A.
2. Load the value 10H (16 in decimal) into register B.
3. Use the DIV AB instruction, which divides the value in register A (dividend)
by the value in register B (divisor).
4. The quotient will be stored in register A.
5. The remainder will be stored in register B.
6. Move the quotient (stored in A) to register R3.
7. Store the Remainder:
8. Move the remainder (stored in B) to register R4.
Program:
34 | P a g e
4. Multiplication:
Algorithm:
1. Load the value 25H (37 in decimal) into the accumulator A.
2. Load the value 25H (37 in decimal) into register B.
3. Use the MUL AB instruction, which multiplies the value in register A by the
value in register B.
4. The lower byte of the result will be stored in register A.
5. The higher byte of the result will be stored in register B.
6. Move the lower byte of the result (stored in register A) to register R3.
7. Move the higher byte of the result (stored in register B) to register R4.
Program:
35 | P a g e
5. Write a Program to Convert Hexadecimal number into Decimal Number
Algorithm:
36 | P a g e
6. Write a Program to Convert Decimal number into Hexadecimal Number
Algorithm:
• Program to write Decimal to Hexadecimal.
37 | P a g e
Assignment 6
Program to perform data transfer/exchange between specified
memory’s locations.
Aim: To implement data transfer and exchange between specified memory locations using the
8051 microcontroller in Assembly language and simulate the program in Keil μVision.
Objectives
1. To learn data transfer techniques between different memory locations in the 8051.
2. To write Assembly programs for memory data exchange.
3. To simulate and debug the program using Keil μVision.
1) To perform the exchange of the given data starting from the locations 30H
to 39H to 40H to 49H.
EQUIPMENT REQUIRED: Keil µvision software
BLOCK DIAGRAM:
38 | P a g e
39 | P a g e
ALGORITHM:
1. Load R0 with 30H to acts as source pointer
2. Load R1 with 40H to act as a destination pointer
3. Initialize the counter with by moving the appropriate value into register (R2 =10)
4. Load the accumulator with the content pointed by R0
5. Exchange the content of accumulator to the destination location pointed by R1
6. Move the content of accumulator into the location pointed by R0.
7. Repeat the loop until the counter value becomes zero.
Program:
OUTPUT:
40 | P a g e
2) To perform the block transfer of the given data starting from the locations 30H to
39H to 40H to 49H.
ALGORITHM:
1. Load R0 with 30H to acts as source pointer
2. Load R1 with 40H to act as a destination pointer
3. Load the accumulator with the content pointed by R0
4. Move the content of accumulator to the destination location pointed by R1
5. Repeat the loop until the counter value becomes zero.
Program:
41 | P a g e
Assignment 7
Aim: To interface LEDs with the 8051 microcontroller and control on-off action of an
using Embedded C programming .
Objectives
1. To write a simple program in Embedded C to turn an LED on and off using
the 8051 microcontroller.
2. To control the LED through a microcontroller's I/O port.
3. To demonstrate basic digital output control with the 8051.
4. To test the functionality using simulation software (Keil μVision) and physical
hardware (circuit board).
42 | P a g e
Algorithm:
Step 1: Initialize the Microcontroller
1. Start the program.
2. Define an LED output pin (e.g., P1.0).
3. Set the LED pin as an output.
Step 2: Enter an Infinite Loop for Blinking
4. Enter an infinite loop (while (1)).
Step 3: Toggle LED State with a Delay
5. Turn ON the LED (set P1.0 = 1).
6. Call a delay function (to keep LED ON for some time).
7. Turn OFF the LED (set P1.0 = 0).
8. Call the delay function again (to keep LED OFF for some time).
Step 4: Repeat the Process
9. Go back to Step 5 and repeat the process indefinitely.
Program: Program in Embedded C
1. Blinking a Single LED
43 | P a g e
1. Blinking Multiple LEDs
In 8051, 1 machine cycle requires 12 crystal pulses to execute and we have use 11.0592Mhz
crystal.
So, time required for 1 machine cycle: 12/11.0592 = 1.085us
So, 1275*1.085=1.3ms, 1275 times of “for” loop gives nearly 1ms of delay.
The exact time delay produce by “C” program is very difficult to compute, when measuring from
oscilloscope (CRO), for (j=0; j<1275; j++) give delay of nearly 1ms.
44 | P a g e
Assignment No. 8
Interfacing of Switch & LED to 8051 Microcontroller
Circuit Diagram:
45 | P a g e
Circuit Diagram:
Algorithm:
• Start
• Initialize Ports:
46 | P a g e
o Turn OFF the LED (P1.1 = 0).
Program:
47 | P a g e
• When microcontroller pin is made high (in code). Microcontroller pin becomes high and
current will find no way to flow and led switches off.
You can also change the polarity of the circuit by connecting led anode to microcontroller and
cathode to external ground. In that configuration microcontroller will supply current to glow led.
Which is not good. There is a very big chance of microcontroller to reset due to power shortage.
Hence the below led microcontroller configuration is recommended to utilize in circuits involving
led and microcontroller. In below configuration external source will supply current to led.
48 | P a g e
Assignment No. 9
Waveform generation using DAC Interface to 8051 Microcontroller.
Aim: Waveform Generation Using DAC Interface to 8051 Microcontroller
Objective To generate analog waveforms (e.g., sine wave, triangular wave, square wave) using
the 8051 microcontroller and a Digital-to-Analog Converter (DAC).
Theory
1. Digital-to-Analog Converter (DAC): o Converts digital signals from the microcontroller into
analog signals. o Common DACs, like the DAC0808, accept an 8-bit digital input and provide an
equivalent analog output.
2. Waveform Generation:
o Square Wave: Alternating between high and low digital values at regular intervals.
o Triangular Wave: Gradually increasing and decreasing digital values to form a triangular shape.
o Sine Wave: Approximated by sending precomputed sine values from a lookup table to the DAC.
2. 8051-DAC Interface: o DAC data pins are connected to an 8-bit port
Digital-to-analog conversion can degrade a signal, so conversion details are normally chosen so
that the errors are negligible.
Due to cost and the need for matched components DACs are almost exclusively manufactured on
integrated circuits (ICs).
The suitability of a particular DAC for an application is determined by a variety of measurements
including speed and resolution.
Microcontroller are used in wide variety of applications like for measuring and control of physical
quantity like temperature, pressure, speed, distance, etc.
In these systems microcontroller generates output which is in digital form, but the controlling
system requires analog signal as they don't accept digital data thus making it necessary to use DAC
which converts digital data into equivalent analog voltage.
49 | P a g e
Circuit Diagram – IC – 0808 (DAC)
50 | P a g e
❑ In this example we will consider 30° increments and calculate the values from
degree to DAC input. We are assuming full-scale voltage of 10V for DAC output.
We can follow this formula to get the voltage ranges.
• Repeat the loop infinitely to continuously generate the sine wave pattern.
• End.
51 | P a g e
Program:
Waveform:
• Initialize j = 0.
• Increment j until it reaches 255.
52 | P a g e
• Assign j to P0, sending an increasing pattern to Port 0.
• Initialize j = 255.
• Decrement j by 2 in each iteration until j > 0.
• Assign j to P0, sending a decreasing pattern to Port 0.
Program:
53 | P a g e
Triangular waveform:
• Initialize i = 0.
• Increment i until it reaches 100.
• This loop acts as a simple time delay.
54 | P a g e
Square Waveform:
55 | P a g e
Program:
Sawtooth Waveform:
56 | P a g e
Assignment No. 10
Interfacing LCD to 8051 Microcontroller
Objective:
To interface a 16x2 LCD with the 8051 microcontroller in 8-bit mode and display a message.
LCD 16 X 2 Display:
57 | P a g e
To initialize LCD to the 8051 the following instruction and commands are
to be embed in to the functions
• 0x38 is used for 8-bit data initialization.
• 0xoC for making LCD display on and cursor off.
• 0X01 for clearing the display of the LCD.
• 0x80 for positioning the cursor at first line .
Sending Commands to the LCD
• E=1; enable pin should be high
• RS=0; Register select should be low for sending commands
• Placing the data on the data registers
• R/W=0; Read/Write pin should be low for writing the data.
Writing the Data to the LCD
• E=1; enable pin should be high
• RS=1; Register select should be high for writing data
• Placing the data on the data registers
• R/W=0; Read/Write pin should be low for writing the data.
58 | P a g e
LCD HEX Commands :
There are some preset command instructions in LCD, we have used them in our
program below to prepare the LCD (in lcd_init() function). Some important
command instructions are given below:
59 | P a g e
Circuit Diagram :
Algorithm:
1. Start
2. Include the header file reg51.h.
3. Define display_port as P2 for LCD data pins.
4. Define control pins (RS, RW, E) connected to P3.2, P3.3, and P3.4.
5. Create a function msdelay(time) to generate a time delay.
6. Create a function lcd_cmd(command) to send commands to the LCD.
7. Create a function lcd_data(disp_data) to send data to the LCD.
8. Create a function lcd_init() to initialize the LCD with required settings:
9. Use lcd_cmd() to set LCD mode, turn display ON, clear screen, and set cursor
position.
10. Define main () function:
11. Declare a string a[] = "FYBCA_Practical".
12. Call lcd_init() to initialize the LCD.
60 | P a g e
13. Use a while loop to send each character of the string to the LCD until the null
terminator (\0) is encountered.
14. Add a small delay after displaying each character.
15. End.
Program:
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
sbit rs = P3^2; //RS pin connected to pin 2 of port 3
sbit rw = P3^3; // RW pin connected to pin 3 of port 3
sbit e = P3^4; //E pin connected to pin 4 of port 3
void msdelay(unsigned int time) // Function for creating delay in milliseconds.
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command) //Function to send command instr to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}
void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}
61 | P a g e
void lcd_init() //Function to prepare the LCD and get it ready
{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen
msdelay(10);
lcd_cmd(0x81); // bring cursor to position 1 of line 1
msdelay(10);
}
void main()
{
unsigned char a[15]="FYBCA_Practical"; //string of 14 characters with a null
terminator.
int l=0;
lcd_init();
while(a[l] != '\0') // searching the null terminator in the sentence
{
lcd_data(a[l]);
l++;
msdelay(50);
}}
62 | P a g e
Assignment: 11
Stepper motor interfacing to 8051 microcontrollers.
Objective
To interface a Stepper Motor with the 8051 microcontroller and control its rotation direction
(clockwise and counterclockwise) and steps.
Stepper Motor:
A stepper motor works on the principle of magnetic attraction and repulsion. The coils of stators
are placed on electromagnets which are energized and de-energized by the pulses of the
microcontroller which creates magnetic north and south on the stator poles.
63 | P a g e
Steps A B C D HEX
1 1 0 0 0 0x08
2 0 1 0 0 0x04
3 0 0 1 0 0x02
4 0 0 0 1 0x01
Steps A B C D HEX
1 1 1 0 0 0x0C
2 0 1 1 0 0x06
3 0 0 1 1 0x03
4 1 0 0 1 0x09
64 | P a g e
Steps A B C D HEX
1 1 0 0 0 0x08
2 1 1 0 0 0x0C
3 0 1 0 0 0x04
4 0 1 1 0 0x06
5 0 0 1 0 0x02
6 0 0 1 1 0x03
7 0 0 0 1 0x01
IC L293D:
65 | P a g e
Circuit Diagram:
Algorithm:
1. Start
2. Include the header file reg52.h.
3. Define motor phase values (phase_a, phase_b, phase_c, phase_d) corresponding to
P2.
4. Define EN (Enable pin) at P0.0 as active high.
5. Create a delay(cnt) function to introduce a small delay.
6. Create a clockwise() function to drive the stepper motor in a clockwise direction:
7. Set P2 to phase_a, delay.
8. Set P2 to phase_b, delay.
9. Set P2 to phase_c, delay.
10. Set P2 to phase_d, delay.
11. Define main() function:
12. Set EN = 1 to enable the motor.
13. Use an infinite while(1) loop to continuously call clockwise() function.
14. End.
66 | P a g e
Program:
Write a Program for Anticlockwise direction:
#include<reg52.h>
void clockwise(void);
void clockwise(void)
{
P2=phase_a;
delay(100);
P2=phase_b;
delay(100);
P2=phase_c;
delay(100);
P2=phase_d;
delay(100);
}
void main(void)
{
EN=1;
while(1)
{
clockwise();
}
}
67 | P a g e
Write a Program for clockwise direction:
#include<reg52.h>
#define phase_d 0x09
#define phase_c 0x05
#define phase_b 0x06
#define phase_a 0x0a
sbit EN=P0^0;
void delay(int cnt);
void delay(int cnt)
{
int i,j;
for(i=0;i<cnt;i++)
for(j=0;j<00;j++);
}
void clockwise(void);
void clockwise(void)
{
P2=phase_d;
delay(100);
P2=phase_c;
delay(100);
P2=phase_b;
delay(100);
P2=phase_a ;
delay(100);
}
void main(void)
68 | P a g e
{
EN=1;
while(1)
{
clockwise();
}
}
69 | P a g e