SlideShare a Scribd company logo
Microcontroller & Embedded Systems
Course Code: 21CS43 CIE Marks 50
Teaching Hours/Week
(L:T:P:S)
3:0:2:0 SEE Marks 50
Total Hours of Pedagogy 40 T + 20 P Total Marks 100
Credits 04 Exam Hours 03
Course Learning Objectives:
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.
CLO 4: 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 outcome (Course Skill Set)
At the end of the course, the student will be able to:
CO 1. Explain C-Compilers and optimization
CO 2. Describe the ARM microcontroller's architectural features and program module.
CO 3. Apply the knowledge gained from programming on ARM to different applications.
CO 4. Program the basic hardware components and their application selection method.
CO 5. Demonstrate the need for a real-time operating system for embedded system applications.
Laboratory Component:
Module 1: ARM Processor Fundamentals
Sl. No Name
Page
no
1
Using Keil software, observe the various registers, dump, CPSR, with a
simple ALP programme
Module 2: Introduction to the ARM Instruction Set
2 Write a program to find the sum of the first 10 integer numbers.
3 Write a program to find the 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 a look-up
table.
6
Write a program to find the largest or smallest number in an array of 32
numbers
Module 3: C Compilers and Optimization, ARM programming using Assembly
language
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.
9 Display “Hello World” message using Internal UART.
Module 4: Embedded System Components:
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.
Module 5: RTOS and IDE for Embedded System Design:
1 Demonstration of IoT applications by using Arduino and Raspberry Pi
ADDITIONAL EXPERIMENTS
1 ALP to determine the given 16 bit number is ODD or EVEN.
2
Interface a simple Switch and display its status through Relay, Buzzer and
LED.
Conduct of Practical Examination:
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)
Introduction to ARM7
TheLPC2141/42/44/46/48microcontrollersarebasedona16-bit/32-bitARM7TDMI-SCPUwith
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
criticalcodesizeapplications,thealternative16-bitThumbmodereducescodebymorethan30
% 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
asaccesscontrolandpoint-ofsale.SerialcommunicationsinterfacesrangingfromaUSB2.0Full- 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
channelsand45fastGPIOlineswithuptonineedgeorlevelsensitiveexternalinterruptpinsmake these
microcontrollers suitable for industrial control and medical systems.
BLOCK Diagram of ARMLPC2148
Step 1: After opening Keil uV4, Go to Project tab and click on close project
Then 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.]
Step4:NowgotoFileandcreatenewfileandsaveitwith .C extension if you will write program
in C languageorsavewith .asmforassemblylanguage.
Step5:Nowwrite your program and save it again.
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
Step6:Afterthatonleftyouseeprojectwindow[ifit’snotthere….gotoViewtabandclickon project window.
Right Click on Target and click on options for target.
Here you can change your device also
Click output tab here and check the HEX files, if you are generating hex files
Step 7: Now expand Target and you will see the Source Group
Step 8: Build Target and Run the program if no errors
Module1: ARM Processor Fundamentals
Program 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 R1
MOV R2, #3200 ; STORE SECOND NUMBER IN R2
MUL R3, R1, R2 ; MULTIPLICATION
NOP
NOP
END ; Mark end of file
Output:
1st Input : Register R1 =6400
2nd Input : Register R2 =3200
Result : Register R3=13880000
Module2: Introduction to the ARM Instruction Set
Program 2: Write a program to find the sum of the 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:
Result can be viewed in Register R2 in hex decimal values
Program3:Writeaprogramtofindfactorialofanumber.
AREA FACTORIAL, CODE, READONLY
ENTRY ; Mark first instruction to execute
START
MOV R0, #7 ; 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
NOP
NOP
NOP
END ; Mark end of file
Output:
Result can be viewed in Register R0
Program 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
LDR R1, =VALUE1 ; LOADS THE ADDRESS OF 1ST VALUE
LOOP
LDRH R3, [R1], #02 ; READ 16 BIT DATA
ADD R0, R0, R3 ; ADD R0=R0+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 R0
JMP B JMP
VALUE1 DCW 0X1111,0X2222,0X3333,0XAAAA,0XBBBB,0XCCCC
; 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:
Result can be viewed in Memory location address specified in R4 and also in register R0
MES LAB MANUAL for engineering students.
Program 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
LDR R2, = 0X40000000
LDR R1, [R2] ; Load no whose square is to be find
MOV R1, R1, LSL #0x2
; Generate address corresponding to square of given no
ADD R0, R0, R1 ; Load address of element in Lookup table
LDR R3, [R0] ; Get square of given no in R3
NOP
NOP
NOP
;Lookup table contains Squares of nos from 0 to 10 (in hex)
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
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:
Enter Input number in memory location specified in Register R2
Result can be viewed in Register R3
MES LAB MANUAL for engineering students.
Program6a: Write a programto find the largest/smallest number in an array
of 32 numbers.
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 1st NUMBER IS > THEN GOTO LOOP1
MOV R2, R4 ; IF THE 1st 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 0XAAAAAAAA
DCD 0X88888888
DCD 0X99999999
AREA DATA2, DATA, READWRITE
RESULT DCD 0X0 ; ; TO STORE RESULT IN GIVEN ADDRESS
END ; Mark end of file
Output:
Result can be viewed in Memory location address specified in R4 and also in register
R2
Experiment No 6b. Write a program to find the smallest number in an array of 32
numbers.
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 1st NUMBER IS < THEN GOTO LOOP1
MOV R2, R4 ; IF THE 1st 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
NOP
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:
Result can be viewed in Memory location address specified in R4 and also in register
R2
Program7:Writeaprogramtoarrangeaseriesof32-bitnumbersinascending 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 1st 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 1st NUMBER IS < THEN GOTO LOOP2
STR R2, [R1], #-4
STR R3, [R1]
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
NOP
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
Output:
Result can be viewed at location DVALUE (stored in R3)
MES LAB MANUAL for engineering students.
Program8:Writeaprogramtocountthenumberofonesandzerosintwo consecutive memory
locations.
AREA ONEZERO , CODE, READONLY
ENTRY ; Mark first instruction to execute
START
MOV R2, #0 ; COUNTER FOR ONES
MOV R3, #0 ; COUNTER FOR ZEROS
MOV R7, #2 ; COUNTER TO GET TWO WORDS
LDR R6, =VALUE ; LOADS 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
NOP
NOP
NOP
JMP B JMP
VALUE DCD 0X11111111, 0XAA55AA55 ; TWO VALUES IN AN ARRAY
END ; Mark end of file
Output:
Result in R2 for ONES & R3 for ZEROS
Program9:Display“HelloWorld”messageusingInternalUART.
#include<lpc214x.h>
void uart_init(void);
unsigned int delay;
unsigned char *ptr;
unsigned char arr[]="HELLO WORLDr";
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++);
}
}
void uart_init(void)
{
PINSEL0=0X0000005; //select TXD0 and RXD0
lines U0LCR = 0X00000083; //enable baud rate divisor
loading U0DLM = 0X00; //select the data format
U0DLL = 0x13; //select baud rate 9600
bps U0LCR = 0X00000003;
}
Result: Hello World will be displayed on terminal.
Program 1: Interface and Control a DC Motor.
Note: DC motor terminals are connected to port pins P0.8 to P0.11.
#include<lpc214x.h>
void clock_wise(void);
void anti_clock_wise(void);
unsigned int 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++);
//End of while (1)
}
} //End of Main
void 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 IO0
SET = 0X00000900; //Selecting the P0.11 line for clockwise
}
void anti_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 IO0
SET = 0X00000100; //not selecting the P0.11 line for Anti clockwise
}
Result: DC Motor rotates in Clockwise for a period of time and then in Anticlockwise
direction.
Program 11: Interface a Stepper motor and rotate it in clockwise and anti- clockwise
direction.
Note: Stepper motor is connected to port pins P0.12 to P0.15.
#include <LPC21xx.H>
void clock_wise(void);
void anti_clock_wise(void);
unsigned long int var1, var2;
unsigned int i=0, j=0, k=0;
int main(void)
{
PINSEL0 = 0x00FFFFFF; //P0.12 to P0.15
GPIo IO0DIR = 0x0000F000; //P0.12 to P0.15 output
while (1)
{
for (j=0; j<50; j++)
clock_wise();
for (k=0; k<65000; k++);
for (j=0; j<50; j++)
anti_clock_wise();
for (k=0; k<65000; k++);
} // End of while (1)
} // End of main
void clock_wise(void)
{
var1 = 0x00000800; //For Clockwise
for (i=0; i<=3; i++) // for A B C D Stepping
{
var1 = var1<<1; //For Clockwise
IO0PIN = var1;
for (k=0; k<3000; k++); //for step speed variation
}
}
void anti_clock_wise(void)
{
var1 = 0x00010000; //ForAnticlockwise
for (i=0; i<=3; i++) // for A B C D Stepping
{
var1 = var1>>1; //ForAnticlockwise
IO0PIN = var1;
for(k=0;k<3000;k++); //forstepspeedvariation
}
}
Result: Stepper Motor rotates 3600in Clockwise and after a small delay
rotates in 3600in anticlockwise direction.
Program12:DetermineDigitaloutputforagivenAnaloginputusingInternal ADC
of ARM controller.
//10-bit internal ADC
//AIN0 pin is selected
//you can change the channel by changing PINSEL1 and ADCR value
#include <lpc214x.h>
#include <Stdio.h>
void lcd_init(void);
void wr_cn(void);
void clr_disp(void);
void delay(unsigned int);
void lcd_com(void);
void wr_dn(void);
void lcd_data(void);
unsigned int data_lcd=0;
unsigned int adc_value=0,temp_adc=0,temp1,temp2;
float temp;
char var[15],var1[15];
char *ptr,arr[]= "ADC O/P= ";
char *ptr1,dis[]="A I/P = ";
#define vol 3.3 //Reference voltage
#define fullscale 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 1st
pos
lcd_com();
delay (800);
while (*ptr!='0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
ptr1 = arr;
temp1 = 0xC0; //Display starting address of second line 4 th pos
lcd_com();
delay (800);
while(*ptr1!='0')
{
temp1 = *ptr1;
lcd_data();
ptr1 ++;
}
//infinite loop
while (1)
{
//CONTROL register for ADC
AD0CR = 0x01200010; //command register forADC-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_dta();
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
void lcd_init()
{
temp2=0x30;
wr_cn();
delay (800);
temp2=0x30;
wr_cn();
delay(800);
temp2=0x30;
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);
}
void lcd_com(void)
{
temp2= temp1 & 0xf0;
wr_cn();
temp2 = temp1 & 0x0f;
temp2 = temp2 << 4;
wr_cn();
delay (500);
}
// command nibble o/p routine
void wr_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 void wr_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
// and lower nibble next void lcd_data(void)
{
temp2 = temp1 & 0xf0;
wr_dn();
temp2= temp1 & 0x0f;
temp2= temp2 << 4;
wr_dn();
delay (100);
}
void delay(unsigned int r1)
{
unsigned int r; for(r=0;r<r1;r++);
}
void clr_disp(void)
{
temp1 = 0x01;
lcd_com();
delay (500);
}
Result: Analog values given by Potentiometer (POT) is converted to digital value and displayed on LCD.
Program13:InterfaceaDACandgenerateTriangularandSquarewaveforms.
Note:8bitDAC0800isconnectedtoportpinsP0.16toP0.23. Square
wave:
#include<lpc21xx.h>
unsigned int var;
voiddelay(void);
int main ()
{
while(1)
{
}
}
PINSEL0 = 0x00000000 ; // Configure P0.0 to P0.15 as GPIO PINSEL1 = 0x00000000 ;
//ConfigureP0.16toP0.31as GPIO IO0DIR = 0x00FF0000 ;
IO0PIN=0x00000000; var=
0x00000000;
delay();
IO0PIN=0x00FF0000;
var= 0x00FF0000;
delay();
void delay(void)
{
unsigned int i=0;
for(i=0;i<=950;i++);
}
Result:SquarewavecanbeobservedonCROandalsoonLogicAnalyzer.
TriangularWave:
#include<LPC21xx.h>
unsignedlonginttemp=0x00000000;
int main ()
{
unsignedint i=0;
PINSEL0 = 0x00000000 ; // Configure P0.0 to P0.15 as GPIO PINSEL1 =
0x00000000 ; //ConfigureP0.16toP0.31as GPIO
IO0DIR=0x00FF0000;
while(1)
{
for(i=0;i!=0xFF;i++)
{
temp=i;
temp=temp<<16; IO0PIN=temp;
}
for(i=0xFF;i!=0;i--)
{
temp=i;
temp=temp<<16; IO0PIN=temp;
}
}
}
Result:TriangularwavecanbeobservedonCROandalsoonLogicAnalyzer.
Program 14: Interface a 4X4 keyboard and display the key code on an LCD.
/*Program to demonstrate keyboard operation
Takes a key from key board and displays it on LCD screen*/
#include<lpc21xx.h>
#include<stdio.h>
/******* FUNCTION PROTOTYPE*******/
void lcd_init(void);
void clr_disp(void);
void lcd_com(void);
void lcd_data(void);
void wr_cn(void);
void wr_dn(void);
void scan(void);
void get_key(void);
void display(void);
void delay(unsigned int);
void init_port(void);
unsigned long int scan_code[16]=
{0x00EE0000,0x00ED0000,0x00EB0000,0x00E70000,
0x00DE0000,0x00DD0000,0x00DB0000,0x00D70000,
0x00BE0000,0x00BD0000,0x00BB0000,0x00B70000,
0x007E0000,0x007D0000,0x007B0000,0x00770000};
unsigned char ASCII_CODE[16]= {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
unsigned char row,col;
unsigned char temp,flag,i,result,temp1;
unsigned int r,r1;
unsigned long int var,var1,var2,res1,temp2,temp3,temp4;
unsigned char *ptr,disp[] = "4X4 KEYPAD";
unsigned char disp0[] = "KEYPAD TESTING";
unsigned char disp1[] = "KEY = ";
int main()
{
// ARMLIB_enableIRQ();
init_port(); //port intialisation
delay(3200); //delay
lcd_init(); //lcd intialisation
delay(3200); //delay
clr_disp(); //clear display
delay(500); //delay
//........LCD DISPLAY TEST. //
ptr = disp;
temp1 = 0x81; // Display starting address lcd_com();
delay (800);
while (*ptr!='0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
//........KEYPAD Working//
while (1)
{
get_key ();
display ();
}
} //end of main()
void get_key(void) //get the key from the keyboard
{
unsigned int i;
flag = 0x00;
IO1PIN=0x000f0000;
while (1)
{
for(row=0X00;row<0X04;row++) //Writing one for col's
{
If (row == 0X00)
{
temp3=0x00700000;
}
else if(row == 0X01)
{
temp3=0x00B00000;
}
else if(row == 0X02)
{
temp3=0x00D00000;
}
else if(row == 0X03)
{
temp3=0x00E00000;
}
var1 = temp3;
IO1PIN = var1; // each time var1 value is put to port1
IO1CLR =~var1; // Once again Conforming (clearing all other bits) scan ();
delay (100); //delay
if (flag == 0xff)
break;
} // end of for
if (flag == 0xff)
break;
} // end of while
for (i=0;i<16;i++)
{
if (scan_code[i] == res1) //equate the scan_code with res1
{
result = ASCII_CODE[i]; //same position value of ascii code
break; //is assigned to result
}
}
}// end of get_key();
void scan(void)
{
unsigned long int t;
temp2 = IO1PIN; // status of port1
temp2 = temp2 & 0x000F0000; // Verifying column key
if(temp2 != 0x000F0000) // Check for Key Press or Not
{
delay (1000); //delay(100)//give debounce delay check again temp2 = IO1PIN;
temp2 = temp2 & 0x000F0000; //changed condition is same
if (temp2 != 0x000F0000)// store the value in res1
{
flag = 0xff;
res1 = temp2;
t = (temp3 & 0x00F00000); //Verfying Row Write
res1 = res1 | t; //final scan value is stored in res1
}
else
{
flag = 0x00;
}
}
} // end of scan()
void display(void)
{
ptr = disp0;
temp1 = 0x80; // Display starting address of first line lcd_com();
while (*ptr!='0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
ptr = disp1;
temp1 = 0xC0; // Display starting address of second line
lcd_com();
while (*ptr!='0')
{
temp1 = *ptr; lcd_data ();
ptr ++;
}
temp1 = 0xC6; //display address for key value
lcd_com();
temp1 = result;
lcd_data();
}
void lcd_init (void)
{
temp = 0x30; wr_cn();
delay(3200);
temp = 0x30; wr_cn();
delay(3200);
temp = 0x30; wr_cn();
delay(3200);
temp = 0x20;
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;
lcd_com();
delay (800);
}
void lcd_data(void)
{
temp = temp1 & 0xf0; wr_dn();
temp= temp1 & 0x0f; temp= temp << 4; wr_dn();
delay(100);
}
void wr_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;
}
void lcd_com(void)
{
temp = temp1 & 0xf0;
wr_cn();
temp = temp1 & 0x0f; temp = temp << 4; wr_cn();
delay(500);
}
void wr_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);
IO0CLR = 0x00000008;
}
void clr_disp(void)
{
// command to clear lcd display temp1 = 0x01;
lcd_com(); delay(500);
}
void delay(unsigned int r1)
{
for(r=0;r<r1;r++);
}
void init_port()
{
IO0DIR = 0x000000FC; //configure o/p lines for lcd
IO1DIR = 0XFFF0FFFF;
}
Result: out of 16 keys, one of the key is pressed and values assigned to that key is
displayed on LCD.
Program15:DemonstratetheuseofanexternalinterrupttotoggleanLED
On
/Off.
#include<lpc214x.h>
void Extint0_Isr(void) irq;
//declarationofISR
unsigned char int_flag = 0, flag = 0;
int main(void){
IO1DIR
=0X02000000
; IO1SET = 0X02000000;
PINSEL1 =0X00000001; //SetupP0.16toalternatefunctionEINT0
EXTMODE =0x01; //edgei.efallingedgetriggerand activelow
EXTPOLAR= 0X00;
VICVectAddr0=(unsignedlong) Extint0_Isr; //AssigntheEINT0ISRfunction
VICVectCntl0 = 0x20 | 14; //AssigntheVICchannelEINT0tointerruptpriority0
VICIntEnable |= 0x00004000; //Enable the EINT0 interrupt
while(1) //waitingforinterrupttooccur
{
if(int_flag==0x01)
{
if(flag==0)
{
IO1CLR=0X02000000;
flag= 1;
}
elseif(flag==1)
{
IO1SET=0x02000000;
flag= 0;
}
int_flag=0x00;
}
}
}
voidExtint0_Isr(void) irq
{ //wheneverthereisalowlevelon
EINT0 EXTINT |= 0x01; //Clearinterrupt int_flag = 0x01;
VICVectAddr=0; //AcknowledgeInterrupt
}
Result:LEDconnectedtoportpinP0.25togglewhenexternalinterruptoccurs.
Program16:Display theHex digits0 toFona7-segment
LEDinterface,with an appropriate delay in between.
Note:SevensegmentLEDSareconnectedtoportpinsP0.16toP0.23andselectlinesof4sev
en segment LEDS are connected to port pins P0.28 to P0.31.
#include<LPC21xx.h>
unsignedintdelay,count=0, Switchcount=0;
unsigned int Disp[16]={0x003F0000, 0x00060000,
0x005B0000, 0x004F0000,
0x00660000,0x006D0000, 0x007D0000, 0x00070000, 0x007F0000,
0x006F0000,
0x00770000,0x007C0000, 0x00390000,0x005E0000, 0x00790000, 0x00710000
};
#defineALLDISP0xF0000000 //Selectall displayint main(void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IO0DIR = 0xF0FF0000;
while(1)
{
IO0SET=ALLDISP;IO0CLR=0x00FF0000;
for(delay=0;delay<100;delay++);
IO0SET = Disp[Switchcount];
//displaythevalues0toFonea
ftertheother for(delay=0;delay<1000000;delay++);
Switchcount++;
if(Switchcount==16) //afterFgobackto0
{
Switchcount=0;
}
}
}
Result:Numbers0000, 1111,
2222,…….FFFFisdisplayedonSevensegmentdisplaywith a delay in
between.
MES LAB MANUAL for engineering students.

