MES LABORATORY Manual 2020

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

Microcontroller & Embedded Systems 18CSL48

Introduction
LPC-P2148 Features
LPC-P2148is prototype board forLPC2148 ARM7TDMI-S microcontroller with USB 2.0 Full-speed device,
multiple UARTs, SPI, SSP to I2C-bus and on-chip SRAM up to 40 kB, produced by NXP Semiconductors.
With LPC-P2148 you can explore the features of LPC21XX family on budged, the board has everything
necessary to build simple applications: reset and oscillator circuits, JTAG port for programming and debugging,
USB, RS232, SD/MMC, Buzzer, three status LEDs, potentiometer and two user buttons. There are plenty of
GPIOs on extension headers and also a prototype area where you can connect your additional circuits.

 16-bit/32-bit ARM7TDMI-S microcontroller in a tiny LQFP64 package.


 8 kB to 40 kB of on-chip static RAM and 32 kB to 512 kB of on-chip flash memory. 128-bit wide
interface/accelerator enables high-speed 60 MHZ operation.
 In-System Programming/In-Application Programming (ISP/IAP) via on-chip boot loader software.
 Single flash sector/full chip erase in 400 ms and programming of 256 bytes in 1 ms.
 USB 2.0 Full-speed compliant device controller with 2 kB of endpoint RAM. In addition, the LPC2146/48
provides 8 kB of on-chip RAM accessible to USB by DMA.
 One or two (LPC2141/42 vs. LPC2144/46/48) 10-bit ADCs provide a total of 6/14 analog inputs, with
conversion times as low as 2.44 μs per channel.
 Single 10-bit DAC provides variable analog output (LPC2142/44/46/48 only).
 Two 32-bit timers/external event counters (with four capture and four compare channels each), PWM unit (six
outputs) and watchdog.
 Low power Real-Time Clock (RTC) with independent power and 32 kHz clock input. Multiple serial interfaces
including two UARTs (16C550), two Fast I2C-bus (400 kbit/s), SPI and SSP with buffering and variable
data length capabilities.
 Vectored Interrupt Controller (VIC) with configurable priorities and vector addresses.
 Up to 45 of 5 V tolerant fast general purpose I/O pins in a tiny LQFP64 package. Up to 21 external interrupt
pins available.
 On-chip integrated oscillator operates with an external crystal from 1 MHz to 25 MHZ.
 Power saving modes include Idle and Power-down.
 Individual enable/disable of peripheral functions as well as peripheral clock scaling for additional power
optimization.
 Processor wake-up from Power-down mode via external interrupt or BOD.
 Single power supply chip with POR and BOD circuits:
 CPU operating voltage range of 3.0 V to 3.6 V (3.3 V ± 10 %) with 5 V tolerant I/O pads.

Software Tool
Keil MDK-ARM™ is a complete software development toolkit for ARM processor-based microcontrollers. It is
the right choice for embedded applications based on the ARM Cortex™-M series, ARM7™, ARM9™, and
Cortex-R4 processors.

Dept. of Computer Science & Engineering , SJBIT Page 1


Microcontroller & Embedded Systems 18CSL48

Fig 1:Components Details of Online Board

Dept. of Computer Science & Engineering , SJBIT Page 2


Microcontroller & Embedded Systems 18CSL48

Fig 3: Architecture Diagram of LPC2148

Dept. of Computer Science & Engineering , SJBIT Page 3


Microcontroller & Embedded Systems 18CSL48

Fig 4: Pin Diagram of LPC 2148

Dept. of Computer Science & Engineering , SJBIT Page 4


Microcontroller & Embedded Systems 18CSL48
1. Develop an Assembly Language Programs to test Data Transfer Instructions.

AREA ALP1, CODE, READONLY


ENTRY
LDR R5, =5
LDR R7, =8
MOV r7, r5
MVN r7, r5
STOP B STOP
END

2. Develop an Assembly Language Programs to test Arithmetic Operations.


AREA ALP3, CODE, READONLY
ENTRY
LDR R0, =0x00000000
LDR R1, =0x00000002
LDR r2, =0x00000001
ADD r0, r1, r2
ADD r0, r1, r1, LSL #1
SUB r0, r1, r2
STOP B STOP
END

3. Develop an Assembly Language Programs to test Reverse subtraction.


AREA ALP4, CODE, READONLY
ENTRY
LDR R0, =0x00000000
LDR R1, =0x00000077
RSB r0, r1, #0 ; RSB subtracts r1 from constant value #0 r0= -r1 (performs 2's complement)
STOP B STOP
END

4. Develop an Assembly Language Programs to test logical operations.

AREA ALP6, CODE, READONLY


ENTRY
LDR R0, =0x00000000
LDR R1, =0x02040608
LDR R2, =0x10305070
ORR r0, r1, r2
AND r0, r1, r2
EOR r0, r1, r2
BIC r0, r1, r2
STOP B STOP
END

Dept. of Computer Science & Engineering , SJBIT Page 5


Microcontroller & Embedded Systems 18CSL48

5. Develop an Assembly Language Programs to test Load and store instructions.

AREA ALP8, CODE, READONLY


ENTRY
LDR R0, =0x40000000
LDR R1, [R0]
LDR R2, =0x40000050
STR R1, [R2]
STOP B STOP
END

