0% found this document useful (0 votes)
46 views

Microcontrollers Lab BEC456A

The Microcontrollers Lab Manual for the 2024-25 academic year at Aditya College of Engineering and Technology outlines the course objectives and outcomes for the Microcontrollers course (BEC456A) in the IV semester of Electronics and Communication Engineering. It includes detailed instructions for programming the 8051 Microcontroller using Assembly Language and C, along with various experiments and algorithms for data transfer, arithmetic, and logical operations. The manual also emphasizes the importance of theoretical knowledge, engineering ethics, and practical skills in the field.

Uploaded by

Manjunath M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Microcontrollers Lab BEC456A

The Microcontrollers Lab Manual for the 2024-25 academic year at Aditya College of Engineering and Technology outlines the course objectives and outcomes for the Microcontrollers course (BEC456A) in the IV semester of Electronics and Communication Engineering. It includes detailed instructions for programming the 8051 Microcontroller using Assembly Language and C, along with various experiments and algorithms for data transfer, arithmetic, and logical operations. The manual also emphasizes the importance of theoretical knowledge, engineering ethics, and practical skills in the field.

Uploaded by

Manjunath M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Microcontrollers LAB MANUAL 2024-25

ADITYA COLLEGE OF ENGINEERING AND TECHNOLOGY


No 2/9, Kamakshipura, Hesaraghatta Hobli, Yelahanka, Bengaluru- 560089
Affiliated to VTU, Belagavi | Approved by UGC/AICTE/Govt. of Karnataka, Bengaluru

DEPARTMENT OF
ELECTRONICS & COMMUNICATIONENGINEERING

Lab Manual
Microcontrollers
(BEC456A)
IV SEMESTER E&CE

Dept of ECE,ACET Page 1


Microcontrollers LAB MANUAL 2024-25

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

C266.4 Apply Embedded C concepts to develop code for 8051


L4
Microcontroller using tool Keil tool.
C266.5 Design and develop code for interfacing different modules like
stepper motor and DAC with 8051 using embedded C with L3
Keil.

Dept of ECE,ACET Page 2


Microcontrollers LAB MANUAL 2024-25

Knowledge and Attitude Profile (WK)

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.

WK3: A systematic, theory-based formulation of engineering fundamentals required in the


engineering 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

Dept of ECE,ACET Page 5


Microcontrollers Lab Manual BECL456A
BECL456A
BECL456A
Microcontroller Lab Manual BECL456A

EXECUTION STEPS using KEIL µ vision

1. Double Click on the Keil µVision3 icon on the desktop.

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

5. Select No when the following option is displayed.

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.

Dept of ECE, ACET, Bengaluru


Microcontroller Lab Manual BECL456A

8. To select the asm file change the files types to ASM source file and the select the file

Dept of ECE, ACET,Bengaluru


Microcontroller Lab Manual BECL456A

9. Build the project; using Project -> Build Project.

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

Program - 1: Data Transfer Programming

a. Write an Assembly Language Program (ALP) to transfer N bytes of data from

source(20h) to destination (40h) using internal RAM

Algorithm:
Step 1: Start
Step 2: Initialize source memory
Step 3: Initialize the destination memory

Step 4: Initialize Iteration counter


Step 5: Transfer all the data’s from source location to destination
location byte by byte

Step 6: Stop

Assembly Language Program (ALP):

org 0000h

mov r0, #20h ; Initialize the source memory pointer

mov r1, #40h ; Initialize the destination memory pointer

mov r2, #05h ; Initialize iteration counter

back: mov a, @r0 ; Get the data from source memory pointer

mov @r1, a ; store the data into destination memory pointer

inc r0 ; increment the source memory pointer

inc r1 ; increment the destination memory pointer

djnz r2, back ; decrement count and repeat if count not equal to 0

end ; Terminate the program


Microcontroller Lab Manual BECL456A

RESULT:

BEFORE EXECUTION : AFTER EXECUTION :


Microcontroller Lab Manual BECL456A

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

Assembly Language Program (ALP):

org 0000h
mov r0, #05h ; Initialize count
mov r1, #20h ; Initialize source memory location

mov r2, #00h


