0% found this document useful (0 votes)
4 views32 pages

Bcs402 Cse MC Lab Manual

The document outlines the course structure for 'Microcontrollers' (BCS402) at RV Institute of Technology and Management, detailing the objectives, outcomes, and assessment methods. It includes a comprehensive curriculum covering ARM architecture, programming, exception handling, and cache management, with both theoretical and practical components. The course aims to equip students with the necessary skills and knowledge to become industry-ready professionals in the field of Computer Science and Engineering.

Uploaded by

Akarsh AK
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)
4 views32 pages

Bcs402 Cse MC Lab Manual

The document outlines the course structure for 'Microcontrollers' (BCS402) at RV Institute of Technology and Management, detailing the objectives, outcomes, and assessment methods. It includes a comprehensive curriculum covering ARM architecture, programming, exception handling, and cache management, with both theoretical and practical components. The course aims to equip students with the necessary skills and knowledge to become industry-ready professionals in the field of Computer Science and Engineering.

Uploaded by

Akarsh AK
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/ 32

Rashtreeya Sikshana Samithi Trust

RV Institute of Technology and Management ®


(Affiliated to VTU, Belagavi)

JP Nagar, Bengaluru – 560076

Department of Computer Science and Engineering

Course Name: MICROCONTROLLERS


Course Code: BCS402 / IPCC

IV Semester

2022 Scheme
Prepared By:
Mrs. Padmasree
Associate Professor,
Department of Computer Science and Engineering,
RVITM, Bengaluru – 560076
padmasree.rvitm@rvei.edu.in
RV Institute of Technology & Management®
Department of Computer Science & Engineering

Institute Vision

Strive to impart quality technical education with global competence and societal
commitment, leading to breakthrough innovations.
Department Vision

To be at the frontier of Computer Science and Engineering with a focus on


innovation and industry needs and to produce global professionals contributing to
society.
Department Mission

• To impart world-class education in Computer Science and Engineering through


contemporary pedagogical practices.
• To inculcate quality research leading to innovation through latest tools and various
technical forums.
• To impart technical skills and domain knowledge focusing on emerging areas to
produce industry-ready graduates and entrepreneurs
• To impart knowledge to students in interdisciplinary areas that extend the scope of
computer science for the benefit of society.

Program Educational Objectives(graduates are expected to perform and achieve during


the first few years after graduation.)

PEO 1: Impart strong foundation in solving hardware & software engineering problems and
adapt to the changes in technology through continuous-learning.
PEO 2: Inculcate innovation in new technologies and systems in their key domains thus
encouraging them to pursue higher education and research.
PEO 3: Disseminate their knowledge in advance areas of Computer science to become
industry ready graduates or entrepreneurs.
PEO 4: Exhibit professional ethics & values, effective communication and team work with
a multidisciplinary approach to meet the needs of the society.

Program Specific Objectives(students should be able to do at the time of graduation.)

PSO 1: Analyse the core concepts by Computer Science Engineering for building intensive
systems to improve efficiency with different algorithms and tools.
PSO 2: Able to use professional, managerial and interdisciplinary skills in identifying the
research gaps to develop cost effective and innovative solutions leading to
automation.

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 2 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering
Program Outcomes (POs)

Engineering knowledge: Apply the knowledge of mathematics, science, engineering