6. Develop an Assembly Language Programs to test for carry flag c in CPSR.

AREA ALP9, CODE, READONLY


ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
LDR R3, =0x00000001
MOVS R0, R1, LSL #1
SUBS R1, R3, #1
STOP B STOP
END

7. Develop an Assembly Language Programs to test MOVS and MOVCS instructions.

AREA ALP13, CODE, READONLY


ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
MOVS R0, R1, LSL #1
MOVCS R0, R1 ; if carry is set then R0:=R1
STOP B STOP
END

8. Develop an Assembly Language Programs to test ADDCS instruction.


AREA ALP9, CODE, READONLY
ENTRY
LDR R0, =0x00000000
LDR R1, =0x80000004
MOVS R0, R1, LSL #1
ADDCS R2, R0, R1
STOP B STOP

Dept. of Computer Science & Engineering , SJBIT Page 6


Microcontroller & Embedded Systems 18CSL48
END
PART A

Conduct the following experiments by writing program using ARM7TDMI/LPC2148 using


An evaluation board/simulator and the required software tool.
1. Write a program to multiply two 16 bit binary numbers.

;/* PROGRAM TO MULTIPLY TWO 16BIT NUMBERS */


;/* VALUE1: 1900H (6400) (IN R1) */
;/* VALUE2: 0C80H (3200) (IN R2) */
;/* RESULT: 1388000H(20480000) (IN R3) */
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK THE RESULT*/

AREA MULTIPLY, CODE, READONLY

ENTRY ;Mark first instruction to execute


START

MOV r1, #6400 ; STORE FIRST NUMBER IN R0


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

NOP
NOP
NOP

END ;Mark end of file

Dept. of Computer Science & Engineering , SJBIT Page 7


Microcontroller & Embedded Systems 18CSL48

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

AREA SUM10, CODE, READONLY


START

MOV R0, #10


MOV R1, #00

LOOP
ADD R1, R1, R0
SUBS R0, R0, #1
BNE LOOP

STOP B STOP
END

Dept. of Computer Science & Engineering , SJBIT Page 8


Microcontroller & Embedded Systems 18CSL48
3. Write a program to find factorial of a number.
PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER
; In this example we have taken n=7
; Check the result in R2/R1 register =13B0H (5040) */

AREA FACTORIAL, CODE, READONLY


START

MOV R0, #7
MOV R1, #1

LOOP
MUL R2, R1, R0
MOV R1, R2
SUBS R0, R0, #1
BNE LOOP

STOP B STOP
END

Dept. of Computer Science & Engineering , SJBIT Page 9


Microcontroller & Embedded Systems 18CSL48

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

;/* PROGRAM TO ADD an array of 16BIT NUMBERS & STORE IN INTERNAL RAM*/
;/*ARRAY OF 6 NUMBERS0X1111,0X2222,0X3333,0XAAAA,0XBBBB,0XCCCC */
;/* THE SUM IS 29997H THE RESULT CAN BE VIEWED IN LOCATION 0X40000000 & ALSO IN R0*/
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK THE RESULT */

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
LDR R2, [R1], #2 ; WORD ALIGN T0 ARRAY ELEMENT
ADD R0, R0, R2 ; ADD THE ELEMENTS
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

NOP
NOP
NOP

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

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

Dept. of Computer Science & Engineering , SJBIT Page 10


Microcontroller & Embedded Systems 18CSL48
;/* Assembly Program to find square of Number */
;/* GIVEN NUMBER IS 6 (R1) THEN RESULT IS IN R3=24H(36) */
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK THE RESULT*/

AREA SQUARE, CODE, READONLY


ENTRY ; Mark first instruction to execute

MOV R2, #00


MOV R1, #7 ;Load no whose square is to be find
LDR R0, =LOOKUP ; Load start address of Lookup table
LDRB R2, [R0, R1] ; Get square of given no in R2

STOP B STOP ; Lookup table contains Squares of no’s from 0 to 10 (in hex)

LOOKUP DCB 0,1,4,9,16,25,36,49,64,81,100

END

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


Dept. of Computer Science & Engineering , SJBIT Page 11
Microcontroller & Embedded Systems 18CSL48

;/* PROGRAM TO FIND LARGEST NUMBER IN AN ARRAY & STORE IN INTERNAL RAM*/
;/* ARRAY OF 7 NUMBERS 0X44444444 ,0X22222222,0X11111111,0X33333333,0XAAAAAAAA */
;/* 0X88888888 ,0X99999999 */
;/* RESULT CAN BE VIEWED IN LOCATION 0X40000000 & ALSO IN R2 */
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK THE RESULT */

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

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

Dept. of Computer Science & Engineering , SJBIT Page 12


Microcontroller & Embedded Systems 18CSL48
;/* PROGRAM TO FIND SMALLEST NUMBER IN AN ARRAY & STORE IN INTERNAL RAM */
;/* ARRAY OF 7 NUMBERS 0X44444444 ,0X22222222,0X11111111,0X22222222,0XAAAAAAAA*/
;/* 0X88888888 ,0X99999999 */
;/* RESULT CAN BE VIEWED IN LOCATION 0X40000000 & ALSO IN R2 */
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK THE RESULT */

