Microcontrollers Lab BEC456A
Microcontrollers Lab BEC456A
DEPARTMENT OF
ELECTRONICS & COMMUNICATIONENGINEERING
Lab Manual
Microcontrollers
(BEC456A)
IV SEMESTER E&CE
INDEX
Page
Sl. Description
No.
No.
1 Course objectives 2
2 Course outcomes 2
3 ABET Student Outcomes, NBA Programme 3
4 Course Outcomes Mapping with Programme outcomes 5
5 Course Syllabus 6
6 Experiments Beyond the Syllabus 7
1. COURSE OBJECTIVES
This course will enable students to:
1. Understand the basic programming of Microcontrollers.
2. Develop the 8051 Microcontroller-based programs for various applications using
Assembly Language & C Programming.
3. Develop the 8051 Microcontroller-based programs for various applications using C
Programming
4. Program 8051Microcontroller to control an external hardware using suitable I/O ports.
2. COURSE OUTCOMES
Level of Blooms
COs After Completion of the course the student will be able to
Taxonomy
C2.1 Apply different 8051 microcontroller instructions to develop L4
assembly language code for illustrating data transfer
and arithmetic operations using Keil tool.
C266.2 Use different 8051 microcontroller instructions to develop L2
assembly language code for illustrating logical operations
using
C266.3 Design and develop assembly language program for 8051
using different branch control instructions in Keil. L3
WK1: A systematic, theory-based understanding of the natural sciences applicable to the discipline
and awareness of relevant social sciences.
WK2: Conceptually-based mathematics, numerical analysis, data analysis, statistics and formal
aspects of computer and information science to support detailed analysis and modelling applicable to
the discipline.
WK4: Engineering specialist knowledge that provides theoretical frameworks and bodies of
knowledge for the accepted practice areas in the engineering discipline; much is at the forefront of
the discipline.
WK5: Knowledge, including efficient resource use, environmental impacts, whole-life cost, re-use
of resources, net zero carbon, and similar concepts, that supports engineering design and operations
in a practice area.
WK6: Knowledge of engineering practice (technology) in the practice areas in the engineering
discipline.
WK7: Knowledge of the role of engineering in society and identified issues in engineering practice
in the discipline, such as the professional responsibility of an engineer to public safety and sustainable
development.
WK8: Engagement with selected knowledge in the current research literature of the discipline,
awareness of the power of critical thinking and creative approaches to evaluate emerging issues.
WK9: Ethics, inclusive behaviour and conduct. Knowledge of professional ethics, responsibilities,
and norms of engineering practice. Awareness of the need for diversity by reason of ethnicity, gender,
age, physical ability etc. with mutual understanding and respect, and of inclusive attitudes
Reference:https://www.nbaind.org/files/2-SAR-UG-EG-T2-14-1-2025
2. Close any previous projects that were opened using – Project -> Close.
3. Create a new project using Project – New Project – give project name – save.
4. Select the device for target from the device database (Database – Atmel -
AT89C51ED2). Click - OK, Select No when the following option is displayed.
Microcontroller Lab Manual BECL456A
Microcontroller Lab Manual BECL456A
6. Create a source file (using File->New), type the assembly program and save the file
with an extension .asm (filename.asm)
Microcontroller Lab Manual BECL456A
7. Add the source file to the project - right click on the Source Group in the Project
Window and select Add Files to Group option.
8. To select the asm file change the files types to ASM source file and the select the file
10. Any errors in the code are indicated by – “Target not created” in the Build window,
along
with the error line. Debug the errors.
11. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog.
Microcontroller Lab Manual BECL456A
12. Run the program using the Debug-Run command & halt the program using Debug-
Stop Running.
Microcontroller Lab Manual BECL456A
Algorithm:
Step 1: Start
Step 2: Initialize source memory
Step 3: Initialize the destination memory
Step 6: Stop
org 0000h
back: mov a, @r0 ; Get the data from source memory pointer
djnz r2, back ; decrement count and repeat if count not equal to 0
RESULT:
b. Write an Assembly Language Program (ALP) to transfer N bytes of data from one
location [2000H] to another location [2050H].
Algorithm:
Step 1: Start
Step 2: Initialize Count
Step 3: Fetch Source memory location address Step
4: Fetch Destination memory location address
Step 5: Transfer all the data’s from source memory location to destination memory
location byte by byte
Step 6: Stop
org 0000h
mov r0, #05h ; Initialize count
mov r1, #20h ; Initialize source memory location
RESULT:
c. Write an ALP To exchange the source block starting with address 20h, (Internal RAM)
containing N (05) bytes of data with destination block starting with address 40h (Internal
RAM).
Algorithm:
Step 1: Start
Step 2: Initialize Count
Step 3: Fetch address of first memory location
Step 4: Fetch address of second memory location
Step 5: Exchange all the data’s from first memory location to second memory
location byte by byte
Step 6: Stop
org 0000h
mov r0, #20h ; Initialize the source memory address
mov r1, #40h ; Initialize the destination memory address
RESULT:
d. Write an ALP to exchange the source block starting with address 10h (Internal memory),
containing n (06) bytes of data with destination block starting at location 00h (External
memory).
Algorithm:
Step 1: Start
Step 2: Initialize Count
Step 3: Fetch address of internal memory location
org 0000h
mov ro, #10h ; Initialize internal memory location
mov dptr, #2000h ;Initialize external memory location
RESULT:
2.a Write an ALP to add the byte in the RAM at 34h and 35h, store the result in the register
R5 (LSB) and R6 (MSB), using Indirect Addressing Mode.
Algorithm:
Step 1: Start
Step 2: Fetch the address of first data to be added with second
Step 3: Fetch address of second data
RESULT:
2.b) Write an ALP to subtract the bytes in Internal RAM 34h &35h store the result in
register R5 (LSB) & R6 (MSB).
Algorithm:
Step 1: Start
Step 2: Fetch the address of first data
Step 3: Fetch address of second data
2.C) Write an ALP to multiply two 8-bit numbers stored at 30h and 31h and store16- bit
result in 32h and 33h of Internal RAM.
Algorithm:
Step 1: Start
Step 2: Fetch the address of first data
Step 3: Fetch address of second data
2)d) Write an ALP to perform division operation on 8-bit number by 8-bit number.
Algorithm:
Step 1: Start
Step 2: Fetch the address of first data
Step 3: Fetch address of second data
Algorithm:
Step 1: Start
Algorithm:
Step 1: Start
Algorithm:
Step 1: Start
Step 2: Initialize Count
Step 3: Compare the current element with the next element
Step 4: If the current element is greater than the next element, swap them.
Step 5: If the current element is less than the next element, move to the next element.
Note: For descending order change the instruction jnc skip in the program to jc skip
6. Write an ALP to find Largest & Smallest number from a given array starting from 20h &
store it in Internal Memory location 40h.
Algorithm:
Step 1: Start
Step 2: Initialize Count
Step 3: Compare the current element with the next element
Step 4: If the current element is greater than the next element,
then current element is larger
Step 5: If the current element is less than the next element, then the next
element is larger
Note: For smaller number change the instruction jc back3 in the program to jnc back3
Microcontroller Lab Manual BECL456A
3. Counter Programming
a. Write an Assembly Language Program (ALP) to implement decimal up counter
Algorithm:
Step 1: Start
Step 2: Move 00 to A register
Step 3: Call the delay subroutine for 1 second delay
Step 7: Stop
Note: For decimal down counter change the instruction mov a, #00h to mov a, #99h and inc a
to add a, #99h
b. Write an Assembly Language Program (ALP) to implement Hexadecimal up counter
Algorithm:
Step 1: Start
Step 2: Initialize accumulator with 00h
Step 3: Call the delay subroutine for delay
Step 6: Stop
Note: For Hexadecimal down counter change the instruction mov a, #00h to mov a, #0FFh
and inc a to dec a.
Microcontroller Lab Manual BECL456A
II. C Programming
1. Write an 8051 C program to find the sum of first 10 integer numbers.
Algorithm:
Step 3: Within the loop, add each number to the sum variable
Program
#include <reg51.h>
void main()
{
unsigned int i, sum = 0;
ACC = sum;
P0 = sum;
}
Algorithm:
Step 4: Multiply the factorial variable with the current loop counter.
Step 5: After the loop is complete, Check the value of the factorial variable.
Microcontroller Lab Manual BECL456A
Program:
#include <reg51.h>
void main()
{
unsigned int i, num = 5;
unsigned int factorial =1;
ACC = factorial;
P0 = factorial;
}
3. Write a 8051 C program to find the square of a number (1 to 10) using look-up table
Algorithm:
Step 1: Create a look-up table for square values of numbers 1 to 10.
Step 2: Take input number whose square is to be found.
Step 3: Check if the input is between 1 and 10.
Step 4: If the input is valid, retrieve the square value from the look-up table and display it.
Program:
#include <reg51.h>
void main()
{
unsigned char square_lookup_table [] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
unsigned char n = 5;
unsigned int square = square_lookup_table[n];
}
Microcontroller Lab Manual BECL456A
4. Write a 8051 C program to count number of Ones and Zeros in two consecutive
memory locations.
Algorithm:
Step 1: Take a array of two numbers.
Step 2: Right shift the number to check for ones and zeros using carry flag
Step 3: Count the number of ones and zeros
Step 4: Repeat the procedure for second number
Step 5: Check the count of ones and zeros
Program:
#include <reg51.h>
void main()
{
unsigned char array[] = {0x57,0xfc};
unsigned char i, ones, zeros;
CY = 0;
for ( i = 0; i< 8; i++) // To check the count of 1’s and 0’s of first number
{
array [0]>>=1;
if (CY==1) ones++;
else zeros++;
}
for ( i = 0; i< 8; i++) // To check the count of 1’s and 0’s of first number
{
array [1]>>=1;
if (CY==1) ones++;
else zeros++;
}
}
Microcontroller Lab Manual BECL456A
1. Write an 8051 C program to rotate stepper motor in clock & anticlockwise direction
AIM: To write an 8051 C program for interfacing stepper motor with 8051.
THEORY: A Stepper Motor or a step motor is a brushless, synchronous motor which divides a full
rotation into a number of steps. Unlike a brushless DC motor which rotates continuously when a fixed
DC voltage is applied to it, a step motor rotates in discrete step angles. The Stepper Motors therefore
are manufactured with steps per revolution of 12, 24, 72, 144, 180, and 200, resulting in stepping
angles of 30, 15, 5, 2.5, 2, and 1.8 degrees per step. The stepper motor can be controlled with or
without feedback. Stepper motors work on the principle of electromagnetism. There is a soft iron or
magnetic rotor shaft surrounded by the electromagnetic stators. The rotor and stator have poles which
may be teethed or not depending upon the type of stepper. When the stators are energized the rotor
moves to align itself along with the stator (in case of a permanent magnet type stepper) or moves to
have a minimum gap with the stator (in case of a variable reluctance stepper). This way the stators
are energized in a sequence to rotate the stepper motor.
Microcontroller Lab Manual BECL456A
Full step drive (4 steps per revolution) for clockwise rotation is shown below.
Full step drive (4 steps per revolution) for anti-clockwise rotation is shown below.
Microcontroller Lab Manual BECL456A
Program:
# include <reg51.h>
void delay(unsigned int);
sbit sw = P3^1;
void main(void)
{
P2 = 0x00; // configure P2 as output port
While (1) // continue monitor the status of key
{
If(sw ==0) // for clockwise direction
{
P2 = 0x03; // Pattern for full step drive
Delay(100);
P2 = 0x06;
Delay(100);
P2 = 0x0C;
Delay(100);
P2 = 0x09;
Delay(100);
}
Else // for anti-clockwise direction
{
P2 = 0x09;
Delay(100);
P2 = 0x0C;
Delay(100);
P2 = 0x06;
Delay(100);
P2 = 0x03;
Delay(100);
}
}
}
Microcontroller Lab Manual BECL456A
2. Write an 8051 C program to generate sine wave & square waveform using DAC
interface.
AIM: To write an 8051 C program to generate square waveform and sine waveform using DAC.
Program:
delay(250);
P1=0xff;
delay(250);
}
}
void delay (unsigned itime)
{
unsigned int i;
for (i=0; i<=itime; i++);
}
Program:
Add on Program:
Algorithm:
Step 1: Start
Step 2: Initialize Count
Step 3: Fetch address of first memory location
Algorithm:
Step 1: Start
Step 6: Add MSB of first number and second number with previous carry
org 000h
mov a, r1
add a, r3 ; Add lower bytes
mov a, r0
Microcontroller Lab Manual BECL456A
Viva Questions