0% found this document useful (0 votes)
37 views27 pages

18CSL48 Microcontroller and Embedded Systems Laboratory

The document describes software programs written for experiments on a microcontroller and embedded systems laboratory. It includes programs to multiply two 16-bit binary numbers, find the sum of the first 10 integers, calculate the factorial of a number, add an array of 16-bit numbers and store the 32-bit result, find the square of a number using a lookup table, and find the largest number in an array of 32 numbers. It also lists hardware experiments involving interfacing components like DC motors, stepper motors, keyboards, and seven-segment displays.

Uploaded by

shahtashakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views27 pages

18CSL48 Microcontroller and Embedded Systems Laboratory

The document describes software programs written for experiments on a microcontroller and embedded systems laboratory. It includes programs to multiply two 16-bit binary numbers, find the sum of the first 10 integers, calculate the factorial of a number, add an array of 16-bit numbers and store the 32-bit result, find the square of a number using a lookup table, and find the largest number in an array of 32 numbers. It also lists hardware experiments involving interfacing components like DC motors, stepper motors, keyboards, and seven-segment displays.

Uploaded by

shahtashakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Microcontroller and Embedded Systems Laboratory 18CSL48

S.NO PAGE
LIST OF THE EXPERIMENTS NO

SOFTWARE PART
1 Write a program to multiply two 16 bit binary numbers. 02
Write a program to find the sum of first 10 integer numbers.
2 03
Write a program to find factorial of a number.
3 04
Write a program to add an array of 16 bit numbers and store the 32 bit result in
4 internal RAM 05
Write a program to find the square of a number (1 to 10) using look-up table.
5 06
Write a Write program to find the largest number in an array of 32 numbers.
6a 07
Write a program to find the smallest number in an array of 32 numbers.
6.b 09
Write a program to arrange a series of 32 bit numbers in ascending order.
7.a) 11
Write a program to arrange a series of 32 bit numbers in descending order.
7.b) 13
Write a program to count the number of ones and zeros in two consecutive
8 memory locations. 15

HARDWARE PART
Display “Hello World” message using Internal UART.
1. 16

2 Interface and Control a DC Motor. 18

Interface a Stepper motor and rotate it in clockwise and anti-clockwise


3 19
direction.

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

5 Interface a DAC and generate Triangular and Square waveforms. 22

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

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

Display the Hex digits 0 to F on a 7-segment LED interface, with an


8 27
appropriate delay in between

Department of Computer Science and Engineering, AITM, Belagavi Page 1


Microcontroller and Embedded Systems Laboratory 18CSL48

SOFTWARE PROGRAMS: PART A

Experiment No 1:
Write a program to multiply two 16 bit binary numbers.

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
NOP
END ; Mark end of file

Output:
1st Input : Register R1 =6400
2nd Input : Register R2 =3200

Result : Register R3=13880000

Department of Computer Science and Engineering, AITM, Belagavi Page 2


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 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:
Result can be viewed in Register R2 in hex decimal values.

Department of Computer Science and Engineering, AITM, Belagavi Page 3


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 3. Write a program to find factorial of a


number.

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

Department of Computer Science and Engineering, AITM, Belagavi Page 4


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 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

Experiment 5. Write a program to find the square of a number


(1 to 10) using look-up table.
Department of Computer Science and Engineering, AITM, Belagavi Page 5
Microcontroller and Embedded Systems Laboratory 18CSL48

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

Department of Computer Science and Engineering, AITM, Belagavi Page 6


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 6a. Write a program to find the largest 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
NOP
NOP
; ARRAY OF 32 BIT NUMBERS (N=7)
VALUE1
DCD 0X44444444
DCD 0X22222222
DCD 0X11111111
DCD 0X33333333
DCD 0XAAAAAAAA
DCD 0X88888888
DCD 0X99999999

Department of Computer Science and Engineering, AITM, Belagavi Page 7


Microcontroller and Embedded Systems Laboratory 18CSL48

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

Department of Computer Science and Engineering, AITM, Belagavi Page 8


Microcontroller and Embedded Systems Laboratory 18CSL48

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

Department of Computer Science and Engineering, AITM, Belagavi Page 9


Microcontroller and Embedded Systems Laboratory 18CSL48

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

Department of Computer Science and Engineering, AITM, Belagavi Page 10


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 7a. Write a program to arrange a series of 32


bit numbers in 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 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

Department of Computer Science and Engineering, AITM, Belagavi Page 11


Microcontroller and Embedded Systems Laboratory 18CSL48

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)

Department of Computer Science and Engineering, AITM, Belagavi Page 12


Microcontroller and Embedded Systems Laboratory 18CSL48

7b. Write a program to arrange a series of 32 bit numbers in


descending order.

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],#4 ; WORD ALIGN T0 ARRAY ELEMENT
LDR R3,[R1] ; LOAD SECOND NUMBER
CMP R2,R3 ; COMPARE NUMBERS
BGT LOOP2 ; IF THE 1st 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