AREA SMALLEST , CODE, READONLY


ENTRY ;Mark first instruction to execute
START
MOV R5,#6 ; INTIALISE COUNTER TO 6(i.e. N=7)
LDR R1,=VALUE1 ; LOADS THE ADDRESS OF FIRST VALUE
LDR R2,[R1],#4 ; WORD ALIGN T0 ARRAY ELEMENT
LOOP
LDR R4,[R1],#4 ; WORD ALIGN T0 ARRAY ELEMENT
CMP R2, R4 ; COMPARE NUMBERS
BLS LOOP1 ; IF THE FIRST NUMBER IS < THEN GOTO LOOP1
MOV R2, R4 ; if the first number is > then mov content r4 to r2
LOOP1
SUBS R5, R5,#1 ; DECREMENT COUNTER
CMP R5, #0 ; COMPARE COUNTER TO 0
BNE LOOP ; LOOP BACK TILL ARRAY ENDS
LDR R4, =RESULT ; LOADS THE ADDRESS OF RESULT
STR R2, [R4] ; STORES THE RESULT IN R1
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

7. A) Write a program to arrange a series of 32 bit numbers in ascending order.


Dept. of Computer Science & Engineering , SJBIT Page 13
Microcontroller & Embedded Systems 18CSL48

;/* array of 4 numbers 0x44444444 ,0x11111111,0x33333333,0x22222222 */


;/* set a breakpoint at start1 lable & run the program */
;/* check the unsorted numbers at location 0x40000000 next */
;/* set a breakpoint at nop instruction,run the program & check the result */
;/* result can be viewed at location 0x40000000 */

AREA SORT, CODE, READONLY


START
MOV R1, #5 ; INTIALISE COUNTER TO 5(i.e. N=5)

OUTLOOP MOV R3, R1


LDR R0, =0x40000000 ; LOADS THE ADDRESS OF FIRST VALUE

INLOOP LDR R2, [R0], #4 ; WORD ALIGN T0 ARRAY ELEMENT


LDR R4, [R0] ; LOAD SECOND NUMBER
CMP R4, R2 ; COMPARE NUMBERS
BCS SKIP ; IF THE FIRST NUMBER IS < THEN GOTO LOOP2

STR R2, [R0], #-4 ; INTERCHANGE NUMBER R2 & R0


STR R4, [R0], #4 ; INTERCHANGE NUMBER R4 & R0

SKIP SUBS R3, R3, #1 ; DECREMENT COUNTER


BNE INLOOP ; LOOP BACK TILL ARRAY ENDS
SUBS R1, R1, #1
BNE OUTLOOP

STOP B STOP
END ; Mark end of file

b) Write a program to arrange a series of 32 bit numbers in descending order.

Dept. of Computer Science & Engineering , SJBIT Page 14


Microcontroller & Embedded Systems 18CSL48
;/* PROGRAM TO sort in Descending order */
;/* ARRAY OF 4 NUMBERS 0X44444444 ,0X11111111,0X33333333,0X22222222 */
;/* SET A BREAKPOINT AT START1 LABLE & RUN THE PROGRAM */
;/* CHECK THE UNSORTED NUMBERS AT LOCATION 0X40000000 NEXT */
;/* SET A BREAKPOINT AT NOP INSTRUCTION,RUN THE PROGRAM & CHECK THE RESULT */
;/* RESULT CAN BE VIEWED AT LOCATION 0X40000000 */

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

Dept. of Computer Science & Engineering , SJBIT Page 15


Microcontroller & Embedded Systems 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 ;
DVALUE

DCD 0X00000000 ; ARRAY OF 32 BIT NUMBERS IN DATA


REGION

END ; Mark end of file

Dept. of Computer Science & Engineering , SJBIT Page 16


Microcontroller & Embedded Systems 18CSL48
8. Write a program to count the number of ones and zeros in two consecutive memory locations.

AREA COUNT, CODE, READONLY


START
LDR R0, =0x40000050
LDRH R1, [R0]
MOV R2, #16
LOOP
MOVS R1, R1, LSR #1
ADDCS R3, #1 ; number of ones in R3
ADDCC R4, #1 ;NUMBER of zeros in R4
SUBS R2, #1 ;Decrement counter
BNE LOOP

STOP B STOP
END

PART B

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

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


//******************************************************************************
// FileName : 8-bit LCD Programming using Port-1
// Microcontroller : LPC2148
// Compiler : Keil v-4
// Target Hardware : ARM7 Development Board
// Description :Printing some character in LCD using LPC2148 controller
// Pin Connection :`lcd dataline (d0 to d7) connected to port-1 pins of p1.16 to p1.23 respectively an
//Rs(Register Select) pin of LCD connected to P1.24 and
//En(Enable) pin of LCD connected to P1.25
//R/W pin of LCD always ground
//******************************************************************************

#include<lpc214x.h> // Header file for LPC2148

//*************** Function Declaration **************//

void delay(void);
void LCD_init(void);
void LCD_cmd(char cmd);
void LCD_data(char data);
void LCD_string(char *ptr);

Dept. of Computer Science & Engineering , SJBIT Page 17


Microcontroller & Embedded Systems 18CSL48

//*************** END ofFunction Declaration **************//


