Microcontroller Lab Manual - SCEM - 11

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30
At a glance
Powered by AI
The manual provides instructions for experiments involving microcontrollers and embedded systems for students. It aims to give hands-on experience with interfacing devices discussed in theory classes.

The lab manual is intended to help Information Science and Engineering students at Sahyadri College of Engineering and Management understand microcontrollers and embedded systems in a practical way. It contains experiments that map to the syllabus to enhance learning.

The manual covers experiments involving binary arithmetic, arrays, sorting, interrupts, motor control, and more across two sections - one using just the microcontroller and another using an evaluation board for interfacing.

1

SAHYADRI
COLLEGE OF ENGINEERING & MANAGEMENT,
MANGALURU

Department of Information Science & Engineering

MICROCONTROLLER AND EMBEDDED


SYSTEMSLAB MANUAL-18CSL48

Prepared By: Mr. Janardhana D.R./ Mr. Ganaraj K


Assistant Professors
Dept. of ISE

Department of Information Science & Engineering, SCEM, Mangaluru.


2

PREFACE
Microcontrollers and Embedded System Laboratory is designed for Information Science & Engineering
students, Sahyadri College of Engineering and Management, Mangaluru.

The lab experiments enhance the learning curve for the students and give them hands-on Experience on
the different interfacing devices that were discussed in the theory classes.

The manual contains the prescribed experiments for easy & quick understanding of the students. The
authors have gathered materials from Books, Journals and Web resources.

The student should be able to analyze and develop programs based on input conditions or expected
output requirements. We hope that, this practical manual will be helpful for the students of Information
Science & Engineering for understanding the subject from the point of view of applied aspects.

There is always scope for improvement in the manual. We would appreciate to receive valuable
suggestions from readers and users for future use.

This Laboratory manual is based on the syllabus provided by VTU under the subject code 18CSL48.

Department of Information Science & Engineering, SCEM, Mangaluru.


3

ACKNOWLEDGMENTS

We are highly indebted to Dr. Shamanth Rai. Associate Professor & H.O.D. Department of
Information Science &Engineering for providing us the opportunity to prepare this manual as well as for
his guidance and constant supervision.

We would like to express our special gratitude and thanks to Dr. Srinivas Rao Kunte, Principal
Sahyadri College of Engineering & Management, Mangaluru.

We are also thankful to Dr. D. L. Prabhakara, Director Sahyadri Group of Institutions, and
Dr. Manjunath Bhandary, Chairman, Sahyadri Group of Institutions.

We are also thankful to Mr. Praveen T Damale for proof reading and constant support in the
Laboratories.

We are thankful to the students and colleagues for their support and encouragement.

Mr. Janardhana D.R


Mr. Ganaraj K

Department of Information Science & Engineering, SCEM, Mangaluru.


4

List of Experiments
Expt.N Objective of the Page
o. Experiment No

PART – A

1. Write a program to multiply two 16 bit binary numbers. 9

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

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

4. Write a program to add an array of 16 bit numbers and store the 32 10


bit result in internal RAM
5. Write a program to find the square of a number (1 to 10) using look- 11
up table.
6. Write a program to find the largest/smallest number in an array of 12
32 numbers.
7. Write a program to arrange a series of 32 bit numbers in 13
ascending/descending order.
8. Write a program to count the number of ones and zeros in two 14
consecutive memory locations.
PART – B ( Interfacing Using Evaluation Board)

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

10. Interface and Control a DC Motor. 16

11. Interface a Stepper motor and rotate it in clockwise and anti- 17-18
clockwise direction.
12. Determine Digital output for a given Analog input using Internal 19-22
ADC of ARM controller.
13. Interface a DAC and generate Triangular and Square waveforms. 23

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

15. Demonstrate the use of an external interrupt to toggle an LED 29


On/Off.
16. 30
Display the Hex digits 0 to F on a 7-segment LED interface, with an
appropriate delay in between

Department of Information Science & Engineering, SCEM, Mangaluru.


5

Conduct of Practical Examination:


 Experiment distribution
 For laboratories having only one part: Students are allowed to pick one
experiment from
the lot with equal opportunity.
 For laboratories having PART A and PART B: Students are allowed to pick
oneexperiment from PART A and one experiment from PART B, with equal
opportunity.
 Change of experiment is allowed only once and marks allotted for procedure to
