You are on page 1of 20

GAUHATI UNIVERSITY INSTITUTE OF

SCIENCE AND TECHNOLOGY

ECE DEPARTMENT

MICROCONTROLLER LAB MANUAL


SEMESTER VI
Microcontroller Lab

INDEX

Sl.No. Program Page Date Sign.


No.
1 Addition of two 8 bits number
2 Subtract a small number from a large number
3 Addition of two 8 bits number with carry
4 Copy a block of 10 bytes of data from one location to another
5 Block transfer without overlapping
6 Exchange a block of data
7 The word „MICROCONTROLLER‟ is to be burned in the flash
ROM starting from 0400H. WAP to do this and to read this data
into internal RAM locations from 60H.
8 Create a square wave of 50% duty cycle on bit 0 of Port 1 and
Port 2
9 Addition of a block of data
10 WAP to generate and store natural numbers from 1 to N terms
and also find the sum of these numbers. Assume that the value N
is stored in location 30H. Store generated natural number from
40H. Store the resultant sum in Accumulator.
11 WAP to add two 16 bit numbers.
12 WAP to subtract two 8 bit numbers stored in external RAM
location 4501 (minuend) and 4500 (subtrahend). The result should
be in location 4502 and the sign bit in location 4503.
13 WAP to find the largest number from a given array. Assume that
the array is present in internal RAM from 50H. Size of the array
is 10H
14 WAP to search and find address of a given byte of data from 10
memory location in external RAM starting from 1000H. Save the
address in R2 (Lower order address) and R3 (Higher order
address).
15 WAP to count number of positives, negative and zeros in an array
of signed numbers stored in external RAM starting at 1000H. Size
of array is 100 bytes.
16 WAP to count even and odd numbers in an array of 10 numbers
stored in external RAM starting at address 1000H.
17 WAP to sort 10 numbers in internal RAM location from 40H in
ascending order.

ECE DEPARTMENT, GUIST Page 2


Microcontroller Lab

WORKING PROCEDURE WITH KEIL:

KEIL IDE is basically an assembler and a compiler. Either an Assembly or C language code can
be written in KEIL.

1) Create a new folder for project to avoid getting mixed up with files.
2) Go to PROJECT > NEW uVISION PROJECT
3) Give some name and click SAVE
4) When prompted, select proper device that you're using ( i.e. ATMEL > AT89C51 )
5) When prompted, select YES
6) Click on FILE > NEW button and write your code (Assembly or C language)
7) SAVE the file with proper extension. For Assembly “name.asm” and for C “name.c”
8) Now write the code and notice how the code gets highlighted according to syntax.
9) Right click SOURCE GROUP 1 in the Project workspace.
10) Add the code you have written into the Project workspace.
11) Go to PROJECT Menu > Build target
12) Notice successful build. Ignore the warnings.

13) Debugging in Keil uVision: When you‟re done writing your code, compile it. Keil will
notify you of all the syntax errors (if any) in the program. Double click the error message and
the cursor will automatically move to the exact statement containing the error. Read the error
message and make changes accordingly.
14) When you‟re done fixing all the syntax errors, you may proceed to debugging your code.
You can do this by using the Debug Menu.

Go to DEBUG > Start Stop Debug session

When prompted, simply click OK button to confirm that you‟re aware of this fact that the
evaluation version of Keil uVision which has a code size limit of 2K bytes.

15) Once the Debugging Mode is active, there are only few and simple things that you need to
know about. Here is the list as labelled in the picture below

a – Present values of all registers in the micro-controller


b – Run/Execute the program all at once (till break-point)
c – Execute each statement step by step
d – Execute blocks of statement in one go (i-e execute a function block in one step)
e – Execute till the cursor position in code
f – Insert breakpoints (to halt program execution during debugging)

ECE DEPARTMENT, GUIST Page 3


Microcontroller Lab

These are the simple steps to write a code in Keil and compile it.

ECE DEPARTMENT, GUIST Page 4