Department of Computer Science and Engineering, AITM, Belagavi Page 13


Microcontroller and Embedded Systems Laboratory 18CSL48

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)

Department of Computer Science and Engineering, AITM, Belagavi Page 14


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 8. Write a program to count the number of ones


and zeros in two 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
Department of Computer Science and Engineering, AITM, Belagavi Page 15
Microcontroller and Embedded Systems Laboratory 18CSL48

HARDWARE PROGRAMS: PART B

Experiment No 1: Display “Hello World” message using Internal


UART.
APPARATUS REQUIRED:
Apparatus Range Quantity 1
ARM Development Kit LPC 2148 1 2 Keil µVision3 IDE - 1

PROCEDURE:
Step 1: Give a double click on µvision 4 icon on the desk top, it will generate a window as shown below
Step 2: To create new project go to project select new micro vision project.
Step 3: select a drive where you would like to create your project.
Step 4: Create a new folder and name it with your project name.
Step 5: Open that project folder and give a name of your project executable and save it.
Step 6: After saving it will show some window there you select your microcontroller company i.e. NXP
from Phillips
Step 7: Select your chip as ARM DEVELOPMENT KIT
Step 8: After selecting chip click on OK then it will display some window asking to add STARTUP.
Select YES.
Step 9: A target is created and startup is added to your project target and is shown below.
Step 10: To write your project code select a new from menu bar.
Step 11: It will display some text editor, to save that select SAVE option from menu bar.
Step 12: By giving a name with extension .c and save it.
Step 13: Write the code of your project and save it.
Step 14: To add the c to target give a right click on Source Group, choose “ADD s to Group” option.
Step 15: It will display some window there select the file and click on ADD option.
Step 16: It will be added to our target and it shows in the project window.
Step 17: Select right click on target in the project window and select “Options for Target.”
Step 18: It will show some window, in that go to output option and choose Create Hex option by
selecting that box.
Step 20: Now to compile your project go to Project select Build Target option or press F7.
Step 21: Check the concern block of output and observe the results.

Department of Computer Science and Engineering, AITM, Belagavi Page 16


Microcontroller and Embedded Systems Laboratory 18CSL48

PROGRAM:
#include<lpc214x.h>

unsigned int delay;


unsigned char *ptr,arr[]="HELLO WORLD\r";

int main()
{
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;

while(1)
{
ptr = arr;
while(*ptr!='\0')
{
U0THR=*ptr++;
while(!(U0LSR & 0x20)== 0x20);
for(delay=0;delay<=600;delay++);
}
for(delay=0;delay<=60000;delay++);
}
}

Department of Computer Science and Engineering, AITM, Belagavi Page 17


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 2: Interface and Control a DC Motor.


Program:
Program to test Working of DC Motor
#include<lpc214x.h>

unsigned int j=0;

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

while(1)
{
//clock_wise
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
for(j=0;j<400000;j++); //delay
//anti_clock_wise
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
for(j=0;j<400000;j++); //delay
} //End of
while(1)
} //End of Main

Department of Computer Science and Engineering, AITM, Belagavi Page 18


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 3: Interface a Stepper motor and rotate it in clockwise


and anti-clockwise direction.

PROGRAM:
#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 = 0x00000000; //P0.12 to P0.15 GPIo
IO0DIR = 0x0000F000; //P0.12 to P0.15 output

while(1)
{
for(j=0;j<50;j++) // 50 times in Clock wise Rotation(360
degree)
clock_wise();

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

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


Rotation(3600)
//anti_clock_wise();

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

} // End of while(1)

} // End of main

void clock_wise(void)
{
var1 = 0x00001000; //For Clockwise
for(i=0;i<=3;i++) // for A B C D Stepping
{
IO0PIN = var1;
for(k=0;k<=30000;k++); //for step speed variation
var1 = var1<<1; //For Clockwise
}
}

void anti_clock_wise(void)
{
var1 = 0x00008000; //For Anticlockwise
for(i=0;i<=3;i++) // for A B C D Stepping
{
IO0PIN = var1;
for(k=0;k<=30000;k++); //for step speed variation
var1 = var1>>1; //For Anticlockwise
}
}

Department of Computer Science and Engineering, AITM, Belagavi Page 19


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 4: Determine Digital output for a given Analog input


using Internal ADC of ARM controller.

PROGRAM:
#include <lpc214x.h>
#include <Stdio.h>
#include "lcd_h.h"

unsigned int adc_value=0,temp_adc=0;


float adc_ip;
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

lcd_init(); //LCD initialization


delay(3200);

ptr = dis;
temp1 = 0x80; //Display starting address of first line 1 th 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 for ADC-AD0.4

Department of Computer Science and Engineering, AITM, Belagavi Page 20


Microcontroller and Embedded Systems Laboratory 18CSL48

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


//to check the interrupt bit