be made zero ofthe changed part only.
Marks Distribution(Courseed to change in accordance with university regulations)
 For laboratories having only one part – Procedure + Execution + Viva-Voce:
15+70+15 =100 Marks
 For laboratories having PART A and PART B
Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks
Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks

Department of Information Science & Engineering, SCEM, Mangaluru.


6

DOS AND DON’TS IN LABO RATORY

1. Remove footwear and keep it in the rack before entering the lab.
2. Make entry in the Log Book as soon as you enter the Laboratory.
3. All the students should sit according to their roll numbers starting from their left to right.
4. All the students are supposed to enter the Equipment number in the logbook.
5. Do not change the place of Laboratory Equipment on which you are working.
6. Strictly observe the instructions given by the Professor/Lab Instructor.
7. The Laboratory Record Book should have following details:
(a) Date of the Experiment
(b) Aim
(c) Apparatus & Components Required
(d) Programs with Comments
(e) Algorithm Steps
(f) Results

8. Before starting Laboratory work follow all written and verbal instructions carefully. If you do not
understand a direction or part of a procedure, CLARIFY YOUR CONCERN WITH THE
INSTRUCTOR BEFORE PROCEEDING WITH THE ACTIVITY
9. Protect yourself from getting electric shock.
10. Break-down of equipment must be reported to the Instructor.
11. After the completion of the experiment should return the components to the respective lab
instructors.
12. Before leaving the lab, should check whether they have switch off the power supplies and keep
their chairs properly.

Department of Information Science & Engineering, SCEM, Mangaluru.


7

Institute Vision
To be a premier institution in Technology and Management by fostering excellence in education,
innovation, incubation and values to inspire and empower the young minds.
Institute Mission
M1: Creating an academic ambience to impart holistic education focusing on individual growth, integrity,
ethical values and social responsibility.
M2: Develop skill based learning through industry-institution interaction to enhance competency and
promote entrepreneurship.
M3: Fostering innovation and creativity through competitive environment with state-of-the-art
infrastructure.
Department Vision

To be a center of excellence in Information Science and Engineering through interactive Teaching-learning


process, research, and innovation.

Department Mission
M1: Creating competitive ambience to enhance the innovative and experiential learning process through
state-of the art infrastructure
M2: Grooming young minds through industry-institute interactions to solve societal issues and inculcate
affinity towards research and entrepreneurship.
M3: Promoting team work and leadership qualities through inter-disciplinary activities in diversified areas
of information science and engineering

Programme Educational Objectives

PEO1: Possess theoretical and practical knowledge to identify, scrutinize, formulate and solve challenging
problems related to dynamically evolving information science.
PEO2: Inculcate core competency, professionalism, teamwork, and ethics to cater industrial needs and to
solve societal problems.
PEO3: Engage in Lifelong learning and stay intact to the transformation in technologies and pursue
research.
Programme Specific Outcomes

PSO1: Exhibit competency and skills in distributed computing, information security, cyber security, data
analytics and machine learning
PSO2: Able to provide sustainable solution to implement and validate information science projects.

Department of Information Science & Engineering, SCEM, Mangaluru.


8

Course Outcomes

CO1 Demonstrate ALP using ARM7TDMI/LPC2148. CL3

Demonstrate to display “Hello World” message using internal


CO2 CL3
UART.
Demonstration of ALP to interface and control a DC motor and a Steeper
CO3 CL3
motor
Demonstration of ALP to interface 4x4 keyboard to display the key code
CO4 and interface DAC and ADC to genearate waveforms and determining CL3
digital outputs.
Demonstrate the use of an external interrupt to toggle an LED On/Off
CO5 CL3
and display the Hex digits 0 to F by interfacing 7-segment LED.

CO-PO-PSOs Mapping:
Programme Specific
Program Outcomes (PO)
CO Outcome (PSO)
No.
1 2 3 4 5 6 7 8 9 10 11 12 1 2

C219.1 3 3 3 2 2 3 2 2 1 1

C219.2 3 3 3 2 2 3 2 2 1 1

C219.3 3 3 3 2 2 3 2 2 1 1

C219.4 3 3 3 2 2 3 2 2 1 1

C219.5 3 3 3 2 2 3 2 2 1 1

Department of Information Science & Engineering, SCEM, Mangaluru.


9

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

AREA MULTIPLY, CODE, READONLY