More Related Content

PPTX
ARM instruction set
PPTX
Introduction to Microprocesso programming and interfacing.pptx
PDF
micro controllers lecture notes unit 1 and 2
PPT
Lecture 16 17 code-generation
DOCX
Introduction to 8085 & it's description(includes basic lab experiments)
PPT
Bca 2nd sem-u-3.1-basic computer programming and micro programmed control
PPT
B.sc cs-ii-u-3.1-basic computer programming and micro programmed control
PPT
how to generate sms
ARM instruction set
Introduction to Microprocesso programming and interfacing.pptx
micro controllers lecture notes unit 1 and 2
Lecture 16 17 code-generation
Introduction to 8085 & it's description(includes basic lab experiments)
Bca 2nd sem-u-3.1-basic computer programming and micro programmed control
B.sc cs-ii-u-3.1-basic computer programming and micro programmed control
how to generate sms

Similar to MES LAB MANUAL for engineering students. (20)

PPT
Microcontroller 8051- soft.ppt
PPT
Addressing modes
PPTX
Introduction to computer architecture .pptx
PPT
Emb day2 8051
PDF
Question Bank microcontroller 8051
PPT
1. Instructionset.pptfor engineering student
PPTX
Basic programming of 8085
PPT
8255 programming
PPT
My seminar new 28
PPTX
heuring_jordan_----------ch02(1) (1).pptx
PPTX
Programming the basic computer
PPT
Picmico
PPTX
Microcontroller
PDF
Assembler Programming
PDF
Eee iv-microcontrollers [10 es42]-assignment
PPT
Lect05 Prog Model
PPT
Stack and subroutine
DOCX
Mpi lab manual eee
PPT
Mca i-u-3-basic computer programming and micro programmed control
PPT
basic computer programming and micro programmed control
Microcontroller 8051- soft.ppt
Addressing modes
Introduction to computer architecture .pptx
Emb day2 8051
Question Bank microcontroller 8051
1. Instructionset.pptfor engineering student
Basic programming of 8085
8255 programming
My seminar new 28
heuring_jordan_----------ch02(1) (1).pptx
Programming the basic computer
Picmico
Microcontroller
Assembler Programming
Eee iv-microcontrollers [10 es42]-assignment
Lect05 Prog Model
Stack and subroutine
Mpi lab manual eee
Mca i-u-3-basic computer programming and micro programmed control
basic computer programming and micro programmed control
Ad

