You are on page 1of 39

BANGALORE INSTITUTE OF TECHNOLOGY

K.R. ROAD, V.V PURAM, BANGALORE – 560 004

DEPARTMENT
OF
INFORMATION SCIENCE AND ENGINEERING
(AFFILIATED TO VTU, BELAGAVI)

CODE: 21CS43

MICROCONTROLLER AND EMBEDDED SYSTEMS


As per Choice Based Credit System Scheme (CBCS)

FOR IV SEMESTER CSE/ISE AS PRESCRIBED BY VTU

Academic year 2022-2023

Prepared By:
Dr. M.SHILPA
Associate Professor
Dept. of ISE, BIT
21CS43 Microcontroller and Embedded System Laboratory

Introduction

An embedded system is an electronic/electro-mechanical system designed to perform a specific


function and is a combination of both hardware and firmware (software). An embedded system
combines mechanical, electrical, and chemical components along with a computer, hidden inside, to
perform a single dedicated purpose.
There are more computers on this planet than there are people, and most of these computers are
single-chip microcontrollers that are the brains of an embedded system. Embedded systems are a
ubiquitous component of our everyday lives. We interact with hundreds of tiny computers every
day that are embedded into our houses, our cars, our bridges, our toys, and our work. As our world
has become more complex, so have the capabilities of the microcontrollers embedded into our
devices. Therefore, the world needs a trained workforce to develop and manage products based on
embedded microcontrollers.
A general-purpose computing system is a combination of generic hardware and general-purpose
operating system for executing a variety of application, whereas an embedded system is a
combination system is a combination of special purpose hardware and embedded OS/firmware for
executing a specific set of applications.
The ARM microcontroller stands for Advance Risk Machine; it is one of the extensive and most
licensed processor cores in the world. The first ARM processor was developed in the year 1978 by
Cambridge University, and the first ARM RISC processor was produced by the Acorn Group of
Computers in the year 1985.
These processors are specifically used in portable devices like digital cameras, mobile phones,
home networking modules and wireless communication technologies and other embedded systems
due to the benefits, such as low power consumption, reasonable performance, etc. This article gives
an overview of ARM architecture with each module’s principle of working.
Keil MDK is the complete software development environment for a wide range of ARM Cortex-M
based microcontroller devices. MDK includes the μVision IDE and debugger, Arm C/C++ compiler,
and essential middleware components. It supports all silicon vendors with more than 6,000 devices
and is easy to learn and user.

Dept of ISE, BIT Page 2


21CS43 Microcontroller and Embedded System Laboratory

Features of ARM Processor:

The ALS/EVBRD/ARM7T9 board is a study Board which includes LPC2148 ARM7TDMI-S

micro-controller with USB 2.0 Full speed device, multiple UARTs, SPI, I2C & on- chip 512K Flash

and SRAM up to 40kB, produced by NXP semiconductors. The ARM7TDMI-S is a general-purpose

32-bit microprocessor, which offers high performance and very low power consumption. The ARM

processor is based on Reduced Instruction Set (RISC) architecture, And the instruction set and

related decode mechanism are much simpler than those of micro programmed Complex Instruction

Set Computers. This simplicity results in a high instruction throughput and impressive real-time

interrupt response from a small and cost-effective processor Core. Pipeline techniques are employed

so that all parts of the processing and memory systems can operate continuously. Typically, while

one instruction is being executed, its successor is being decoded, and a third instruction is being

fetched from memory. The ARM7TDMI-S processor also employs a unique architectural strategy

known as THUMB, which makes it ideally suited to high-volume applications with memory

restrictions or applications where code density is an issue. The key idea behind THUMB is that of a

super reduced instruction set. Essentially, the ARM7TDMI-S processor has two Instruction sets:

• The standard 32-bit ARM instruction set.

• A 16-bit THUMB instruction set.

The THUMB set’s 16-bit instruction length allows it to approach twice the density of standard

ARM code while retaining most of the Arm’s performance advantage over a traditional 16-bit

Processor using 16-bit registers. This is possible because THUMB code operates on the same

32-bit register set as ARM code. THUMB code is able to provide up to 65% of the code size of

ARM, and 160% of the performance of an equivalent ARM Processor connected to a 16-bit

Memory system.

Dept of ISE, BIT Page 3


21CS43 Microcontroller and Embedded System Laboratory

MICROCONTROLLER FEATURES:

Following are the features of 16 bit /32 bit LPC2148 Arm Microcontroller:
• PHILIPS LPC2148is a 16-bit or 32-bit Microcontroller in a LQFP64-pin Package.
• 40 kB of on-chip static RAM and 512 kB of on-chip flash memory. 128-bit wide
Interface/accelerator enables high-speed 60 MHz operation.
• The LPC2148 provides 100000 erase/write cycles and 20 years of Data-retention.
• In-System Programming/In-Application Programming (ISP/IAP) via on-chip boot
Loader software. Single flash sector or full chip erases takes 400ms and Flash
Programming takes 1ms per 256-byte line.
• USB 2.0 Full speed compliant device controller with 2 kB of endpoint RAM. In Addition, the
LPC2148 provides 8 kB of on-chip RAM accessible to USB by DMA.
• Embedded ICE-RT and Embedded Trace Macro cell (ETM) interfaces offer real time Debugging
with on-chip Real Monitor software and high-speed real-time tracing of Instruction execution.
• Two 10-bit ADCs provide a total of 14 analog inputs, with conversion times as low as 2.44μs per
channel.
• Single 10-bit DAC provides variable analog output.
• Two 32-bit Timers/External event Counters (with four Capture and four Compare Channels
each), PWM unit (six outputs) and watchdog.
• Low power Real-Time Clock (RTC) with independent power and 32 kHz clock input.
• Multiple serial interfaces including two UARTs (16C550 equivalent), two Fast I2Cbus( 400
kbit/s), SPI and SSP with buffering and variable data length capabilities.
• Vectored interrupt controller (VIC) with configurable priorities and vector addresses. Up to 45
numbers of 5 V tolerant fast general purpose I/O pins in a tiny LQFP64 Package.
• Up to nine edge or level sensitive external interrupt pins available.
• 60 MHz maximum CPU clock available from programmable on-chip PLL with settling Time of
100 μs.
• On-chip integrated oscillator operates with an external crystal in range from 1 MHz to 30 MHz
and with an external oscillator up to 50 MHz
• Power saving modes include Idle and Power-down.
• Individual power enable/disable of peripheral functions as well as peripheral clock Scaling for
additional power optimization.
• Processor wake-up from Power-down mode via external interrupt, USB, Brown-Out Detect
(BOD) or Real-Time Clock (RTC).
• Single power supply chip with Power-On Reset (POR) and BOD circuits: CPU Operating voltage
range of 3.0 V to 3.6 V (3.3 V } 10 %) with 5 V tolerant I/O pads.