ENTRY ; Mark first instruction to execute
MOV R1, #6400 ; STORE FIRST NUMBER IN R0
MOV R2, #3200 ; STORE SECOND NUMBER IN R1
MUL R3, R1, R2 ; MULTIPLICATION
NOP
NOP
NOP
END ; Mark end of file

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 R1
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

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


AREA FACTORIAL, CODE, READONLY
ENTRY ; MARK FIRST INSTRUCTION TO EXECUTE
MOV R0, #3 ; STORE FACTORIAL NUMBER IN R0
MOV R1,R0 ;MOVE THE SAME NUMBER IN R1
FACT SUBS R1, R1, #1 ; SUBTRACTION
CMP R1, #1 ; COMPARISON
BEQ STOP
MUL R3, R0, R1 ; MULTIPLICATION
MOV R0, R3 ; RESULT
BNE FACT ; BRANCH TO THE LOOP IF NOT EQUAL
STOP
NOP
NOP
NOP
END ; MARK END OF FILE

Department of Information Science & Engineering, SCEM, Mangaluru.


10

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 FIRST VALUE
LOOP
LDRH R3,[R1],#02 ; READ 16 BIT DATA
ADD R0,R0,R3 ; ADD R2=R2+R3
SUBS R5,R5,#1 ; DECREMENT COUNTER CMP R5,#0
BNE LOOP ; LOOK BACK TILL ARRAY ENDS
LDR R4, =RESULT ; LOADS THE ADDRESS OF RESULT
STR R0, [R4] ; STORES THE RESULT IN R1
JMP B JMP

VALUE1 DCW 0X1111, 0X2222, 0X3333, 0XAAAA, 0XBBBB, 0XCCCC ; 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

Department of Information Science & Engineering, SCEM, Mangaluru.


11
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

Department of Information Science & Engineering, SCEM, Mangaluru.


12
6. Write a program to 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 FIRST NUMBER IS > THEN GOTO LOOP1

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


TO R2
LOOP1
SUBS R5,R5,#1 ; DECREMENT COUNTER
CMP R5,#0 ; COMPARE COUNTER TO 0
BNE LOOP ; LOOP BACK TILL ARRAY ENDS

LDR R4,=RESULT ; LOADS THE ADDRESS OF RESULT


STR R2,[R4] ; STORES THE RESULT IN R2

NOP
NOP
NOP

; ARRAY OF 32 BIT NUMBERS(N=7)

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

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

RESULT DCD 0X0

END ; Mark end of file

Department of Information Science & Engineering, SCEM, Mangaluru.


13

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


AREA ASCENDING, CODE, READONLY

ENTRY ; Mark first instruction to execute

START

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


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

SUBS R8,R8,#1 ; DECREMENT COUNTER


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

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


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

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


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

LOOP2

SUBS R5,R5,#1 ; DECREMENT COUNTER


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

NOP
NOP
NOP

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

Department of Information Science & Engineering, SCEM, Mangaluru.


14
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

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,#1 ; COUNTER TO GET TWO WORDS
LDR R6,=VALUE ; LOADS THE ADDRESS OF VALUE

LOOP MOV R1,#32 ; 32 BITS COUNTER


LDR R0,[R6],#4 ; GET THE 32 BIT VALUE

LOOP0 MOVS R0,R0,ROR #1 ; RIGHT SHIFT TO CHECK CARRY BIT (1's/0's)


BHI ONES ; IF CARRY BIT IS 1 GOTO ONES BRANCH
OTHERWISE NEXT

ZEROS ADD R3,R3,#1 ; IF CARRY BIT IS 0 THEN INCREMENT THE


COUNTER BY 1(R3)
B LOOP1 ; BRANCH TO LOOP1

ONES ADD R2,R2,#1 ; IF CARRY BIT IS 1 THEN INCREMENT THE


COUNTER BY 1(R2)

LOOP1 SUBS R1,R1,#1 ; COUNTER VALUE DECREMENTED BY 1


BNE LOOP0 ; IF NOT EQUAL GOTO TO LOOP0 CHECKS 32BIT

SUBS R7,R7,#1 ; COUNTER VALUE DECREMENTED BY 1


CMP R7,#0 ; COMPARE COUNTER R7 TO 0
BNE LOOP ; IF NOT EQUAL GOTO TO LOOP

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

Department of Information Science & Engineering, SCEM, Mangaluru.