//*************** MAIN Program **************//

int main()
{
PINSEL2 = 0x00000000; //P1.16 to P1.31 configured as GPIO
IODIR1 = 0x03FF0000; //P1.16 to P1.25 configured as output port
LCD_init(); // Function call

while(1)
{
LCD_cmd(0x80); //LCD row 1st selection
LCD_string(" Welcome to ");//for multiple charactors(String) to be printed on the display
LCD_cmd(0xC0); //LCD 2nd row selection
LCD_string(" PESIT ");//for multiple charactors(string) to be printed on the display
}
}

//*************** END of MAIN Program **************//


//*************** Delay Program **************//

void delay(void)
{
unsigned int i, j;
for(i = 0 ; i < 1000 ; i++)
for(j = 0 ; j < 500 ; j++);
}

//*************** END of Delay Program **************//


//*************** LCD initilization Program **************//

void LCD_init()
{
LCD_cmd(0x38); //selection of the 8-data lines, 0x28 for 4-bit data
LCD_cmd(0x0C); //Display ON cursor OFF
LCD_cmd(0x06); //move the cursor right
LCD_cmd(0x01); //clear the LCD
// LCD_cmd(0x80); //firat line starting address
}

//*************** END of LCD initilization Program **************//


//*************** LCD Command Program **************//

Dept. of Computer Science & Engineering , SJBIT Page 18


Microcontroller & Embedded Systems 18CSL48
void LCD_cmd(char cmd)
{
IOCLR1 = 0x03FF0000; // P1.16 to P1.25 port pin made as low for clearing the data
IOSET1 = cmd << 18 | 0x00020000; // cmd varible content data hasbeen shifted 16-times and ORed
with 0x02000000 and result copied to the IOSET1 reg.
delay(); // Function call
IOCLR1 = 0x03FF0000; // P1.16 to P1.25 port pin made as low for clearing the data
}

//*************** END of LCD Command Program **************//


//*************** LCD DATA Program **************//

void LCD_data(char data)


{
IOCLR1 = 0x03FF0000; // P1.16 to P1.25 port pin made as low for clearing the data
IOSET1 = data << 18 | 0x00030000; // data varible content data hasbeen shifted 16-times and
ORed with 0x03000000 and result copied to the IOSET1 reg.
delay(); // Function call IOCLR1 = 0x03FF0000;
// P1.16 to P1.25 port pin made as low for clearing the data
}

//*************** END of LCD DATA Program **************//


//*************** LCD string Program **************//
void LCD_string(char *ptr)
{
while(*ptr)
LCD_data(*ptr++);
}

//*************** END of LCD string Program **************//

Dept. of Computer Science & Engineering , SJBIT Page 19


Microcontroller & Embedded Systems 18CSL48

10. Interface and Control a DC Motor.

//******************************************************************************
// FileName : DC motor Programming using Port-1
// Microcontroller : LPC2148
// Compiler : Keil v-4
// Target Hardware : ARM7 Development Board
// Description :DC motor rotating clockwise and anticlockwise direction
// Pin Connection :P1.30 and P1.31 pins of Port-0 connected to L293D IC(DC motor driver)
//******************************************************************************

#include<lpc214x.h> // Header file for LPC2148


//*************** Function Declaration **************//
void delay(void);
void dc_clock(void);
void dc_A_clock(void);
//*************** END ofFunction Declaration **************//
//*************** MAIN Program **************//
int main()
{
PINSEL2 = 0x00000000; // P1.0 to P1. 31 configured as GPIO
IODIR1 = 0xFFFF0000; // P1.16 to P1. 31 configured as ouput port
while(1)

Dept. of Computer Science & Engineering , SJBIT Page 20


Microcontroller & Embedded Systems 18CSL48
{
dc_A_clock(); // Function calling
delay();delay();delay();
dc_clock();
delay();delay();delay(); // Function calling
}
}

//*************** END of MAIN Program **************//


//*************** Delay Program **************//
void delay(void) // Delay Sub program
{
unsigned int i,j;

for(i=0;i<1000;i++)
for(j=0;j<2000;j++);
}

//*************** END of Delay Program **************//


//*************** DC motor Program **************//

void dc_A_clock(void) // DC-motor anticlockwise rotation Sub program


{
IOSET1 = 0x80000000;
delay();
IOCLR1 = 0x80000000;
delay();
}
void dc_clock(void) // DC-motor anticlockwise rotation Sub program
{
IOSET1 = 0x20000000;
delay();
IOCLR1 = 0x20000000;
delay();
}

//*************** END of DC motor Program **************//

Dept. of Computer Science & Engineering , SJBIT Page 21


Microcontroller & Embedded Systems 18CSL48

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

//******************************************************************************
// FileName : Stepper Motor Programming using Port-1
// Microcontroller : LPC2148
// Compiler : Keil v-4
// Target Hardware : ARM7 Development Board
// Description :Stepper Motor Roatating Clockwise direction after certain
delay roatating Anticlockwise direction
// Pin Connection :P1.28 to P1.31 connected to Stepper motor driver(ULN2003/2803)
//******************************************************************************

#include<lpc214x.h> // Header file for LPC2148


//**** Function Declaration ********//
void delay(void);
void stepper_clock(void);
void stepper_A_clock(void);