mov r3, #50h ; Initialize destination memory location
mov dph, r1
again: mov dpl, r2 ; Fetch the source address
movx a, @dptr ; Copy the source data
mov dpl, r3 ; Fetch the destination address

movx @dptr, a ; Store the source data


inc r2 ; Increment source address
inc r3 ; Increment destination address
djnz r0, again ; Decrement count and repeat if count not equal to 0

end ; Terminate the program


Microcontroller Lab Manual BECL456A

RESULT:

Before execution After Execution


Microcontroller Lab Manual BECL456A

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

Assembly Language Program (ALP):

org 0000h
mov r0, #20h ; Initialize the source memory address
mov r1, #40h ; Initialize the destination memory address

mov r2, #05h ; Initialize iteration counter


back: mov a, @r0 ; Get the data from source memory pointer and load into
accumulator
xch a, @r1 ; Exchange data between accumulator and destination
memory address
mov @r0, a ; Store the data into source memory address
inc r0 ; Increment the source memory address
inc r1 ; Increment the destination memory address
djnz r2, back ; Decrement count and repeat if count not equal to 0
end ; Terminate the program
Microcontroller Lab Manual BECL456A

RESULT:

Before execution After Execution


Microcontroller Lab Manual BECL456A

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

Step 4: Fetch address of external memory location


Step 5: Exchange all the data’s from internal memory location to external memory
location & vice versa byte by byte
Step 6: Stop

Assembly Language Program (ALP):

org 0000h
mov ro, #10h ; Initialize internal memory location
mov dptr, #2000h ;Initialize external memory location

mov r2, #06h ; Initialize count


Back: movx a, @dptr
mov b, @ro
xch a,b ;Exchange the data between two memory locations
movx @dptr, a ; Store the data in external memory location
mov @ro,b ; Store the data in internal memory location
inc ro ; Increment internal memory address
inc dptr ; Increment external memory address
djnz r2, Back ; Decrement count and repeat if count not equal to 0
end ;Terminate the program
Microcontroller Lab Manual BECL456A

RESULT:

Before execution After Execution


Microcontroller Lab Manual BECL456A

2. Arithmetic & Logical Operation Programs:

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

Step 4: Add first data with second data


Step 5: Store sum in register r5 and location 36h and carry in register r6 & location
37h
Step 6: Stop

Assembly Language Program (ALP):

org 0000h ; Defining program to start from location 0000h


mov ro, #34h ; Initialize ro with first data location
mov a,@ro ; Transfer first data to accumulator
inc ro ; Increment ro by 1 to load second data location
mov b, @ro ; Transfer second data to register b
add a,b ; Perform addition of two 8-bit datas
inc ro ; Increment ro by 1 to store sum(LSB)
mov@ro,a ; Transfer sum to 36h
mov r5,a ; Transfer sum to register r5
jnc Exit ; If no carry jump to exit
mov r6, #01 ; If carry load register r6 with 01
inc ro ; Increment ro by 1 to store carry(MSB)
mov a,r6
mov@r0,a ; Transfer carry to 37h
Exit: sjmp Exit
end ; Terminate the program
Microcontroller Lab Manual BECL456A

RESULT:

Before execution After Execution


Microcontroller Lab Manual BECL456A

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

Step 4: Subtract second data from first data


Step 5: Store remainder in register r5 and location 36h and borrow in register r6 &
location 37h
Step 6: Stop

Assembly Language Program (ALP):

org 0000h ; Defining program to start from location 0000h


mov ro, #34h ; Initialize ro with first data location
mov r6, #00h ; Clear register r6 to store borrow
mov a,@ro ; Transfer first data to accumulator
inc ro ; Increment ro by 1 to load second data location
mov b, @ro ; Transfer second data to register b
subb a,b ; Perform subtraction of two 8-bit data’s
inc ro ; Increment ro by 1 to store remainder(LSB)
mov@ro,a ; Transfer remainder to 36h
mov r5,a ; Transfer remainder to register r5
jnc Exit ; If no carry jump to exit
dec r6
inc ro ; Increment ro by 1 to store borrow(MSB)
mov r6, a ; Transfer borrow(ff) to r6
mov @ro, #0xff ; Transfer ff h to 37h
Exit: sjmp Exit
end ; Terminate the program
Microcontroller Lab Manual BECL456A

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

Step 4: Multiply first data with second data


