0% found this document useful (0 votes)
100 views52 pages

CSE - DS-Microcontrollers Lab Manual-V8.B

Lab manual.

Uploaded by

Kailash S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views52 pages

CSE - DS-Microcontrollers Lab Manual-V8.B

Lab manual.

Uploaded by

Kailash S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

RNS INSTITUTE OF TECHNOLOGY

(AICTE Approved, VTU Affiliated and NAAC‘A+ Grade’ Accredited)


(UG programs – CSE, ECE, ISE, EIE and EEE have been Accredited by NBA up to 30/6/2025)
Channasandra, Dr. Vishnuvardhan Road, Bengaluru - 560 098
Department of Computer Science & Engineering (Data Science)

MICROCONTROLLERS LABORATORY
MANUAL
Course Code: BCS402
Fourth Semester B.E (VTU 2022 Batch)

NAME : .......................................................................................................................................................................................................................................................................................................................................................................................................................................

USN : .....................................................................................................................................................................................................................................................................................................................................................................................................................................................

SECTION : ................................................................................................................................................................... BATCH : ...........................................................................................................................................................................................


VISION AND MISSION OF INSTITUTION
Vision
Building RNSIT into a World Class Institution

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.

We are grateful to Dr. M K Venkatesha, Director, RNSIT, Dr. Ramesh Babu,


Principal, and Dr. Kiran P, Head of CSE department for their constant encouragement
and support. Some portions of the manual have been taken from the Microcontrollers
manual of the CSE department, with the syllabus being the same. The technical help
received from the CSE department faculty is gratefully acknowledged.

Department of CSE(DS)
Microcontroller and Embedded Systems Laboratory (21CS43)

v
Microcontroller and Embedded Systems Laboratory (21CS43)

SYALLABUS
SEMESTER – IV
MICROCONTROLLERS LABORATORY
(2023 Scheme)

Course Code – BCS402 CIE Marks - 25

Number of Contact Hours/Week -3:0:2:0

Total Number of Lab Contact Hours - 26

Course Learning Objectives:


This course will enable students to:
• Develop and test Program using ARM7TDMI/LPC2148
• Conduct the experiments on an ARM7TDMI/LPC2148 evaluation board using
evaluation version of Embedded 'C' & Keil Uvision-4 tool/compiler.

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)

Laboratory Outcomes: The student should be able to:

Practical Sessions need to be assessed by appropriate rubrics and viva-voce method.


This will contribute to 20 marks.

Note: Minimum of 80% of the laboratory components have to be covered.

• 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)

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)

GENERAL BLOCK DIAGRAM:

Page 2
Microcontroller and Embedded Systems Laboratory (21CS43)

PIN DIAGRAM OF ARMTDMIS/LPC2148:

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

2. Logical Shift Left(LSL)


AREA ALP2,CODE,READONLY
ENTRY
LDR R5,#5
LDR 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
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)

8.Load and store instructions


AREA ALP8,CODE,READONLY
ENTRY
LDR R0, =0x40000000
LDR R1,[R0]
LDR R2,=0x40000050
STR R1,[R2]
STOP B STOP
END

9. To check for carry flag c in CPSR


AREA ALP9,CODE,READONLY
ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
MOVS R0,R1,LSL #1
STOP B STOP
END

10.Comparision instruction to check for C,Z flags in CPSR


AREA ALP10,CODE,READONLY
ENTRY
LDR R0, =0x00000002
LDR R1, =0x00000002
CMP R0,R1
STOP B STOP
END

11.Instruction to check for C,Z in CPSR


AREA ALP11,CODE,READONLY
ENTRY
LDR R1, =0x00000001

SUBS R1,R1,#1
STOP B STOP
END

Page 6
Microcontroller and Embedded Systems Laboratory (21CS43)

12. Logical bit clear


AREA ALP12,CODE,READONLY
ENTRY
LDR R1, = 0xF
LDR R2, = 0x5
BIC R0,R1,R2 ; R0 = R1 AND (!R2)
STOP B STOP
END

13. MOVS and MOVCS instructions


AREA ALP13,CODE,READONLY
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

14. ADDCS instruction


AREA ALP9,CODE,READONLY
ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
MOVS R0,R1,LSL #1
ADDCS R2,R0,R1
STOP B STOP
END

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:

1. Click on the KEIL uVision4 Icon to invoke the platform