Dept of ISE, BIT Page 4


21CS43 Microcontroller and Embedded System Laboratory

CONNECTORS DETAILS:

CN1 CONNECTOR: 28 pin 14 X 2 HEADER is connected to the controller.

PIN # DESCRIPTION PIN # DESCRIPTION


1 TDO-JTAG 15 GND
2 TD1-JTAG 16 RTCX1
3 TMS-JTAG 17 RESET
4 TRST-JTAG 18 RTCX2
5 TCK-JTAG 19 VBAT
6 P0.11 20 P0.25
7 P0.11 21 NC
8 P0.11 22 NC
9 P0.11 23 NC
10 3.3V 24 NC
11 GND 25 NC
12 3.3V 26 P1.24(2148)
13 NC 27 NC
14 NC 28 NC

CN2 CONNECTOR: 28 pin 14 X 2 HEADER is connected to the controller.

PIN # DESCRIPTION PIN # DESCRIPTION


1 NC 15 NC
2 NC 16 NC
3 NC 17 NC
4 P0.8 18 GND
5 3.3V 19 3.3V
6 NC 20 NC
7 NC 21 NC
8 GND 22 NC
9 NC 23 NC
10 NC 24 NC
11 P1.20 25 P0.22
12 P1.21 26 P0.22
13 P1.22 27 P0.22
14 P1.23 28 NC

CN3 CONNECTOR: 28 pin 14 X 2 HEADER is connected to the controller.

PIN # DESCRIPTION PIN # DESCRIPTION


1 NC 15 P1.17
2 NC 16 P0.7
3 P1.25 17 P0.6
4 P0.3 18 P0.5
5 P0.14 19 P0.4
6 3.3V 20 P0.3
7 GND 21 P0.2
8 P0.31 22 P0.15
9 P0.30 23 3.3V
10 P0.22 24 GND
11 P0.28 25 P0.14
12 P1.19 26 P0.13
13 P1.18 27 P0.12
14 P1.16 28 NC

Dept of ISE, BIT Page 5


21CS43 Microcontroller and Embedded System Laboratory

CN4 CONNECTOR: 28 pin 14 X 2 HEADER is connected to the controller.

PIN # DESCRIPTION PIN # DESCRIPTION


1 P0.21 15 NC
2 P0.20 16 NC
3 P0.19 17 NC
4 P0.18 18 NC
5 P0.17 19 NC
6 P0.16 20 NC
7 NC 21 3.3V
8 GND 22 GND
9 3.3V 23 TXD0
10 NC 24 RXD0
11 NC 25 RTCK-JTAG
12 NC 26 NC
13 NC 27 NC
14 NC 28 NC

CN7 CONNECTOR: 26 pin FRC connected to the controller, which is compatible Standard External Interfaces.
SHORT jumper JP4 while using External Interfaces.

PIN # DESCRIPTION PIN # DESCRIPTION


1 P0.12 14 P0.17
2 P0.13 15 P0.30
P0.10 P0.31 THROUGH
3 16 JP2(1,2) OR P1.24
THROUGH JP2(2,3)
4 P0.11 17 P0.28
5 P0.8 18 P0.28
6 P0.9 19 P0.28
7 P0.22 20 P0.28
8 P0.23 21 P0.28
9 P0.20 22 P0.28
P0.21 P0.14(Short JP13 to use
10 23
this pin)
11 P0.18 24 P0.15
P0.19 +5V THROUGH JP4
12 25
13 P0.16
13 P0.16 26 GND

TEST POINT DETAILS:

TP1 +5V
TP2 +3.3V
TP3 GROUND
TP4 PWM output

POT DETAILS:

50K ANVI POT for


POT1 LCD Contrast.

Dept of ISE, BIT Page 6


21CS43 Microcontroller and Embedded System Laboratory

General Block Diagram:

POWER SUPPLY:

The external power can be AC or DC, with a voltage between (9V/12V, 1A output) at 230V AC
input.
The ARM board produces +5V using an LM7805 voltage regulator, which provides supply to the
Peripherals.
LM1117 Fixed +3.3V positive regulator used for processor & processor related peripherals.

FLASH PROGRAMMING UTILITY:

NXP (Philips) NXP Semiconductors produce a range of Microcontrollers that feature both on-chip Flash
memory and the ability to be reprogrammed using In-System Programming technology.

Dept of ISE, BIT Page 7


21CS43 Microcontroller and Embedded System Laboratory

SYLLABUS
Course Learning Objectives
This course (21CS43) will enable students to:
CLO 1: Understand the fundamentals of ARM-based systems, including programming modules with registers and
the CPSR.
CLO 2: Use the various instructions to program the ARM controller.
CLO 3: Program various embedded components using the embedded C program.
CLO4: Identify various components, their purpose, and their application to the embedded system's applicability.
CLO 5: Understand the embedded system's real-time operating system and its application in IoT

Course Outcomes (COs)