Microcontroller Lab

Enabling Hex File:

Go to options for the project as shown below

Enable Hex File generation

ECE DEPARTMENT, GUIST Page 5


Microcontroller Lab

Use the HEX file that is generated to program your IC

ECE DEPARTMENT, GUIST Page 6


Microcontroller Lab

Expt 1: Addition of two 8 bits number

Aim:
To write and execute an assembly language program to add the given two 8 bit
numbers.

Objectives:
To add any two 8 bit numbers using immediate addressing and to store the result

Algorithm:

1. Start the Program.


2. Move the Data1 to the accumulator.
3. Add the Data2 with the accumulator, the result stored in accumulator.
4. Move the result which is in accumulator to the 30H address location.
5. Stop the Program.

Program:

ORG 0000H
MOV A, #Data1
ADD A, #Data2
MOV 30H, A
HLT: SJMP HLT
END

Results:
Input: Output:

Memory Address Data Memory Address Data


- Data 1 30H
- Data 2

Conclusion:
Thus an assembly language program of 8 bit addition is written and executed
successfully.

ECE DEPARTMENT, GUIST Page 7


Microcontroller Lab

Expt 2: Subtract a small number from a large number

Aim:
To write and execute an assembly language program to subtract the given two 8
bit numbers.

Objectives:
To subtract a small number from a large number using immediate addressing and to
store the result

Algorithm:

1. Start the Program.


2. Move the Data1 to the accumulator.
3. Subtract the Data2 with the accumulator, the result stored in accumulator.
4. Move the result which is in accumulator to the 30H address location.
5. Stop the Program.

Program:

Results:
Input: Output:
Memory Address Data Memory Address Data
- Data 1 30H
- Data 2

Conclusion:
Thus an assembly language program of 8 bit subtraction is written and executed
successfully.

ECE DEPARTMENT, GUIST Page 8


Microcontroller Lab

Expt 3: Addition of two 8 bit number with Carry

Aim:
To write and execute an assembly language program to add the given two 8 bit
numbers with carry.

Objectives:
To add any two 8 bit numbers using immediate addressing and to store the result and
carry

Algorithm:

1. Start the Program.


2. Move the Data1 from 40H Memory location to ACC.
3. Add the 41H memory location with the accumulator, the result is in accumulator.
4. Move the result which is in accumulator to the 42H address location.
5. Clear the 43H memory location
6. Check the carry flag, if carry is there, then increment 43H memory location
7. Stop the Program.

Program:

Results:

Conclusion:

ECE DEPARTMENT, GUIST Page 9


Microcontroller Lab

Expt 4: Copy a block of 10 bytes of data from one location to another

Aim:
To write and execute an assembly language program to copy a block of 10 bytes of data
from one location to another

Objectives:
To copy a block of 10 bytes of data from one location to another and show the result

Algorithm:

1. Start the program.


2. Initialize a source pointer and a destination pointer.
3. Set the counter.
4. Copy a byte from source to destination using the pointers through accumulator.
5. Decrement counter by 1.
6. Check if counter = 0. If yes, go to step 7 or else go to step 4.
7. Terminate the program.

Program:

Results:

Conclusion:

ECE DEPARTMENT, GUIST Page 10


Microcontroller Lab

Expt 5: Block transfer without overlapping


Aim:
To write and execute an assembly language program to transfer a block of data
without overlapping.

Objectives:
To move a block of 8 bytes of data from address 50H to 57H to location starting
from 51H.

Algorithm:

1. Start the Program.


2. Initialize a source pointer (base address) and a destination for higher address.
3. Set a counter.
4. Copy a byte via accumulator from source to destination.
5. Decrement the pointers after copying.
6. Decrement counter by 1. If counter not equal to zero jump to step 4 else go to step 7.
7. Terminate the Program.

Program:

Results:

Conclusion:

ECE DEPARTMENT, GUIST Page 11


Microcontroller Lab

Expt 6: Exchanging a block of data

Aim:
To write and execute an assembly language program to exchange a block of 8
byte of data from RAM address 50H to 57H with 60H to 67H.

Objectives:
To transfer the content of a block of data to another block and vice versa

Algorithm:

1. Start the Program.


2. Initialize R0 as source pointer of first 8 byte data and R1 as source pointer of the second
8 byte data.
3. Set a counter.
4. Copy the content from the first block to the accumulator and then copy the content from
the second block to address say 03H. Copy the contents of accumulator to R1 and
contents of 03H to R0.
5. Increment the pointers after copying.
6. Decrement counter by 1. If counter not equal to zero jump to step 4 else go to step 7
7. Terminate the Program

Program:

Results:

Conclusion:

EXERCISE:

6.1 Repeat the program using the function XCH.

ECE DEPARTMENT, GUIST Page 12


Microcontroller Lab

Expt 7: The word ‘MICROCONTROLLER’ is to be burned in the flash ROM starting


from 0400H. WAP to do this and to read this data into internal RAM locations from 60H.

Aim:
To write and execute an assembly language program to burn the word
„MICROCONTROLLER‟ in the flash ROM and also read the data into internal
RAM locations

Objectives:
To burn the word „MICROCONTROLLER‟ in the flash ROM starting from
0400H and also read the data into internal RAM locations starting from 60H

Algorithm:

1. Initialize DPTR as source pointer and R0 as destination pointer.


2. Load R3 by 15H to serve as counter.
3. Clear ACC to hold the copied data.
4. Copy a byte via ACC, from source to destination using R0 and DPTR.
5. Increment the source and destination pointer by 1
6. Decrement counter by 1. If counter not equal to zero jump to step 3 else go to step 7
7. Terminate the Program

Program:

Result:

Conclusion:

ECE DEPARTMENT, GUIST Page 13


Microcontroller Lab

Expt 8: Create a square wave of 50% duty cycle on bit 0 of Port 1

Aim and Objectives:


To write and execute an assembly language program to create a square wave of
50% duty cycle on bit 0 of Port 1

Algorithm:

1. Set bit 0 of port 1


2. Call delay for 50% duty cycle
3. Now clear bit 0 of port 1
4. Call delay
5. Keep doing this indefinitely
6. Now put time delay at some address
7. Set a counter for delay subroutine (Load R5 by 11)
8. Load R4 by 255 and R3 by 255
9. Decrement R3 and stay here until R3 becomes zero
10. Decrement R4 and keep loading R3 until R4 becomes zero
11. Decrement R5 and keep loading R4 until R5 becomes zero
12. Return to Caller
13. Terminate the process

Program:

Result:

Conclusion:

EXERCISE:

8.1 Create a square wave of 66% duty cycle on bit 0 of Port 2

ECE DEPARTMENT, GUIST Page 14


Microcontroller Lab

Expt 9: Addition of a block (Array) of data.

Aim and Objectives:


To write and execute an assembly language program to find the sum of values
stored in RAM locations from 50H to 57H. Store the MS byte of the sum in 40H
and LS byte in 41H.

Algorithm:

1. Start the program.


2. Clear the Accumulator. Clear the destination address say 40H (MSB) and 41H (LSB).
3. Initialize R0 as a source pointer (base address).
4. Load R7 with value 08H to serve as a counter.
5. Add the content of ACC with source data.
6. Copy the content of ACC to LS byte destination address.
7. Check if carry is generated, if yes, increment the MS byte destination address to keep
track of carry, else jump to step 8
8. Increment source pointer R0 by 1
8. Decrement counter by 1, if counter not equal to zero jump to step 5 else go to step 9
9. Terminate the Program

Program:

Result:

Conclusion:

ECE DEPARTMENT, GUIST Page 15


Microcontroller Lab

Expt 10: WAP to generate and store natural numbers from 1 to N terms and also find the
sum of these numbers. Assume that the value N is stored in location 30H. Store generated
natural number from 40H. Store the resultant sum in Accumulator.