Recently uploaded (20)

PDF
Well-logging-methods_new................
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
OOP with Java - Java Introduction (Basics)
PPT
introduction to datamining and warehousing
PPTX
additive manufacturing of ss316l using mig welding
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
Project quality management in manufacturing
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Sustainable Sites - Green Building Construction
PPTX
web development for engineering and engineering
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
Well-logging-methods_new................
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
UNIT-1 - COAL BASED THERMAL POWER PLANTS
OOP with Java - Java Introduction (Basics)
introduction to datamining and warehousing
additive manufacturing of ss316l using mig welding
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
Project quality management in manufacturing
R24 SURVEYING LAB MANUAL for civil enggi
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Lecture Notes Electrical Wiring System Components
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Sustainable Sites - Green Building Construction
web development for engineering and engineering
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Foundation to blockchain - A guide to Blockchain Tech
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Ad

MES LAB MANUAL for engineering students.

  • 1. Microcontroller & Embedded Systems Course Code: 21CS43 CIE Marks 50 Teaching Hours/Week (L:T:P:S) 3:0:2:0 SEE Marks 50 Total Hours of Pedagogy 40 T + 20 P Total Marks 100 Credits 04 Exam Hours 03 Course Learning Objectives: 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. CLO 4: 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 outcome (Course Skill Set) At the end of the course, the student will be able to: CO 1. Explain C-Compilers and optimization CO 2. Describe the ARM microcontroller's architectural features and program module. CO 3. Apply the knowledge gained from programming on ARM to different applications. CO 4. Program the basic hardware components and their application selection method. CO 5. Demonstrate the need for a real-time operating system for embedded system applications.
  • 2. Laboratory Component: Module 1: ARM Processor Fundamentals Sl. No Name Page no 1 Using Keil software, observe the various registers, dump, CPSR, with a simple ALP programme Module 2: Introduction to the ARM Instruction Set 2 Write a program to find the sum of the first 10 integer numbers. 3 Write a program to find the 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 a look-up table. 6 Write a program to find the largest or smallest number in an array of 32 numbers Module 3: C Compilers and Optimization, ARM programming using Assembly language 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. 9 Display “Hello World” message using Internal UART. Module 4: Embedded System Components: 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.
  • 3. 16 Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in between. Module 5: RTOS and IDE for Embedded System Design: 1 Demonstration of IoT applications by using Arduino and Raspberry Pi ADDITIONAL EXPERIMENTS 1 ALP to determine the given 16 bit number is ODD or EVEN. 2 Interface a simple Switch and display its status through Relay, Buzzer and LED.
  • 4. Conduct of Practical Examination: 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) Introduction to ARM7 TheLPC2141/42/44/46/48microcontrollersarebasedona16-bit/32-bitARM7TDMI-SCPUwith 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 criticalcodesizeapplications,thealternative16-bitThumbmodereducescodebymorethan30 % 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 asaccesscontrolandpoint-ofsale.SerialcommunicationsinterfacesrangingfromaUSB2.0Full- 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 channelsand45fastGPIOlineswithuptonineedgeorlevelsensitiveexternalinterruptpinsmake these microcontrollers suitable for industrial control and medical systems.
  • 5. BLOCK Diagram of ARMLPC2148 Step 1: After opening Keil uV4, Go to Project tab and click on close project Then Create new uVision project Now Select new folder and give name to Project.
  • 6. Step 2: After Creating project now Select your device model. Example.NXP-LPC2148 [You can change it later from project window.]
  • 7. Step4:NowgotoFileandcreatenewfileandsaveitwith .C extension if you will write program in C languageorsavewith .asmforassemblylanguage. Step5:Nowwrite your program and save it again. 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 Step6:Afterthatonleftyouseeprojectwindow[ifit’snotthere….gotoViewtabandclickon project window.
  • 8. Right Click on Target and click on options for target. Here you can change your device also
  • 9. Click output tab here and check the HEX files, if you are generating hex files
  • 10. Step 7: Now expand Target and you will see the Source Group Step 8: Build Target and Run the program if no errors
  • 11. Module1: ARM Processor Fundamentals Program 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 R1 MOV R2, #3200 ; STORE SECOND NUMBER IN R2 MUL R3, R1, R2 ; MULTIPLICATION NOP NOP END ; Mark end of file Output: 1st Input : Register R1 =6400 2nd Input : Register R2 =3200 Result : Register R3=13880000
  • 12. Module2: Introduction to the ARM Instruction Set Program 2: Write a program to find the sum of the 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: Result can be viewed in Register R2 in hex decimal values Program3:Writeaprogramtofindfactorialofanumber.
  • 13. AREA FACTORIAL, CODE, READONLY ENTRY ; Mark first instruction to execute START MOV R0, #7 ; 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 NOP NOP NOP END ; Mark end of file Output: Result can be viewed in Register R0
  • 14. Program 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 LDR R1, =VALUE1 ; LOADS THE ADDRESS OF 1ST VALUE LOOP LDRH R3, [R1], #02 ; READ 16 BIT DATA ADD R0, R0, R3 ; ADD R0=R0+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 R0 JMP B JMP VALUE1 DCW 0X1111,0X2222,0X3333,0XAAAA,0XBBBB,0XCCCC ; 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: Result can be viewed in Memory location address specified in R4 and also in register R0
  • 16. Program 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 LDR R2, = 0X40000000 LDR R1, [R2] ; Load no whose square is to be find MOV R1, R1, LSL #0x2 ; Generate address corresponding to square of given no ADD R0, R0, R1 ; Load address of element in Lookup table LDR R3, [R0] ; Get square of given no in R3 NOP NOP NOP ;Lookup table contains Squares of nos from 0 to 10 (in hex) 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 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: Enter Input number in memory location specified in Register R2 Result can be viewed in Register R3
  • 18. Program6a: Write a programto find the largest/smallest number in an array of 32 numbers. 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 1st NUMBER IS > THEN GOTO LOOP1 MOV R2, R4 ; IF THE 1st 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 0XAAAAAAAA DCD 0X88888888 DCD 0X99999999 AREA DATA2, DATA, READWRITE
  • 19. RESULT DCD 0X0 ; ; TO STORE RESULT IN GIVEN ADDRESS END ; Mark end of file Output: Result can be viewed in Memory location address specified in R4 and also in register R2
  • 20. Experiment No 6b. Write a program to find the smallest number in an array of 32 numbers. 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 1st NUMBER IS < THEN GOTO LOOP1 MOV R2, R4 ; IF THE 1st 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 NOP NOP NOP ; ARRAY OF 32 BIT NUMBERS (N=7) VALUE1 DCD 0X44444444 DCD 0X22222222 DCD 0X11111111 DCD 0X22222222 DCD 0XAAAAAAAA DCD 0X88888888 DCD 0X99999999
  • 21. AREA DATA2, DATA, READWRITE ; TO STORE RESULT IN GIVEN ADDRESS RESULT DCD 0X0 END ; Mark end of file Output: Result can be viewed in Memory location address specified in R4 and also in register R2
  • 22. Program7:Writeaprogramtoarrangeaseriesof32-bitnumbersinascending 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 1st 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 1st NUMBER IS < THEN GOTO LOOP2 STR R2, [R1], #-4 STR R3, [R1] MOV R7, #1 ; FLAG DENOTING EXCHANGE HAS TAKEN PLACE ADD R1, #4 ; RESTORE THE PTR
  • 23. 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 NOP 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 Output: Result can be viewed at location DVALUE (stored in R3)
  • 25. Program8:Writeaprogramtocountthenumberofonesandzerosintwo consecutive memory locations. AREA ONEZERO , CODE, READONLY ENTRY ; Mark first instruction to execute START MOV R2, #0 ; COUNTER FOR ONES MOV R3, #0 ; COUNTER FOR ZEROS MOV R7, #2 ; COUNTER TO GET TWO WORDS LDR R6, =VALUE ; LOADS 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 NOP NOP NOP JMP B JMP VALUE DCD 0X11111111, 0XAA55AA55 ; TWO VALUES IN AN ARRAY
  • 26. END ; Mark end of file Output: Result in R2 for ONES & R3 for ZEROS Program9:Display“HelloWorld”messageusingInternalUART. #include<lpc214x.h> void uart_init(void); unsigned int delay; unsigned char *ptr;
  • 27. unsigned char arr[]="HELLO WORLDr"; 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++); } } void uart_init(void) { PINSEL0=0X0000005; //select TXD0 and RXD0 lines U0LCR = 0X00000083; //enable baud rate divisor loading U0DLM = 0X00; //select the data format U0DLL = 0x13; //select baud rate 9600 bps U0LCR = 0X00000003; } Result: Hello World will be displayed on terminal. Program 1: Interface and Control a DC Motor. Note: DC motor terminals are connected to port pins P0.8 to P0.11.
  • 28. #include<lpc214x.h> void clock_wise(void); void anti_clock_wise(void); unsigned int 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++); //End of while (1) } } //End of Main void 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 IO0 SET = 0X00000900; //Selecting the P0.11 line for clockwise } void anti_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 IO0 SET = 0X00000100; //not selecting the P0.11 line for Anti clockwise } Result: DC Motor rotates in Clockwise for a period of time and then in Anticlockwise direction. Program 11: Interface a Stepper motor and rotate it in clockwise and anti- clockwise direction. Note: Stepper motor is connected to port pins P0.12 to P0.15. #include <LPC21xx.H> void clock_wise(void);
  • 29. void anti_clock_wise(void); unsigned long int var1, var2; unsigned int i=0, j=0, k=0; int main(void) { PINSEL0 = 0x00FFFFFF; //P0.12 to P0.15 GPIo IO0DIR = 0x0000F000; //P0.12 to P0.15 output while (1) { for (j=0; j<50; j++) clock_wise(); for (k=0; k<65000; k++); for (j=0; j<50; j++) anti_clock_wise(); for (k=0; k<65000; k++); } // End of while (1) } // End of main void clock_wise(void) { var1 = 0x00000800; //For Clockwise for (i=0; i<=3; i++) // for A B C D Stepping { var1 = var1<<1; //For Clockwise IO0PIN = var1; for (k=0; k<3000; k++); //for step speed variation } } void anti_clock_wise(void) { var1 = 0x00010000; //ForAnticlockwise for (i=0; i<=3; i++) // for A B C D Stepping {
  • 30. var1 = var1>>1; //ForAnticlockwise IO0PIN = var1; for(k=0;k<3000;k++); //forstepspeedvariation } } Result: Stepper Motor rotates 3600in Clockwise and after a small delay rotates in 3600in anticlockwise direction. Program12:DetermineDigitaloutputforagivenAnaloginputusingInternal ADC of ARM controller. //10-bit internal ADC //AIN0 pin is selected //you can change the channel by changing PINSEL1 and ADCR value
  • 31. #include <lpc214x.h> #include <Stdio.h> void lcd_init(void); void wr_cn(void); void clr_disp(void); void delay(unsigned int); void lcd_com(void); void wr_dn(void); void lcd_data(void); unsigned int data_lcd=0; unsigned int adc_value=0,temp_adc=0,temp1,temp2; float temp; char var[15],var1[15]; char *ptr,arr[]= "ADC O/P= "; char *ptr1,dis[]="A I/P = "; #define vol 3.3 //Reference voltage #define fullscale 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 1st pos lcd_com(); delay (800); while (*ptr!='0') {
  • 32. temp1 = *ptr; lcd_data(); ptr ++; } ptr1 = arr; temp1 = 0xC0; //Display starting address of second line 4 th pos lcd_com(); delay (800); while(*ptr1!='0') { temp1 = *ptr1; lcd_data(); ptr1 ++; } //infinite loop while (1) { //CONTROL register for ADC AD0CR = 0x01200010; //command register forADC-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);
  • 33. ptr = var1; while(*ptr!+’0’) { temp1=*ptr; lcd_dta(); 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 void lcd_init() { temp2=0x30; wr_cn(); delay (800); temp2=0x30; wr_cn(); delay(800); temp2=0x30; wr_cn(); delay (800); temp2=0x20;
  • 34. 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); } void lcd_com(void) { temp2= temp1 & 0xf0; wr_cn(); temp2 = temp1 & 0x0f; temp2 = temp2 << 4; wr_cn(); delay (500); } // command nibble o/p routine void wr_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; }
  • 35. // data nibble o/p routine void wr_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 // and lower nibble next void lcd_data(void) { temp2 = temp1 & 0xf0; wr_dn(); temp2= temp1 & 0x0f; temp2= temp2 << 4; wr_dn(); delay (100); } void delay(unsigned int r1) { unsigned int r; for(r=0;r<r1;r++); } void clr_disp(void) { temp1 = 0x01; lcd_com(); delay (500); } Result: Analog values given by Potentiometer (POT) is converted to digital value and displayed on LCD.
  • 36. Program13:InterfaceaDACandgenerateTriangularandSquarewaveforms. Note:8bitDAC0800isconnectedtoportpinsP0.16toP0.23. Square wave: #include<lpc21xx.h> unsigned int var; voiddelay(void); int main () { while(1) { } } PINSEL0 = 0x00000000 ; // Configure P0.0 to P0.15 as GPIO PINSEL1 = 0x00000000 ; //ConfigureP0.16toP0.31as GPIO IO0DIR = 0x00FF0000 ; IO0PIN=0x00000000; var= 0x00000000; delay(); IO0PIN=0x00FF0000; var= 0x00FF0000; delay(); void delay(void) { unsigned int i=0; for(i=0;i<=950;i++); } Result:SquarewavecanbeobservedonCROandalsoonLogicAnalyzer.
  • 37. TriangularWave: #include<LPC21xx.h> unsignedlonginttemp=0x00000000; int main () { unsignedint i=0; PINSEL0 = 0x00000000 ; // Configure P0.0 to P0.15 as GPIO PINSEL1 = 0x00000000 ; //ConfigureP0.16toP0.31as GPIO IO0DIR=0x00FF0000; while(1) { for(i=0;i!=0xFF;i++) { temp=i; temp=temp<<16; IO0PIN=temp; } for(i=0xFF;i!=0;i--) { temp=i; temp=temp<<16; IO0PIN=temp; } } } Result:TriangularwavecanbeobservedonCROandalsoonLogicAnalyzer.
  • 38. Program 14: Interface a 4X4 keyboard and display the key code on an LCD. /*Program to demonstrate keyboard operation Takes a key from key board and displays it on LCD screen*/ #include<lpc21xx.h> #include<stdio.h> /******* FUNCTION PROTOTYPE*******/ void lcd_init(void); void clr_disp(void); void lcd_com(void); void lcd_data(void); void wr_cn(void); void wr_dn(void); void scan(void); void get_key(void); void display(void); void delay(unsigned int); void init_port(void); unsigned long int scan_code[16]= {0x00EE0000,0x00ED0000,0x00EB0000,0x00E70000, 0x00DE0000,0x00DD0000,0x00DB0000,0x00D70000, 0x00BE0000,0x00BD0000,0x00BB0000,0x00B70000, 0x007E0000,0x007D0000,0x007B0000,0x00770000}; unsigned char ASCII_CODE[16]= {'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'}; unsigned char row,col; unsigned char temp,flag,i,result,temp1; unsigned int r,r1; unsigned long int var,var1,var2,res1,temp2,temp3,temp4; unsigned char *ptr,disp[] = "4X4 KEYPAD"; unsigned char disp0[] = "KEYPAD TESTING"; unsigned char disp1[] = "KEY = "; int main() { // ARMLIB_enableIRQ(); init_port(); //port intialisation
  • 39. delay(3200); //delay lcd_init(); //lcd intialisation delay(3200); //delay clr_disp(); //clear display delay(500); //delay //........LCD DISPLAY TEST. // ptr = disp; temp1 = 0x81; // Display starting address lcd_com(); delay (800); while (*ptr!='0') { temp1 = *ptr; lcd_data(); ptr ++; } //........KEYPAD Working// while (1) { get_key (); display (); } } //end of main() void get_key(void) //get the key from the keyboard { unsigned int i; flag = 0x00; IO1PIN=0x000f0000; while (1) { for(row=0X00;row<0X04;row++) //Writing one for col's { If (row == 0X00) { temp3=0x00700000; } else if(row == 0X01)
  • 40. { temp3=0x00B00000; } else if(row == 0X02) { temp3=0x00D00000; } else if(row == 0X03) { temp3=0x00E00000; } var1 = temp3; IO1PIN = var1; // each time var1 value is put to port1 IO1CLR =~var1; // Once again Conforming (clearing all other bits) scan (); delay (100); //delay if (flag == 0xff) break; } // end of for if (flag == 0xff) break; } // end of while for (i=0;i<16;i++) { if (scan_code[i] == res1) //equate the scan_code with res1 { result = ASCII_CODE[i]; //same position value of ascii code break; //is assigned to result } } }// end of get_key(); void scan(void) { unsigned long int t; temp2 = IO1PIN; // status of port1 temp2 = temp2 & 0x000F0000; // Verifying column key if(temp2 != 0x000F0000) // Check for Key Press or Not { delay (1000); //delay(100)//give debounce delay check again temp2 = IO1PIN; temp2 = temp2 & 0x000F0000; //changed condition is same
  • 41. if (temp2 != 0x000F0000)// store the value in res1 { flag = 0xff; res1 = temp2; t = (temp3 & 0x00F00000); //Verfying Row Write res1 = res1 | t; //final scan value is stored in res1 } else { flag = 0x00; } } } // end of scan() void display(void) { ptr = disp0; temp1 = 0x80; // Display starting address of first line lcd_com(); while (*ptr!='0') { temp1 = *ptr; lcd_data(); ptr ++; } ptr = disp1; temp1 = 0xC0; // Display starting address of second line lcd_com(); while (*ptr!='0') { temp1 = *ptr; lcd_data (); ptr ++; } temp1 = 0xC6; //display address for key value lcd_com(); temp1 = result; lcd_data(); }
  • 42. void lcd_init (void) { temp = 0x30; wr_cn(); delay(3200); temp = 0x30; wr_cn(); delay(3200); temp = 0x30; wr_cn(); delay(3200); temp = 0x20; 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; lcd_com(); delay (800); } void lcd_data(void) { temp = temp1 & 0xf0; wr_dn(); temp= temp1 & 0x0f; temp= temp << 4; wr_dn(); delay(100);
  • 43. } void wr_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; } void lcd_com(void) { temp = temp1 & 0xf0; wr_cn(); temp = temp1 & 0x0f; temp = temp << 4; wr_cn(); delay(500); } void wr_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); IO0CLR = 0x00000008; } void clr_disp(void) { // command to clear lcd display temp1 = 0x01; lcd_com(); delay(500); } void delay(unsigned int r1) { for(r=0;r<r1;r++); }
  • 44. void init_port() { IO0DIR = 0x000000FC; //configure o/p lines for lcd IO1DIR = 0XFFF0FFFF; } Result: out of 16 keys, one of the key is pressed and values assigned to that key is displayed on LCD.
  • 45. Program15:DemonstratetheuseofanexternalinterrupttotoggleanLED On /Off. #include<lpc214x.h> void Extint0_Isr(void) irq; //declarationofISR unsigned char int_flag = 0, flag = 0; int main(void){ IO1DIR =0X02000000 ; IO1SET = 0X02000000; PINSEL1 =0X00000001; //SetupP0.16toalternatefunctionEINT0 EXTMODE =0x01; //edgei.efallingedgetriggerand activelow EXTPOLAR= 0X00; VICVectAddr0=(unsignedlong) Extint0_Isr; //AssigntheEINT0ISRfunction VICVectCntl0 = 0x20 | 14; //AssigntheVICchannelEINT0tointerruptpriority0 VICIntEnable |= 0x00004000; //Enable the EINT0 interrupt while(1) //waitingforinterrupttooccur { if(int_flag==0x01) { if(flag==0) { IO1CLR=0X02000000; flag= 1; } elseif(flag==1) { IO1SET=0x02000000; flag= 0; } int_flag=0x00; }
  • 46. } } voidExtint0_Isr(void) irq { //wheneverthereisalowlevelon EINT0 EXTINT |= 0x01; //Clearinterrupt int_flag = 0x01; VICVectAddr=0; //AcknowledgeInterrupt } Result:LEDconnectedtoportpinP0.25togglewhenexternalinterruptoccurs.
  • 47. Program16:Display theHex digits0 toFona7-segment LEDinterface,with an appropriate delay in between. Note:SevensegmentLEDSareconnectedtoportpinsP0.16toP0.23andselectlinesof4sev en segment LEDS are connected to port pins P0.28 to P0.31. #include<LPC21xx.h> unsignedintdelay,count=0, Switchcount=0; unsigned int Disp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000, 0x00660000,0x006D0000, 0x007D0000, 0x00070000, 0x007F0000, 0x006F0000, 0x00770000,0x007C0000, 0x00390000,0x005E0000, 0x00790000, 0x00710000 }; #defineALLDISP0xF0000000 //Selectall displayint main(void) { PINSEL0=0x00000000; PINSEL1=0x00000000; IO0DIR = 0xF0FF0000; while(1) { IO0SET=ALLDISP;IO0CLR=0x00FF0000; for(delay=0;delay<100;delay++); IO0SET = Disp[Switchcount]; //displaythevalues0toFonea ftertheother for(delay=0;delay<1000000;delay++); Switchcount++; if(Switchcount==16) //afterFgobackto0 { Switchcount=0; } } } Result:Numbers0000, 1111, 2222,…….FFFFisdisplayedonSevensegmentdisplaywith a delay in between.