//*************** END ofFunction Declaration **************//


//*************** MAIN Program **************//
int main()
{
PINSEL2 = 0x00000000; // P1.16 to P1.31 configured as GPIO
IODIR1 = 0xFF << 26; // P1.16 to P1.31 configured as ouput port
while(1)
{
stepper_clock(); // Function calling (P1.28 to P1.31 port pins connected to stepper motor)
delay();
stepper_A_clock();
delay(); // Function calling (P1.28 to P1.31 port pins connected to stepper motor)
}
}

//*************** END of MAIN Program **************//


//*************** Delay Program **************//
void delay(void)
{
unsigned int i,j;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++);

Dept. of Computer Science & Engineering , SJBIT Page 22


Microcontroller & Embedded Systems 18CSL48
}

//*************** END of Delay Program **************//


//*************** Stepper MOtor Clockwise and Anti clockwise Program **************//
void stepper_A_clock(void) //Stepper Motor Clockwise subprogram
{
unsigned int i;
for(i=0;i<5;i++)
{
IOSET1 = 0x9 << 26;
delay();
IOCLR1 = 0x9 << 26;
delay();
IOSET1 = 0xC << 26;
delay();
IOCLR1 = 0xC << 26;
delay();
IOSET1 = 0x6 << 26;
delay();
IOCLR1 = 0x6 << 26;
delay();
IOSET1 = 0x3 << 26;
delay();
IOCLR1 = 0x3 << 26;
delay();
}
}
void stepper_clock(void) //Stepper Motor A_Clockwise subprogram
{
unsigned int i;
for(i=0;i<5;i++)
{
IOSET1 = 0x3 << 26;
delay();
IOCLR1 = 0x3 << 26;
delay();
IOSET1 = 0x6 << 26;
delay();
IOCLR1 = 0x6 << 26;
delay();
Dept. of Computer Science & Engineering , SJBIT Page 23
Microcontroller & Embedded Systems 18CSL48

IOSET1 = 0xC << 26;


delay();
IOCLR1 = 0xC << 26;
delay();
IOSET1 = 0x9 << 26;
delay();
IOCLR1 = 0x9 << 26;
delay();
IOSET1 = 0x3 << 26;
delay();
IOCLR1 = 0x3 << 26;
delay();
}
}

//*************** END of Stepper MOtor Clockwise and Anti clockwise Program **************//

Dept. of Computer Science & Engineering , SJBIT Page 24


Microcontroller & Embedded Systems 18CSL48
12. Determine Digital output for a given Analog input using Internal ADC of ARM controller.

//******************************************************************************
// FileName : ADC and LCD Programming
// Microcontroller : LPC2148
// Compiler : Keil v-4
// Target Hardware : ARM7 Development Board
// Description :Analog Signals Are Converted Into Digital Form And Result Printed In Lcd
// Pin Connection :LCD dataline (D0 to D7) connected to Port-1 pins of P1.16 to P1.23 respectively and
//Rs(Register Select) pin of LCD connected to P1. and
//En(Enable) pin of LCD connected to P1.25
//R/W pin of LCD always ground
//ADC channel Connected to Sensors or Resistor Pot. ADC cahnnels are - (P0.30, P0.29, P0,28, P0.25)
//******************************************************************************

#include<lpc214x.h>
#include<string.h>
void delay(void);
void LCD_init(void);
void LCD_cmd(char cmd);
void LCD_data(char data);
void LCD_string(char *ptr);
void adc_channel_select(unsigned int channel);
void adc(void);
void hex_dec(unsigned int lcd_data);
//********************* MAIN Program ******************//
int main()
{
PINSEL0 = 0x00000000; // P0.0 to P0.15 configured as GPIO
PINSEL1 = 0x01000000; // ADC cinfigure (P0.30, P0.29, P0,28, P0.25), remaining as GPIO
PINSEL2 = 0x00000000; // P1.0 to P1.31 configured as GPIO
IODIR1 = 0x03FF0000; // P1.16 to P1.25 configured as 0utput port
LCD_init(); // LCD-init function calling
LCD_cmd(0x01); // clear the display
LCD_cmd(0x80); // LCD row one selection
LCD_string("*** Welcome to ***"); // sting of data sent
LCD_cmd(0xC0); // LCD row second selection
LCD_string("*V V Tech*"); // sting of data sent
while(1)
{
adc(); // adc function calling
Dept. of Computer Science & Engineering , SJBIT Page 25
Microcontroller & Embedded Systems 18CSL48

}
}
// ******************** END of MAIN Program *********************//
// ******************** DELAY Program *********************//
void delay()
{
unsigned int i, j;
for(i = 0 ; i < 1000 ; i++)
for(j = 0 ; j < 1000 ; j++);
}
// ******************** END of DELAY Program *********************//
// ******************** LCD initilization Program *********************//
void LCD_init()
{
LCD_cmd(0x38); //selection of the 8-data lines, 0x28 for 4-bit data
LCD_cmd(0x0C); //Display ON cursor OFF
LCD_cmd(0x06); //move the cursor right
LCD_cmd(0x01); //clear the LCD
LCD_cmd(0x80); //firat line starting address
}
// ******************** END of LCD-init Program *********************//
// ******************** LCD CMD Program *********************//