Step 5: Store LSB of the product to location 32h and MSB to location 33h
Step 6: Stop

Assembly Language Program (ALP):

org 0000h ; Defining program to start from location 0000h


mov r0, #30h ; Initialize ro with first data location
mov a,@ro ; Transfer first data to accumulator
inc r0 ; Increment ro by 1 to load second data location
mov b, @r0 ; Transfer second data to register b
mul ab ; Perform multiplication of two 8-bit data’s to get 16 bit product
inc r0 ; Increment ro by 1 to store LSB of the product
mov@r0,a ; Transfer LSB of the product to 32h
inc r0 ; Increment ro by 1 to store LSB of the product
mov@r0,b ; Transfer MSB of the product to 33h
end ; Terminate the program

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

Step 4: Divide first data by second data


Step 5: Store quotient in location 32h and remainder in location 33h
Step 6: Stop
Microcontroller Lab Manual BECL456A

Assembly Language Program (ALP):

org 0000h ; Defining program to start from location 0000h


mov r0, #30h ; Initialize r0 with dividend data location
mov a,@ro ; Transfer dividend to accumulator
inc r0 ; Increment r0 by 1 to load divisor
mov b, @r0 ; Transfer divisor to register b
div ab ; Perform division of two 8-bit data’s
inc r0 ; Increment r0 by 1 to store quotient
mov@r0,a ; Transfer quotient to 32h
inc r0 ; Increment r0 by 1 to store LSB of the product
mov@r0,b ; Transfer remainder to 33h
end ; Terminate the program

3) Write an ALP to separate positive and negative number in a given array.

Algorithm:
Step 1: Start

Step 2: Initialize the array length

Step 3: Initialize the internal RAM address for array

Step 4: Initialize the array address to store positive numbers

Step 5: Initialize the array address to store negative numbers


Step 5: Check the number is whether positive or negative
Step 6: Store the number to corresponding location
Step 7: Stop

Assembly Language Program (ALP):

org 0000h ; Defining program to start from location 0000h


mov r2, #08h ; Initialize the array length
mov r0, #30h ; Pointer to the array in internal ram
mov r1, #50h ; Pointer to store positive number in internal RAM
mov dptr, #60h ; Pointer to store negative number in internal RAM
Microcontroller Lab Manual BECL456A

next_number: mov a, @r0 ; Load the current number


inc r0 ; Increment the array pointer
jnb acc.7, positive ; Check if the number is positive (check the msb)
negative: movx @dptr, a ; Store the negative number
inc dpl ; Increment the lower byte of dptr
sjmp next_step
positive: mov @r1, a ; Store the positive number
inc r1 ; Increment the array of positive number
next_step: djnz r2, next_number ; Decrement the array length and repeat if not zero
end ; Terminate the program

4) Write an ALP to separate even or odd elements in a given array

Algorithm:
Step 1: Start

Step 2: Initialize the array length

Step 3: Initialize the internal RAM address for array

Step 4: Initialize the array address to store even numbers

Step 5: Initialize the array address to store odd numbers


Step 5: Check the number is whether even or odd
Step 6: Store the number to corresponding location
Step 7: Stop

Assembly Language Program (ALP):

org 0000h ; Defining program to start from location 0000h


mov r2, #08h ; Initialize the array length
mov r0, #30h ; Pointer to the array in internal ram
mov r1, #50h ; Pointer to store even number in internal RAM
mov dptr, #60h ; Pointer to store odd number in external RAM
next_number: mov a, @r0 ; Load the current number
Microcontroller Lab Manual BECL456A

inc r0 ; Increment the array pointer


jnb acc.0, ; Check if the number is even(check the LSB)
odd: even movx ; Store the odd number

inc dpl ; Increment the lower byte of dptr


sjmp next_step
even: mov @r1, a ; Store the even number
inc r1 ; Increment the array of even number
next_step: djnz r2, next_number ; Decrement the array length and repeat if not zero
end ; Terminate the program

5) Write an ALP to arrange the numbers in Ascending & Descending order

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.

Step 6: Repeat till all the elements are sorted.


Step 7: Stop

Assembly Language Program (ALP):

org 000h ; Defining program to start from location 0000h


mov r0, #04 ; Initialize count (one less than no of data’s)
again: mov dptr , #2000h ; Initialize memory location

mov r1, 00h ; Initialize count1

back: mov r2, dpl


movx a, @dptr ; Fetch the data from current memory location

mov b, a ; Store the data and assume it as largest


inc dptr ; Fetch the next memory location address
Microcontroller Lab Manual BECL456A

movx a, @dptr ; Fetch the data from current memory location


cjne a, b, down ; Compare the data with largest

jmp skip ; If equal go to label skip


down: jnc skip ; If largest go to label skip
mov dpl, r2
movx @dptr, a ; If smallest exchange the numbers
inc dptr
mov a, b
movx @dptr, a
skip: djnz r1, back ; Decrement count1 and repeat if count not equal to 0
djnz r0, again ; Decrement count and repeat if count not equal to 0
end ; Terminate the program

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

Step 6: Repeat till all the elements are compared


Step 7: Stop

Dept of ECE, ACET,Bengaluru


Microcontroller Lab Manual BECL456A

Assembly Language Program (ALP):


org 000h ; Defining program to start from location 0000h
mov r1, #20h ; Initialize memory location
mov r2, #04h ; Initialize count (one less than no of data’s)
mov a, @r1 ; Fetch the data from current memory location
mov b, a ; Store the data and assume it as largest
back inc r1 ; Fetch the next memory location address
2:
mov a, @r1 ; Fetch the data from current memory location
cjne a, b, back1 ; Compare the data with largest
sjmp back3 ; If equal go to label back3
back jc back3 ; If smaller go to label back3
1:
mov b, a ; If greater store it as largest
back djnz r2, back2 ; Decrement count and repeat if count not equal to 0
3:
mov r0, #40h ; Fetch the memory location address to store largest
number
mov a,b

mov @r0, a ; Store the largest number


end ; Terminate the program

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 4: Increment A register (add 99h for down counter)


Step 5: Convert the value into decimal for the BCD up/down counter
Step 6: Count till last value

Step 7: Stop

Assembly Language Program (ALP):

org 000h ; Defining program to start from location 000h

mov dptr, #2000h ; Initialize memory location

mov a, #00h ; Initialize the first value


next: movx @dptr, a ; Store the value in the memory location
call delay ; Call delay subroutine to have delay between two values

inc a ; Generate next value


da a ; Convert the value into decimal
inc dpl ; Generate the next memory location address
jnz next ; Repeat till all the 8-bit values are generated

here: sjmp here


delay: mov r1, #0ffh ; Delay subroutine
loop1: mov r2, #0ffh
loop2: mov r3, #0ffh
loop3: djnz r3, loop3
djnz r2, loop2
djnz r1, loop1
ret
end ; Terminate the program
Microcontroller Lab Manual BECL456A

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 4: Increment Accumulator


Step 5: Count till last value

Step 6: Stop

Assembly Language Program (ALP):

org 000h ; Defining program to start from location 000h


mov dptr, #2000h ; Initialize memory location
mov a, #00h ; Initialize the first value
next: movx @dptr, a ; Store the value in the memory location
call delay ; Call delay subroutine to have delay between two values

inc a ; Generate next value

inc dpl ; Generate the next memory location address


jnz next ; Repeat till all the 8-bit values are generated

here: sjmp here


delay: mov r1, #0ffh ; Delay subroutine
loop1: mov r2, #0ffh
loop2: mov r3, #0ffh
loop3: djnz r3, loop3
djnz r2, loop2
djnz r1, loop1
ret
end ;Terminate the program

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 1: Initialize a variable sum to 0

Step 2: Use a for loop to iterate from 1 to 10

Step 3: Within the loop, add each number to the sum variable

Step 4: Check the final value of the output in sum

Program

#include <reg51.h>
void main()
{
unsigned int i, sum = 0;

for (i = 1; i <= 10; i++)


{
sum = sum + i; // sum now contains the sum of the first 10 integers
}

ACC = sum;
P0 = sum;
}

2. Write an 8051 C program to find factorial of a given numbers.

Algorithm:

Step 1: Take the input number from the user.

Step 2: Set the factorial variable to 1.

Step 3: Start a loop from 1 to the input number.

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;

for (i = 1; i <= num; i++)


{
factorial = factorial * i;
}

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