After studying this course, students are able to
CO1: Describe the ARM microcontroller fundamentals, Instruction set, C compiler and
optimization, embedded system component, RTOS and IDE for embedded system.

CO2: Demonstrate the development of software program using Assembly level language and
embedded c using state of the art hardware platforms, based on microcontroller and different
sensors and peripherals.

CO3: Compare the difference between microprocessor and micro controller, RISC and CISC,
RTOS and NRTOS, Multitasking and Multi processing, space and Time complexity.

CO4: Design smart system using various I/O peripherals, sensors, embedded protocols like
UART,I2C, SPI using modern tools like keil IDE software for various domains like health
care, automation, agriculture, smart cities and others.

PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12

CO1 3 2
CO2 3 2 1 2
21CS43
CO3 2 1
CO4 3 2 2 1 2 2 2 1 2 2 2 2

PSO1 PSO2

CO1 2 2

CO2 2 2
21CS43
CO3 2 2

CO4 2 2

Dept of ISE, BIT Page 8


21CS43 Microcontroller and Embedded System Laboratory

Microcontroller and Embedded Systems Laboratory

Subject Code: 21CS43 CIE Marks: 40


Hours/Week: 0:2:2 Exam Hours: 03
Total Hours: 40 SEE Marks: 60

List of Programs
Sl.
Name of Experiment
No
PART A: Conduct the following experiments by writing program using ARM7TDMI/LPC2148 using an
evaluation board/simulator and the required software tool.
1. Write a program to multiply two 16-bit binary numbers.
2. Write a program to find the sum of first 10 integer numbers.
3. Write a program to find factorial of a number.
4. Write a program to add an array of 16-bit numbers and store the 32-bit result in internal RAM.
5. Write a program to find the square of a number (1 to 10) using look-up table.
6. Write a program to find the largest/smallest number in an array of 32 numbers
7. Write a program to arrange a series of 32-bit numbers in ascending/descending order.
8. Write a program to count the number of ones and zeros in two consecutive memory locations.
PART –B Conduct the following experiments on an ARM7TDMI/LPC2148 evaluation board using evaluation
version of Embedded 'C' & Keil Uvision-4 tool/compiler.
9. Display “Hello World” message using Internal UART.
10. Interface and Control a DC Motor.
11. Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.
12. Determine Digital output for a given Analog input using Internal ADC of ARM controller.
13. Interface a DAC and generate Triangular and Square waveforms.
14. Interface a 4x4 keyboard and display the key code on an LCD.
15. Demonstrate the use of an external interrupt to toggle an LED On/Off.
16. Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in between

Laboratory Outcomes:
The student should be able to:
• Develop and test program using ARM7TDMI/LPC2148
• Conduct the following Programs on an ARM7TDMI/LPC2148 evaluation board using evaluation version
of Embedded 'C' & Keil Uvision-4 tool/compiler.

Conduct of Practical Examination:

Three Unit Tests each of 20 Marks (duration 01 hour)


1. First test at the end of 5th week of the semester
2. Second test at the end of the 10th week of the semester
3. Third test at the end of the 15th week of the semester Two assignments each of 10 Marks
4. First assignment at the end of 4th week of the semester
5. Second assignment at the end of 9th week of the semester Practical Sessions need to be assessed by appropriate
rubrics and viva-voce method.
This will contribute to 20 marks.
• Rubrics for each Experiment taken average for all Lab components – 15 Marks.
• Viva-Voce– 5 Marks (more emphasized on demonstration topics) The sum of three tests, two assignments, and
practical sessions will be out of 100 marks and will be scaled down to 50 marks.

Dept of ISE, BIT Page 9


21CS43 Microcontroller and Embedded System Laboratory

1. Using Keil software, observe the various registers, dump, CPSR, with a simple ALP
programme

AREA MULTIPLY, CODE, READONLY

ENTRY ; Mark first instruction to execute

START

MOV r1, #6400 ; STORE FIRST NUMBER IN R0


MOV r2, #3200 ; STORE SECOND NUMBER IN R1
MUL r3, r1, r2 ; MULTIPLICATION

back B back

END ; Mark end of file

Program to multiply two 16 bit numbers defined in memory and display the result in
register

AREA MULTIPLY, CODE, READONLY ; NAME THE CODE BLOCK


ENTRY
START
LDR R0, =VALUE1
LDRH R1,[R0]
LDR R0,=VALUE2
LDRH R2,[R0]
MUL R3,R2,R1

BACK B BACK
VALUE1 DCW &BBBB ; OR 0XBBBB
VALUE2 DCW &CCCC ; OR 0XCCCC
END

Dept of ISE, BIT Page 10


21CS43 Microcontroller and Embedded System Laboratory

Output:

Dept of ISE, BIT Page 11


21CS43 Microcontroller and Embedded System Laboratory

2. Write a program to find the sum of first 10 integer numbers.

AREA SUM, CODE, READONLY

ENTRY

MOV R1, #10 ; load 10 to register


MOV R2, #0 ; empty the register to store result
Loop
ADD R2, R2, R1 ; add the content of R1 with result at R2
SUBS R1, #0x01 ; Decrement R1 by 1
BNE loop ; repeat till r1 goes 0

back B back ; jumps back to C code


END

Output:

Dept of ISE, BIT Page 12


21CS43 Microcontroller and Embedded System Laboratory

3. Write a program to find factorial of a number.

AREA FACTORIAL, CODE, READONLY

ENTRY ; Mark first instruction to execute

START

MOV r0, #3 ; STORE FACTORIAL NUMBER IN R0


MOV r1, r0 ; MOVE THE SAME NUMBER IN R1

FACT SUBS r1, r1, #1 ; SUBTRACTION


CMP r1, #1 ; COMPARISON
BEQ STOP
MUL r3, r0, r1 ; MULTIPLICATION
MOV r0, r3 ; Result
BNE FACT ; BRANCH TO THE LOOP IF NOT EQUAL
STOP