void LCD_cmd(char cmd)


{
IOCLR1 = 0x03FF0000; // P1.16 to P1.25 port pin made as low for clearing the data
IOSET1 = cmd << 18 | 0x02<<16; // cmd varible content data hasbeen shifted 18-times and
//ORed with 0x02000000 and result copied to the IOSET1 reg.
delay();
IOCLR1 = 0x03FF0000; // P1.16 to P1.25 port pin made as low for clearing the data
}
// ******************** END of LCD CMD Program *********************//
// ******************** LCD data Program *********************//
void LCD_data(char data)
{
IOCLR1 = 0x03FF0000; // P1.16 to P1.25 port pin made as low for clearing the data
IOSET1 = data << 18 | 0x03<<16; // data varible content data hasbeen shifted 18-times and ORed with
//0x03000000 and result copied to the IOSET1 reg.
delay();
IOCLR1 = 0x03FF0000; // P1.16 to P1.25 port pin made as low for clearing the data

Dept. of Computer Science & Engineering , SJBIT Page 26


Microcontroller & Embedded Systems 18CSL48
}

// ******************** END of LCD data Program *********************//


// ******************** LCD string Program *********************//

void LCD_string(char *ptr)


{
while(*ptr)
LCD_data(*ptr++);
}

// ******************** END of LCD string Program *********************//


// ******************** ADC channel selection sub Program *********************//

void adc_channel_select(unsigned int channel)


{
switch(channel)
{
case 1:AD0CR=0x01200402; //channel 1 selecting
break;
case 2:AD0CR=0x01200404; //channel 2 selecting
break;
default:break;
}
}
// ******************** END of ADC chan sub Program *********************//
// ******************** ADC Program *********************//
void adc()
{
unsigned int temp, ldr, voltage;
// Variables
LCD_cmd(0x01); // clear the display
LCD_cmd(0x80); // LCD row one selection
LCD_string("Temp:"); // printing in LCD as S_1:
adc_channel_select(1); // selection of ADC channel-2
while(!(AD0GDR&0x80000000)); // checking for DONE bit high or not
temp = (AD0GDR & 0x0000FFC0) >> 6; // take the result value from AD0GDR, Masking,
// Right shift 6 times and store it in variable
hex_dec(temp); // Convert result value into ASCII/ decimal Value
delay();
Dept. of Computer Science & Engineering , SJBIT Page 27
Microcontroller & Embedded Systems 18CSL48

LCD_cmd(0xC8);
voltage=temp*0.003;
LCD_cmd(0xC0); // LCD second row selection
LCD_string("Light:");
adc_channel_select(2); //selection of ADC channel 3
while(!(AD0GDR&0x80000000)); //checking for DONE bit high or not
ldr = (AD0GDR & 0x0000FFC0) >> 6; //take the result value from AD0GDR, Masking,
//Right shift 6 times and store it in variable
hex_dec(ldr); //Convert result value into ASCII/ decimal Value
delay();
// if(temp>45)
// {
// LCD_cmd(0xD4);
// LCD_string("Temperature is high ");
// }
// else
// {
// LCD_cmd(0xD4);
// LCD_string(" ");
// }
//
// if(ldr<250)
// {
// LCD_cmd(0xD4);
// LCD_string("Light Intensity Low ");
// }
// else
// {
// LCD_cmd(0xD4);
// LCD_string(" ");
// }
//
// if(gas>750)
// {
// LCD_cmd(0xD4);
// LCD_string(" Gas Detected ");
// }
// else
// {
// LCD_cmd(0xD4);

Dept. of Computer Science & Engineering , SJBIT Page 28


Microcontroller & Embedded Systems 18CSL48
// LCD_string(" ");
// }
}
// ******************** END of ADC Program *********************//
// ******************** CONVERTION Program *********************//

void hex_dec(unsigned int lcd_data) //Hex to decimal conversion


{
unsigned int d1,d2,d3,d4;
d1 = lcd_data % 0x0a; //Modulus operation, reminder value stored in variable
lcd_data = lcd_data / 0x0a; //Division operation, quotient value stored in the variable
d2 = lcd_data % 0x0a;
lcd_data = lcd_data / 0x0a;
d3 = lcd_data % 0x0a;
lcd_data = lcd_data / 0x0a;
d4 = lcd_data % 0x0a;
LCD_data(d4+0x30); //Data to be displayed on LCD
LCD_data(d3+0x30); //Data to be displayed on LCD
LCD_data(d2+0x30); //Data to be displayed on LCD
LCD_data(d1+0x30); //Data to be displayed on LCD
}
// ******************** END of CONVERTION Program *********************//

Dept. of Computer Science & Engineering , SJBIT Page 29


Microcontroller & Embedded Systems 18CSL48

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


#include <LPC214x.h>
unsigned int value;
int main()
{
PINSEL1=0x00080000; // Enable AOUT the DAC Now we simply need to access the //DACR
register and provide the values there is nothing to //enable/disable
now.
// The value needs to be 10 bit wide which should be inside
//the 6-15 bits from 0-0x3FF we can generate the DAC.
while(1)
{
value = 0;
while ( value != 1023 )
{
DACR = ( (1<<16) | (value<<6) );
value++;
}
while ( value != 0 )
{
DACR = ( (1<<16) | (value<<6) );
value--;
}
}
return 0;
}

