Microcontollers Lab Manual BCS402
Microcontollers Lab Manual BCS402
IV Semester
MICROCONTROLLERS (BCS402)
LAB MANUAL
Microcontrollers (BCS402)
Lab Manual
Laboratory Certificate
This is to certify that ................................................................................. bearing
USN ………………………..has satisfactorily completed the experiments in
practical MICROCONTROLLERS prescribed by the Visvesvaraya Technological
University for the course with course code BCS402 in the Laboratory of this college
in the year 2024-25.
College Vision:
To produce technically competent engineers having innovative skills, positive attitude, leadership and
professional ethics, with a focus on developing sustainable and new technology
College- Mission:
Create a conductive environment for teaching, learning and innovation by providing state of the
art infrastructure.
Learn sustainable and new technologies through industry institute collaborations.
Produce technically competent engineers with ethics, discipline and social consciousness
through holistic education.
Dept.-Vision
To produce highly competent and innovative Computer Science professionals through excellence in
teaching, training and research
Dept.- Mission
M1: To provide appropriate infrastructure to impart need-based technical education through effective
teaching and research.
M2: To involve the students in innovative projects on emerging technologies to fulfill the industrial
requirements
M3: To render leadership skills and ethical responsibilities in students that leads them to become
globally competent professionals
BE: Program Outcomes (POs)
At the end of the B.E program, students are expected to have developed the following outcomes.
2. Problem analysis: Identify, formulate, research literature, and analyse complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern Tool Usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex engineering activities
with an understanding of the limitations.
6. The Engineer and Society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and Sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of need for
sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of the engineering practice.
9. Individual and Team Work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
11. Project Management and Finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one‘s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
COURSE OBJECTIVES AND OUTCOMES
Course Objectives:
CLO1 Understand the fundamentals of ARM-based systems and basic architecture of CISC and
RISC.
CLO2 Familiarize with ARM programming modules along with registers, CPSR and Flags
CLO3 Develop ALP using various instructions to program the ARM controller
CLO5 Discuss the ARM Firmware packages and Cache memory polices
Course Outcomes:
CO1 Acquire the fundamental knowledge of ARM processor system and basic architecture of
CISC and RISC
CO2 Understand the various instructions for program of the ARM Miro-controller and develop
ALP
CO4 Apply the concepts of exceptions, Interrupt handling and firmware mechanism in developing
Applications
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam
(SEE) is 50%.
The minimum passing mark for the CIE is 40% of the maximum marks (20 marks out of 50).
The minimum passing mark for the SEE is 35% of the maximum marks (18 marks out of 50).
A student shall be deemed to have satisfied the academic requirements and earned the credits
allotted to each subject/course if the student secures not less than 35% (18 Marks out of 50) in the
semester-end examination (SEE), and a minimum of 40% (40 marks out of 100) in the sum total of
the CIE (Continuous Internal Evaluation) and SEE (Semester End Examination) taken together.
The CIE marks for the theory component of the IC shall be 25 marks and for the laboratory
component 25 Marks.
Rubrics for Lab Performance Assessment
In Each Lab, students will be assessed on their participation and performance in the lab.
The marks obtained in each lab will be totaled and combined with Final Lab test or
Viva-voce. The students need to attend 85% of the labs to qualify to be eligible to attend
the university examination.
Microcontrollers
BCS402
Contents
Exp. Page
Title of the Experiment
No No
12-25
1. Using Keil software, observe the various Registers, Dump, CPSR, with a simple
Assembly Language Programs (ALP).
2. Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical 26-28
Operations (Demonstrate with the help of a suitable program)
Develop an ALP to multiply two 16-bit binary numbers
3. 29
Develop an ALP to find the sum of first 10 integer numbers.
4. 30
5. Develop an ALP to find the largest/smallest number in an array of 32 numbers. 31-33
Develop an ALP to count the number of ones and zeros in two consecutive memory
6. locations 34-36
7. Simulate a program in C for ARM microcontroller using KEIL to sort the numbers in 37-39
ascending/descending order using bubble sort
Simulate a program in C for ARM microcontroller to find factorial of a number
8. 40
9. Simulate a program in C for ARM microcontroller to demonstrate case conversion of 41
characters from upper to lowercase and lower to uppercase.
10. Demonstrate enabling and disabling of Interrupts in ARM. 42-43
11. Demonstrate the handling of divide by zero, Invalid Operation and Overflow 44-45
exceptions in ARM
12 Viva Questions 46-49
13 Additional programs 50-53
EVALVUATION SHEET
Faculty
Conduction
Sl. Date of Date of sign with
TITLE OF THE EXPERIMENT Record
No Conduction Submission Date
1
Using Keil software, observe the various Registers,
Dump, CPSR, with a simple Assembly Language
Programs (ALP).
Develop and simulate ARM ALP for Data Transfer,
2
Arithmetic and Logical operations (Demonstrate
with the help of a suitable program)
3 Develop an ALP to multiply two 16-bit binary
numbers
Develop an ALP to find the sum of first 10 integer
4 numbers.
Develop an ALP to find the largest/smallest number
5
in an array of 32 numbers.
6 Develop an ALP to count the number of ones and
zeros in two consecutive memory locations
7
Simulate a program in C for ARM microcontroller
using KEIL to sort the numbers in
ascending/descending order using bubble sort
8 Simulate a program in C for ARM microcontroller to
find factorial of a number
Simulate a program in C for ARM microcontroller to
9
demonstrate case conversion of characters from
upper to lowercase and lower to uppercase.
Demonstrate enabling and disabling of Interrupts in
10
ARM.
Demonstrate the handling of divide by zero, Invalid
11
Operation and Overflow exceptions in ARM
LPC2148 Microcontroller
The LPC2148microcontroller is designed by Philips(NXP Semiconductor) with
several in-built features & peripherals. Due to these reasons, it will make more
reliable as well as the efficient option for an application developer. LPC2148 is
a16-bit or 32-bit microcontroller based on ARM7 family.
Programmer's Model
ARM has a 32-bit data bus and a 32-bit address bus. The data types the processor
supports are Words (32 bits), where words must be aligned to four byte
boundaries. Instructions are exactly one word, and data operations (e.g. ADD) are
only performed on word quantities. Load and store operations can transfer words.
Registers
The processor has a total of 37 registers made up of 31 general 32 bit registers
and 6 status registers. At any one time 16 general registers (R0 to R15) and one
or two status registers are visible to the programmer. The visible registers depend
on the processor mode and the other registers (the banked registers) are switched
in to support IRQ, FIQ, Supervisor, Abort and undefined mode processing. The
register bank organization is shown in below figure. The banked registers are
shaded in the diagram.
In all modes 16 registers, R0 toR15, are directly accessible. All registers
except R15 are general purpose and may be used to hold data or address values.
Register R15 holds the Program Counter (PC). When R15 is read, bits [1:0] are
zero and bits [31:2] contain the PC.A seventeenth register (the CPSR -Current
Program Status Register) is also accessible. It contains condition code flags
and the current mode bits and may be thought of as an extension to the PC. R14
is used as the subroutine link register and receives a copy of R15 when a Branch
and Link instruction is executed. It may be treated as a general- purpose register
at all other times. R14_svc, R14_irq, R14_fiq, R14_abt and R14_und are used
similarly to hold the return values of R15 when interrupts and exceptions arise,
or when Branch and Link instructions are executed within interrupt or exception
routines.
For ALP program, CLICK “NO”, For “C” program click on “YES”
Step 7: Now you see Startup.s is already added which is necessary for
running code for Keil.
Step 09: Write Code for Blink LED in C OR ASM and FileName.c/ASM Save.
Step 11: Now you add LED.c file by adding Source Group 1 Add
files to Group ‘Source Group 1’.
Step 14: Go to Options for Target ‘Target 1’. Click on Check Box
Create HEX File.
Step 15: Then go to Linker. Click on Use Memory Layout for Target
Dialog.
Step 17: Now you see 0 Error(s), 0 Warning (s). Then Hex File will
create in Specific Folder. Now to download it for you target hardware.
Go to options in the main menu and select advanced options the following screen displays.
Select hardware config tab and uncheck the use DTR and RTS option and press the OK button
Select the comm. Port (Check in device manager about UART bridge COM port).
Select the baud rate as 9600, select device as LPC2148, interface is None ISP, Oscillator is 12.000
Browse the appropriate hex file provided on the CD and click on the start Button. The
programs will the downloaded in the Kit,
The problem: P = Q + R + S
Let Q = 2, R = 4, S = 5. Assume that r1 = Q, r2 = R, r3 = S. The
result Q will go in r0.
The Code
The program
Notes:
The first line is AREA Example1, CODE; READONLY is an assembler directive and
is required to set up the program. It is a feature of the development system and not the
ARM assembly language. An assembler from a different company may have a
different way of defining the start of a program.
In this case, AREA refers to the segment of code, Example1 is the name we’ve given
it, CODE indicates executable code rather than data, and READONLY state that it
cannot be modified at run time.
Anything starting in column 1 (in this case Stop) is a label that can be used to refer to
that line.
The instruction Stop B Stop means ‘Branch to the line labelled Stop’ and is used
to create an infinite loop. This is a convenient way of ending programs in simple
examples like these.
Figure 1: The state of the system after loading the code for Example 1
Note that the contents of r0 are 2 + 4 + 5 = 11 = 0x0B. This is the result we expected
Example 2: Additions
The Code
The program
Note how we have to create a data area at the end of the program. We
have reserved spaces for P, Q, R, and S. We use the SPACE directive for
S to reserve 4 bytes of memory space for the variable S. After that we
reserve space for Q, R, and S. In each case we use a DCD assembler
directive to reserve a word location (4 bytes) and to initialize it. For
example, Q DCD 2 ;create variable Q with initial value 2 means ‘call the
current line Q and store the word 0x00000002 at that location.
Figure 2 shows the state of the program after it has been loaded. In this case
we’ve used the view memory command to show the memory space. We have
highlighted the three constants that have been pre- loaded into memory.
Take a look at the disassembled code. The pseudoinstruction LDR r1,Q was
actually translated into the real ARM instruction LDR r1,[PC,#0x0018]. This
is still a load instruction but the addressing mode is register indirect. In this
case, the address is the contents of the program counter, PC, plus the
hexadecimal offset 0x18. Note also that the program counter is always 8
bytes beyond the address of the current instruction. This is a feature of the
ARM’s pipeline.
Consequently, the address of the operand is [PC] + 0x18 + 8 = 0 + 18 + 8
= 0x20.
If you look at the memory display area you will find that the contents of
0x20 are indeed 0x00000002.
The code
The address of the first data These are the three data values
element on this line is we’ve stored in memory at
0x0000001C. The first locations
0x00000020
2 Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical operations
(Demonstrate with the help of a suitable program)
a) Data Transfer
Algorithm:
Program
b) Arithmetic Operation
Algorithm:
Program
c) Logical operation
Algorithm:
Program:
Algorithm:
Program:
Algorithm:
Program:
Algorithm:
Step1: Define code segment and Data segment
Step 2: Initialize variables:
- Counter R5 to keep track of the number of iterations (initialized to 6,
representing N=7).
- Register R1 to hold the address of the array.
- Registers R2 and R4 to temporarily hold array elements for comparison.
- Register R3 to hold the largest number found.
- Register R4 to hold the address of the RESULT variable.
Step 3: Load the starting address of the array into register R1.
Step 4: Load the first two numbers of the array into registers R2 and R4.
Step 5: Compare the two numbers:
- If the first number is greater than the second number, store it in R3 (largest
number).
- If the second number is greater, store it in R3.
Or
- If the first number is smaller than the second number, store it in R3 (smallest
number).
- If the second number is smallest, store it in R3.
Step 6: Enter a loop:
- Decrement the counter R5.
- If the counter is not zero, loop back to step 4 to compare the next pair of
numbers.
- If the counter is zero, exit the loop.
Step 7: Store the largest number (in R3) in the RESULT variable.
Step 8: End
Program 5a:
Program 5b:
6. Develop an ALP to count the number of ones and zeros in two consecutive memory
locations.
Algorithm:
Step1: Define Code segment and Data segment
Step 2: Initialize variables:
- R2 to count the number of ones.
- R3 to count the number of zeros.
- R7 as a loop counter initialized to 2 (for processing two 32-bit words).
- R6 to hold the address of the VALUE array.
- R1 as a loop counter initialized to 32 (to iterate through each bit of the word).
Step 3: Load the address of the VALUE array into register R6.
Step 4: Enter the outer loop:
- Load a 32-bit word from the VALUE array into register R0.
- Rotate R0 right by 1 bit position (ROR) to check each bit.
Step 5: Inside the outer loop:
- Check if the carry flag is set after rotation:
- If set, it indicates a one bit, so increment the ones count (R2).
- If not set, it indicates a zero bit, so increment the zeros count (R3).
Step 6: Continue looping through each bit until all 32 bits are processed.
Step 7: After processing one word:
- Decrement the loop counter (R7).
- If there are more words to process, repeat the outer loop.
- If not, exit the loop.
Step 8: At the end, R2 contains the count of ones and R3 contains the count of zeros.
Step 9: End
Program:
7. Simulate a program in C for ARM microcontroller using KEIL to sort the numbers in
Ascending/descending order using bubble sort
Program
Algorithm:
Step1: Start with a non-negative integer number for which factorial needs to be calculated.
Step 2: Check if the number is 0 or 1:
- If yes, return 1 (Factorial of 0 and 1 is 1).
Step 3: Otherwise, recursively calculate the factorial of (number - 1) and multiply it by the
number.
Step 4: Repeat step 3 until the number becomes 1.
Step 5: Return the factorial value.
Step 6 : End
Program:
11. Demonstrate the handling of divide by zero, Invalid Operation and Overflow
exceptions in ARM
Algorithm;
Step 1: Initialize System: Set up basic configurations
Step 2. Check for Divide-by-Zero:
If the denominator is 0, display an error message.
Otherwise, perform the division.
Step3: Check for Integer Overflow:
Perform addition using assembly instructions and detect overflow
using flags.
Step 4: Handle Invalid Operations:
Use an Undefined Instruction Exception Handler to catch invalid
operations.
Step 5: Execute Safe Operations:
Perform calculations with error handling.
Program:
Viva Questions
3. What is an interrupt?
Hardware or software can communicate to micro controller through interrupts. Interrupts are
external signal that controls the micro processor. Interrupt signals are generated by sources
like software programs or hardware controls.
6. What is an Accumulator?
Accumulator has another name called Register 'A'. Accumulator is an important register in
8051, because it saves all arithmetic and logical values.
7. List some of the 8051 microcontroller manufacturers?
Intel
Philips
Infineon
Maxim/Dellas semiconductor
Atmel
8. What is difference between microprocessor and microcontroller?
The microprocessor has no ROM, RAM and no I/O ports on the chip itself.
Whereas the microcontroller has a CPU in addition to a fixed amount of RAM,ROM, I/O
ports and a timer all on a single chip.
9. List out some of the features of the 8051?
ROM – 4K bytes
RAM – 128 bytes
Timer – 2 no
I/O Pins – 32
Serial Port – 1
Interrupt sources – 6
10. What are the various types of memories used in microcontroller/microprocessor?
ROM – Read Only Memory
RAM – Random Access Memory
PROM – Programmable Read Only Memory
EPROM – Erasable Programmable Read Only Memory
EEROM – Electrically Erasable Programmable Read Only Memory
11. What are the different instruction sets ?
1. . Data transfer instruction.2.Arithmetic instruction.3.Logical Instruction.4.Boolean
variable manipulation instruction.5. Program branching instructions.
Microprocessor Microcontroller
Microprocessor contains ALU, general Microcontroller obtains the circuitry of
purpose registers, stack pointer, program microprocessor and in addition it has
counter, clock timing circuit and interrupt built- in ROM, RAM, I/O devices, timers
circuit. and counters.
It has many instructions to
It has one or two instructions to move data
move data between memory
between memory and CPU.
and CPU.
It has one or two bit handling
It has many bit handling instructions.
instructions.
Access times for memory and I/O Less access times for built-in memory and
devices are more. I/O devices.
Microcontroller based system requires
Microprocessor based system
less hardware reducing PCB size and
requires more hardware.
increasing the reliability.
13. Name the five interrupt sources of 8051?
The interrupts are:
Vector address
External interrupt 0 : IE0 : 0003H
Timer interrupt 0 : TF0 : 000BH
External interrupt 1 : IE1 : 0013H
Timer Interrupt 1 : TF1 : 001BH
Serial Interrupt
Receive interrupt : RI : 0023H
Transmit interrupt: TI : 0023H
Keyboards, Floppy disk are the examples of input devices. Printer, LED / LCD display,
CRT Monitor are the examples of output devices.
Addition programs
1. Write an ALP to find square of a number (1 to 10 ) using look up table.
Algorithm:
Step 1: Define an array named TABLE1 with 12 elements.
Step 2: Define a variable named RESULT and initialize it to 0.
Step 3: Load the base address of TABLE1 into register R0.
Step 4: Load the value 9 into register R1.
Step 5: Multiply the value in R1 by 4 and store the result back in R1.
Step 6: Add the value in R1 to the base address in R0 and store the result back in R0.
Step 7: Load the value from the memory address pointed to by R0 into register R3.
Step 8: Load the address of the RESULT variable into register R5.
Step 9: Store the value from R3 into the memory location pointed to by R5.
Step 10: End
Program:
Algorithm:
Step 1: Set a variable, let's call it "largest", to an initial value. You can set it to the first
element of the array for simplicity.
Step 2: Iterate through each element of the array:
a. Compare the current element with the "largest" variable.
b. If the current element is greater than "largest", update the value of "largest" to the current
element.
Step 3: After iterating through all elements of the array, the "largest" variable will hold the
largest value.
Step 4: You can then use or display the value stored in the "largest" variable as needed.
Program;