2. Create a New Project:
(a) Project -> New µVision Project
File Name: {Enter File Name: Example Project-1}
Save As Type: {Choose from the drop down menu: Project Files *.uvproj}
Click : Save

(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:

4. Open a New application file:


File-> New {Enter}
5. Enter the Application program
AREA PGM1, CODE, READONLY
ENTRY
MOV R0, #20
MOV R1, #30
ADD R0,R1; R0 = R0+R1
ADD R2,R0; R2 = R0+R1
STOP B STOP
END
6. Save As: {Enter the file nam : Ex., Experiment-1} [Enter]
- File Type: All Files “.”)
7. Right Click on ‘Source Group 1’ on the left hand bar and choose (“Add Existing Files to Group (source group 1’)
8. A window will appear as follows:

Page 8
Microcontroller and Embedded Systems Laboratory (21CS43)

9. “Choose Assembly Language File” and click OK

10. BUILD Process

BUILD

Page 9
Microcontroller and Embedded Systems Laboratory (21CS43)

11. Execute the program :


Debug-> Start/Stop Debug Session

12. Step-Wise Debug

EXPERIMENT 1:
Using Keil software, observe the various Registers, Dump, CPSR, with a
simple Assembly Language Programs (ALP).
Program Listing:

AREA DUMP, CODE, READONLY


; Experiment-1: Using Keil software, observe the various Registers, Dump, CPSR, with a simple ;
; Assembly Language Programs (ALP).
ENTY
LDR R4, =0x40000000 ;Load Register R4 with Hex Number 0x40000000
MOV R6, #5 ; Move Decimal 5 to Register 6 (as a Counter)
MOV R0, #1 ; Move Register R0 with Decimal 1

LOOP ADD R0, R0, #48 ; Start of Loop, Add Decimal 48 to R0


STR R0, [R4] ; Store R0 value in the Memory Location 0x40000000
; (Default R/W location in KEIL);

ADD R4, R4, #4 ; Increment R4 to R4 + 4


ADD R0, R0, #1 ;Increment R0 to Ro+1
SUBS R6, R6, #1 ; Decrement Counter
BNE LOOP ; Branch to LOOP if R6 is not equal to Zero
STOP B STOP
END

Page 10
Microcontroller and Embedded Systems Laboratory (21CS43)

Program Output:

CPSR Content:

Page 11
Microcontroller and Embedded Systems Laboratory (21CS43)

Memory Content:

To Elaborate and justify in the Lab Record:


• Content of Registers (with respect to decimal values) in each iteration (Iteration1,2,3 ..)

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

• 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)

To Elaborate and justify the Result content in the Lab Record:


• Manual calculation for each step

• Content of Registers (with respect to decimal values)

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

Page 14
Microcontroller and Embedded Systems Laboratory (21CS43)

EXPERIMENT-3
Develop an ALP to multiply two 16-bit binary numbers.
Experiment 3.1:

; PROGRAM TO MULTIPLY TWO 16BIT DECIMAL NUMBERS


; Experiment-3.1: Develop an ALP to multiply two DEcimal numbers.

; Multiplication of two decimal numbers


; VALUE1: 6400 (Decimal)
; VALUE2: 3200 (Decimal)

AREA MULTIPLY, CODE, READONLY

ENTRY
MOV R1, #6400 ; STORE FIRST NUMBER IN R1
MOV R2, #3200 ; STORE SECOND NUMBER IN R2

MUL R3, R1, R2 ; MULTIPLICATION


STOP B STOP

END ; Marks end of file

OUTPUT:

Page 15
Microcontroller and Embedded Systems Laboratory (21CS43)

To Elaborate and justify the Result content in the Lab Record:


• Manual calculation for each step

• Content of Registers (with respect to decimal values)

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

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

AREA MULTIPLY, CODE, READONLY


ENTRY
MOV R5, #2_11111111
MOV R6, #2_11111111

MUL R7, R5, R6 ; MULTIPLICATION


STOP B STOP
END ; Marks end of file

OUTPUT:

CPSR CONTENT:

Page 17
Microcontroller and Embedded Systems Laboratory (21CS43)

To Elaborate and justify the Result content in the Lab Record:


• Manual calculation for each step

• Content of Registers (with respect to decimal values)

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

Page 18
Microcontroller and Embedded Systems Laboratory (21CS43)

EXPERIMENT 3.3
To Multiply two 16-Bit Hexadecimal Numbers

; Experiment 3.3: To Load two 16-Bit Hex values from memory


; and multiply

AREA DUMP, CODE, READONLY


ENTRY
LDR R0, = 0x1122334E ; Load R0 with 0x1122334E
LDR R1, = 0x5566404F ; Load R1 with 0x5566404F
LDR R4, = 0x40000000 ; Load R4 with 0x40000000 (Mem Location)
LDR R5, = 0x40000004 ; Load R5 with 0x40000004 (Mem Location)
STR R0, [R4] ; Store R0 in Location 0x40000000
STR R1, [R5] ; Store R1 in Location 0x40000004

LDRH R6, [R4] ; Load R6 with the LSB 16 Bits of [R4]


LDRH R7, [R5] ; Load R5 with the LSB 16 Bits of [R5]
MUL R8, R6, R7 ; Multiply R6 & R7, Store Result in R8

STOP B STOP
END

OUTPUT:

Page 19
Microcontroller and Embedded Systems Laboratory (21CS43)

CPSR CONTENT:

Memory Content:

To Elaborate and justify the Result content in the Lab Record:


• Manual calculation for each step

• Content of Registers (with respect to decimal values)

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

• 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

MOV R0,#10 ; Move #10 to R0

MOV R1,#00 ; Initiatize R1 to zero

LOOP ; Loop for 10 times till Ro becomes zero

ADD R1,R1,R0 ; Add Ro ro R1 and store the result in R1

SUBS R0,R0,#1 ; Decrement Ro

BNE LOOP ; Move back to loop till R0=0

STOP B STOP

END ; End of Program

OUTPUT

CPSR Content:

Page 21
Microcontroller and Embedded Systems Laboratory (21CS43)

To Elaborate and justify the Result (including Register content) in the


Lab Record:
• Manual calculation for each step

• Content of Registers (with respect to decimal values)

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

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.

AREA SQUARE, CODE, READONLY


ENTRY ; first instruction to execute
MOV R0, #00 ; Initialize R0(Starting address of table)
MOV R3, #00 ; SUM is available in R3 finally.

MOV R1, #10 ; Counter to decrement from 10 downwards


LOOP ; LOOP BACK
LDR R0, =LOOKUP ; Load first element of table to R0

LDRB R2, [R0, R1] ; Get R2 = R1th element in the table


ADD R3, R3, R2; ; Add R2 to R2 and place result in R3
SUBS R1, R1, #1; ; Decrement counter
BNE LOOP ; Loop back till the counter is 0

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:

To Elaborate and justify the Result (including Register content) in the


Lab Record:
• Manual calculation for each step

• Content of Registers (with respect to decimal values)

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

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

LDR R3,[R0],#4 ; R3 is loaded with content of R0 and R0 is incremented

CMP R2,R3 ; R2 & R3 are compared

MOVCC R2, R3 ; If R2<R3, R2 is replaced by R3 (R3 being Higher)

; R2 contains the highest number at any time

; Use MOVCC to find the largest element and MOVCS to find the

; Smallest Element

SUBS R1, R1, #1 ; Decrement R1 counter

CMP R1,#1 ; Continue the Loop till R1 = 1

BNE LOOP ; Goto LOOP if R1 >1

STOP B STOP

TABLE ; Contains the starting address of the TABLE in memory

DCD 0x11111111,0x55555555,0x12345678,0x45673245,0x33333333,0x67543876,

DCD 0xff24521F,0xbcd45432, 0xfe120102, 0xDE321172

END

RESULT:

Page 26
Microcontroller and Embedded Systems Laboratory (21CS43)
R2 contains the Largest Element in TABLE

OUTPUT

CPSR CONTENT:

INCLUDE OUTPUT FOR “LARGEST AND SMALEST” CASES


To Elaborate and justify the Result (including Register content) in the
Lab Record:
• Use your own 10 numbers in TABLE

• 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

• Content of Registers (with respect to decimal values) in the final output

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

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

; No. of 1’s and 0’2

MOV R1,R0 ; Move R0 to R1

MOV R2,#32 ; R2 has #32 (used as a counter)

LOOP

MOVS R1,R1,LSR #1 ; Carry out Logical Left Shift by one bit and store the result

; Back in R2

ADDCS R3,#1 ; number of ones in R3

ADDCC R4,#1 ;number of zeros in R4

SUBS R2,#1 ;Decrement counter

BNE LOOP ; Continue the loop for a count of 32

STOP B STOP

END

Page 28
Microcontroller and Embedded Systems Laboratory (21CS43)

OUTPUT:

CPSR CONTENT:

To Elaborate and justify the Result (including Register content) in the


Lab Record:
• Use your own 32-Bit numbers as input

• Manual calculation of the number of Bits for the first 4 Left Shifts

• Content of Registers including the R3 & R4 Registers

• Content of Stack Pointer, Link Register and Program Counter

• Content of CPSR including the Mode.

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

MOV R2, #0; Counter for Ones;

MOV R3, #0; Counter for Zeros;

MOV R7, #2; Counter to get Two Words

LDR R6,=VALUE; Loads address of VALUE (The start address of the two

values in memory)

LOOP MOV R1, #32; 32-Bit Counter

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)

BHI ONES; If Carry Bit is 1, branch to ONES

ZEROS ADD R3, R3, #1; Increment No. of Zeros by 1

B LOOP1

ONES ADD R2, R2, #1; If Carry Bit is 1, Increment No. of 1's by 1

LOOP1 SUBS R1, R1, #1; Decrement Counter by 1

BNE LOOP0; If not Zero, goto LOOP0

SUBS R7, R7, #1; CMP R7 with ZEROS

BNE LOOP;

BACK B BACK

VALUE DCD 0x11111111, 0xAA55AA55


Page 30
Microcontroller and Embedded Systems Laboratory (21CS43)
END

OUTPUT:

CPSR CONTENT:

To Elaborate and justify the Result (including Register content) in the


Lab Record:
• Use your own TWO 32-Bit numbers as input

• 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)

• Content of Stack Pointer, Link Register and Program Counter


Page 31
Microcontroller and Embedded Systems Laboratory (21CS43)
• Content of CPSR including the Mode.

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:

Step 1: Project New-> New Project

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”.

Click OK. The following window appears:

Page 35
Microcontroller and Embedded Systems Laboratory (21CS43)

Click “YES”

Step 2: File-new : Enter the program

File -> Save as:

File Name: EXPT7.c; Save type as: all Files (*.*)

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)

Click “Add” and click ‘Close’. The following window appears:

Step 4: Select the line “Return 0” and click on “Insert/Remove Break Point (F9)

Page 37
Microcontroller and Embedded Systems Laboratory (21CS43)

Step 5: Build the system

At the bottom window, “0 Errors” will appear

Step 6: Debug Session:

Debug-> Start/Stop Debug Session

Step 7: Get the Starting Address of the “arr” array

View-> Watch Window-> Watch 1

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

The content is shown as [5 1 4 2 8] as the given array (before sort)

Step 8:

Run the program step-wise

Watch the memory content finally changing to the sorted array of [1 2 4 5 8]

Page 39
Microcontroller and Embedded Systems Laboratory (21CS43)

TO ELABORATE IN THE LAB RECORD:

(1) Program Listing with detailed comments for each line


(2) Detailed Procedure starting from creating the project
(3) Choosing the given numbers (different from those in the program listing)
(4) Manual calculation for sorting the array (minimum 3 iterations)
(5) Final Sorted Array
(6) Showing the final sorted array as the actual output (paste the screen shot)

Page 40
Microcontroller and Embedded Systems Laboratory (21CS43)

Experiment 8: Simulate a program in C for ARM microcontroller to find factorial of a number.

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) :

PROGRAM: 8a – Factorial included in the mail function


#include <lpc21xx.h>
int main(){
unsigned int num = 5, i;
unsigned long fact = 1;
for(i = 1; i<=num; i++)
fact = fact * i;
return 0;
}

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:

EXPT 8.B – Factorial using a recursive function

PROGRAM:

#include <lpc21xx.h>
int main(){
int num = 7;
int factorial;

factorial = fact (num);

return 0;
}

//Recursice function to find the factorial of a number

int fact (int y){

if (y == 0)
return 1;
return y*fact (y-1);

OUTPUT: Watch the Registers for the Result:

Page 42
Microcontroller and Embedded Systems Laboratory (21CS43)

Register R5 contains the Factorial of 7 [Decimal 5040, Hex: 0x13B0

EXPERIMENT 9:

Simulate a program in C for ARM microcontroller to demonstrate case conversion of characters


from upper to lowercase and lower to uppercase.

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

You might also like