Dept. of Computer Science & Engineering , SJBIT Page 30


Microcontroller & Embedded Systems 18CSL48
13.b) Interface a DAC and generate Square waveforms.
#include <LPC214x.h>
void delay(void);
unsigned int value;
int main()
{
PINSEL1=0x00080000; // Enable AOUT the DAC
// Now we simply need to access the DACR register and
//provide the values there is nothing to enable/disable now.
// The value needs to be 10 bit wide which should be
// inside the 6-15 bits from 0-0x3FF we can generate the DAC.

while(1)
{
value = 0;
DACR = ( (1<<16) | (value<<6) );
delay();
value = 1023;
DACR = ( (1<<16) | (value<<6) );
delay();
}
return 0;
}
void delay()
{ int i;
for (i=0; i<2000;i++)
{
;
}
}

Dept. of Computer Science & Engineering , SJBIT Page 31


Microcontroller & Embedded Systems 18CSL48

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

//******************************************************************************
// FileName : 8-bit LCD Programming using Port-1
// Microcontroller : LPC2148
// Compiler : Keil v-4
// Target Hardware : ARM7 Development Board
// Description :Printing some character in LCD using LPC2148 controller
// Pin Connection : LCD dataline (D0 to D7) connected to Port-0 pins
of P0.16 to P0.23 respectively and
// Rs(Register Select) pin of LCD connected to P0.6 and
// En(Enable) pin of LCD connected to P0.7
// R/W pin of LCD always ground
// : Keyapd Connected to Port-1
// : row(R1,R2,R3,R4) - (P1.22,P1.23,P1.24,P1.25)
// : col(C1,C2,C3,C4) - (P1.18,P1.19,P1.20,P1.21)
//******************************************************************************
#include"prototype.h"

unsigned char number_1[11] = "8496809651", number_2[11] = "9964497104", buffer[50],temp[11], i, j,


key_value[5], key_correct[5] = "1234";

unsigned char msg[20];


unsigned int first = 0, gate = 0;
char keys[4][4] = {
{'A', '*', '5', '1'}, //1,2,3,4
{'B', '0', '6', '2'}, //5,6,7,8
{'C', '9', '7', '3'}, //9,10,11,12
{'D', '#', '8', '4'} //13,14,15,16
};

int main()
{
PINSEL0 = 0x00050000;
PINSEL1 = 0x15000000;
PINSEL2 = 0x00000000;
IODIR0 = 0x00FFFCE0;
IODIR1 |= 0x0FC00000; //P1.25 to P1.22 as Output
IODIR1 &= ~(0x003C0000); //P1.18 to P1.21 as Input
IOCLR0 = 0x00FFF000;
LCD_init();

Dept. of Computer Science & Engineering , SJBIT Page 32


Microcontroller & Embedded Systems 18CSL48
LCD_cmd(0x80);
LCD_string("LCD Initialized ");

L1: LCD_cmd(0x01);
LCD_cmd(0x80);
LCD_string("Enter Key:");
for(i = 0 ; i < 16 ; i++)
{
key_value[i] = _getKey(); // c-4 to c-1 of kepad connected to P1.18 to P1.21 respectively and
R1 to R4 of keypad connected to P1.22 to P1.25 respectively
delay();delay();
LCD_cmd(0X8B);
LCD_data(key_value[i]);
delay_wait(50);
}
goto L1;
}
unsigned char _getKey(void)
{
while(1)
{
IOPIN1 &= ~0x03C00000;
IO1PIN |= (0x07<<22); //OP1 HIGH
if(((IOPIN1&0x003C0000)>>18) == 0x0E)
return(keys[3][0]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0D)
return(keys[2][0]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0B)
return(keys[1][0]);
else if(((IOPIN1&0x003C0000)>>18) == 0x07)
return(keys[0][0]);
IOPIN1 &= ~0x03C00000;
IO1PIN |= (0x0B<<22); //OP2 HIGH
if(((IOPIN1&0x003C0000)>>18) == 0x0E)
return(keys[3][1]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0D)
return(keys[2][1]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0B)
return(keys[1][1]);
else if(((IOPIN1&0x003C0000)>>18) == 0x07)

Dept. of Computer Science & Engineering , SJBIT Page 33


Microcontroller & Embedded Systems 18CSL48

return(keys[0][1]);

IOPIN1 &= ~0x03C00000;


IO1PIN |= (0x0D<<22); //OP3 HIGH
if(((IOPIN1&0x003C0000)>>18) == 0x0E)
return(keys[3][2]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0D)
return(keys[2][2]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0B)
return(keys[1][2]);
else if(((IOPIN1&0x003C0000)>>18) == 0x07)
return(keys[0][2]);
IOPIN1 &= ~0x03C00000;
IO1PIN |= (0x0E<<22); //OP4 HIGH
if(((IOPIN1&0x003C0000)>>18) == 0x0E)
return(keys[3][3]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0D)
return(keys[2][3]);
else if(((IOPIN1&0x003C0000)>>18) == 0x0B)
return(keys[1][3]);
else if(((IOPIN1&0x003C0000)>>18) == 0x07)
return(keys[0][3]);
}
}

