MCES Lab Manual
MCES Lab Manual
Prepared by
Abhishek S. Rao
Asst. Professor, Dept. of IS&E
for
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
Course Outcomes:
Students will be able to:
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).
Head-ISE
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.
Here is simple guide to start working with Keil uVision which can be used for
Debugging program
This is simple guide on Keil uVision 4 though also applicable on previous versions also.
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.
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
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]
INTRODUCTORY PROGRAMS
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
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.
1+2+3+4+5+6+7+8+9+10=2D
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.
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.
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.
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.
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’
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
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.
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.
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++);
}
}
# 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++);
}
Procedure: Connect Stepper Motor to FRC1 and the rotation starts clockwise followed by anti-
clockwise rotations.
#include<LPC214X.H>
int main(void)
{
IO0DIR = 0x00FF0000;
while(1)
{
clockwise(10, 100);
anticlockwise(10, 100);
delay(100);
}
}
void delay(int n)
{
int i,j;
for (i=1; i<=n; i++)
for(j=0; j<=10000; j++);
}
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++);
}
#include<LPC214X.H>
#include<STRING.H>
#include<stdio.h>
#include<keyboard.h>
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;
}
#define O1 0X00E00000
#define O2 0X00D00000
#define O3 0X00B00000
#define O4 0X00700000
#define I1 0x000E0000
#define I2 0x000D0000
#define I3 0x000B0000
#define I4 0x00070000
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
}
}
#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++);
}
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--;
}
}