III. Hardware Interfacing Program

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

void delay(unsigned int k);


{
for( i=1; i<k; i++)
{
For( j=1; j<100; j++);
}
}

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:

#include <reg51.h> // To generate square wave form


void delay (unsigned int);
void main(void)
{
while(1)
{
P1=0x00;
Microcontroller Lab Manual BECL456A

delay(250);
P1=0xff;
delay(250);
}
}
void delay (unsigned itime)
{
unsigned int i;
for (i=0; i<=itime; i++);
}

Program:

#include <reg51.h> // To generate sine wave


sfr dacdata = 0x90;
void main(void)
{
unsigned char sine_value[12] ={128, 192,238,255,238,192,128,64,17,0,17,64};
unsigned int x;
while(1)
{
for (x=0; x<12; x++)
dacdata = sine_value[x];
}
}
Microcontroller Lab Manual BECL456A

Add on Program:

1. Write an Assembly Language Program (ALP) to exchange N bytes of data between


external memory locations 2000H – 2004H and 2010H – 2014H

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 from first memory location to second memory
location byte by byte
Step 6: Stop

Assembly Language Program (ALP):


org 0000h
mov r0, #05h ; Initialize count
mov r1, #20h ; Initialize first memory location
mov r2, #00h

mov r3, #10h ; Initialize second memory location


mov dph, r1
again: mov dpl, r2 ; Fetch the first memory location address
movx a, @dptr ; Copy the data from first memory location
mov b, a
mov dpl, r3 ; Fetch the second memory location address
movx a, @dptr
xch a, b ; Exchange the data between two memory locations
movx @dptr, a ; Store the data into second memory location
mov dpl, r2 ; Fetch the first memory location address
mov a, b
movx @dptr, a ; Store the data into first memory location
Microcontroller Lab Manual BECL456A

inc r2 ; Increment first memory location address


inc r3 ; Increment second memory location address
djnz r0, again ; Decrement count and repeat if count not equal to 0

end ; Terminate the program

2. Write an Assembly Language Program (ALP) to add of two 16-bit numbers.

Algorithm:
Step 1: Start

Step 2: Fetch LSB of first number and second number

Step 3: Add LSB of first number with LSB of second number

Step 4: Store LSB result


Step 5: Fetch MSB of first number and second number

Step 6: Add MSB of first number and second number with previous carry

Step 7: Store MSB result


Step 8: Store Carry
Step 9: Stop

Assembly Language Program (ALP):

org 000h

mov r0, #12h ; Copy higher byte of first number

mov r1, #34h ; Copy lower byte of first number

mov r2, #0feh ; Copy higher byte of second number

mov r3, #0dch ; Copy lower byte of second number

mov a, r1
add a, r3 ; Add lower bytes

mov 22h, a ; Store lower byte result

mov a, r0
Microcontroller Lab Manual BECL456A

addc a, r2 ; Add higher bytes with previous carry

mov 21h, a ; Store higher byte result


mov a, #00h
addc a, #00h

mov 20h, a ; Store carry

end ; Terminate the program


Microcontroller Lab Manual BECL456A

Viva Questions

1. What is meant by Microcontroller?


2. Intel 8051 follows which architecture?
3. Why 8051 is called an 8-bit microcontroller?
4. What is the width of the data bus?
5. What is the width of the address bus?
6. List the features of the 8051 microcontrollers?
7. How Much on-chip RAM is available?
8. List out addressing Modes in MCS-51.
9. How much total external data memory that can be interfaced to the 8051?
10. What is meant by Register?
11. What is an Accumulator?
12. What is the difference between microprocessor and microcontroller?
13. What are the various types of memories used in microcontroller?
14. What are the types of interrupts in 8051?
15. What are the four distinct types of memory in 8051?
16. How are labels named in assembly language?
17. Are all the bits of flag register used in 8051?
18. Which bit of the flag register is set when output overflows to the sign bit?
19. Which 2 ports combine to form the 16-bit address for external memory access?
20. What are the various jump instructions in microcontroller?
21. What is the difference between timer and counter of microcontroller?
22. What is an Interrupt service routine?
23. Which interrupt in the microcontroller has the highest priority?
24. Define bit, byte and nibble.
25. Define serial communication

You might also like