adc_value = AD0GDR; //reading the ADC value


adc_value >>=6;
adc_value &= 0x000003ff;
adc_ip = ((float)adc_value * (float)vol)/(float)fullscale;
sprintf(var1,"%4.2fV",adc_ip);
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()

Department of Computer Science and Engineering, AITM, Belagavi Page 21


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 5: Interface a DAC and generate Triangular and Square


waveforms.

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

int main ()
{
unsigned int i=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()

// program to generate square wave with DAC interface

#include <lpc21xx.h>

unsigned int delay;

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

while(1)
{
IO0PIN = 0x00000000;
for(delay=0;delay<=950;delay++);
IO0PIN = 0x00FF0000;
for(delay=0;delay<=950;delay++);
}
}

Department of Computer Science and Engineering, AITM, Belagavi Page 22


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 6: Interface a 4x4 keyboard and display the key code on


an LCD.
PROGRAM
/*Program to demonstrate keyboard operation
Takes a key from key board and displays it on LCD screen*/
#include<lpc21xx.h>
#include<stdio.h>
#include "lcd_h.h"

/******* FUNCTION PROTOTYPE*******/


void get_key(void);
void display(void);
void scan(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 flag,i,result;
unsigned long int res1,temp2,temp3;
unsigned char *ptr,disp1[] = "KEY PRESSED = ";
int main()
{
lcd_init(); //lcd intialisation
delay(3200); //delay
ptr = disp1;
temp1 = 0xC0; // Display starting address of second line
lcd_com();
IO1DIR = 0X00F00000;

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

while(1)
{
get_key();
}

} //end of main()

Department of Computer Science and Engineering, AITM, Belagavi Page 23


Microcontroller and Embedded Systems Laboratory 18CSL48

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;
}
IO1PIN = temp3;
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
temp1 = 0xCE; //display address
for key value
lcd_com();
delay(3200); //delay
temp1 = result;
lcd_data();
delay(3200); //delay
break; //is assigned to
result
}
}
}// end of get_key();

Department of Computer Science and Engineering, AITM, Belagavi Page 24


Microcontroller and Embedded Systems Laboratory 18CSL48

void scan(void)
{
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 | temp3;
}
else
{
flag = 0x00;
}
}
} // end of scan()

Department of Computer Science and Engineering, AITM, Belagavi Page 25


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 7: Demonstrate the use of an external interrupt to toggle


an LED On/Off.

// The program demonstrates About the External interrupt to the Controller

#include<lpc214x.h>

void Extint0_Isr(void) __irq; //declaration of ISR

unsigned char int_flag = 0, flag = 0;

int main(void)
{
IO1DIR = 0X02000000;
IO1SET = 0X02000000;
PINSEL1 =0X00000001; //Setup P0.16 to alternate
function EINT0

EXTMODE =0x01; //edge i.e falling egge


trigger and active low
EXTPOLAR= 0X00;
VICVectAddr0 = (unsigned long) Extint0_Isr; //Assign the EINT0 ISR
function
VICVectCntl0 = 0x20 | 14; //Assign the VIC channel
EINT0 to interrupt priority 0
VICIntEnable |= 0x00004000; //Enable the EINT0 interrupt

while(1) //waiting for interrupt to occur


{
if(int_flag == 0x01)
{
if(flag == 0)
{
IO1CLR = 0X02000000;
flag = 1;
}
else if(flag == 1)
{
IO1SET = 0x02000000;
flag = 0;
}
int_flag = 0x00;
}
}
}

void Extint0_Isr(void)__irq
{ //whenever there is a low level on EINT0
EXTINT |= 0x01; //Clear interrupt
int_flag = 0x01;
VICVectAddr = 0; //Acknowledge
Interrupt
}

Department of Computer Science and Engineering, AITM, Belagavi Page 26


Microcontroller and Embedded Systems Laboratory 18CSL48

Experiment No 8: Display the Hex digits 0 to F on a 7-segment LED


interface, with an appropriate delay in between

PROGRAM:

#include <LPC21xx.h>

unsigned int delay, count=0, Switchcount=0;

/*cmmon cathode seven segment data for 0 to F*/


unsigned int Disp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,0x006D0000,
0x007D0000, 0x00070000, 0x007F0000,
0x006F0000, 0x00770000,0x007C0000,
0x00390000, 0x005E0000, 0x00790000,
0x00710000 };

int main (void)


{
PINSEL1 = 0x00000000;
IO0DIR = 0x10FF0000;

while(1)
{
//Display values on Seven Segment
IO0SET = 0x10000000;
IO0CLR = 0x00FF0000;

IO0SET = Disp[Switchcount]; // display


the values 0 to F one after the other
for(delay=0;delay<=300000;delay++);
Switchcount++;
if(Switchcount == 16) // after F go
back to 0
{
Switchcount = 0;
}
}
}

Department of Computer Science and Engineering, AITM, Belagavi Page 27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy