0% found this document useful (0 votes)
26 views

MCES Lab Manual

Uploaded by

Aditya Sakalekar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

MCES Lab Manual

Uploaded by

Aditya Sakalekar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

MCES Lab (19IS506) - Lab Manual 2021-22

Department of Information Science & Engineering

MICROCONTROLLER AND EMBEDDED


SYSTEMS LAB MANUAL
(19IS506)

Prepared by

Abhishek S. Rao
Asst. Professor, Dept. of IS&E

for

B.E 5th Semester IS&E


2021 - 22

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 1


MCES Lab (19IS506) - Lab Manual 2021-22

MICROCONTROLLER & EMBEDDED SYSTEMS LAB

Sub Code: 19IS506 Credits: 1


Hrs/Week: 0+0+2+0 Total Hours: 26

Course Learning Objectives:


This course will enable students:

1. To gain an understanding of the design and operation of the ARM7 TDMI microcontroller.
2. To explore how different ARM instructions are used in assembly language programs.
3. To design simple ARM assembly language programs using KEIL IDE software.
4. To design ARM processor-based devices and interfaces.
5. To gain a better understanding of embedded systems and their applications.

Course Content:
Introductory Programs
Sl. Programs Page
No. No.
1. Write a program to demonstrate data transfer instructions. 19
2. Write a program to demonstrate arithmetic instructions 19
3. Write a program to demonstrate logical and shift/rotate instructions 19
4. Write a program to multiply two 16-bit binary numbers 20

Part A Software Programs


Sl. Programs Page
No. No.
1. Write a program to find the sum of the first 10 integer numbers. 22
2. Write a program to find the largest/smallest number in an array of 32 numbers. 24
3. Write a program to find the factorial of a number. 27
4. Write a program to add an array of 16-bit numbers and store the 32-bit result 30
in internal RAM.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 2


MCES Lab (19IS506) - Lab Manual 2021-22
5. Write a program to find the square of a number (1 to 10) using a look-up table. 33
6. Write a program to arrange a series of 32-bit numbers in 36
ascending/descending order.
7. Write a program to count the number of ones and zeros in two consecutive 39
memory locations.
Part B Hardware Programs
8. Display “Hello World” message using Internal UART. 42
9. Interface and Control a DC Motor. 43
10. Interface a Stepper motor and rotate it in the clockwise and anti-clockwise 44
direction.
11. Determine Digital output for a given Analog input using Internal ADC of 46
ARM controller.
12. Interface a 4x4 keyboard and display the key code on an LCD. 48
13. Display the Hex digits 0 to F on a 7-segment LED interface, with an 52
appropriate delay in between.
14. Demonstrate the use of an external interrupt to toggle an LED on / Off. 53
Sample VIVA Questions 55

Course Outcomes:
Students will be able to:

Sl. No. Course Outcome Blooms


Taxonomy
Level
CO1 Identify the architectural characteristics of LPC2148 L2
microcontrollers.
CO2 Inspect ARM's instruction set as well as its development tools. L4
CO3 Develop and test programs on arithmetic, logic, and memory L3
mapping operations using ARM7TDMI/LPC2148.
CO4 Analyze the hardware and peripheral devices that interface with the L4
LPC2148.
Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 3
MCES Lab (19IS506) - Lab Manual 2021-22
CO5 Demonstrate the experiments on interfacing the motors,7-segment L4
LED display and LCD, ADC on ARM7TDMI/LPC2148 evaluation
board using an evaluation version of Embedded 'C' & Keil tool.

Mapping of POs & COs


POs/ PO PO PO PO PO PO PO PO PO PO PO PO PSO PSO
COs 1 2 3 4 5 6 7 8 9 10 11 12 1 2
CO1 2 2 1 1 1 1
CO2 2 2 1 1 1 1
CO3 2 3 2 2 1 1 1 2
CO4 2 3 3 1 2 1 1 1 2
CO5 2 3 3 1 2 1 1 1 2
(L/1 = Low 30%-49%, M/2 = Medium 50%-69%, H/3 = High >70%)

TEXTBOOK:
1. ARM system developers guide, Andrew N Sloss, Dominic Symes, and Chris Wright,
Elsevier, Morgan Kaufman Publishers, 2008.
2. Shibu K V, “Introduction to Embedded Systems”, Tata McGraw Hill Education, Private
Limited, 2nd Edition.

REFERENCE BOOKS:
1. Raghunandan G.H, Microcontroller (ARM) and Embedded System, Cengage learning
Publication, 2019.

E-RESOURCES
1. https://www.udemy.com/course/arm-processor-a-to-z-introduction-part-1/, ARM Processor A
to Z (Introduction, Part 1), (Udemy).
2. https://www.mooc-list.com/course/introduction-arm-ost, Introduction to ARM (OST),
(MooC).

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 4


MCES Lab (19IS506) - Lab Manual 2021-22

INSTRUCTIONS TO THE STUDENTS

1. This laboratory manual is an interim record of the sample experiments conducted


on programming concepts during the regular laboratory sessions under the above
said subject code.
2. Students should come with through preparation for the programs to be developed.
3. Students will not be permitted to attend the laboratory unless they bring the
practical record and observation fully completed in all respects pertaining to the
experiment conducted in the previous class.
4. Students are supposed to wear their college ID cards before attending the lab.
5. Students are supposed to occupy the systems allotted to them and are not
supposedto talk or make noise in the lab.
6. The students are expected to execute / debug the code to fulfill the aim of the
experiment.
7. The students need to test the written programs with the varieties of inputs
otherthan the sample inputs and display the output in the record book.
8. Practical record and observation book should be neatly maintained.
9. Students should obtain the signature of the staff-in-charge / Co – in-charge in
theobservation book after completing each program.

Head-ISE

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 5


MCES Lab (19IS506) - Lab Manual 2021-22

A Microcontroller is a compact integrated circuit designed to govern a specific


operation in an embedded system.
A typical microcontroller includes a processor, memory, and input/output (I/O)
peripherals on a single chip.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 6


MCES Lab (19IS506) - Lab Manual 2021-22

ARM7 LPC2148 MICROCONTROLLER

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 7


MCES Lab (19IS506) - Lab Manual 2021-22

USER MANUAL FOR KEIL μVISION4 SOFTWARE

INTRODUCTION TO KEIL SOFTWARE

Keil MicroVision is a 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.

The Keil Software Flow:


The menu bar provides you with manus for editor operations, project maintenance, development
tool option settings, program debugging, external tool control, window selection and manipulation,
and on-line help.
The toolbar button allows you to rapidly execute uVision commands.
A status bar provides editor and debugger information.
The various toolbars and the status bar can be enabled or disabled from the view menu commands.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 8


MCES Lab (19IS506) - Lab Manual 2021-22

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 9


MCES Lab (19IS506) - Lab Manual 2021-22

Here is simple guide to start working with Keil uVision which can be used for

 Writing programs in C/C++ or Assembly language

 Compiling and Assembling Programs

 Debugging program

 Creating Hex and Axf file

 Testing your program without Available real Hardware (Simulator Mode)

This is simple guide on Keil uVision 4 though also applicable on previous versions also.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 10


MCES Lab (19IS506) - Lab Manual 2021-22
Step 1: After opening Keil uV4, Go to Project tab and
Create new uVision project
Now Select new folder and give name to Project.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 11


MCES Lab (19IS506) - Lab Manual 2021-22
Step 2: After Creating project now Select your device model. Example.NXP-LPC2148

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 12


MCES Lab (19IS506) - Lab Manual 2021-22
Step 3: so now your project is created, and Message window will appear to add startup file of your
Device click on Yes so it will be added to your project folder.

Step 4: Now go to File and create new file and save it with .C extension if you will write program
in C language or save with .asm for assembly language.

Step 5: Now write your program and save it again. You can try example given at end of this
tutorial.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 13


MCES Lab (19IS506) - Lab Manual 2021-22
Step 6: After that on left you see project window [if it’s not there…. go to View tab and click on
project window]

Right click on target and click on options for target


Here you can change your device also.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 14


MCES Lab (19IS506) - Lab Manual 2021-22

Click output tab here & check create Hex file if you want to generate hex file. Now click on ok so
it will save changes.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 15


MCES Lab (19IS506) - Lab Manual 2021-22

Step 7: Now Expand target and you will see source group
Right click on group and click on Add files to source group

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 16


MCES Lab (19IS506) - Lab Manual 2021-22
Now add your program file which you have written in C/assembly.

You can see program file added under source group.

Step 8: 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 9: 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]

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 17


MCES Lab (19IS506) - Lab Manual 2021-22

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 18


MCES Lab (19IS506) - Lab Manual 2021-22

INTRODUCTORY PROGRAMS

1. Write a program to demonstrate Data Transfer Operations

TTL ARM IN ALP FOR DATA TRANSFER OPERATIONS


AREA PROG, CODE, READONLY
ENTRY
START
MOV r0, #1 ; r0 = 0x00000001
MOV r1, #0x21 ; r1 = 0x00000021
MOV r2, #21 ; r2 = 0x00000015
MOV r3, r1 ; r3 = r1 = 0x00000021
MVN r0, #0xff ; r0 = 0xffffff00, move the NOT of 32-bit value
MVN r0, r3 ; r0 = ~r3 = 0xffffffde
LDR r0, [r1] ; load r0 with the contents of the memory address pointed by r1
STR r0, [r1] ; store the contents of r0 to the memory address pointed by r1
STOP B STOP
END

2. Write a program to demonstrate Arithmetic Operations

TTL ARM IN ALP FOR ARITHMETIC OPERATIONS


AREA PROG, CODE, READONLY
ENTRY
START
MOV r0, #0 ; r0 = 0x00000000
MOV r1, #2 ; r1 = 0x00000002
MOV r2, #1 ; r2 = 0x00000001
MOV r3, #3 ; r3 = 0x00000003
ADD r0, r1, r2 ; r0 = r1 + r2 = 0x00000003
SUB r0, r1, r2 ; r0 = r1 – r2 = 0x00000001
RSB r0, r1, #0 ; r0 = 0 – r1 = 0xfffffffe
MUL r0, r1, r2 ; r0 = r1 * r2 = 0x00000002
MLA r0, r1, r2, r3 ; r0 = (r1 * r2) + r3 = 0x00000005
STOP B STOP
END

3. Write a program to demonstrate Logical and Shift/Rotate Operations

TTL ARM IN ALP FOR LOGICAL OPERATIONS


AREA PROG, CODE, READONLY
ENTRY
START
MOV r0, #0 ; r0 = 0x00000000
Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 19
MCES Lab (19IS506) - Lab Manual 2021-22
MOV r1, #0x00000004 ; r1 = 0x00000004
MOV r2, #0x00000006 ; r2 = 0x00000006
ORR r0, r1, r2 ; r0 = 0x00000006 (logical bitwise OR)
AND r0, r1, r2 ; r0 = 0x00000004 (logical bitwise AND)
EOR r0, r1, r2 ; r0 = 0x00000002 (logical bitwise XOR)
BIC r0, r1, r2 ; r0 = 0x00000000 (logical bit clear (R1 AND NOT (R2)))

; Illustration of SHIFT/ROTATE Instructions */

MOV r5, #5 ; r5 = 0x00000005


MOV r6, #4 ; r6 = 0x00000004
MOV r7, #8 ; r7 = 0x00000008
MOV r8, #0x80000004 ; r8 = 0x80000004
MOV r0, r7, LSL #2 ; r0 = 0x00000020, logical shift left by 2
MOV r0, r7, LSR #2 ; r0 = 0x00000002, logical shift right by 2
MOV r0, r8, ASR #2 ; r0 = 0xe0000001, arithmetic shift right by 2
MOV r0, r7, ROR #2 ; r0 = 0x00000002, rotate right by 2
MOV r0, r7, ROR r4 ; r0 = 0x00000008, rotate right by r4
MOV r0, r7, ROR r5 ; r0 = 0x40000000, rotate right by r5
MOV r7, r5, LSL #2 ; r7 = r5 * 4 = (r5 << 2) = 0x00000014
STOP B STOP
END

4. Write a program to multiply two 16-bit binary numbers.

Algorithm:
Two numbers are stored in DATA memory using DCW (Define Constant Word) directive.
* Step 1: Get the first number in R1 register.
* Step 2: Get the second number in R2 register.
* Step 3: Multiply the two 16-bit numbers, and save the result in R0

TTL 16-BIT MULTIPLICATION


AREA PROG1, CODE, READONLY
ENTRY
START
LDRH R1, Num1 ; load the first number.
LDRH R2, Num2 ; load the second number.
MUL R0, R1, R2 ; multiply them together into R0 (R0=R1*R2).
STOP B STOP
AREA DATA1, DATA, READONLY
Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 20
MCES Lab (19IS506) - Lab Manual 2021-22
Num1 DCW 0xC123 ; first value to be multiplied.
Num2 DCW 0x02AA ; second value to be multiplied.
ALIGN
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 21


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 1: Write a program to find the sum of the first 10 integer numbers.

Algorithm:
Initially, Sum is set to 0 in DATA memory using DCD (Define Constant Data) directive.
* Step 1: Load the initial value of Sum into register R0. Let R1 be the iterator & initial Sum value
0 be in register R2.
* Step 2: Check iterator R1 to the maximum value 10; and if it is not 10, add each number and save
to R2.
* Step 3: Repeat Step2; until iterator R1 becomes 10; and if it is 10, store the final Sum value.

TTL INTEGER SUM


AREA PROG2, CODE, READONLY
ENTRY
START
LDR R0,=SUM
MOV R1,#1 ;Loop iterator.
MOV R2,#0 ;Initial Sum = 0.
NEXT CMP R1,#10 ;repeat for summing first 10 numbers.
BEQ STORE
ADC R2,R2,R1 ;add each number and save to R2.
ADD R1,R1,#1 ;increment loop iterator.
LOOP B NEXT ;repeat for summing next number.
STORE STR R2,[R0] ;store the final Sum.
SUM DCD 0
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 22


MCES Lab (19IS506) - Lab Manual 2021-22
Output:

1+2+3+4+5+6+7+8+9+10=2D

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 23


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 2: Write a program to find the largest/smallest number in an array of 32 numbers.

Algorithm:
Array of elements is stored in DATA memory using DCD (Define Constant Data) directives.
Location (LS) is reserved in DATA memory using DCD (Define Constant Data) directive to store
the largest/ smallest number.

* Step 1: Load the first element of the array into R3 and increment the pointer, load the second
element of the array into R4 and increment the pointer; then compare.
* Step 2: If second element is greater, copy that into first register, R3.
* Step 3: Decrement the counter, repeat step 1 and 2 until completion of Array.
* Step 4: Save the final largest element (R3) into memory location.

TTL LARGEST/SMALLEST NUMBER


AREA PROG3,CODE,READONLY
ENTRY

START
LDR R0,=ARRAY ;to point base of Array.
MOV R1,#9 ;length of the Array.
LDR R2,=LS ;point to result location.
LDR R3, [R0],#04 ;load first number and increment address by 4.
UP LDR R4, [R0],#04 ;load second number and increment address by 4.
CMP R3,R4 ;compare the two numbers.
MOVLS R3,R4 ;Largest-LS:unsigned lower or same; Smallest-HI:unsigned higher
SUB R1,R1,#1 ;decrement the counter.
CMP R1,#0 ;check for end of Array.
BNE UP
LDR R0,=LS ;get the address of memory location to store largest
STR R3,[R0] ;save the largest to memory location.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 24


MCES Lab (19IS506) - Lab Manual 2021-22
STOP B STOP

AREA SRC, DATA, READONLY


ARRAY DCD 0x33333333, 0x22222222, 0x55555555, 0x11111111, 0x44444444, 0x77777777,
0xFFFFFFFF, 0x99999999, 0x88888888, 0x45678912
ALIGN

AREA DST, DATA


LS DCD 0
ALIGN
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 25


MCES Lab (19IS506) - Lab Manual 2021-22
Output:

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 26


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 3: Write a program to find the factorial of a number.

Algorithm:
Number (Num) whose factorial is to be calculated is stored in DATA memory using DCW (Define
Constant Word) directive.

* Step 1: Get the first Num in R0 register & make a copy into R1 register.
* Step 2: Check whether the Num is 2; and if 2, then make the factorial value 1, and if Num is less
than 1 then Stop.
* Step 3: If Num is greater than 2; subtract one from the Num and multiply Num with the Num-1
value, until Num becomes equal to 1.
* Step 4: If Num becomes equal to 1, then Stop.

TTL FACTORIAL OF A NUMBER


AREA PROG4, CODE, READONLY
ENTRY
START
LDR R0,NUM ;load the value into R0.
MOV R1,R0 ;make a copy.
CMP R0, #02 ;check whether the value is less than 2.
MOV R2, #01 ;if less than 2, then make factorial value 1.
BLT STOP ;if value is less than 1 then Stop.
REPEAT
SUBS R1,R1,#01 ;subtract the number by 1.
CMP R1,#01 ;compare whether the number is 1.
BEQ STOP ;if value is equal to 1 then Stop.
MUL R2,R0,R1 ;multiply to get factorial
MOV R0, R2 ;store result, R0 has the final value.
B REPEAT
STOP B STOP

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 27


MCES Lab (19IS506) - Lab Manual 2021-22
AREA DATA3, DATA, READONLY
NUM DCW 0x5 ;Number whose factorial is to be calculated.
ALIGN
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 28


MCES Lab (19IS506) - Lab Manual 2021-22

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 29


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 4: Write a program to add an array of 16-bit numbers and store the 32-bit result in internal
RAM.

Algorithm:
Array of 10 elements is Sum is stored in DATA memory using DCW (Define Constant Word)
directive & Sum is set to 0 in DATA memory using DCD (Define Constant Data) directive.

* Step 1: Load Array address to R0 & Sum address to R5. Let R1 be the iterator & initial Sum in
R4 is 0.
* Step 2: Load the first Array number to R3, add with carry (if any), increment the loop iterator.
* Step 3: Repeat Step2, until the iterator becomes 10.
* Step 4: If all numbers added, save the result to Sum location from R4.

TTL 16-BIT ADDITION


AREA PROG5, CODE, READONLY
ENTRY
START
LDR R0,=ARRAY ;load array address and sum
LDR R5,=SUM
MOV R1,#0 ;Loop Iterator
MOV R4,#0 ;Initial Sum =0
NEXT CMP R1,#10 ;load total numbers in array, say 10.
BEQ STORE
LDRH R3, [R0],#2 ;load the number from array and point to next word (16-bit
number).
ADC R4,R4,R3 ;add & update carry if any.
ADD R1,R1,#1 ;increment loop iterator, stop if reached 10.
LOOP B NEXT
STORE STR R4,[R5]
STOP B STOP

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 30


MCES Lab (19IS506) - Lab Manual 2021-22
AREA Data4, DATA, READONLY
ARRAY DCW 0x0011,0x0022,0x0033,0x0044,0x0055,0x0066,0x0077,0x0088,0x0099
,0x00AA
ALIGN
AREA Data41, DATA
SUM DCD 0
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 31


MCES Lab (19IS506) - Lab Manual 2021-22
Output:

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 32


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 5: Write a program to find the square of a number (1 to 10) using a look-up table.

Algorithm:
Array of 10 elements (lookup table) & the Number/ Value whose square is to be retrieved from the
lookup table are stored in DATA memory using DCD (Define Constant Data) directives. Result is
set to 0 in DATA memory using DCD (Define Constant Data) directive.