15
END ; Mark end of file

PART –B
Conduct the following experiments on an ARM7TDMI/LPC2148 evaluation board using Evaluation
version of Embedded 'C' & Keil Uvision-4 tool/compiler.

9. Display “Hello World” message using internal UART

#include <lpc214x.h>

void uart_interrupt(void) irq ;

unsigned char temp , temp1 = 0x00 ;


unsigned char rx_flag = 0 , tx_flag = 0 ;

int main(void)
{
PINSEL0=0X0000005; //select TXD0 and RXD0 lines

U0LCR = 0X00000083; //enable baud rate divisor loading and


U0DLM = 0X00; //select the data format
U0DLL = 0x13; //select baud rate 9600 bps
U0LCR = 0X00000003;
U0IER = 0X03; //select Transmit and Recieve interrupt

VICVectAddr0 = (unsigned long)uart_interrupt; //UART 0 INTERRUPT


VICVectCntl0 = 0x20|6; // Assign the VIC channel uart-0 to interrupt priority 0
VICIntEnable = 0x00000040; // Enable the uart-0 interrupt

rx_flag = 0x00;
tx_flag = 0x00;

while(1)
{
while(rx_flag == 0x00); //wait for receive flag to set
rx_flag = 0x00; //clear the flag

U0THR = temp1 ;

while(tx_flag == 0x00); //wait for transmit flag to set


tx_flag = 0x00; //clear the flag
}
}

void uart_interrupt(void) irq


{
temp = U0IIR;
temp = temp & 0x06; //check bits, data sending or receiving

if(temp == 0x02) //check data is sending

Department of Information Science & Engineering, SCEM, Mangaluru.


16
{
tx_flag = 0xff; // flag that indicate data is sending via UART0
VICVectAddr=0;
}

else if(temp == 0x04) // check any data available to receive


{
// U0THR = U0RBR;
temp1 = U0RBR ; // copy data into variable
rx_flag = 0xff; // set flag to indicate that data is received
VICVectAddr=0;
}
}

10. Interface and Control a DC Motor

#include<lpc214x.h>

void clock_wise(void);
void anti_clock_wise(void);
unsigned int j=0;

int main()
{
PINSEL2 = 0XFFFFFFF0;
//IO1CLR = 0X0000ff00;
IO1DIR= 0X00030000; //p1.16 and p1.17 are selected as outputs.
IO1SET= 0X00010000; //P1.16 should always high.

while(1)
{

clock_wise();
for(j=0;j<500000;j++); //delay

anti_clock_wise();
for(j=0;j<500000;j++); //delay

} //End of while(1)
} //End of Main

void clock_wise(void)
{
IO1CLR = 0x00030000; //stop motor and also turn off relay
for(j=0;j<500000;j++);//small delay to allow motor to turn off
IO1SET = 0X00030000;
//Selecting the P1.17 line for clockwise and turn on motor
}

Department of Information Science & Engineering, SCEM, Mangaluru.


17
void anti_clock_wise(void)
{

IO1CLR = 0X00030000; //stop motor and also turn off relay


for(j=0;j<1000000;j++); //small delay to allow motor to turn off
IO1SET = 0X00010000; //not selecting the P1.17 line for Anti clockwise

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

#include <LPC21xx.h>

void clock_wise(void) ;
void anti_clock_wise(void) ;

unsigned int var1 ;


unsigned long int i = 0 , j = 0 , k = 0 ;

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

while(1)
{

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


clock_wise() ; // rotate one round clockwise

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


anti_clock_wise() ; // rotate one round anticlockwise

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

}// End of main

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

IO1CLR =0x00F00000 ; //clearing all 4 bits

Department of Information Science & Engineering, SCEM, Mangaluru.


18

IO1SET = var1 ; // setting perticular bit

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

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

IO1CLR =0x00F00000 ; //clearing all 4 bits

IO1SET = var1 ;

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

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


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

IO1SET = var1 ; // setting perticular bit

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

}
}

Department of Information Science & Engineering, SCEM, Mangaluru.


19

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

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

#define vol 3.3 //Reference voltage


#define fullscale 0x3ff //10 bit adc fullscale

unsigned int data_lcd=0,i=0,n=0;