back B back
NOP
NOP
Dept of ISE, BIT Page 13
21CS43 Microcontroller and Embedded System Laboratory

END ; Mark end of file

Output:

4. Write a program to add an array of 16 bit numbers and store the 32 bit result
in internal RAM

AREA ADDITION, CODE, READONLY

ENTRY ; MARK FIRST INSTRUCTION TO


EXECUTE

START
MOV R5, #6 ; INTIALISE COUNTER TO 6(i.e. N=6)
MOV R0, #0 ; INTIALISE SUM TO ZERO
LDRR1, =VALUE1 ; LOADS THE ADDRESS OF FIRSTVALUE

LOOP
LDRH R3, [R1], #02 ; READ 16 BIT DATA
ADD R0, R0, R3 ; ADD R2=R2+R3
SUBS R5, R5, #1 ; DECREMENT COUNTER
CMP R5, #0
BNE LOOP ; LOOK BACK TILL ARRAY ENDS
LDR R4, =RESULT ; LOADS THE ADDRESS OF RESULT
STR R0, [R4] ; STORES THE RESULT IN R1
JMP B JMP
VALUE1 DCW 0X1111, 0X2222, 0X3333, 0XAAAA, 0XBBBB, 0XCCCC

Dept of ISE, BIT Page 14


21CS43 Microcontroller and Embedded System Laboratory
; ARRAY OF 16 BIT NUMBERS (N=6)

AREA DATA2, DATA, READWRITE; TO STORE RESULT IN GIVEN ADDRESS

RESULT DCD 0X0

END ; MARK END OF FILE

Output:

5. Write a program to find the square of a number (1 to 10) using look-up table.

AREA SQUARE, CODE, READONLY

ENTRY ; Mark first instruction to execute

START
LDR R0, = TABLE1 ; Load start address of Lookup table

MOV R1,#0x03 ; Load no whose square is to be find


MOV R1, R1, LSL#0x2 ; Generate address corresponding to square off
given no
ADD R0, R0, R1 ; Load address of element in Lookup
table
LDR R3, [R0] ; Get square of given no in R3

BACK B BACK
NOP
NOP ; Lookup table contains Squares of no’s from 0 to 10 (in
hex)
No DCD 02
TABLE1 DCD 0X00000000; SQUARE OF 0=0
DCD 0X00000001; SQUARE OF 1=1
DCD 0X00000004; SQUARE OF 2=4
DCD 0X00000009; SQUARE OF 3=9

Dept of ISE, BIT Page 15


21CS43 Microcontroller and Embedded System Laboratory
DCD 0X00000010; SQUARE OF 4=16
DCD 0X00000019; SQUARE OF 5=25
DCD 0X00000024; SQUARE OF 6=36
DCD 0X00000031; SQUARE OF 7=49
DCD 0X00000040; SQUARE OF 8=64
DCD 0X00000051; SQUARE OF 9=81
DCD 0X00000064; SQUARE OF 10=100

END ; Mark end of file

Output:

6. Write a program to find the largest/smallest number in an array of 32 numbers

LARGEST NUMBER:

AREA LARGEST, 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 OF FIRST VALUE
LDR R2,[R1],#4 ; WORD ALIGN T0 ARRAY ELEMENT
LOOP
LDR R4, [R1], #4 ; WORD ALIGN T0 ARRAY ELEMENT
CMP R2, R4 ; COMPARE NUMBERS
BHI 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


Dept of ISE, BIT Page 16
21CS43 Microcontroller and Embedded System Laboratory
STR R2, [R4] ; STORES THE RESULT IN R2

BACK B BACK
NOP
NOP

; ARRAY OF 32 BIT NUMBERS (N=7)

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

AREA DATA2, DATA, READWRITE; TO STORE RESULT IN GIVEN ADDRESS

RESULT DCD 0X0

END ; Mark end of file

SMALLEST NUMBER:

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 OF FIRST VALUE
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 R1

Dept of ISE, BIT Page 17


21CS43 Microcontroller and Embedded System Laboratory

BACK B BACK
NOP
NOP

; ARRAY OF 32 BIT NUMBERS (N=7)

VALUE1
DCD 0X44444444 ;
DCD 0X22222222 ;
DCD 0X11111111 ;
DCD 0X22222222 ;
DCD 0XAAAAAAAA ;
DCD 0X88888888 ;
DCD 0X99999999 ;

AREA DATA2, DATA, READWRITE; TO STORE RESULT IN GIVEN ADDRESS

RESULT DCD 0X0

END ; Mark end of file

Output: LARGEST NUMBER:

Dept of ISE, BIT Page 18


21CS43 Microcontroller and Embedded System Laboratory

Output: SMALLEST NUMBER:

7. Write a program to arrange a series of 32 bit numbers in ascending/descending


order.

Dept of ISE, BIT Page 19


21CS43 Microcontroller and Embedded System Laboratory

ASCENDING ORDER:
AREA ASCENDING, CODE, READONLY

ENTRY ; Mark first instruction to execute

START

MOV R8, #4 ; INTIALISE COUNTER TO 4(i.e. N=4)


LDR R2, =CVALUE ; ADDRESS OF CODE REGION
LDR R3, =DVALUE ; ADDRESS OF DATA REGION

LOOP0
LDR R1, [R2], #4 ; LOADING VALUES FROM CODE REGION
STR R1, [R3], #4 ; STORING VALUES TO DATA REGION

SUBS R8, R8, #1 ; DECREMENT COUNTER


CMP R8, #0 ; COMPARE COUNTER TO 0
BNE LOOP0 ; LOOP BACK TILL ARRAY ENDS

START1 MOV R5, #3 ; INTIALISE COUNTER TO 3(i.e. N=4)


MOV R7, #0 ; FLAG TO DENOTE EXCHANGE HAS OCCURED
LDR R1, =DVALUE ; LOADS THE ADDRESS OF FIRST VALUE

LOOP LDR R2, [R1], #4 ; WORD ALIGN T0 ARRAY ELEMENT


LDR R3, [R1] ; LOAD SECOND NUMBER
CMP R2, R3 ; COMPARE NUMBERS
BLT LOOP2 ; IF THE FIRST NUMBER IS < THEN GOTO LOOP2
STR R2, [R1], #-4 ; INTERCHANGE NUMBER R2 & R3
STR R3, [R1] ; INTERCHANGE NUMBER R2 & R3
MOV R7, #1 ; FLAG DENOTING EXCHANGE HAS TAKEN PLACE
ADD R1, #4 ; RESTORE THE PTR

LOOP2

SUBS R5, R5, #1 ; DECREMENT COUNTER


CMP R5, #0 ; COMPARE COUNTER TO 0
BNE LOOP ; LOOP BACK TILL ARRAY ENDS
CMP R7, #0 ; COMPARING FLAG
BNE START1 ; IF FLAG IS NOT ZERO THEN GO TO START1 LOOP
BACK B BACK
NOP
NOP
; ARRAY OF 32 BIT NUMBERS (N=4) IN CODE REGION
CVALUE
DCD 0X44444444 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0X22222222 ;

AREA DATA1, DATA, READWRITE ;


; ARRAY OF 32 BIT NUMBERS IN DATA REGION
DVALUE
DCD 0X00000000 ;
END ; Mark end of file

DESCENDING ORDER:
Dept of ISE, BIT Page 20
21CS43 Microcontroller and Embedded System Laboratory
AREA DESCENDING, CODE, READONLY

ENTRY ; Mark first instruction to execute

START
MOV R8, #4 ; INTIALISE COUNTER TO 4(i.e. N=4)
LDR R2, =CVALUE ; ADDRESS OF CODE REGION
LDR R3, =DVALUE ; ADDRESS OF DATA REGION

LOOP0
LDR R1, [R2], #4 ; LOADING VALUES FROM CODE REGION
STR R1, [R3], #4 ; STORING VALUES TO DATA REGION

SUBS R8, R8, #1 ; DECREMENT COUNTER


CMP R8, #0 ; COMPARE COUNTER TO 0
BNE LOOP0 ; LOOP BACK TILL ARRAY ENDS

START1 MOV R5, #3 ; INTIALISE COUNTER TO 3(i.e. N=4)


MOV R7, #0 ; FLAG TO DENOTE EXCHANGE HAS
OCCURED
LDR R1, =DVALUE ; LOADS THE ADDRESS OF FIRST VALUE

LOOP LDR R2, [R1], and #4 ; WORDALIGNS T0 ARRAY ELEMENT


LDR R3, [R1] ; LOAD SECOND NUMBER
CMP R2, R3 ; COMPARE NUMBERS
BGT LOOP2 ; IF THE FIRST NUMBER IS > THEN GOTO
LOOP2
STR R2, [R1], #-4 ; INTERCHANGE NUMBER R2 & R3
STR R3, [R1] ; INTERCHANGE NUMBER R2 & R3
MOV R7, #1 ; FLAG DENOTING EXCHANGE HAS
TAKEN PLACE
ADD R1, #4 ; RESTORE THE PTR

LOOP2
SUBS R5, R5, #1 ; DECREMENT COUNTER
CMP R5, #0 ; COMPARE COUNTER TO 0
BNE LOOP ; LOOP BACK TILL ARRAY ENDS
CMP R7, #0 ; COMPARING FLAG
BNE START1 ; IF FLAG IS NOT ZERO THEN GO TO START1
LOOP

BACK B BACK
NOP
NOP

; ARRAY OF 32 BIT NUMBERS (N=4) IN CODE REGION


CVALUE
DCD 0X44444444 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0X22222222 ;
AREA DATA1, DATA, READWRITE ;
; ARRAY OF 32 BIT NUMBERS IN DATA REGION
DVALUE
DCD 0X00000000 ;

END ; Mark end of file

Dept of ISE, BIT Page 21


21CS43 Microcontroller and Embedded System Laboratory

Output:ASCENDING ORDER:

Output:DESCENDING ORDER:

8. Write a program to count the number of ones and zeros in two consecutive memory
locations.
AREA ONEZERO, CODE, READONLY

Dept of ISE, BIT Page 22


21CS43 Microcontroller and Embedded System Laboratory

ENTRY ; Mark first instruction to execute

START
MOV R2, #0 ; COUNTER FOR ONES
MOV R3, #0 ; COUNTER FOR ZEROS
MOV R7, #1 ; COUNTER TO GET TWO WORDS
LDR R6, =VALUE ; LOADS THE ADDRESS OF VALUE
LOOP MOV R1, #32 ; 32 BITS COUNTER
LDR R0, [R6], #4 ; GET THE 32 BIT VALUE
LOOP0 MOVS R0, R0, ROR #1 ; RIGHT SHIFT TO CHECK CARRY BIT (1's/0's)
BHI ONES ; IF CARRY BIT IS 1 GOTO ONES BRANCH OTHERWISE
NEXT
ZEROS ADD R3, R3, #1 ; IF CARRY BIT IS 0 THEN INCREMENT THE COUNTER BY
1(R3)
B LOOP1 ; BRANCH TO LOOP1
ONES ADD R2, R2, #1 ; IF CARRY BIT IS 1 THEN INCREMENT THE COUNTER BY
1(R2)
LOOP1 SUBS R1, R1, #1 ; COUNTER VALUE DECREMENTED BY 1
BNE LOOP0 ; IF NOT EQUAL GOTO TO LOOP0 CHECKS
32BIT
SUBS R7, R7, #1 ; COUNTER VALUE DECREMENTED BY 1
CMP R7, #0 ; COMPARE COUNTER R7 TO 0
BNE LOOP ; IF NOT EQUAL GOTO TO LOOP