* Step 1: Load the address of lookup table to R0, Value to R1, and address of Result to R2.
* Step 2: Based on the Value (R1), adjust the point location where square is stored. Adjust is
required for 32-bit (4 bytes) data.
* Step 3: Get the result from lookup table memory location to register R3 & save into memory
location ‘Result’

TTL SQUARE OF A NUMBER


AREA PROG6,CODE,READONLY
ENTRY

START
LDR R0, =Lookup ;to point base of lookup table.
LDR R1, Value ;value whose square is to be retrieved from lookup table.
LDR R2, =Result ;point to result.
MOV R1, R1, LSL #0x2 ;adjust to point location where square is stored in look -
up table.
ADD R0, R0, R1 ;adjust is required for 32-bit data (4 bytes).
LDR R3, [R0] ;get result from memory location (lookup table) to R3.
STR R3, [R2] ;store result.
STOP B STOP

AREA SRC, DATA, READONLY

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 33


MCES Lab (19IS506) - Lab Manual 2021-22
Lookup DCD 0,1,4,9,16,25,36,49,64,81,100 ;Lookup table contains Squares of Nos from 0 to
10.
Value DCD 0x00000009 ;Number whose square is to be retrieved from
lookup table.
ALIGN

AREA DST, DATA


Result DCD 0x00000000 ;memory location to save the result.
ALIGN
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 34


MCES Lab (19IS506) - Lab Manual 2021-22
Output:

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 35


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 6: Write a program to arrange a series of 32-bit numbers in ascending/descending order.

Algorithm:
Array of elements is stored in DATA memory using DCD (Define Constant Data) directives. Some
10 locations are reserved in DATA memory to store the Array in ascending/ descending order.

* Step 1: Copy the elements of Array elements from READONLY DATA memory to the memory
section.
* Step 2: Initialize the counter, flag, and the address of the memory.
* Step 3: Load the first and second numbers into register, compare, and (if required) swap the
values in memory; set the flag.
* Step 4: Decrement the counter and repeat Step 3 if counter is not zero.
* Step 5: Now, check all flags for zero; if not, repeat Steps 2, 3, and 4.

TTL ASCENDING/ DESCENDING


AREA PROG7, CODE, READONLY
ENTRY
START
MOV R1,#4 ;initialize counter to 4(i.e. number of elements are 4).
LDR R2, =Array ;load the address of the first element of the Array.
LDR R3, =DST ;load the address of the first element position of destination.
UP LDR R4, [R2], #4 ;load the first element of Array into R4.
STR R4, [R3], #4 ;store the first element to the destination region.
SUBS R1, R1, #1 ;decrement the counter.
CMP R1, #0 ;compare the counter to 0.
BNE UP ;loop back till Array ends.
MOV R7, #3 ;(NO OF PASS IS 3)
PASS
MOV R1, #3 ;(NO OF INNER COMPARISSION IS 3)
LDR R0, =DST ;load the address of the first element of the memory.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 36


MCES Lab (19IS506) - Lab Manual 2021-22
COMP LDR R2, [R0], #4 ;load the first number.
LDR R3, [R0] ;load the second number.
CMP R2, R3 ;compare two numbers.
BLS CHECK ;if the first number is < then go to Loop2.
STR R2, [R0],#-4 ;Interchange the position of first and second numbers.
STR R3,[R0]
ADD R0, #4 ;restore the pointer
CHECK
SUBS R1, R1, #1 ;decrement the counter.
CMP R1, #0 ;compare counter to 0.
BNE Comp ;Loop back till Array ends.
SUBS R7, R7, #1
CMP R7, #0 ;check the flag.
BNE Pass ;if flag is not zero, then go to Initial.
STOP B STOP
AREA SRC, DATA, READONLY
Array DCD 0x33333333, 0x22222222, 0x99999999, 0x55555555
ALIGN
AREA DST, DATA
SPACE 10
ALIGN
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 37


MCES Lab (19IS506) - Lab Manual 2021-22
Output:

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 38


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 7: Write a program to count the number of ones and zeros in two consecutive memory
locations.

Algorithm:
The two 8-bit numbers whose number of ones and zeros is to be calculated are stored in DATA
memory using DCB (Define Constant Byte) directives.

* Step 1: Load the address of the first number, clear the contents of two registers to hold the result,
initialize counter for two consecutive memory locations.
* Step 2: Initialize the counter for 8-bits of a number, load the first number to R3, test for 1’s, and
increment the register (r6 or R5) accordingly. Check all the 8-bits of a number.
* Step 3: Access the second number and repeat Step 2.
* Step 4: Save the values of ones and zeros to memory location.

TTL NUMBER OF ONES & ZEROS


AREA PROG8, CODE, READONLY
ENTRY
START LDR R0, =SRC ;load the first num address.
EOR R5,R5,R5 ;R5 and R6 are used to save result.
EOR R6,R6,R6 ;Initially, R5 = R6 = 0.
MOV R2, #2 ;two 8-bit numbers (consecutive memory locations) to check.
Up MOV R1, #8 ;each number is of 8-bit size.
LDRB R3, [R0] ;load the first 8-bit number to R3.
Top TST R3, #01 ;test for 1's.
BEQ Inc_Zero
ADD R6, #1 ;increment ones count.
B Down
Inc_Zero ADD R5, #1 ;increment zeros count.
Down LSR R3, #1 ;logical shift right.
SUB R1, #1 ;decrement the bit counter.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 39