unsigned int adc_value=0,temp_adc=0,temp1,temp2,adc[8];
float temp,adc1[8];
unsigned char var[15],var1[15],fst_flag=0xff;
unsigned char *ptr,arr[]= "ADC O/P= ";
unsigned char *ptr1,dis[]="A I/P = ";

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);

int main()
{
PINSEL1 = 0X04000000; //AD0.4 pin is selected
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 1st line on LCD
lcd_com();
delay(800);

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

ptr1 = arr;

Department of Information Science & Engineering, SCEM, Mangaluru.


20
temp1 = 0xC0; //Display starting address of 2nd line on LCD
lcd_com();
delay(800);

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

while(1) //infinite loop


{
temp = 0;
adc_value = 0;
AD0CR = 0x01200004; ////CONTROL register for ADC-AD0.4

while(((temp_adc = AD0GDR) &0x80000000) == 0x00000000); //to check the interrupt bit

adc_value = AD0GDR; //reading the ADC value


adc_value >>=6;
adc_value &= 0x000003ff;
temp = ((float)adc_value * (float)vol)/(float)fullscale;

if(fst_flag)
{
fst_flag = 0x00;
for(i=0;i<8;i++)
{
adc[i] = adc_value;
adc1[i] = temp;
}
}

else
{
n=7;
for(i=n;i>0;i--)
{
adc[i] = adc[i-1];
adc1[n] = adc1[n-1];
n = n-1;
}
adc[0] = adc_value;
adc1[0] = temp;
} temp=0;
adc_value=0;
for(i=0;i<8;i++)
{

Department of Information Science & Engineering, SCEM, Mangaluru.


21
temp += adc1[i];
adc_value += adc[i];
}
temp = (temp/8);
adc_value = (adc_value/8);

sprintf(var1,"%4.2fV",temp);
sprintf(var,"%3x",adc_value);

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

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

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;

Department of Information Science & Engineering, SCEM, Mangaluru.


22
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; // ENABLE=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; // ENABLE=1
delay(10);
IO0CLR = 0x00000008;
}

Department of Information Science & Engineering, SCEM, Mangaluru.


23

// 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);
}

13. Program to generate square wave with DAC interface

#include <lpc21xx.h>

void delay(void);

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

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

void delay(void)
{
unsigned int i=0;

Department of Information Science & Engineering, SCEM, Mangaluru.


24
for(i=0;i<=3000;i++);
}
14. Interface a 4x4 keyboard and display the key code on an LCD.

#include<lpc21xx.h>
#include<stdio.h>

void scan(void);
void get_key(void);
void display(void);
void delay(unsigned int);
void init_port(void);

void lcd_init(void);
void clr_disp(void);
void lcd_com(void); // LCD routines
void lcd_data(void);
void wr_cn(void);
void wr_dn(void);

unsigned long int scan_code[16]= { 0x0000EE00,0x0000ED00,0x0000EB00,0x0000E700 ,


0x0000DE00,0x0000DD00,0x0000DB00,0x0000D700 ,
0x0000BE00,0x0000BD00,0x0000BB00,0x0000B700 ,
0x00007E00,0x00007D00,0x00007B00,0x00007700 };