PO1
fundamentals, and an engineering specialization to the solution of complex engineering problems.
Problem analysis: Identify, formulate, review research literature, and analyze complex
PO2 engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for
PO3
the public health and safety, and the cultural, societal, and
environmental considerations.
Conduct investigations of complex problems: Use research-based knowledge and
PO4 research methods including design of experiments, analysis and interpretation of data, andsynthesis
of the information to provide valid conclusions
Modern tool usage: Create, select, and apply appropriate techniques, resources, and
PO5 modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
The engineer and society: Apply reasoning informed by the contextual knowledge toassess
PO6 societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
Environment and sustainability: Understand the impact of the professional engineering
PO7 solutions in societal and environmental contexts, and demonstrate the knowledge of, andneed for
sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities
PO8
and norms of the engineering practice.
Individual and team work: Function effectively as an individual, and as a member or
PO9
leader in diverse teams, and in multidisciplinary settings.
Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports
PO10
and design documentation, make effective presentations, and give
and receive clear instructions.
Project management and finance: Demonstrate knowledge and understanding of the
PO11 engineering and management principles and apply these to one’s own work, as a memberand
leader in a team, to manage projects and in multidisciplinary environments.
Life-long learning: Recognize the need for, and have the preparation and ability to
PO12
engage in independent and life-long learning in the broadest context of technologicalchange.

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 3 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering
MICROCONTROLLERS Semester 4
Course Code BCS402 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 3:0:2:0 SEE Marks 50
Total Hours of Pedagogy 40 hours Theory + 8-10 Lab Slots Total Marks 100
Credits 04 Exam Hours 3
Examination nature (SEE) Theory
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.
Teaching-Learning Process
These are sample Strategies, which teachers can use to accelerate the attainment of the various course outcomes.
1. Lecturer method (L) needs not to be only a traditional lecture method, but alternative effective teaching
methods could be adopted to attain the outcomes.
2. Use of Video/Animation to explain functioning of various concepts.
3. Encourage collaborative (Group Learning) Learning in the class.
4. Ask at least three HOT (Higher order Thinking) questions in the class, which promotes critical thinking.
5. Adopt Problem Based Learning (PBL), which fosters students’ Analytical skills, develop design thinking skills
such as the ability to design, evaluate, generalize, and analyze information rather than simply recall it.
6. Introduce Topics in manifold representations.
7. Show the different ways to solve the same problem with different circuits/logic and encourage the students to
come up with their own creative ways to solve them.
8. Discuss how every concept can be applied to the real world - and when that's possible, it helps improve the
students understanding.
9. Use any of these methods: Chalk and board, Active Learning, Case Studies.
MODULE-1 No. of Hours: 8
ARM Embedded Systems: The RISC design philosophy, The ARM Design Philosophy, Embedded System
Hardware, Embedded System Software.
ARM Processor Fundamentals: Registers, Current Program Status Register, Pipeline, Exceptions, Interrupts, and
the Vector Table, Core Extensions
Textbook 1: Chapter 1 - 1.1 to 1.4, Chapter 2 - 2.1 to 2.5
RBT: L1, L2, L3
MODULE-2 No. of Hours: 8
Introduction to the ARM Instruction Set: Data Processing Instructions, Branch Instructions, Software Interrupt
Instructions, Program Status Register Instructions, Coprocessor Instructions, Loading Constants.
Textbook 1: Chapter 3 - 3.1 to 3.6
RBT: L1, L2, L3
MODULE-3 No. of Hours:8
C Compilers and Optimization: Basic C Data Types, C Looping Structures, Register Allocation, Function Calls,
Pointer Aliasing, Portability Issues.
Textbook 1: Chapter 5.1 to 5.7 and 5.13
RBT: L1, L2, L3

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 4 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering
MODULE-4 No. of Hours:8
Exception and Interrupt Handling: Exception handling, ARM processor exceptions and modes, vector table,
exception priorities, link register offsets, interrupts, assigning interrupts, interrupt latency, IRQ and FIQ exceptions,
basic interrupt stack design and implementation.
Firmware: Firmware and bootloader, ARM firmware suite, Red Hat redboot, Example: sandstone, sandstone
directory layout, sandstone code structure.
Textbook 1: Chapter 9.1 and 9.2, Chapter 10
RBT: L1, L2, L3
MODULE-5 No. of Hours:08
CACHES: The Memory Hierarchy and Cache Memory, Caches and Memory Management Units: CACHE
Architecture: Basic Architecture of a Cache Memory, Basic Operation of a Cache Controller, The Relationship
between Cache and Main Memory, Set Associativity, Write Buffers, Measuring Cache Efficiency, CACHE
POLICY: Write Policy—Writeback or Writethrough, Cache Line Replacement Policies, Allocation Policy on a
Cache Miss. Coprocessor 15 and caches.
Textbook 1: Chapter 12.1 to 12.4
RBT: L1, L2, L3
PRACTICAL COMPONENT OF IPCC (May cover all / major modules)