Aim and Objectives:


To write and execute an assembly language program to generate and store natural numbers from
1 to N terms and also find the sum of these numbers. Assume that the value N is stored in
location 30H. Store generated natural number from 40H. Store the resultant sum in Accumulator.

Algorithm:

1. Start the program.


2. Initialize R0 as destination pointer for storage of generated natural numbers.
3. Load R7 from 30H by N to serve as counter.
4. Clear R6 and ACC to generate the numbers and calculate the sum.
5. Increment R6 by 1 to generate natural number and calculate the sum.
6. Copy the content of R6 to location 40H through register R0.
7. Increment R0 by 1 to move to next location.
8. Decrement R7 by 1. If R7 is not equal to zero then go to step 5, else move to step 9.
9. Terminate the Program

Program:

Result:

Conclusion:

ECE DEPARTMENT, GUIST Page 16


Microcontroller Lab

Expt 11: WAP to add two 16 bit numbers with carry.

Aim and Objectives:


To write and execute an assembly language program to add two 16 bit numbers with carry.

Algorithm:

1. Start the program.


2. Clear carry flag.
3. Move the content of LS byte source address of first number into ACC.
4. Add the content of ACC with the content of LS byte source address of second number
along with carry.
5. Store the result in LS byte destination address.
6. Move the content of MS byte source address of first number in ACC.
7. Add the content of ACC with the content of MS byte source address of second
number along with carry.
8. Store the result in MS byte destination address.
9. Clear memory location 5FH to store the carry.
10. Check carry flag; if it is 1, increment 5FH location.
11. Terminate the Program

Program:

Result:

Conclusion:

ECE DEPARTMENT, GUIST Page 17


Microcontroller Lab

Expt 12: WAP to subtract two 8 bit numbers stored in external RAM location 4501
(minuend) and 4500 (subtrahend). The result should be in location 4502 and the sign bit in
location 4503.

Aim and Objectives:


To write and execute an assembly language program to subtract two 8 bit numbers stored in
external RAM locations 4501 (minuend) and 4500 (subtrahend). The result should be in location
4502 and the sign bit in location 4503.

Algorithm:

1. Start the program.


2. Set DPTR as pointer for data.
3. Move the subtrahend from external memory to ACC and save it in register R1.
4. Move the minuend from external memory to ACC.
5. Clear R0 register for sign bit.
6. Clear carry flag.
7. Subtract the content of R1 from ACC.
8. Check the carry status. If the carry is not set, go to step 11 else continue step 9.
9. 2‟s complement the content of ACC.
10. Increment R0 register to keep track of the carry.
11. Increment DPTR and save the result (content of ACC) in external memory.
12. Increment DPTR and move R0 (sign bit) to external memory.
13. Terminate the Program

Program:

Result:

Conclusion:

ECE DEPARTMENT, GUIST Page 18


Microcontroller Lab

Expt 13: WAP to find the largest number from a given array. Assume that the array is
present in internal RAM from 50H. Size of the array is 10H

Expt 14: WAP to search and find address of a given byte of data from 10 memory location
in external RAM starting from 1000H. Save the address in R2 (Lower order address) and
R3 (Higher order address).

Expt 15: WAP to count number of positives, negative and zeros in an array of signed
numbers stored in external RAM starting at 1000H. Size of array is 100 bytes.

Expt 16: WAP to count even and odd numbers in an array of 10 numbers stored in
external RAM starting at address 1000H.

Expt 17: WAP to sort 10 numbers in internal RAM location from 40H in ascending order.

ECE DEPARTMENT, GUIST Page 19


Microcontroller Lab

Suggested Readings:

1. Microcontrollers- Rajkamal, Pearson Education;


2. 8051 Microcontroller- Mazidi, Pearson Education;
3. Microprocessor and Microcontroller- William Kleitz, Prentice Hall
4. The 8051 Microcontroller – Kenneth J. Ayala

ECE DEPARTMENT, GUIST Page 20

You might also like