MC Lab Manual - RNSIT
MC Lab Manual - RNSIT
MICROCNTROLLERS LABORATORY
INTERNAL EVALUATION SHEET
TEST-1
TEST-2 𝟒𝟎 10
Write
Date of Exen. Viva Total Teacher
Program # Lab programs up
Execution (10) (10) 30 Signature
(10)
10
11
Total
Marks 330 3𝟎 10
Microcontrollers Laboratory (BCS402)
C: RECORD (5) 25
Microcontrollers Laboratory (BCS402)
1. Introduction
Basic ARM Assembly Programs Week 1
2.
Using Keil software, observe the various Registers, Dump,
CPSR, with a simple Assembly Language Programs (ALP).
3. Develop and simulate ARM ALP for Data Transfer,
Arithmetic and Logical operations (Demonstrate with the
help of a suitable program).
4. Develop an ALP to multiply two 16-bit binary numbers.
14.
Microcontrollers Laboratory (BCS402)
SYALLABUS
SEMESTER – IV
MICROCONTROLLERS LABORATORY
(Effective from the academic year 2023-2024)
Course Objectives:
CLO 1: Understand the fundamentals of ARM-based systems and basic architecture of CISC and RISC.
CLO 2: Familiarize with ARM programming modules along with registers, CPSR and Flags.
CLO 3: Develop ALP using various instructions to program the ARM controller.
CLO 4: Understand the Exceptions and Interrupt handling mechanism in Microcontrollers.
CLO 5: Discuss the ARM Firmware packages and Cache memory polices.
Programs List:
Sl. Experiments
No IDE Used: Keil uVision 4 or 5
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 operations
(Demonstrate with the help of a suitable program).
3.
Develop an ALP to multiply two 16-bit binary numbers.
4.
Develop an ALP to find the sum of first 10 integer numbers.
5.
Develop an ALP to find the largest/smallest number 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.
9. Simulate a program in C for ARM microcontroller to demonstrate case conversion of characters
from upper to lowercase and lower to uppercase.
10.
Demonstrate enabling and disabling of Interrupts in ARM.
11. Demonstrate the handling of divide by zero, Invalid Operation and Overflow exceptions in
ARM.
Microcontrollers Laboratory (BCS402)
● 15 marks for the conduction of the experiment and preparation of laboratory record, and 10 marks
for the test to be conducted after the completion of all the laboratory sessions.
● On completion of every experiment/program in the laboratory, the students shall be evaluated including
viva-voce and marks shall be awarded on the same day.
● The CIE marks awarded in the case of the Practical component shall be based on the continuous evaluation
of the laboratory report. Each experiment report can be evaluated for 10 marks. Marks of all experiments’
write-ups are added and scaled down to 15 marks.
● The laboratory test (duration 02 hours) after completion of all the experiments shall be conducted for 50
marks and scaled down to 10 marks.
● Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory
component of IPCC for 25 marks.
● The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of the IPCC.
CO-PO MATRIX
COURSE
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3 PSO4
OUTCOMES
CO1 3 3 3 3 1 3 1 2
CO2 3 3 3 3 1 3 1 2
CO3 3 3 3 2 1 2 1 2
CO4 3 3 3 2 1 3 1 2
CO5 3 3 3 1 2 1 2
Microcontrollers Laboratory (BCS402)
TABLE OF CONTENTS
SL. PAGE
CONTENTS
NO. NO.
1 INTRODUCTION
2 A SIMPLE GUIDE ON KEILUVISION 4
3 ARM PROGRAMMING
4 SAMPLE PROGRAMS
5 LABORATORY PROGRAMS
6 Using Keil software, observe the various Registers, Dump, CPSR, with a
simple Assembly Language Programs (ALP).
7 Develop and simulate ARM ALP for Data Transfer, Arithmetic and
Logical operations (Demonstrate with the help of a suitable program).
8 Develop an ALP to multiply two 16-bit binary numbers.
9 Develop an ALP to find the sum of first 10 integer numbers.
10 Develop an ALP to find the largest/smallest number in an array of 32
numbers.
11 Develop an ALP to count the number of ones and zeros in two consecutive
memory locations.
12 Simulate a program in C for ARM microcontroller using KEIL to sort the
numbers in ascending/descending order using bubble sort.
13 Simulate a program in C for ARM microcontroller to find factorial of a
number.
14 Simulate a program in C for ARM microcontroller to demonstrate case
conversion of characters from upper to lowercase and lower to uppercase.
15 Demonstrate enabling and disabling of Interrupts in ARM.
16 Demonstrate the handling of divide by zero, Invalid Operation and
Overflow exceptions in ARM.
17 VIVA QUESTIONS
18 LIST OF ADDITIONAL PROGRAMS
Microcontrollers Laboratory (BCS402)
MICROCONTOLLERS LABORATORY
INTRODUCTION
MICROCONTROLLER
A small , low cost computer
It is designed to do a specific Task
A highly Integrated chip
Part of Embedded System
In addition to CPU, It includes RAM, ROM, I/O Ports and Timers also.
I/O Ports are GPIO Pins
Microprocessor Microcontroller
Executes big application Executes a single task within an application
High Cost Low cost
Not easy to replace Easy to replace
More power consumption Less power consumption
It doesn’t consist of RAM, ROM, I/O Ports. It uses It consists of CPU, RAM, ROM, I/O Ports.
its pins to interface to peripheral devices.
Application of Microcontrollers:
Light sensing & controlling devices
Temperature sensing and controlling devices
Fire detection & safety devices
Industrial instrumentation devices
Process control devices
Power Tools
Implantable medical devices
Automobile Engine control systems
Remote control appliances, Toys.
Office Machines.
ARM
Advanced RISC Machine
The Project started in the year 1983
Developed by ARM Holdings, a British company. They licensed the ARM architecture to other
companies, who design their own products using this architecture.
Microcontrollers Laboratory (BCS402)
It is a 32-Bit Microcontroller.
Uses RISC (Reduced Instruction Set Computing) Architecture.
It has 32 Bit ALU, 32 Bit data bus.
32 Bit Address bus. Hence 2 pow 32 = 4GB Memory
It uses Von Newman Model in which Program and Data stored in common memory
There are 37 Registers – 32 Bits each (16 Available at a time – R0 to R15)
Based on Load – Store Model. (All RISC processor work on Registers and not not memory)
It has 7 operating modes
It has 7 Interrupts and 7 Addressing modes
Data formats available in ARM are 8 Bit – Byte format, 16 Bit- Half word format and 32 Bit –
Word format
In ARM everything is aligned.
ARM instructions are 3 types
o ARM – When ARM state – 32 Bit instruction
o Thumb When in Thumb state – Purely 16 bit instruction
o Jazelle – For Java Byte code – 8 Bit instructions
Applications:
ARM processor is widely found in
LED TV
Mobile phone, Tablets
Multimedia devices, Gaming devices, ipods, Apple’s Mobile, Raspberry pi 3
Embedded system is the future. Lots of career opportunities available in Embedded System. Some of them are as
follows.
Embedded Software Engineer(Firmware)
System Software Engineer (Kernal and RTOs)
Application Software Engineer (Device drivers)
Embedded Hardware Engineer
Software Test Engineer
Embedded System Trainer
LPC2148
LPC means Linear Programming Control. 2148 is the version of LPC controller.
It is one of the most commonly used ARM based Microcontroller.
It is a product of NXP (A subsidiary of Philips Company).
It is a 32-Bit microcontroller. It is based on ARM7 and uses RISC Technology.
It has Two Input/output port namely P0, P1. Each port has 32 pins. These pins are called GPIO
Pins. Most of the pins are multifunctional.
Its Features:
o 512 KB on-chip Flash Memory. 32 KB on-chip SRAM.
o Supports up to 2KB USB RAM.
o Speed - 60 MHz operation.
KEIL
KeilMicroVision is free software which solves many of the pain points for an embedded program
developer. This software is an integrated development environment (IDE), which integrated a text editor
to write programs, a compiler and it will convert your source code to hex files too. Keil can be used for
Writing programs in C/C++ or Assembly language
Step 7 and 8 are for Hardware Demonstration Programs only. Omit these steps for Software
Assembly and Embedded C programs
Step 7:
Right click on target and click
on options for target
Step 8:
Output Tab: Check Create hex file
The various setting are as
follows: Linker Tab: Check Use Memory layout from Target Dialog
Step 9: Now Click on Build target. You can find it under Project tab or in toolbar. It can also be done by
pressing F7 key.
Step 10:
you can see Status of your
program in Build
output window
[If it’s not there go to view and
click on Build output window]
Microcontrollers Laboratory (BCS402)
ARM PROGRAMMING
We do programming using
Assembly Language
Embedded C Programming Language
Assembly Language:
After machine level language, the next level of development in the evolution of computer languages was
the Assembly Language. Assembly language is a low-level programming language for a computer or
other programmable device. Programs written in assembly languages are compiled by an assembler.
Every assembler has its own assembly language, which is designed for one specific
computer architecture.
The Reasons to write computer instructions in assembly language:
01. To speed computer operation
02. To reduce the size of the program.
03. To write programs for special situations.
04. To save money
05. To better understand how computer operate.
Directives
Directive Remark
The AREA directive allows the programmer to specify the memory locations where
AREA programs, subroutines,
or data will reside.
EQU The EQUATE directive allows the programmer to equate names with addresses or data.
ALIGN
DATA Indicates that the label points to the data rather than code.
ENTRY This directive specifies the program entry point for the linker.
Directive Remark
TTL Title of the Program
END Marks the end of the assembly language source program.
INCLUDE Will include the contents of a named file into the current file.
Directive Remark
Allocates one or more bytes of memory, aligned on 2-byte boundaries, and
DCB
defines the initial runtime contents of the memory
Allocates one or more words of memory, aligned on 2-byte boundaries, and
DCD
defines the initial runtime contents of the memory
Allocates one or more halfwords of memory, aligned on 2-byte boundaries, and
DCW
defines the initial runtime contents of the memory
Earlier, many embedded applications were developed using assembly level programming. However, they did not
provide portability. This disadvantage was overcome by the advent of various high level languages like C, Pascal,
and COBOL. However, it was the C language that got extensive acceptance for embedded systems, and it
continues to do so. The C code written is more reliable, scalable, and portable; and in fact, much easier to
understand.
Differences between C and Embedded C
Used for desktop applications, OS and PC Used for limited resources like RAM, ROM
memories. and I/O peripherals on embedded controller.
SAMPLE PROGRAMS
Data Processing Instructions
1. Mov Instruction
AREA ALP1, CODE, READONLY
ENTRY
MOV R5, #5
MOV R7,#8
MOV R7,R5
STOP B STOP
END
ENTRY
MOV R5,#5
MOV R7, #8
MOV R7, R5, LSL #2
STOP B STOP
END
3. Arithmetic Operations
AREA ALP3, CODE, READONLY
ENTRY
LDR R0,=0x00000000
LDR R1,=0x00000002
LDR R2,=0x00000001
ADDr0,r1,r2
SUB r0,r1,r2
STOP B STOP
END
4. Reverse subtraction
AREA ALP4, CODE, READONLY
ENTRY
LDR R0,=0x00000000
Microcontrollers Laboratory (BCS402)
ENTRY
LDR R0,=0x00000000
LDR R1,=0x00000005
ADD R0, R1, R1, LSL#1
STOP B STOP
END
6. Logical Instructions
AREA ALP6 ,CODE, READONLY
ENTRY
LDR R0,=0x00000000
LDR R1,=0x02040608
LDR R2,=0x10305070
ORR R0, R1, R2
AND R0,R1,R2
EOR R0,R1,R2
STOP B STOP
END
7. MVN Instruction
AREA ALP7 ,CODE, READONLY
ENTRY
LDR R0,=0x0000000A
MVN R1,R0
STOP B STOP
END
ENTRY
Microcontrollers Laboratory (BCS402)
ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
MOVS R0,R1,LSL#1
STOP B STOP
END
ENTRY
LDR R1,=0x00000001
SUBS R1,R1,#1
STOP B STOP
END
ENTRY
LDR R1, = 0Xf
Microcontrollers Laboratory (BCS402)
ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
MOVS R0,R1,LSL#1
MOVCS R0, R1 ;if carry is set then R0:=R1
STOP B STOP
END
ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
MOVS R0,R1,LSL #1
ADDCS R2, R0, R1
STOP B STOP
END
ENTRY
LDR R0, =0x00000004
CMP R0,R1
ADDEQ R2, R0, R1
STOP B STOP
END
Microcontrollers Laboratory (BCS402)
LABORATORY PROGRAMS
1. Using Keil software, observe the various Registers, Dump, CPSR, with a simple Assembly
Language Programs (ALP).
OBJECTIVE:
Get familiarize with KEIL SOFTWARE. In this program we are going to learn about the basic
structure of ARM assembly program and symbols, instructions, directives, variable. In the process we
will learn how to observe the register contents, memory dump, and CPSR.
NOTE:
Users should create a working directory and in that directory project, programs should be stored.
All user defined variables names, label names should be typed starting from Column 1.
Example: Labels like MAIN, STOP, L1, L2, etc., variables like Value1, Value2 should start from
Column 1 (should not have a space before it)
All directives like TTY, AREA, ENTRY, END and instructions like LDR, MOV, STR, ADDS,
SUB etc. should start after one tab space.
Assembly program file should be stored with extension .asm or .s
Always either type in uppercase or lower case but not in mixed case.
PROGRAM:
AREA PGM1, CODE, READONLY Instructs the assembler to assemble a new code
section
ENTRY Marks the beginning of the code section
MAIN Label indicating the starting point (optional)
Load the content of memory location VALUE1 to
LDR R1, VALUE1 register R1.
Load the content of memory location VALUE2 to
LDR R2, VALUE2 register R2
ADDS R1, R2 R1 = R1 + R2 and update the carry flag. Check the
flag in CPSR
LDR R3, =DESTINATION Load the address of DESTINATION memory
STR R1, [R3]
Store the sum R1 to memory location pointed to by
R3.
STOP B STOP
VALUE1 DCD 0X71234567 Assign the value 0X71234567, and 0X9ABCDEF to
VALUE2 DCD 0X9ABCDEF0 memory location VALUE1 and VALUE2
Create a data section to store the result with read-
AREA DATA1, DATA, READWRITE write permission.
DESTINATION DCD 0
END
OUTPUT:
Microcontrollers Laboratory (BCS402)
After Execution, observe the changes in contents of registers involved in the program, CPSR,
and memory contents.
Microcontrollers Laboratory (BCS402)
2. Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical operations
(Demonstrate with the help of a suitable program).
OBJECTIVE:
Get familiarize with KEIL SOFTWARE. In this program we are going to learn about the basic
structure of ARM assembly program and symbols, instructions, directives, variable .In the process we
will learn how to declare variables, data transfer, arithmetic, logic operations.
NOTE:
Users should create a working directory and in that directory project, programs should be stored.
All user defined variables names, label names should be typed starting from Column 1.
Example: Labels like MAIN, STOP, L1, L2, etc., variables like Value1, Value2 should start from
Column 1 (should not have a space before it)
All directives like TTY, AREA, ENTRY, END and instructions like LDR, MOV, STR, ADDS,
SUB etc. should start after one tab space.
Assembly program file should be stored with extension .asm or .s
Always either type in uppercase or lower case but not in mixed case.
PROGRAM:
LDR R1, Value1 ; Loads the contents of memory location Value1 to register R1
LDR R2, Value2
LDR R3, Value3
LDR R4, Value4
LDR R5, Value5
SUB R11,R2,#1
MUL R12,R1,R2
STOP B STOP
;Memory Declarations
END
OUTPUT:
Build the program and fix all the errors.
Go to Debugging session to run the program and view the results.
Perform single step execution and observe the changes in register contents.
Notes:
1. The semicolon indicates a user-supplied comment. Anything following a semicolon on the same
line is ignored by the assembler.
2. The first line is AREA PROGRAM, 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, PROGRAM is user defined name, and
CODE indicates executable code rather than data and READONLY state that it cannot be modified
at run time.
3. Anything starting in column 1 (in this case START) is a label that can be used to refer to that line.
4. The instruction STOP B STOP means ‘Branch to the line labeled STOP’ and is used to create an
infinite loop. This is a convenient way of ending programs in simple examples like these.
5. The last line END is an assemble directive that tells the assembler there is not more code to follow.
It ends the program.
Microcontrollers Laboratory (BCS402)
OBJECTIVE:
To to learn about Bit Number format and MUL instruction. The bit number format is as follows.
PROGRAM:
; TO MULTIPLY TWO 16-BIT NUMBERS
A DCD 0X0000FFFF
Assign the value 0X0000FFFF to memory
B DCD 0X0000FFFF location A
Assign the value 0X0000FFFF to memory
END location
OUTPUT:
Microcontrollers Laboratory (BCS402)
OBJECTIVE:
To learn about the loop and branching instructions.
Branching:
Branching instruction is used to change the control flow. The Branch instruction can be used in two ways,
conditionally and unconditionally. An unconditional branch will always branch no matter what the state of the
flags, For a conditional branch, one of the condition codes is given immediately after B.
Example:
In this program we are going to add numbers from 10 to 1. The total we get will be 55. The total we get in
Hexadecimal format will be in R2 register.
PROGRAM:
ENTRY
START
MOV R1, #10 Load 10 to register R1. R1 is a counter
MOV R2, #0 Empty the register to store result
LOOP
ADD R2, R2, R1 Add the content of R1 with result at R2
SUBS R1, #0x01 Decrement R1 by 1 and update the flags
BNE LOOP BNE results in true value as long as R1 contains non-
zero value in it and takes the control to a line labeled
LOOP. When R1 becomes zero, BNE results in false
value and the loop terminates
STOP B STOP
END
OUTPUT:
Microcontrollers Laboratory (BCS402)
Microcontrollers Laboratory (BCS402)
Example:
Input : Array containing 32 bit numbers
Output : Largest number is stored in location 0x40000000 and also in R2.
Check the result in R2 or memory location 0x40000000.
PROGRAM:
LOOP ; Load the next element to R3, and word align to array element
LDR R3, [R1], #4 compare numbers
If R2 < R3, R3 is considered as highest and move to R2 –
CMP R2, R3 unsigned comparisons
MOVLO R2, R3
decrement counter and update the status flag
loop back till array ends
SUBS R5, R5, #1
stores the result where R4 is pointed to
BNE LOOP
STR R2, [R4]
ARRAY OF 32 BIT NUMBERS(N=7)
STOP B STOP
MYARRAY
DCD 0X44444444
DCD 0X22222222
DCD 0X11111111
DCD 0X33333333
DCD 0XAAAAAAAA
DCD 0X88888888
DCD 0X99999999
to store result in given address
AREA DATA2, DATA, READWRITE Mark end of file
Microcontrollers Laboratory (BCS402)
RESULT DCD 0
END
OUTPUT:
; Load the next element to R3, and word align to array element
CMP R2, R3 compare numbers
MOVHS R2, R3 If R2 >= R3, R3 is considered as highest and move to R2 -
unsigned comparisons
SUBS R5, R5, #1
decrement counter and update the status flag
BNE LOOP
loop back till array ends
STR R2, [R4]
stores the result where R4 is pointed to
STOP B STOP
MYARRAY
ARRAY OF 32 BIT NUMBERS(N=7)
Microcontrollers Laboratory (BCS402)
DCD 0X44444444
DCD 0X22222222
DCD 0X11111111
DCD 0X33333333
DCD 0XAAAAAAAA
DCD 0X88888888
DCD 0X99999999 to store result in given address
Mark end of file
AREA DATA2, DATA, READWRITE
RESULT DCD 0
END
OUTPUT:
Microcontrollers Laboratory (BCS402)
6. Develop an ALP to count the number of ones and zeros in two consecutive memory locations.
OBJECTIVE:
To learn about barrel shift instructions – LSR
To understand how conditional execution simplifies the program development.
To learnt new instructions ADDCS and ADDCC
LSR – LOGICAL SHIFT RIGHT: Right Shifts behave like dividing the contents of a register by 2 s
where s is the shift amount, if you assume the contents of the register are unsigned.
PROGRAM:
STOP B STOP
MYVALUE DCW 0X5ADF
AREA DATA1, DATA, READWRITE
Data section is created with read and write permission to
ONES DCW 0
store number of ones and zeros in memory locations ONES
ZEROS DCW
and ZEROS respectively.
END
Microcontrollers Laboratory (BCS402)
OUTPUT:
Microcontrollers Laboratory (BCS402)
7. Simulate a program in C for ARM microcontroller using KEIL to sort the numbers in
ascending/descending order using bubble sort.
OBJECTIVE:
To gain hands-on experience in analyzing memory usage and register allocation, by observing how
values are assigned to registers and tracking changes in register contents during the sorting process,
PROGRAM:
#include <lpc21xx.h>
int main()
{
unsigned int arr[] = { 5, 1, 4, 2, 8 };
unsigned int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
return 0;
}
OUTPUT:
Step 1: Place a breakpoint for return statement. Place the cursor where breakpoint is needed and click
on Insert/Remove breakpoint as shown in the below snapshot.
Step 2: Build the program
Step 3: Start debugging session.
Step 4: View Watch Windows Watch 1
Step 5: In the watch window, enter the name of the variable and name of the array.
Step 6: Get the address of the required variable. Go to view Memory Windows Memory. Type
the address in memory window address bar.
Step 7: Run the program and observe the contents in the given location.
Microcontrollers Laboratory (BCS402)
Breakpoint:
Watch window
Go to view Memory Windows Memory. Type the address in memory window address bar.
Microcontrollers Laboratory (BCS402)
Microcontrollers Laboratory (BCS402)
OBJECTIVE:
To gain insight into the intricacies of register-level operations and memory management, by observing
how values are assigned to registers and tracking changes in register contents during program
execution.
PROGRAM:
#include <lpc21xx.h>
int main(){
return 0;
}
OUTPUT:
Microcontrollers Laboratory (BCS402)
OBJECTIVE:
To understand the process of memory dump analysis and ASCII representation.
In Keil uVision, when we perform a memory dump and the values are displayed in hexadecimal format,
we can easily see their equivalent ASCII characters by looking at the ASCII representation of each
hexadecimal value.
Typically, in the memory dump window, there will be two columns - one displaying the memory
address and the other displaying the memory contents in hexadecimal format. To see the ASCII
representation:
1. Locate the column displaying the hexadecimal values.
2. For each byte of memory content displayed in hexadecimal, you can refer to the ASCII
representation of that byte by right clicking on it and changing to “ASCII”
PROGRAM:
# include <lpc21xx.h>
char src[ ] = "Hello";
char dest[ ] = "";
void caseConvert(){
unsigned int i;
for (i = 0; src[i]!='\0'; i++){
if(src[i] >= 'a' && src[i] <= 'z') {
dest[i] = src[i] - 32;
}
int main(){
caseConvert();
return 0;
}
Microcontrollers Laboratory (BCS402)
OUTPUT:
VIVA QUESTIONS