CSE - DS-Microcontrollers Lab Manual-V8.B
CSE - DS-Microcontrollers Lab Manual-V8.B
MICROCONTROLLERS LABORATORY
MANUAL
Course Code: BCS402
Fourth Semester B.E (VTU 2022 Batch)
NAME : .......................................................................................................................................................................................................................................................................................................................................................................................................................................
USN : .....................................................................................................................................................................................................................................................................................................................................................................................................................................................
Mission
To impart high quality education in Engineering, Technology and Management
with aDifference, Enabling Students to Excel in their Career by
1. Attracting quality Students and preparing them with a strong foundation in fundamentals
so as to achieve distinctions in various walks of life leading to outstanding contributions
2. Imparting value based, need based, choice based and skill based professional education to
the aspiring youth and carving them into disciplined, World class Professionals with social
responsibility
3. Promoting excellence in Teaching, Research and Consultancy that galvanizes academic
consciousness among Faculty and Students
4. Exposing Students to emerging frontiers of knowledge in various domains and make them
suitablefor Industry, Entrepreneurship, Higher studies, and Research & Development
5. Providing freedom of action and choice for all the Stake holders with better visibility
VISION AND MISSION
Of CSE(DS) Department
Vision
Empowering students to solve complex real-time computing problems involving
high volume multi-dimensional data.
Mission
• Provide quality undergraduate education in both theoretical and applied computer
science to solve real world problems.
• Conduct research to develop algorithms that solve complex problems involving multi-
dimensional high volume data through intelligent inferencing.
• Develop good linkages with industry and research organizations within India and abroad
to expose students to global problems and find optimal solutions.
• Creating confident Graduates who can contribute to the nation through high levels of
commitment following ethical practices and with integrity.
PREFACE
The CSE (DS) department has prepared this manual on the Microcontrollers Laboratory course with
the primary intent of reinforcing the learning experienced by the students in the theory course with
practical hands-on experience.
The primary target hardware for this laboratory course is the ARM processor. The entire set of
experiments is designed to be carried out on the KEIL Simulation platform, based on the syllabus
prescribed by VTU. The initial part of the manual discusses on the ARM processor architecture and the
later part elaborates on the assembly language programs. Also covered is the brief procedure on the
usage of the Keil simulation platform.
Departments of CSE
ACKNOWLEDGMENT
A material of this scope would not have been possible without the contribution of
many people. We express our sincere gratitude to Dr. R N Shetty, Chairman, RNS
Group of Companies for his magnanimous support in all our endeavors.
Department of CSE(DS)
Microcontroller and Embedded Systems Laboratory (21CS43)
v
Microcontroller and Embedded Systems Laboratory (21CS43)
SYALLABUS
SEMESTER – IV
MICROCONTROLLERS LABORATORY
(2023 Scheme)
List of Experiments:
Experiments to be conducted on the KEIL simulation platform with LPC2148 as the target
processor.
Expt. Page No.
No.
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.
vi
Microcontroller and Embedded Systems Laboratory (21CS43)
• Rubrics for each Experiment taken average for all Lab components – 15 Marks.
• Viva-Voce– 5 Marks (more emphasized on demonstration topics)
vii
Microcontroller and Embedded Systems Laboratory (21CS43)
INTRODUCTION
LPC2148 Microcontroller
The LPC2148 is a 16 bit or 32-bit ARM7 family-based microcontroller and available in a small
LQFP64 package. ISP (in system programming) or IAP (in application programming) using on-
chip boot loader software. On-chip static RAM is 8 kB-40 kB, on-chip flash memory is 32 kB-
512 kB, the wide interface is 128 bits, or accelerator allows 60 MHz high-speed operation. It takes
400 milliseconds time for erasing the data in full chip and 1 millisecond time for 256 bytes of
programming.
Embedded Trace interfaces and Embedded ICE RT offers real-time debugging with high-speed
tracing of instruction execution and on-chip Real Monitor software. It has 2 kB of endpoint RAM
and USB 2.0 full speed device controller. Furthermore, this microcontroller offers 8kB on-chip
RAM nearby to USB with DMA. One or two 10-bit ADCs offer 6 or 14 analogs i/ps with low
conversion time as 2.44 μs/ channel. Only 10-bit DAC offers changeable analog o/p.
External event counter/32-bit timers-2, PWM unit, & watchdog. Low power RTC (real time
clock) & 32 kHz clock input. Several serial interfaces like two 16C550 UARTs, two I2C-buses
with 400 kbit/s speed. 5 volts tolerant quick general-purpose Input/output pins in a small
LQFP64 package. Outside interrupt pins-21. 60 MHz of utmost CPU CLK-clock obtainable from
the programmable-on-chip phase locked loop by resolving time is 100 μs.
The incorporated oscillator on the chip will work by an exterior crystal that ranges from 1 MHz-
25 MHz. The modes for power-conserving mainly comprise idle & power down. For extra power
optimization, there are individual enable or disable of peripheral functions and peripheral CLK
scaling.
Page 1
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 2
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 3
Microcontroller and Embedded Systems Laboratory (21CS43)
SAMPLE PROGRAMS (To be carried out by the students on their own outside of
the prescribed laboratory hours)
Data Processing Instructions
1. Mov Instruction
AREA ALP1,CODE,READONLY
ENTRY
LDR R5,#5
LDR R7,#8
MOV r7,r5
STOP B STOP
END
3. Arithmetic Operations
AREA ALP3,CODE,READONLY
ENTRY
LDR R0,#0x00000000
LDR R1,#0x00000002
LDR r2,#0x00000001
ADD r0,r1,r2
SUB r0,r1,r2
STOP B STOP
END
4. Reverse subtraction
AREA ALP4,CODE,READONLY
ENTRY
LDR R0,#0x00000000
LDR R1,#0x00000077
Page 4
Microcontroller and Embedded Systems Laboratory (21CS43)
RSB r0,r1,#0 ; RSB subtracts r1 from constant value #0 r0=-r1
STOP B STOP
END
5. Addition with barrel shifter
AREA ALP4,CODE,READONLY
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. Logical Instructions
AREA ALP7,CODE,READONLY
ENTRY
LDR r2, #0x0000000A
MVN r3, r2
STOP B STOP
END
Page 5
Microcontroller and Embedded Systems Laboratory (21CS43)
SUBS R1,R1,#1
STOP B STOP
END
Page 6
Microcontroller and Embedded Systems Laboratory (21CS43)
1. ADDEQ Instruction
AREA ALP9, CODE, READONLY
ENTRY
LDR R0, =0x00000004
LDR R1, =0x00000004
CMP R0,R1
ADDEQ R2,R0,R1
STOP B STOP
END
Page 7
Microcontroller and Embedded Systems Laboratory (21CS43)
LABORATORY PROGRAMS
KEIL Simulation Platform User Guide:
(b) A window appears with “Sepect Device for Target”. Type LPC2148. Click OK
(c) A prompt will ask: “Copy startup.s to project folder and Add File to Project”. Click No.
3. The following UI appears:
Page 8
Microcontroller and Embedded Systems Laboratory (21CS43)
BUILD
Page 9
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 1:
Using Keil software, observe the various Registers, Dump, CPSR, with a
simple Assembly Language Programs (ALP).
Program Listing:
Page 10
Microcontroller and Embedded Systems Laboratory (21CS43)
Program Output:
CPSR Content:
Page 11
Microcontroller and Embedded Systems Laboratory (21CS43)
Memory Content:
• Memory Content
Page 12
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 2:
Develop and simulate ARM ALP for Data Transfer, Arithmetic and
Logical operations (Demonstrate with the help of a suitable program).
PROGRAM LISTING:
AREA Arithmetic,CODE,READONLY
; EXPERIMENT-2: Develop and simulate ARM ALP for Data Transfer,
; Arithmetic and Logical operations
; Compute the function value
; Compute F = 25* ((25 + 8*19 + 7*99 - 27)/4)
ENTRY
MOV r0,#25 ;Load register r0 with A which is 25
MOV r1,#19 ;Load register r1 with B which is 19
MOV r3, #25
ADD r0,r0,r1,LSL #3 ;Add 8 x B to A in r0
MOV r1,#99 ;Load register r1 with C which is 99 (reuse of r1)
MOV r2,#7 ;Load register r2 with 7
MLA r0,r1,r2,r0 ;Add 7 x C to total in r0, r0 = r0 +r1*r2
SUB r0,r0,#27 ;Subtract 27 from the total
MOV r0,r0,LSR #2; Divide by 4
MUL r3, r0, r3;
STOP B STOP
END
PROGERAM OUTPUT:
Page 13
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 14
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT-3
Develop an ALP to multiply two 16-bit binary numbers.
Experiment 3.1:
ENTRY
MOV R1, #6400 ; STORE FIRST NUMBER IN R1
MOV R2, #3200 ; STORE SECOND NUMBER IN R2
OUTPUT:
Page 15
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 3.2
Multiply two 8-Bit Binary Numbers
Page 16
Microcontroller and Embedded Systems Laboratory (21CS43)
Program Listing:
; Multiply two 8-Bit Binary Numbers
; R5=Binary No. 11111111 : 255
; R6 = Binary Number 11111111 : 255
OUTPUT:
CPSR CONTENT:
Page 17
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 18
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 3.3
To Multiply two 16-Bit Hexadecimal Numbers
STOP B STOP
END
OUTPUT:
Page 19
Microcontroller and Embedded Systems Laboratory (21CS43)
CPSR CONTENT:
Memory Content:
• Content of Memory
Page 20
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 4.1:
Write a program to find the sum of first 10 integer numbers
AREA SUM, CODE, READONLY
ENTRY
STOP B STOP
OUTPUT
CPSR Content:
Page 21
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 22
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 23
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 4.2:
Sum of 1 to 10 numbers (in decimal). The numbers are located in a table.
STOP B STOP
; Lookup table contains the numbers from 0 to 10 (in decimal)
LOOKUP
DCB 0,1,2,3,4,5,6,7,8,9,10
END
Page 24
Microcontroller and Embedded Systems Laboratory (21CS43)
OUTPUT
CPSR CONTENT:
Page 25
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT NO. 5
Develop an ALP to find the largest/smallest number in an array of
32 numbers.
; Program to find the largest/smallest number in an array of 32 numbers.
AREA LARGEST,CODE,READONLY
ENTRY
LDR R0, =TABLE ; R0 Register contains the address of the 1st element in the table
MOV R1, #10 ;R1 stores the number of elements in the table (For count down)
LDR R2, [R0], #4 ;R2 is loaded with the contents of R0 (1st value)
; and Ro is incremented by 4
LOOP
; Use MOVCC to find the largest element and MOVCS to find the
; Smallest Element
STOP B STOP
DCD 0x11111111,0x55555555,0x12345678,0x45673245,0x33333333,0x67543876,
END
RESULT:
Page 26
Microcontroller and Embedded Systems Laboratory (21CS43)
R2 contains the Largest Element in TABLE
OUTPUT
CPSR CONTENT:
• Manual calculation for first three steps including the R2 Register value (that has a
higher element) : For both Largest and Smallest numbers
• Calculate R0 for the first three steps: For both Largest and Smallest numbers
Page 27
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT-6
6.1 Develop an ALP to count the number of ones and zeros in a
32-Bit Hex Number
; program to count the number of ones and zeros in a
; 32-Bit Hex Number
AREA COUNT,CODE,READONLY
ENTRY
LDR R0, =0x40000050 ; Load the Register R0 with the Number needed for count of
LOOP
MOVS R1,R1,LSR #1 ; Carry out Logical Left Shift by one bit and store the result
; Back in R2
STOP B STOP
END
Page 28
Microcontroller and Embedded Systems Laboratory (21CS43)
OUTPUT:
CPSR CONTENT:
• Manual calculation of the number of Bits for the first 4 Left Shifts
Page 29
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 6.2:
Develop an ALP to count the number of ones and zeros in two
consecutive memory locations.
AREA ADDCONNUMBERS, CODE, READONLY
ENTRY
START
LDR R6,=VALUE; Loads address of VALUE (The start address of the two
values in memory)
LDR R0, [R6], #4; Get the first 32-bit Value and Increment R6 by 4 Byte
LOOP0 MOVS R0, R0, ROR #1; Logical Right Shift to check Carry Bit (1's/0's)
B LOOP1
ONES ADD R2, R2, #1; If Carry Bit is 1, Increment No. of 1's by 1
BNE LOOP;
BACK B BACK
OUTPUT:
CPSR CONTENT:
• Manual calculation of the number of Bits for the first 4 Right Shifts
• Manual Calculation of the No. of 1’s and No. of 0’s in the two 32-Bit Numbers
• Content of Registers including the R2 & R3 Registers (No. of 1’s and 0’s)
Page 32
Microcontroller and Embedded Systems Laboratory (21CS43)
Experiment 7:
Simulate a program in C for ARM microcontroller using KEIL to sort the numbers in
ascending/descending order using bubble sort.
#include <lpc21xx.h>
void swap(unsigned int* arr, unsigned int i, unsigned int j)
{
unsigned int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// function to implement bubble sort
void bubbleSort(unsigned int arr[], unsigned int n)
{
unsigned int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr, j, j + 1);
}
int main()
{
unsigned int arr[] = { 5, 1, 4, 2, 8 };
unsigned int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
return 0;
}
Page 33
Microcontroller and Embedded Systems Laboratory (21CS43)
Procedure:
File name: EXPT7, Save type as : Project Files (*.uvproj) and click save
Page 34
Microcontroller and Embedded Systems Laboratory (21CS43)
Once you click save, the following table appears. Choose CPU type as “LPC2148”.
Page 35
Microcontroller and Embedded Systems Laboratory (21CS43)
Click “YES”
Step 3: Right Click on “Source Group 1” and select “Add Existing Files to ‘Source Group 1’
Page 36
Microcontroller and Embedded Systems Laboratory (21CS43)
Step 4: Select the line “Return 0” and click on “Insert/Remove Break Point (F9)
Page 37
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 38
Microcontroller and Embedded Systems Laboratory (21CS43)
In the watch window, Note the memory address of the array “err” (It will show as 0x40000444)
Step 7: Choose “memory” and select “memory 1” and enter the address as 0x40000444
Step 8:
Page 39
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 40
Microcontroller and Embedded Systems Laboratory (21CS43)
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.
EXPT 8(a) :
Procedure:
(1) New project creation, entering the program, saving it as factorial.c : same as experiment 7.
(2) Just before Build: Select the breakpoint as in 7 for the “return 0” line.
(3) Build the project
(4) Click on : Start/Stop Debug Mode
(5) Click on view->watch windows-> watch 1
(6) Execute the program step-wise
(7) The factorial value is available in the “ fact” location in watch 1 window.
Page 41
Microcontroller and Embedded Systems Laboratory (21CS43)
OUTPUT:
PROGRAM:
#include <lpc21xx.h>
int main(){
int num = 7;
int factorial;
return 0;
}
if (y == 0)
return 1;
return y*fact (y-1);
Page 42
Microcontroller and Embedded Systems Laboratory (21CS43)
EXPERIMENT 9:
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”
Page 43
Microcontroller and Embedded Systems Laboratory (21CS43)
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;
}
if(src[i] >= 'A' && src[i] <= 'Z') {
dest[i] = src[i] + 32;
}
}
}
int main(){
caseConvert();
return 0;
}
Page 44
Microcontroller and Embedded Systems Laboratory (21CS43)
Page 45