unsigned char ASCII_CODE[16]= {'0','4','8','C',


'1','5','9','D',
'2','6','A','E',
'3','7','B','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;
unsigned char disp0[] = "KEYPAD TESTING";
unsigned char disp1[] = "KEY = ";
int main()
{
PINSEL0 = 0X00000000; // configure P0.0 TO P0.15 as GPIO
init_port(); //port intialisation
delay(3200); //delay
lcd_init(); //lcd intialisation
delay(3200); //delay
clr_disp(); //clear display
delay(500); //delay
clr_disp();

ptr = disp0;

Department of Information Science & Engineering, SCEM, Mangaluru.


25
temp1 = 0x80; // Display starting address of 1st line on LCD
lcd_com();

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

ptr = disp1;
temp1 = 0xC0; // Display starting address of 2nd line on LCD
lcd_com();

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

while(1)
{
get_key();
display();
}

} //end of main()

void get_key(void) //get the key from the keyboard


{
unsigned int k; flag =
0x00;
IO0PIN=0x0000F000;

while(1)
{
for(row=0X00;row<0X04;row++) //Writing one for col's
{
if( row == 0X00)
{
temp3=0x00000E00;
}
else if(row == 0X01)
{
temp3=0x00000D00;
}
else if(row == 0X02)
{
temp3=0x00000B00;

Department of Information Science & Engineering, SCEM, Mangaluru.


26
}
else if(row == 0X03)
{
temp3=0x00000700;
}

var1 = temp3;
IO0PIN = var1; // each time var1 value is put to port1
IO0CLR =~var1; // Once again Confirming (clearing all other bits)

scan();
delay(100); //delay

if(flag == 0xff)
break;

} // end of for loop

if(flag == 0xff)
break;
} // end of while

for(k=0;k<16;k++)
{
if(scan_code[k] == res1) //equate the scan_code with res1
{
result = ASCII_CODE[k]; //same position value of ascii code
break; //is assigned to result
}
}
}// end of get_key();

void scan(void)
{
unsigned long int t;
temp2 = IO0PIN; // status of port1
temp2 = temp2 & 0x0000F000; // Verifying column key
if(temp2 != 0x0000F000) // Check for Key Press or Not
{
delay(3000); //delay(100)//give debounce delay check again
temp2 = IO0PIN; //IO0
temp2 = temp2 & 0x0000F000; //changed condition is same

if(temp2 != 0x0000F000) // store the value in res1


{
flag = 0xff;
res1 = temp2;
t = (temp3 & 0x00000F00); //Verfying Row Write
res1 = res1 | t; //final scan value is stored in res1
}
else

Department of Information Science & Engineering, SCEM, Mangaluru.


27
{
flag = 0x00;
}
}
} // end of scan()

void display(void)
{

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);

temp = 0x28; // load command for lcd function setting with lcd in 4 bit mode,
lcd_com(); // 2 line and 5x7 matrix display
delay(3200);

temp1 = 0x0C; // load a command for display on, cursor on and blinking off
lcd_com();
delay(800);

temp1 = 0x06; // command for cursor increment after data dump


lcd_com();
delay(800);

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

void lcd_data(void)

Department of Information Science & Engineering, SCEM, Mangaluru.


28
{
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; // Enable=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; // Enable=1
delay(10);
IO0CLR = 0x00000008;
}

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

void delay(unsigned int r1)


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

Department of Information Science & Engineering, SCEM, Mangaluru.


29
void init_port()
{

IO0DIR = 0x00000FFC; //Configured LCD Lines and Rows as O/P(P0.8-P0.11) and Columns
as I/P(P0.12-P0.15)

IO0SET = 0x0000FF00; //Set the Rows high.


}

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

#include <LPC21xx.h>

void EINT0_Init(void);
void Extint0_Isr(void) irq;

unsigned char int_flg=0x00, flag=0x00;

int main ( void )


{
PINSEL2 = 0x00000000; //made P0.16 - P0.31 as GPIO
IO1PIN = 0x00000000;
EINT0_Init(); // initialise external int0

while(1)
{
if(int_flg == 0xFF) //check interrupt occur or not
{
int_flg = 0x00;
if(flag == 0x00)
{ // when flag is '0x00' ON the LED
IO1SET = 0x02000000;
flag = 0xFF;
}

else
{ // when flag is
'0xFF' OFF the LED
IO1CLR = 0X02000000;
flag = 0x00;
}
}
}
}

void EINT0_Init(void)
{
IO1DIR |= 0X02000000; // P1.25 for LED indication
PINSEL1 &= ~0x00000003;
PINSEL1 |= 0X00000001; // Setup P0.16 to alternate function
EINT0

Department of Information Science & Engineering, SCEM, Mangaluru.


30
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
}

void Extint0_Isr(void) irq // whenever there is a low level on EINT0


{
EXTINT |= 0x01; // Clear interrupt
int_flg = 0xFF;
VICVectAddr = 0; // Acknowledge Interrupt
}

16. Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in between
#include <LPC21XX.h>
unsigned int delay,j;
unsigned int Switchcount=0;
unsigned int Disp[16]={0x003F0000, 0x00060000, 0x005B0000, 0x004F0000,
0x00660000,0x006D0000,0x007D0000, 0x00070000, 0x007F0000, 0x006F0000,
0x00770000,0x007C0000,0x00390000, 0x005E0000, 0x00790000, 0x00710000 };

int main (void)


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

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

for(j=0;j<20;j++)
for(delay=0;delay<30000;delay++); // 1s delay

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

}
}

Department of Information Science & Engineering, SCEM, Mangaluru.

You might also like