Sl.No. Experiments
Module – 1
1. Using Keil software, observe the various Registers, Dump, CPSR, with a simple Assembly Language
Programs (ALP).
Module – 2
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.
Module – 3
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.
Module – 4 and 5
10. Demonstrate enabling and disabling of Interrupts in ARM.
11. Demonstrate the handling of divide by zero, Invalid Operation and Overflow exceptions in ARM.
Course outcomes (Course Skill Set): At the end of the course, the student will be able to:
● Explain the ARM Architectural features and Instructions.
● Develop programs using ARM instruction set for an ARM Microcontroller.
● Explain C-Compiler Optimizations and portability issues in ARM Microcontroller.
● Apply the concepts of Exceptions and Interrupt handling mechanisms in developing applications.
● Demonstrate the role of Cache management and Firmware in Microcontrollers.
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is 50%. The
minimum passing mark for the CIE is 40% of the maximum marks (20 marks out of 50) and for the SEE minimum
passing mark is 35% of the maximum marks (18 out of 50 marks). A student shall be deemed to have satisfied the
IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 5 |32
RV Institute of Technology & Management®
Department of Computer Science & Engineering
academic requirements and earned the credits allotted to each subject/ course if the student secures a minimum of
40% (40 marks out of 100) in the sum total of the CIE (Continuous Internal Evaluation) and SEE (Semester End
Examination) taken together.
CIE for the theory component of the IPCC (maximum marks 50)
● IPCC means practical portion integrated with the theory of the course.
● CIE marks for the theory component are 25 marks and that for the practical component is 25 marks.
● 25 marks for the theory component are split into 15 marks for two Internal Assessment Tests (Two Tests, each
of 15 Marks with 01-hour duration, are to be conducted) and 10 marks for other assessment methods mentioned
in 22OB4.2. The first test at the end of 40-50% coverage of the syllabus and the second test after covering 85-
90% of the syllabus.
● Scaled-down marks of the sum of two tests and other assessment methods will be CIE marks for the theory
component of IPCC (that is for 25 marks).
● The student has to secure 40% of 25 marks to qualify in the CIE of the theory component of IPCC.
CIE for the practical component of the IPCC
1. 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.
2. 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.
3. 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.
4. The laboratory test (duration 02/03 hours) after completion of all the experiments shall be conducted for 50
marks and scaled down to 10 marks.
5. Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory component of
IPCC for 25 marks.
6. The student has to secure 40% of 25 marks to qualify in the CIE of the practical component of the IPCC.
SEE for IPCC
Theory SEE will be conducted by University as per the scheduled timetable, with common question papers for the
course (duration 03 hours)
1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a module (with a maximum of 3
sub-questions), should have a mix of topics under that module.
3. The students have to answer 5 full questions, selecting one full question from each module.
4. Marks scored by the student shall be proportionally scaled down to 50 Marks.
The theory portion of the IPCC shall be for both CIE and SEE, whereas the practical portion will have a CIE component
only. Questions mentioned in the SEE paper may include questions from the practical component.
Suggested Learning Resources:
Text Books:
1. Andrew N Sloss, Dominic Symes and Chris Wright, ARM system developers guide, Elsevier, Morgan Kaufman
publishers, 2008.
Reference Books:
1. Raghunandan.G.H, Microcontroller (ARM) and Embedded System, Cengage learning Publication, 2019.
2. Insider’s Guide to the ARM7 based microcontrollers, Hitex Ltd.,1st edition, 2005
Activity Based Learning (Suggested Activities in Class)/ Practical Based Learning
Assign the group task to demonstrate the Installation and working of Keil Software.

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 6 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering
General Instructions to Students:
1. Understand the theoretical concept for the respective experiment.
2. Draw the circuit diagram in the given space in the observation book.
3. Tabulate the readings in the observation book and plot the graphs if necessary.
4. Every Student must at least construct one circuit.
5. After the completion of the experiment, get signature in the observation book.
6. Before coming to next lab, make sure that records will be updated and signed from the
concerned faculty.

Guide Lines for Writing Manual:


Blank Page (USE PENCIL)
Practical No:
Aim:
Circuit Diagram:
Theory:
Procedure:
Calculation:
Result:
Ruled Page (USE PEN)
Practical No:
Aim:
Apparatus Required:
Characteristics/Wave form/ Observation Table:
Result:

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 7 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

Introduction to ARM7

The LPC2141/42/44/46/48 microcontrollers are based on a 16-bit/32-bit ARM7TDMI-S CPU


with real-time emulation and embedded trace support, that combine microcontroller with
embedded high-speed flash memory ranging from 32 kB to 512 kB. A 128-bit wide memory
interface and unique accelerator architecture enable 32-bit code execution at the maximum clock
rate. For critical code size applications, the alternative 16-bit Thumb mode reduces code by more
than 30 % with minimal performance penalty. Due to their tiny size and low power consumption,
LPC2141/42/44/46/48 are ideal for applications where miniaturization is a key requirement, such
as access control and point-of sale. Serial communications interfaces ranging from a USB 2.0
Full-speed device, multiple UARTs, SPI, SSP to I2C-bus and on-chip SRAM of 8 kB up to 40
kB, make these devices very well suited for communication gateways and protocol converters,
soft modems, voice recognition and low-end imaging, providing both large buffer size and high
processing power. Various 32-bit timers, single or dual 10-bit ADC(s), 10-bit DAC, PWM
channels and 45 fast GPIO lines with up to nine edge or level sensitive external interrupt pins
make these microcontrollers suitable for industrial control and medical systems. Fig. 1.1 shows
the Blockdiagram of ARMLPC2148.

Fig. 1.1: BLOCK Diagram of ARMLPC2148

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 8 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

Key Points:

1. Header File: Replace #include <LPC214x.h> with the header file appropriate for your
specific ARM microcontroller family.
2. Initialization: The initializeSystem() function is where you would configure system
clocks and other microcontroller-specific initializations. This function depends heavily
on the microcontroller and specific hardware configurations you are using.
3. Sorting Functionality: The bubbleSort() function is a straightforward implementation
of the bubble sort algorithm.
4. Swap Functionality: The swap() function simply swaps two integers in memory.
5. Execution Flow: In an embedded environment, main functions typically have an
infinite loop (while (1)) as they should continue running indefinitely.

To Run in KEIL:
1. Create a New Project: Open KEIL µVision and create a new project for your specific ARM Cortex
microcontroller.
2. Add Your Source File: Add a new .c file to your project and copy the code above into this file.
3. Configure Your Project: Set up your target CPU settings and configure any peripherals you are
using.
4. Compile and Debug: Compile the project and load it onto your microcontroller. Use debugging tools
to step through the code and ensure it's working correctly.

By following these steps, you should be able to implement and run a bubble sort algorithm on an ARM
microcontroller using KEIL. If you want to sort in descending order, adjust the comparison in the
bubbleSort() function from arr[j] >arr[j+1] to arr[j] <arr[j+1].

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 9 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

Step 1: After opening Keil uV4, Go to Project tab and click on close projectThen

Create new uVision project

Now Select new folder and give name to Project.

Step 2: After Creating project now Select your device model. Example.NXP-LPC2148

[You can change it later from project window.]

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 10 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

Step 3: So now your project is created and Message window will appear to add start up 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.

Step 6: After that on left you see project window [if it’s not there….go to View tab and click on
project window]

Now come on Project window.

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 11 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

Right click on target and click on options for target

Here you can change your device also.

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 12 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

Click output tab here & check create Hex file if you want to generate hex file

Now click on ok so it will save changes.

Step 7: Now Expand target and you will see source group

Right click on group and click on Add files to source group

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 13 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

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]

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 14 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

EXPERIMENTS: 1 to 11
Conduct the following experiments by writing program using ARM7TDMI/LPC2148 usingan
evaluation board/simulator and the required software tool.
MODULE – 1 Experiment 1:
Using Keil software, observe the various Registers, Dump, CPSR, with a simple
Assembly Language Programs (ALP).
MODULE – 2 Experiment 2:
Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical operations
(Demonstrate with the help of a suitable program).
Sample ALP Program to multiply two 16 bit binary numbers using Keil Microvision4

AREA Program, CODE, READONLY

ENTRY

LDR R0, MEMORY; load Address of

memory

LDRH R1, [R0]; load First number

LDRH R2, [R0,#2]; load Second number

MUL R2, R1, R2 ;R2=R1*R2

STR R2, [R0, #4]; Store the result

here B here ; all done

MEMORY DCD 0x40000000

END

Fill the First number & Second number in the


Memory Location: 0x40000000
Perform single step execution to observe the movement of Data in Various Registers like
GPR’S - General Purpose Registers: R0 – R12,
R13: SP / Stack Pointer, R14: LR / Link Register, R15: PC / Program Counter
CPSR: Current Program Status Register
SPSR: Saved Program Status Register

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 15 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 2 Experiment 3:
Develop an ALP to multiply two 16-bit binary numbers.

AREA MULTIPLY, CODE, READONLY


ENTRY ; Mark first instruction to execute

START
MOV r1, #6400 ; STORE FIRST NUMBER INR0
MOV r2, #3200 ; STORE SECOND NUMBER INR1
MUL r3, r1, r2 ;
MULTIPLICATION

END ; Mark end of file

OUTPUT:

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 16 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 2 Experiment 4:
Develop an ALP to find the sum of first 10 integer numbers.

AREA SUM, CODE, READONLY


ENTRY
MOVR1, #10 ; load 10 to register
MOVR2, #0 ; empty the register to storeresult

loop

ADDR2, R2, R1 ; add the content of R1 with result at R2


SUBSR1, #0x01 ; Decrement R1 by 1
BNEloop ; repeat till r1 goes0
back Bback; jumps back to C code

END

OUTPUT:

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 17 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 2 Experiment 5: (5a: LARGEST & 5b: SMALLEST)


Experiment 5a: To find LARGEST of a number in an ARRAY.

AREA LARGEST, CODE, READONLY

ENTRY ; Mark first instruction to execute

START

MOVR5, #6 ; INTIALISE COUNTER TO 6(i.e.N=7)

LDRR1, =VALUE1 ; LOADS THEADDRESS

LDRR2, [R1], #4 ; WORD ALIGN T0 ARRAYELEMENT LOOP

LDRR4, [R1], #4 ; WORD ALIGN T0 ARRAYELEMENT

CMPR2, R4 ; COMPARE NUMBERS

BHILOOP1 ; IF THE FIRST NUMBER IS > THEN GOTOLOOP1

MOVR2, R4; IF THE FIRST NUMBER IS < THEN MOV CONTENT R4 TOR2

LOOP1

SUBSR5, R5, #1 ; DECREMENTCOUNTER

CMPR5, #0 ; COMPARE COUNTER TO0

BNELOOP ; LOOP BACK TILL ARRAY ENDS

LDRR4, =RESULT ; LOADS THE ADDRESS

OFRESULT STRR2, [R4] ; STORES THE RESULT INR2

NOP
back B back; ARRAY OF 32-BIT NUMBERS (N=7) VALUE1
DCD0X44444444 ;
DCD 0X22222222 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0XAAAAAAAA ;

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 18 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

DCD 0X88888888 ;
DCD 0X99999999 ;

AREA DATA2, DATA, READWRITE ;T O STORE RESULT IN GIVEN ADDRESS

RESULT DCD 0X0

END ; Mark end offile

Output:
Largest

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 19 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 2 Experiment 5: (5a: LARGEST & 5b: SMALLEST)


Experiment 5b: To find SMALLEST of a number in an ARRAY.

AREA SMALLEST, CODE, READONLY

ENTRY ; Mark first instruction to execute


START

MOV R5, #6 ; INTIALISE COUNTER TO 6(i.e. N=7)


LDR R1, =VALUE1 ; LOADS THE ADDRESS
LDR R2, [R1], #4 ; WORD ALIGN T0 ARRAY ELEMENT

LOOP
LDR R4, [R1], #4 ; WORD ALIGN T0 ARRAY ELEMENT
CMP R2, R4 ; COMPARE NUMBERS
BLS LOOP1 ; IF THE FIRST NUMBER IS < THEN GOTO LOOP1
MOV R2, R4 ; IF THE FIRST NUMBER IS > THEN MOV CONTENT R4
TO R2

LOOP1
SUBS R5, R5, #1 ; DECREMENT COUNTER
CMP R5, #0 ; COMPARE COUNTER TO 0
BNE LOOP ; LOOP BACK TILL ARRAY ENDS LDR R4, =RESULT ; LOADS
THE ADDRESS OF RESULT STR R2, [R4] ; STORES THE RESULT IN R2
NOP

back B back

; ARRAY OF 32-BIT NUMBERS (N=7)


VALUE1
DCD 0X44444444 ;
DCD 0X22222222 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0XAAAAAA ;
DCD 0X88888888 ;
DCD 0X99999999 ;

AREA DATA2, DATA, READWRITE ; T O STORE RESULT IN GIVEN


ADDRESS
RESULT DCD 0X0
END ; Mark end of file

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 20 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

Output:
Smallest

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 21 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 2 Experiment 6:
Develop an ALP to count the number of ones and zeros in two consecutive memory locations.

AREA ONEZERO, CODE, READONLY


ENTRY
LDR R0, MEMORY ; Load Address Memory
LDR R1,[R0] ; Load 32bit Number
MOV R4, #32 ; Load rotation Count

ROTATE
RORS R1, #1 ; Rotate right by 1 bit, update
CPSR
BCS ONES ; is carry =1
ADD R3,R3,#1 ; increment 0 count
B NEXT ; Branch to next Rotation

ONES
ADD R2,R2,#1 ; increment 1 count

NEXT
ADD R4,R4,#-1 ; Decrement Rotation Count
CMP R4, #0 ; if Rotation EQ to 0
BNE ROTATE ; if No, goto Rotate
ADD R0,R0,#04 ; Add Address of no. of 1's
STRB R2,[R0] ; Store No. of 1's
ADD R0, R0, #1 ; Load address of no. of 0's

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 22 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

STRB R3, [R0] ; Store No. of 0's


Here
B Here
MEMORY DCD 0X40000000 ; Memory address

END
OUTPUT:

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 23 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 3 Experiment 7:
Simulate a program in C for ARM microcontroller using KEIL to sort the numbers in ascending/descending
order using bubble sort.

#include <LPC214x.h> // Include appropriate header for your ARM MCU

// Function prototypes
void bubbleSort(int arr[], int n);
void swap(int *xp, int *yp);
void initializeSystem();

int main() {
initializeSystem();

// Example array
int data[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(data) / sizeof(data[0]);

// Sort the array


bubbleSort(data, n);

// After sorting, you can add code here to do something with the sorted array
// For example, display it on an LCD connected to the microcontroller

while (1) {
// Typically, embedded programs have an infinite loop
}

return 0; // This won't really be executed in most embedded applications


}

// Function to perform bubble sort


void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i< n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] >arr[j+1])
swap(&arr[j], &arr[j+1]);
}

// Function to swap two elements


void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 24 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

// Initialize system-specific settings


void initializeSystem() {
SystemInit(); // System-specific initialization function (depends on the MCU)
// Any other peripheral configuration and initializations
}

Key Points:

6. Header File: Replace #include <LPC214x.h> with the header file appropriate for your specific
ARM microcontroller family.
7. Initialization: The initializeSystem() function is where you would configure system clocks and
other microcontroller-specific initializations. This function depends heavily on the microcontroller and
specific hardware configurations you are using.
8. Sorting Functionality: The bubbleSort() function is a straightforward implementation of the
bubble sort algorithm.
9. Swap Functionality: The swap() function simply swaps two integers in memory.
10. Execution Flow: In an embedded environment, main functions typically have an infinite loop (while
(1)) as they should continue running indefinitely.

To Run in KEIL:

5. Create a New Project: Open KEIL µVision and create a new project for your specific ARM Cortex
microcontroller.
6. Add Your Source File: Add a new .c file to your project and copy the code above into this file.
7. Configure Your Project: Set up your target CPU settings and configure any peripherals you are
using.
8. Compile and Debug: Compile the project and load it onto your microcontroller. Use debugging tools
to step through the code and ensure it's working correctly.

By following these steps, you should be able to implement and run a bubble sort algorithm on an ARM
microcontroller using KEIL. If you want to sort in descending order, adjust the comparison in the
bubbleSort() function from arr[j] >arr[j+1] to arr[j] <arr[j+1].

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 25 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

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

#include <LPC214x.h> // Change this include according to your specific ARM MCU family

// Function prototypes
unsigned long longfactorial(unsigned int n);
void initializeSystem();

int main() {
initializeSystem();

unsigned int number = 10; // Example number to compute factorial


unsigned long long result;

// Calculate factorial
result = factorial(number);

// Now, you can use `result` to display on an LCD or debug through serial output
// As this is a simulation, you might set a breakpoint here and inspect `result`

while (1) {
// Embedded programs typically have an infinite loop
}

return 0;
}

// Recursive function to find factorial of n


unsigned long longfactorial(unsigned int n) {
if (n == 0)
return 1; // Base case: 0! = 1
else
return n * factorial(n - 1); // Recursive case
}

// Initialize system-specific settings


void initializeSystem() {
SystemInit(); // System-specific initialization function (depends on the MCU)
// Any other peripheral configuration and initialization can be added here
}

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 26 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 3 Experiment 9:
Simulate a program in C for ARM microcontroller to demonstrate case conversion of characters from upper
to lowercase and lower to uppercase.

#include <LPC214x.h> // Specific header for the LPC1768; adjust for your ARM Cortex-M model

// Function prototypes
char toLowercase(char c);
char toUppercase(char c);
void initializeSystem();

int main() {
initializeSystem();

char exampleUpper = 'A';


char exampleLower = 'z';

char convertedToLower = toUppercase(exampleUpper); // Expect 'A' -> 'A'


char convertedToUpper = toLowercase(exampleLower); // Expect 'z' -> 'Z'

// Normally you would display these results or send them over a communication interface
// Use breakpoints or watch variables to check these conversions in KEIL's debugger

while (1) {
// Infinite loop to keep the microcontroller running
}

return 0;
}

// Converts uppercase letters to lowercase. If the input is not uppercase, it returns the input unchanged.
char toLowercase(char c) {
if (c >= 'A' && c <= 'Z') {
return c + ('a' - 'A');
}
return c;
}

// Converts lowercase letters to uppercase. If the input is not lowercase, it returns the input unchanged.
char toUppercase(char c) {
if (c >= 'a' && c <= 'z') {
return c - ('a' - 'A');
}
return c;
}

// Initialize system-specific settings


void initializeSystem() {
SystemInit(); // System-specific initialization function, e.g., clock setup
// Initialize any peripherals here
}

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 27 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 4 & 5 Experiment 10:


Demonstrate enabling and disabling of Interrupts in ARM.

Basic Concepts

 Enabling Interrupts: In ARM Cortex-M microcontrollers, interrupts are enabled using the
__enable_irq() function, which clears the PRIMASK register, allowing interrupts with configurable
priority.
 Disabling Interrupts: Interrupts are disabled using the __disable_irq() function, which sets the
PRIMASK register, preventing the activation of all interrupts and exceptions with configurable priority.

Setting Up in KEIL:
1. New Project: Start a new project in KEIL for the specific ARM Cortex microcontroller you are using.
2. Add the Code: Copy the above C code into a new .c file in the project.
3. Configure the MCU Settings: Make sure you set up the microcontroller settings correctly, including
clock, memory, and peripherals.
4. Compile and Debug: Compile the project and upload it to your microcontroller. Use the debugger to
step through and observe the conversion functions at work.

#include <LPC214x.h> // Include appropriate header for your MCU

void initializeSystem();
void criticalFunction();

int main() {
initializeSystem();

// Disable interrupts before entering a critical section


__disable_irq();
criticalFunction(); // Execute critical code that must not be interrupted
__enable_irq(); // Re-enable interrupts after critical section

while (1) {
// Normal operation, where interrupts are enabled
}

return 0;
}

// Simulated critical function where interrupts must be disabled


void criticalFunction() {
// Critical code here
// Example: Accessing shared resources, timing-sensitive operations, etc.
}

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 28 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

// System initialization
void initializeSystem() {
SystemInit(); // System specific initialization
// Additional initialization code like configuring system clocks,
// setting up GPIO, or other peripheral setups can be added here.
}

Key Points:
1. System Initialization: initializeSystem() typically configures the system clock, initializes
peripherals, and sets up any required hardware configurations specific to the application and
microcontroller.
2. Disabling/Enabling Interrupts: __disable_irq() and __enable_irq() are used to protect
the critical section, ensuring that the operations within criticalFunction() are not
interrupted. This is crucial for maintaining system integrity in scenarios involving shared data
or specific timing requirements.
3. Infinite Loop: The infinite loop in the main function signifies continuous operation common
in embedded systems, where the program must not terminate under normal operations.

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 29 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

MODULE – 4 & 5 Experiment 11:


Demonstrate the handling of divide by zero, Invalid Operation and Overflow exceptions in ARM.

Handling exceptions such as divide by zero, invalid operations, and overflow in ARM Cortex-M
microcontrollers requires a proper understanding of the ARM exception mechanism. ARM Cortex-M
processors use a fault mechanism called the Fault Exception to handle errors and exceptional
conditions that occur during execution. This includes:

 Usage Fault: Caused by execution of an undefined instruction, illegal unaligned access, or other
erroneous operation codes, such as divide by zero.
 Bus Fault: Caused by memory access errors.
 Memory Management Fault: Typically, this occurs from an invalid access to a protected area of
memory (though not all Cortex-M processors have a Memory Protection Unit).

For the purposes of simplicity and clarity, I will focus on the Usage Fault handler, as it is typically
the one that deals with arithmetic exceptions such as divide by zero and overflow. Here’s how you
could set up handlers for these exceptions using a generic ARM Cortex-M microcontroller, such as
one from the LPC1768 series:

#include <LPC214x.h>

void SystemInit() {
// System Initialization code here
SCB->CCR |= SCB_CCR_DIV_0_TRP_Msk; // Enable Divide by zero trap
}

void HardFault_Handler(void) {
// Handle Hard Fault exception
while(1);
}

void UsageFault_Handler(void) {
// Handle Usage Fault exception
uint32_t faultStatus = SCB->CFSR; // Collecting the fault status register

if (faultStatus& (1 << 25)) { // Check for divide by zero fault


// Handle Divide by Zero Fault
// Optionally clear the divide by zero status in CFSR
SCB->CFSR |= (1 << 25); // Clear the divide by zero fault
}

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 30 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

if (faultStatus& (1 << 0)) { // Check for undefined instruction fault


// Handle Invalid Operation Fault
// Optionally clear the undefined instruction status in CFSR
SCB->CFSR |= (1 << 0); // Clear the undefined instruction fault
}

while(1); // Stop execution


}

int main(void) {
SystemInit();

int a = 10;
int b = 0;
int c = a / b; // This will trigger a divide by zero if b is zero

return 0;
}

Key Components Explained:


1. System Initialization (SystemInit): In the initialization function, the SCB->CCR |=
SCB_CCR_DIV_0_TRP_Msk; line configures the Cortex-M system control block to trap the
divide by zero fault. This is crucial for the microcontroller to invoke the UsageFault handler
when a divide by zero occurs.
2. Usage Fault Handler (UsageFault_Handler): This function is where the microcontroller
jumps when a usage fault occurs. Inside this function, the Cause of the Fault Status Register
(CFSR) is examined to determine the type of fault (e.g., divide by zero, invalid operation).
3. Fault Status Register Checks: Specific bits in the CFSR register indicate different kinds of
faults:
 Bit 25 (DIVBYZERO): Set when a divide by zero operation occurs.
 Bit 0 (UNDEFINSTR): Set when an undefined instruction is executed.
4. Hard Fault Handler (HardFault_Handler): A catch-all for other severe faults that are not
anticipated by more specific handlers. In production code, you'd typically add diagnostics
logging or system recovery procedures here.

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 31 |32


RV Institute of Technology & Management®
Department of Computer Science & Engineering

VIVA QUESTIONS

STUDY OF ARM EVALUATION SYSTEM

1) What are the basic units of ARM 7?


2) What is the address system supported by ARM?
3) Define RISK.
4) What are the instructions used to access the memory in ARM?
5) How are the instructions encoded in ARM machines?
6) What are the basic units of Microprocessor?
7) What is an Instruction?
8) What is clock cycle?
9) Define - RTOS
10) Define – Pipelining
11) What is cache bus?
12) What is cache miss?
13) What is meant by memory mapped I/O?
14) Define – interrupt
15) What is the function of device driver?
16) What is meant by Exception?
17) What is meant by little – endian mode?
18) What is meant by big – endian mode? 19) What is the function of CPSR?
20) What is register - indirect addressing?
21) What are the steps in programming GPIO pins of LPC2148?
22) How to interface an LED with a microcontroller?
23) Explain the PORT pin details of your program.
24) Explain IO0SET and IO0CLR registers.
25) How to disable all the LEDs from being programmed?

IV-Semester, MICROCONTROLLERS Lab / IPCC (BCS402) P a g e 32 |32

You might also like