Dept. of Computer Science & Engineering , SJBIT Page 34


Microcontroller & Embedded Systems 18CSL48
15. Demonstrate the use of an external interrupt to toggle an LED On/Off.

//******************************************************************************
// FileName : LED Programming using Port-0
// Microcontroller : LPC2148
// Compiler : Keil v-4
// Target Hardware : ARM7 Development Board
// Description :Using PORT-0 all port pins of are making HIGH with certain delay and all port pin made LOW.

//******************************************************************************
//************* MAIN PROGRAM *****************************//

#include<lpc214x.h> // Header file for LPC2148 controller


void delay(void); // Function Declaration
unsigned int a = 0x000000C0, j=0;

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

IODIR0 = 0xFFFFFFFF; // Using IODIR0 making P0.0 to P0.31 as output port

while(1)
{
for( j=0;j<=8;j++)
{
IOSET0 = a; // P0.0 to P0.31 as set to high
delay(); // Delay Function Cal
IOCLR0 = a; // P0.0 to P0.31 as set to low
a = (a >> 1);
}
a = 0x3;
for( j=0;j<=8;j++)
{
IOSET0 = a; // P0.0 to P0.31 as set to high
delay(); // Delay Function Cal
IOCLR0 = a; // P0.0 to P0.31 as set to low
a = (a << 1);
}
a = 0x000000C0;
}

Dept. of Computer Science & Engineering , SJBIT Page 35


Microcontroller & Embedded Systems 18CSL48

//************* END of MAIN PROGRAM *****************************//


//************** Delay Program Function *********//

void delay(void) // Function Defination


{
unsigned int i,j; // Defining two varibles

for(i=0;i<1000;i++)
for(j=0;j<1000;j++);
}

//************** END of Delay Program Function *********//

Dept. of Computer Science & Engineering , SJBIT Page 36


Microcontroller & Embedded Systems 18CSL48
16. Display the Hex digits 0 to F on a 7-segment LED interface, with an appropriate delay in
between

//******************************************************************************
// FileName : Seven Segment Display Programming using Port-1
// Microcontroller : LPC2148
// Compiler : Keil v-4
// Target Hardware : ARM7 Development Board
// Description : Printing the numbers 0 to 9 in 7-segment Display using LPC2148 controller
// Pin Connection : P1.24 to P1.31 port pins are connected to Seven segment dispaly of ABCDEFGH
terminals respectively.
//******************************************************************************

#include<lpc214x.h> // Header file for LPC2148


//*************** Function Defination **************//
void delay(void)
{
unsigned int i,j;
for(j=0;j<1000;j++)
for(i=0;i<1000;i++);
}
//*************** END of Function Defination **************//
//*************** MAIN Program **************//
int main()
{
PINSEL2 = 0x00000000; // P1.0 to P1.31 as GPIO
IODIR1 = 0xFF << 18; // P1.16 tp P1.31 as ouput port
while(1)
{

IOSET1 = 0x3F << 18; // To display zero (0-display) (0x3F000000 - This value for common Anode)
delay();delay();delay();delay();
IOCLR1 = 0x3F << 18;;
delay();delay();delay();delay();

IOSET1 = 0x06 << 18; // To display one (1-display) (0x06000000)


delay();delay();delay();delay();
IOCLR1 = 0x06 << 18;
delay();delay();delay();delay();

IOSET1 = 0x5B << 18; // To display two (2-display) (0x5B000000)


delay();delay();delay();delay();
Dept. of Computer Science & Engineering , SJBIT Page 37
Microcontroller & Embedded Systems 18CSL48

IOCLR1 = 0x5B << 18;


delay();delay();delay();delay();

IOSET1 = 0x4F << 18; // To display Three (3-display) (0x4F000000)


delay();delay();delay();delay();
IOCLR1 = 0x4F << 18;
delay();delay();delay();delay();

IOSET1 = 0x99000000; // To display Four (4-display) (0x66000000)


delay();delay();delay();delay();
IOCLR1 = 0x99000000;
delay();delay();delay();delay();

IOSET1 = 0x6D << 18; // To display Five (5-display) (0x6D000000)


delay();delay();delay();delay();
IOCLR1 = 0x6D << 18;
delay();delay();delay();delay();

IOSET1 = 0x7D << 18; // To display Six (6-display) (0x7D000000)


delay();delay();delay();delay();
IOCLR1 = 0x7D << 18;
delay();delay();delay();delay();

IOSET1 = 0x07 << 18; // To display Seven (7-display) (0x07000000)


delay();delay();delay();delay();
IOCLR1 = 0x07 << 18;
delay();delay();delay();delay();

IOSET1 = 0x7F << 18; // To display Eight (8-display) (0x7F000000)


delay();delay();delay();delay();
IOCLR1 = 0x7F << 18;
delay();delay();delay();delay();
IOSET0 = 0x6F << 18; // To display nine (9-display) (0x6F000000)
delay();delay();delay();delay();
IOSET0 = 0x6F <<18;
delay();delay();delay();delay();

}
}
//*************** END of MAIN Program **************//

Dept. of Computer Science & Engineering , SJBIT Page 38

You might also like