MCES Lab (19IS506) - Lab Manual 2021-22
CMP R1, #0 ;check all the bits.
BNE Top
ADD R0, #1 ;access the second 8-bit number.
SUB R2, #1 ;last memory location to check.
CMP R2, #0 ;repeat checking ones & zeros for the second location.
BNE Up
LDR R0, =ONES ;address of the memory location to save number of ones.
STRB R6, [R0] ;R6 holds the number of ones.
LDR R0, =ZEROS ;address of the memory location to save number of zeros.
STRB R5, [R0] ;R6 holds the number of zeros.
STOP B STOP
AREA SRC,DATA,READONLY
DCB 0x01, 0x0F ;two 8-bit numbers in consecutive memory locations.
AREA DST, DATA, READWRITE
ONES DCB 0 ;to hold number of ones.
ZEROS DCB 0 ;to hold number of zeros.
END

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 40


MCES Lab (19IS506) - Lab Manual 2021-22

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 41


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 8: Display “Hello World” message using Internal UART.

Procedure: Download program to ARM Kit and use Flash Magic go to TOOLS->Terminal and
then click hyper terminal and press enter then “Hello World “will be displayed on terminal.

#include <LPC214X.H>
#include <stdint.h>
#include <stdio.h>
void UART0_init(void);
unsigned int delay;
unsigned char *ptr;
unsigned char arr[]="HELLO WORLD \n";
void UART0_init()
{
PINSEL0 = PINSEL0 | 0x00000005;
/* Enable UART0 Rx0 and Tx0 pins of UART0 */
U0LCR = 0x83; /* DLAB = 1, 1 stop bit, 8-bit character length */
U0DLM = 0x00; /* For baud rate of 9600 with Pclk = 15MHz */
U0DLL = 0x61; /* We get these values of U0DLL and U0DLM from formula */
U0LCR = 0x03; /* DLAB = 0 */
}
char sendchar (char sdat)
{
while (!(U0LSR & 0X20));
return (U0THR = sdat);
}
int main(void)
{
while(1)
{
UART0_init();
ptr=arr;
sendchar(0X0d);
sendchar (0X0A);
while(*ptr!='\0')
{
U0THR=*ptr;
ptr++;
while((U0LSR & 0X40)==0);
for(delay=0;delay<=6000; delay++);
}
for(delay=0;delay<=600000; delay++);
}
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 42


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 9: To Interface and control a DC Motor

Procedure: Download program to ARM-KIT and connect DC Motor Module to FRC 1.

# include<lpc214x.h>
void clock_wise(void);
void anti_clock_wise(void);
void delay(unsigned int val);
unsigned int j=0;
int main()
{
IO0DIR = 0X00090000;
IO0SET = 0X00010000;
while(1)
{
clock_wise();
delay(2000);
anti_clock_wise();
delay(2000);
IO0CLR = 0X00090000;
}
}

void clock_wise(void)
{
IO0CLR = 0X00090000;
delay(100);
IO0SET = 0X00080000;
}

void anti_clock_wise(void)
{
IO0CLR = 0X00090000;
delay(100);
IO0SET = 0X00010000;
}
void delay(unsigned int val)
{
unsigned int i,j;
for(i=0;i<val;i++)
{
for(j=0;j<3000;j++);
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 43


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 10: Interface a Stepper motor and rotate it in the clockwise and anti-clockwise direction.

Procedure: Connect Stepper Motor to FRC1 and the rotation starts clockwise followed by anti-
clockwise rotations.

#include<LPC214X.H>

unsigned int CLOCK[4] = {0x00030000,0x00090000,0x000C0000,0x00060000};


unsigned int ANTI_CLOCK[4] = {0x00060000,0x000C0000,0x00090000,0x00030000};

void clockwise(int speed, int rev);


void anticlockwise(int speed, int rev);
void delay(int);

int main(void)
{
IO0DIR = 0x00FF0000;
while(1)
{
clockwise(10, 100);
anticlockwise(10, 100);
delay(100);
}
}

void clockwise(int speed, int rev)


{
unsigned char I,J;
for(J=0;J<rev;J++)
{
for(I=0;I<4;I++)
{
IO0SET |= CLOCK[I];
IO0CLR |= ~CLOCK[I];
delay(speed);
}
}
}

void anticlockwise(int speed, int rev)


{
unsigned char I,J;
for(J=0;J<rev;J++)
{
for(I=0;I<4;I++)
Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 44
MCES Lab (19IS506) - Lab Manual 2021-22
{
IO0SET |= ANTI_CLOCK[I];
IO0CLR |= ~ANTI_CLOCK[I];
delay(speed);
}
}
}

void delay(int n)
{
int i,j;
for (i=1; i<=n; i++)
for(j=0; j<=10000; j++);
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 45


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 11: Determine Digital output for a given Analog input using ADC of ARM Controller.

Procedure: Connect ADC module to FRC5 and use UART Terminal to see the output

#include<lpc214x.h>
#include<stdio.h>
#include <stdint.h>
#include <string.H>
#include <s.h>
void UART0_init(void);
void delay(void);

void UART0_init()
{
PINSEL0 = PINSEL0 | 0x00000005;
/* Enable UART0 Rx0 and Tx0 pins of UART0 */
U0LCR = 0x83; /* DLAB = 1, 1 stop bit, 8-bit character length */
U0DLM = 0x00; /* For baud rate of 9600 with Pclk = 15MHz */
U0DLL = 0x61; /* We get these values of U0DLL and U0DLM from formula */
U0LCR = 0x03; /* DLAB = 0 */
}

int main()
{
int adcdata;
float voltage;
char volt[4],volt1[11];
PINSEL0=0X00000000;
PINSEL1=0X01000000; //Select P0.28 pin function as Analog i/p
AD0CR=0x00210402; ///CHANNEL1 OF ADC0, ad freq=3MHz,
while(1)
{
if(AD0DR1 & 0x80000000) ////EOC bit monitoring
{
adcdata=(AD0DR1 & 0x0000FFC0);
adcdata=adcdata>>6;
voltage=((adcdata/1023.0)*3.3);
sprintf(volt, "%.1f", voltage);
sprintf(volt1, "%d", adcdata);
UART0_init();
sendstr("\n ADC o/p : ");
sendstr(volt1);
sendstr(volt);
delay();
}
Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 46
MCES Lab (19IS506) - Lab Manual 2021-22
}
}

void delay(void)
{
int i,j;
for(i=0;i<1000;i++)
for(j=0;j<10000;j++);
}

//s.h Header File

char sendchar (char sdat)


{
while (!(U0LSR & 0X20));
return (U0THR = sdat);
}
void sendstr(char *MSG)
{
unsigned char COUNT,LENGTH;
LENGTH = strlen(MSG);
sendchar(0x0D);
sendchar(0x0A);
for(COUNT=0;COUNT<LENGTH;COUNT++)
{
sendchar(*MSG);
MSG++;
}
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 47


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 12a: Interface a 4x4 keyboard and display the key code on an LCD.

Procedure: Connect LCD module to FRC2

#include<LPC214X.H>
#include<STRING.H>
#include<stdio.h>
#include<keyboard.h>

#define LCD_RS 0X000000200


#define LCD_RW 0X000000400
#define LCD_EN 0X000000800

void lcd_cmd(unsigned char val);


void lcd_data(unsigned char val);
void delay(unsigned int);
void gpi0_init(void);
void LCDSTR (unsigned char ADDRESS, char *MSG);
unsigned char cmd1[3]={0X38,0X0C,0X06};

int main()
{
unsigned int i;
gpi0_init();
for (i=0;i<4;i++)
lcd_cmd(cmd1[i]);
LCDSTR(0X00000080,"MATRIX KEYPAD ");
LCDSTR(0X000000C0,"Key Pressed: ");
keyboard();
}
void gpi0_init()
{
PINSEL0=0X00000000;
PINSEL2=0X00000000;

IO0DIR =0X00F00E00;
IO0CLR =0X00F00E00;
IO1DIR =0X00FF0000;
IO1CLR =0X00FF0000;
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 48


MCES Lab (19IS506) - Lab Manual 2021-22
void lcd_data(unsigned char val)
{
IO1PIN=(val<<16);
IO0SET = LCD_RS;
IO0CLR = LCD_RW;
IO0SET = LCD_EN;
delay(50);
IO0CLR =LCD_EN;
delay(100);
}

void lcd_cmd(unsigned char val)


{
IO1PIN=(val<<16);
IO0CLR=LCD_RS;
IO0CLR=LCD_RW;
IO0SET=LCD_EN;
delay(50);
IO0CLR =LCD_EN;
delay(100);
}

void LCDSTR (unsigned char ADDRESS, char *MSG)


{
unsigned char COUNT,LENGTH;
lcd_cmd(ADDRESS);
LENGTH = strlen(MSG);
for (COUNT=0; COUNT<LENGTH; COUNT++)
{
lcd_data(*MSG);
MSG++;
}
}

void delay(unsigned int val)


{
unsigned int i,j;
for(i=0;i<val;i++)
{
for(j=0;j<3000;j++);
}
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 49


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 12b: To display message using LCD and keyboard module

Procedure: Connect LCD module to FRC2 Keyboard module to FRC1.


This is a header file include keyboard function required to execute.

#define O1 0X00E00000
#define O2 0X00D00000
#define O3 0X00B00000
#define O4 0X00700000

#define I1 0x000E0000
#define I2 0x000D0000
#define I3 0x000B0000
#define I4 0x00070000

#define CLR 0x00F00000


void LCDSTR (unsigned char ADDRESS, char *MSG);
void delay(unsigned int val);
char scan(int keystatus);

keyboard(void)
{
while(1)
{
IO0CLR = CLR;
IO0SET = O1;
delay(10);
if(scan(I1))
LCDSTR(0x000000CC,"0"); //S1
if(scan(I2))
LCDSTR(0x000000CC,"4"); //S5
if(scan(I3))
LCDSTR(0x000000CC,"8"); //S9
if(scan(I4))
LCDSTR(0x000000CC,"C"); //S13

IO0CLR = CLR;
IO0SET = O2;
if(scan(I1))
LCDSTR(0x000000CC,"1"); //S2
if(scan(I2))
LCDSTR(0x000000CC,"5"); //S6
if(scan(I3))
LCDSTR(0x000000CC,"9"); //S10
if(scan(I4))
Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 50
MCES Lab (19IS506) - Lab Manual 2021-22
LCDSTR(0x000000CC,"D"); //S14

IO0CLR = CLR;
IO0SET = O3;
if(scan(I1))
LCDSTR(0x000000CC,"2"); //S3
if(scan(I2))
LCDSTR(0x000000CC,"6"); //S7
if(scan(I3))
LCDSTR(0x000000CC,"A"); //S11
if(scan(I4))
LCDSTR(0x000000CC,"E"); //S15

IO0CLR = CLR;
IO0SET = O4;
if(scan(I1))
LCDSTR(0x000000CC,"3"); //S4
if(scan(I2))
LCDSTR(0x000000CC,"7"); //S8
if(scan(I3))
LCDSTR(0x000000CC,"B"); //S12
if(scan(I4))
LCDSTR(0x000000CC,"F"); //S16
}
}

char scan(int keystatus) /* scanning a a key */


{
while((IO0PIN & 0X000F0000)==keystatus)
{
delay(50);
if((IO0PIN & 0X000F0000)== 0X000F0000)
return(1);
}
return(0) ;
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 51


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 13: Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in
between.

Procedure: Connect Seven Segment module to FRC4

#include <lpc214x.h>
unsigned int i;
void delay(unsigned int);
unsigned int arr[16]=
{0x000000C0,0X000000F9,0X000000A4,0X000000B0,0X00000099,0X00000092,0X00000082,0
X000000F8,0X00000080,0X00000090,0X00000088,0X00000083,0X000000C6,0X000000A1,0X0
0000086,0X0000008E };

int main()
{
IO0DIR = 0XFFFFFFFF;
while(1)
{
for (i=0; i<16; i++)
{
IO0SET=arr[i];
delay(5000);
IO0CLR=arr[i];
}
}
}
void delay(unsigned int k)
{
int l,m;
for (l=0; l<k; l++)
for (m=0; m<=2000; m++);
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 52


MCES Lab (19IS506) - Lab Manual 2021-22
Exp. 14: Demonstrate the use of external interrupt to toggle an LED ON/OFF.

Procedure: Connect LED Module toFRC1 and download Programme to ARM Kit and press EXT
INT button to observe interrupt.

#include<lpc21xx.h>
void DELAY(unsigned long VALUE);
unsigned int A = 0x00010000;

void ext_interrupt(void)__irq
{
EXTINT = 0X02; /* Clear interrupt flag */
DELAY(5000000);
VICVectAddr = 0x00000000; /* Acknowledge Interrupt */
}

void init_ext(void)
{
PINSEL0 |= 0X20000000; /* enable EXT1 */
EXTMODE = 0X02; /* edge sensitive */
EXTPOLAR = 0X02; /* on rising edge */
VICVectAddr0 =(unsigned int)ext_interrupt; /* Set Interrupt Vector in 0 */
VICVectCntl0 = 0x0000002F; /* Use it for EXT1 Interrupt */
VICIntEnable = 0x00008000; /* enable EXT1 INTERRUPT */
}

int main(void)
{
IO0DIR = 0X00FF0000;
init_ext();
while(1)
{
IO0SET |= A; /* Port0 00-07 High */
DELAY(1000000);
IO0CLR |= A; /* Port0 00-07 High */
A <<=1;
if(A==0x01000000)
A=0x00010000;
DELAY(1000000);
}
}
void DELAY(unsigned long VALUE)
{
while(VALUE>0)
Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 53
MCES Lab (19IS506) - Lab Manual 2021-22
{
VALUE--;
}
}

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 54


MCES Lab (19IS506) - Lab Manual 2021-22

SAMPLE VIVA QUESTIONS

1. What is the processor used by ARM7?


2. What is the instruction set used by ARM7?
3. What is T, D, M, I stand for in ARM7TDMI?
4. ARM stands for _________
5. What are the profiles for ARM architecture?
6. ARM7DI operates in which mode?
7. The synonym of AMBA is _____
8. Pipelining stages of ARM include ______
9. In case of ARM LPC 2148, user mode is ____ mode
10.To more from an ARM register to a status register _____instruction is used.

Dept. of ISE, NMAMIT, Nitte Prepared By: Abhishek S. Rao Page 55

You might also like