BACK B BACK
NOP
NOP
VALUE DCD 0X11111111, 0XAA55AA55; TWO VALUES IN AN ARRAY

END ; Mark end of file

Output:

9. Display “Hello World” message using Internal UART.

#include<lpc214x.h>

Dept of ISE, BIT Page 23


21CS43 Microcontroller and Embedded System Laboratory

voiduart_init(void);
unsignedint delay;
unsigned char *ptr;
unsigned char arr[]="HELLO WORLD\r";

int main()
{
while(1)
{
uart_init();
ptr = arr;
while(*ptr!='\0')
{
U0THR=*ptr++;
while(!(U0LSR & 0x40)== 0x40);
for(delay=0;delay<=600;delay++);
}
for(delay=0;delay<=60000;delay++);
}
}
voiduart_init(void)
{
PINSEL0=0X0000005; //select TXD0 and RXD0 lines
U0LCR = 0X00000083; //enable baud rate divisor loading and
U0DLM = 0X00; //select the data format
U0DLL = 0x13; //select baud rate 9600 bps
U0LCR = 0X00000003;

Note: Jumper (JP7) is common for all the programs.

No Jumper used for “Hello world” program.

10. Interface and Control a DC Motor.


Dept of ISE, BIT Page 24
21CS43 Microcontroller and Embedded System Laboratory

voidclock_wise(void);
voidanti_clock_wise(void);
unsignedint j=0;

int main()
{
IO0DIR= 0X00000900;
IO0SET= 0X00000100; //P0.8 should always high.

while(1)
{
clock_wise();
for(j=0;j<400000;j++); //delay
anti_clock_wise();
for(j=0;j<400000;j++); //delay
} //End of while(1)
} //End of Main

voidclock_wise(void)
{
IO0CLR = 0x00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000900; //Selecting the P0.11 line for clockwise and turn
on motor
}

voidanti_clock_wise(void)
{
IO0CLR = 0X00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000100; //not selecting the P0.11 line for Anti clockwise

Note: Jumper (JP7) is common for all the programs.

No Jumper used for “DC Motor” program.

11. Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.

Dept of ISE, BIT Page 25


21CS43 Microcontroller and Embedded System Laboratory
#include <LPC21xx.h>

voidclock_wise(void) ;
voidanti_clock_wise(void) ;

unsigned int var1 ;


unsigned long inti = 0 , j = 0 , k = 0 ;

int main(void)
{
PINSEL2 = 0x00000000; //P1.20 to P1.23 GPIO
IO1DIR |= 0x00F00000 ; //P1.20 to P1.23 made as output

while(1)
{

for( j = 0 ; j < 50 ; j++ ) // 20 times in Clock wise Rotation


clock_wise() ;

for( k = 0 ; k < 65000 ; k++ ) ; // Delay to show anti_clock Rotation

for( j=0 ; j < 50 ; j++ ) // 20 times in Anti Clock wise Rotation


anti_clock_wise() ;

for( k = 0 ; k < 65000 ; k++ ) ; // Delay to show ANTI_clock Rotation

}// End of main

voidclock_wise(void)
{
var1 = 0x00080000; //For Clockwise
for(i = 0 ; i<= 3 ; i++ ) // for A B C D Stepping
{
var1 <<= 1 ;

IO1CLR =0x00F00000 ; //clearing all 4 bits

IO1SET = var1 ; // setting perticular bit

for( k = 0 ; k < 3000 ; k++ ); //for step speed variation

voidanti_clock_wise(void)
{
var1 = 0x00800000 ; //For Anticlockwise

IO1CLR =0x00F00000 ; //clearing all 4 bits

IO1SET = var1 ;

for( k = 0 ; k < 3000 ; k++ ) ;


Dept of ISE, BIT Page 26
21CS43 Microcontroller and Embedded System Laboratory

for(i = 0 ; i< 3 ; i++ ) // for A B C D Stepping


{
var1 >>=1; //rotating bits
IO1CLR =0x00F00000 ; // clar all bits before setting

IO1SET = var1 ; // setting


perticular bit

for( k = 0 ; k < 3000 ; k++ ) ; //for step speed variation

}
}

Note: Jumper (JP7) is common for all the programs.

Jumper (JP13) used for “Stepper Motor” program.

12. Determine Digital output for a given Analog input using Internal ADC of ARM
controller.

Dept of ISE, BIT Page 27


21CS43 Microcontroller and Embedded System Laboratory

#include <lpc214x.h>
#include <Stdio.h>

voidlcd_init(void);
voidwr_cn(void);
voidclr_disp(void);
void delay(unsigned int);
voidlcd_com(void);
voidwr_dn(void);
voidlcd_data(void);

unsignedintdata_lcd=0;
unsignedintadc_value=0,temp_adc=0,temp1,temp2;
float temp;
charvar[15],var1[15];
char *ptr,arr[]= "ADC O/P= ";
char *ptr1,dis[]="A I/P = ";

#define vol 3.3 //Reference voltage


#definefullscale 0x3ff //10 bit adc

int main()
{
PINSEL1 = 0X00040000; //AD0.4 pin is selected(P0.25)
IO0DIR = 0x000000FC; //configure o/p lines for lcd

delay(3200);
lcd_init(); //LCD initialization
delay(3200);
clr_disp(); //clear display
delay(3200); //delay

ptr = dis;
temp1 = 0x80; //Display starting address of first line 1
thpos
lcd_com();
delay(800);

while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}

ptr1 = arr;
temp1 = 0xC0; //Display starting address of second line 4 thpos
lcd_com();
delay(800);

while(*ptr1!='\0')
{
temp1 = *ptr1;

lcd_data();
ptr1 ++;
Dept of ISE, BIT Page 28
21CS43 Microcontroller and Embedded System Laboratory
}

//infinite loop
while(1)
{
//CONTROL register for ADC
AD0CR = 0x01200010; //command register for ADC-
AD0.4

while(((temp_adc = AD0GDR) &0x80000000) == 0x00000000); //to


check the interrupt bit

adc_value = AD0GDR; //reading the ADC value


adc_value>>=6;
adc_value&= 0x000003ff;
temp = ((float)adc_value * (float)vol)/(float)fullscale;
sprintf(var1,"%4.2fV",temp);
sprintf(var,"%3x",adc_value);

temp1 = 0x89;
lcd_com();
delay(1200);
ptr = var1;

while(*ptr!='\0')
{
temp1=*ptr;
lcd_data();
ptr++;
}

temp1 = 0xc9;
lcd_com();
delay(1200);

ptr1 = var;
while(*ptr1!='\0')
{
temp1=*ptr1;
lcd_data();
ptr1++;
}
} // end of while(1)
} //end of main()

//lcd initialization
voidlcd_init()
{
temp2=0x30;
wr_cn();
delay(800);

temp2=0x30;
wr_cn();
delay(800);

temp2=0x30;
Dept of ISE, BIT Page 29
21CS43 Microcontroller and Embedded System Laboratory
wr_cn();
delay(800);

temp2=0x20;
wr_cn();
delay(800);

temp1 = 0x28;
lcd_com();
delay(800);

temp1 = 0x0c;
lcd_com();
delay(800);

temp1 = 0x06;
lcd_com();
delay(800);

temp1 = 0x80;
lcd_com();
delay(800);
}

voidlcd_com(void)
{
temp2= temp1 & 0xf0;
wr_cn();
temp2 = temp1 & 0x0f;
temp2 = temp2 << 4;
wr_cn();
delay(500);
}

// command nibble o/p routine


voidwr_cn(void) // write command reg
{
IO0CLR = 0x000000FC; // clear the port lines.
IO0SET = temp2; // Assign the value to the PORT lines
IO0CLR = 0x00000004; // clear bit RS = 0
IO0SET = 0x00000008; // E=1
delay(10);
IO0CLR = 0x00000008;
}

// data nibble o/p routine


voidwr_dn(void)
{
IO0CLR = 0x000000FC; // clear the port lines.
IO0SET = temp2; // Assign the value to the PORT lines
IO0SET = 0x00000004; // set bit RS = 1
IO0SET = 0x00000008; // E=1
delay(10);
IO0CLR = 0x00000008;
}

// data o/p routine which also outputs high nibble first


Dept of ISE, BIT Page 30
21CS43 Microcontroller and Embedded System Laboratory
// and lower nibble next
voidlcd_data(void)
{
temp2 = temp1 & 0xf0;
wr_dn();
temp2= temp1 & 0x0f;
temp2= temp2 << 4;
wr_dn();
delay(100);
}

void delay(unsigned int r1)


{
unsignedint r;
for(r=0;r<r1;r++);
}
voidclr_disp(void)
{
temp1 = 0x01;
lcd_com();
delay(500);
}

Note: Jumper (JP7) is common for all the programs.

Jumper (JP9) used for “ARM Controller” program.

13. Interface a DAC and generate Triangular and Square waveforms.

Dept of ISE, BIT Page 31


21CS43 Microcontroller and Embedded System Laboratory
Triangular:

#include <LPC21xx.h>
unsigned long int temp=0x00000000;

int main ()
{
unsignedinti=0;

IO0DIR=0x00FF0000;

while(1)
{
// output 0 to FE
for(i=0;i!=0xFF;i++)
{
temp=i;
temp = temp << 16;
IO0PIN=temp;
}

// output FF to 1
for(i=0xFF; i!=0;i--)
{
temp=i;
temp = temp << 16;
IO0PIN=temp;
}
}//End of while(1)
}//End of main()

Square:

#include <lpc21xx.h>
unsignedintvar;
void delay(void);

int main ()
{
PINSEL0 = 0x00000000 ; // Configure
P0.0 to P0.15 as GPIO
PINSEL1 = 0x00000000 ; // Configure
P0.16 to P0.31 as GPIO
IO0DIR = 0x00FF0000 ;

while(1)
{
IO0PIN = 0x00000000;
var= 0x00000000;
delay();
IO0PIN = 0x00FF0000;
var= 0x00FF0000;
delay();
}
}

Dept of ISE, BIT Page 32


21CS43 Microcontroller and Embedded System Laboratory

void delay(void)
{
unsignedinti=0;
for(i=0;i<=95000;i++);
}

Note: Jumper (JP7) is common for all the programs.

Jumper (JP3,JP11) used for “DAC” program.

14. Interface a 4x4 keyboard and display the key code on an LCD.

Dept of ISE, BIT Page 33


21CS43 Microcontroller and Embedded System Laboratory
#include<lpc214x.h>
#include<stdio.h>

//Function prototypes
voidlcd_init(void);
voidwr_cn(void);
voidclr_disp(void);
void delay(unsigned int);
voidlcd_com(void);
voidwr_dn(void);
voidlcd_data(void);

unsigned char temp1;


unsigned long inttemp,r=0;
unsigned char *ptr,disp[] = "ALS,R&D SECTION,",disp1[] = "BENGALURU-58";

int main()
{
IO0DIR = 0x000000FC; //configure o/p lines for lcd
IO0PIN = 0X00000000;

delay(3200); //delay
lcd_init(); //lcdintialisation
delay(3200); //delay
clr_disp(); //clear display
delay(3200); //delay

//........LCD DISPLAY TEST.........//


temp1 = 0x80; //Display starting address of first line
1stpos
lcd_com();

ptr = disp;
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}

temp1 = 0xC0; // Display starting address of second line 4thpos


lcd_com();

ptr = disp1;
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
while(1);
} //end of main()

// lcd initialisation routine.

voidlcd_init(void)
{
Dept of ISE, BIT Page 34
21CS43 Microcontroller and Embedded System Laboratory
temp = 0x30;
wr_cn();
delay(3200);

temp = 0x30;
wr_cn();
delay(3200);

temp = 0x30;
wr_cn();
delay(3200);

temp = 0x20; // change to 4 bit mode from default 8 bit mode


wr_cn();
delay(3200);

// load command for lcd function setting with lcd in 4 bit mode,
// 2 line and 5x7 matrix display

temp = 0x28;
lcd_com();
delay(3200);

// load a command for display on, cursor on and blinking off


temp1 = 0x0C;
lcd_com();
delay(800);

// command for cursor increment after data dump


temp1 = 0x06;
lcd_com();
delay(800);

temp1 = 0x80; // set the cursor to beginning of line 1


lcd_com();
delay(800);
}

voidlcd_com(void)
{
temp = temp1 & 0xf0;
wr_cn();
temp = temp1 & 0x0f;
temp = temp << 4;
wr_cn();
delay(500);
}

// command nibble o/p routine


voidwr_cn(void) //write command reg
{
IO0CLR = 0x000000FC; // clear the port lines.
IO0SET = temp; // Assign the value to the PORT lines
IO0CLR = 0x00000004; // clear bit RS = 0

IO0SET = 0x00000008; // E=1


delay(10);
Dept of ISE, BIT Page 35
21CS43 Microcontroller and Embedded System Laboratory
IO0CLR = 0x00000008;
}

// data nibble o/p routine


voidwr_dn(void) ////write data reg
{
IO0CLR = 0x000000FC; // clear the port lines.
IO0SET = temp; // Assign the value to the PORT lines
IO0SET = 0x00000004; // set bit RS = 1
IO0SET = 0x00000008; // E=1
delay(10);
IO0CLR = 0x00000008;
}

// data o/p routine which also outputs high nibble first


// and lower nibble next
voidlcd_data(void)
{
temp = temp1 & 0xf0;
temp = temp ;//<< 6;
wr_dn();
temp= temp1 & 0x0f;
temp= temp << 4;
wr_dn();
delay(100);
}

voidclr_disp(void)
{
// command to clear lcd display
temp1 = 0x01;
lcd_com();
delay(500);
}

void delay(unsigned int r1)


{
for(r=0;r<r1;r++);
}

Note: Jumper (JP7) is common for all the programs.

No Jumper used for “4*4 Keyboard” program.

15. Demonstrate the use of an external interrupt to toggle an LED On/Off.

Dept of ISE, BIT Page 36


21CS43 Microcontroller and Embedded System Laboratory

#include <LPC21xx.h>

unsignedintdelay,lg;

int main ()
{
PINSEL1 = 0x00000000 ; // Configure P0.16 to P0.31 as GPIO
IO0DIR = 0x00FF0000 ; // Configure P0.16 to P0.23 as Output

while(1)
{
IO0CLR = 0x00FF0000; // CLEAR (0) P0.10 to P0.13 and P0.18 to P0.21, LEDs
ON
for(delay=0; delay<500000; delay++); // delay
IO0SET = 0x00FF0000; // SET (1) P0.10 to P0.13 and P0.18 to P0.21, LEDs
OFF
for(delay=0; delay<500000; delay++); // delay
}

Note: Jumper (JP7) is common for all the programs.

Jumper (JP12, JP6) used for “LED” program.

Dept of ISE, BIT Page 37


21CS43 Microcontroller and Embedded System Laboratory
16. Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate
delay in between.

#include <LPC21XX.h>
// ****** changes made by JK 0n 090115
/*

\\\\\\\\\\\\\\\DISPLAY ARE CONNECTED IN COMMON CATHODE MODE\\\\\\\\\\\\\\\\\\\\\


Port0 Connected to data lines of all 7 segement displays
a
----
f| g |b
|----|
e| |c
---- . dot
d
a = P0.16
b = P0.17
c = P0.18
d = P0.19
e = P0.20
f = P0.21
g = P0.22
dot = P0.23

Select lines for four 7 Segments


DIS1 P0.28
DIS2 P0.29
DIS3 P0.30
DIS4 P0.31

Values Correspoinding to Alphabets 1, 2, 3 and 4


*/

unsignedint delay;
unsignedintSwitchcount=0;
unsignedintDisp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,0x006D0000,
0x007D0000, 0x00070000, 0x007F0000, 0x006F0000, 0x00770000,0x007C0000,
0x00390000, 0x005E0000, 0x00790000, 0x00710000 };

#define SELDISP1 0x10000000 //P0.28


#define SELDISP2 0x20000000 //P0.29
#define SELDISP3 0x40000000 //P0.30
#define SELDISP4 0x80000000 //P0.31
#define ALLDISP 0xF0000000 //Select all display
#define DATAPORT 0x00FF0000 //P0.16 to P0.23 Data lines connected to drive
Seven Segments

int main (void)


{
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
IO0DIR = 0xF0FF0000;
IO1DIR = 0x00000000;

Dept of ISE, BIT Page 38


21CS43 Microcontroller and Embedded System Laboratory

while(1)
{
IO0SET |= ALLDISP; // select all digits
IO0CLR = 0x00FF0000; // clear the data lines to 7-
segment displays
IO0SET = Disp[Switchcount]; // get the 7-segment display value from
the array

if(!(IO1PIN & 0x00800000)) // if the key is pressed


{
for(delay=0;delay<100000;delay++) // delay
{}

if((IO1PIN & 0x00800000)) // check to see if key has been released


{
Switchcount++;
if(Switchcount == 0x10) // 0 to F has been displayed ? go back to
0
{
Switchcount = 0;
IO0CLR = 0xF0FF0000;
}
}
}
}
}

Note: Jumper (JP7) is common for all the programs.

Jumper (JP2, JP1) used for “7-Segment” program. JP2 should be shorted towards outside board
side for the pin 1, 2.

Dept of ISE, BIT Page 39

You might also like