0% found this document useful (0 votes)
95 views77 pages

MES Lab Manual

The program reads a number from memory, looks it up in a table to find its square, and stores the result in memory. It loads the number from memory into a register, then steps through the table, comparing the number to each entry. When it finds a match, it loads the corresponding square value and stores it in memory. This allows calculating squares by indexing a precomputed table rather than performing multiplication online.

Uploaded by

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

MES Lab Manual

The program reads a number from memory, looks it up in a table to find its square, and stores the result in memory. It loads the number from memory into a register, then steps through the table, comparing the number to each entry. When it finds a match, it loads the corresponding square value and stores it in memory. This allows calculating squares by indexing a precomputed table rather than performing multiplication online.

Uploaded by

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

‘/

MICROCONTROLLER AND EMBEDDED SYSTEMS


LABORATORY

18CL48

ATRIA INSTITUTE OF TECHNOLOGY


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Bengaluru - 560024
2020

1
Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 2 of 75


Microcontroller And Embedded Systems 18CL48

Contents

1 A1 Multiply 4

2 A2 Sum 6

3 A3 Factorial 8

4 A4 Array 10

5 A5 Table lookup 12

6 A6 Min/Max 14

7 A7 Sort 16

8 A8 Count 1s 19

9 B1 UART 22

10 B2 DC Motor 26

11 B3 Stepper Motor 30

12 B4 ADC 34

13 B5 DAC 40

14 B6 Keypad 45

15 B7 EINT 49

16 B8 7SD 54

17 BU LCD 60

CSE Jan, 2020 3 of 75


Microcontroller And Embedded Systems 18CL48
18 LPC2148 65

CSE Jan, 2020 4 of 75


Microcontroller And Embedded Systems 18CL48

1 A1 Multiply

Write a program to multiply two 16 bit binary numbers.

Objective

1. Multiply two 16 bit numbers

Design

1. Initialize memory with 16bit data i.e half words


2. Reserve memory space for the results
3. Load half words from memory into registers
4. Store the multiplication result of 2x16 bit words into memory word
5. Algorithm
int main()
{
int n, n1, n2;
n = n1 * n2;
}

ARM Instructions

1. DCW, DCD directives


2. LDR, LDRH, STR, MUL

Procedure

1. Create project A1/A1.s for the leagcy device LPC2148 w/o startup.s
2. Translate A1.s and build

3. View RO data in memory window 1


4. View RW data in memory window 2 at 0x40000000
5. Trace the program logic step by step (using say F10)

Note: Result is stored in LE format.

CSE Jan, 2020 5 of 75


Microcontroller And Embedded Systems 18CL48

4 3 2 1 1 2 3 4
0x00 00 01 00 -> 00 01 00 00

; A1.s
; Multiplication of 16bit data

AREA AP, CODE, READONLY


ENTRY

START

LDR R0, =RD

LDRH R1, [R0],#2 ; R1 = mem[R0], R0 = R0 +2, Postincrment


LDRH R2, [R0],#2

MUL R3, R2, R1 ; R3 = R2*R1

LDR R0, =WR


STR R3, [R0] ; mem[R0] = R3

STOP B STOP

AREA RO, DATA, READONLY


RD DCW 0x10, 0x10 ; Test with different input values

AREA RW, DATA, READWRITE


WR DCD 0x0 ; Result of multiplication here

END

CSE Jan, 2020 6 of 75


Microcontroller And Embedded Systems 18CL48

2 A2 Sum

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

Objectives

1. Sum of first 10 natural

numbers Design

1. Reserve memory for the result


2. Generate first 10 numbers
3. Compute incremental sum
4. Store the sum
5. Algorithm
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}

ARM Instructions

1. Directives DCD
2. MOV, ADD, CMP, BLE, LDR, STR

Procedure

1. Create project A2/A2.s for the leagcy device LPC2148 w/o startup.s
2. Translate A2.s and build
3. View RW data in memory window 1 at 0x40000000
4. Trace the program logic step by step (using say F10)
5. Store the results and verify it to be n(n+1)/2

; A2.s
; Sum of first 10 natural numbers

CSE Jan, 2020 7 of 75


Microcontroller And Embedded Systems 18CL48

AREA AP, CODE, READONLY


ENTRY

START
MOV R0, #0 ; R0=0, accumulates total
MOV R1, #1 ; R1=1, counts from 1 to 10

LOOP
ADD R0, R0, R1 ; R0 = R0 + R1
ADD R1, R1, #1 ; R1 = R1 + 1
CMP R1, #10 ; repeat if R1 < 10
BLE LOOP

LDR R1, =WR


STR R0, [R1] ; mem[R1] = R0

STOP B STOP

AREA RW, DATA, READWRITE


WR DCD 0x0 ; Result is stored here

END

CSE Jan, 2020 8 of 75


Microcontroller And Embedded Systems 18CL48

3 A3 Factorial

Write a program to find factorial of a number.

Objectives

1. Fact(n)

Design

1. Input is n
2. Reserve memory for the result n!
3. Computer factorial as n.(n-1). . . 3.2.1

4. Store the results in memory


5. Algorithm
fact = 1;
for (i=n; i > 0; i--)
{
fact = fact*i;
}

ARM Instructions

1. Directives DCD
2. MOV, MUL, SUB[S], BNE, LDR, STR

Procedure

1. Create project A3/A3.s for the leagcy device LPC2148 w/o startup.s
2. Translate A3.s and build
3. View RW data in memory window 1 at 0x40000000
4. Trace the program logic step by step (using say F10)
5. Store the results and verify it to be n.(n-1). . . 3.2.1

CSE Jan, 2020 9 of 75


Microcontroller And Embedded Systems 18CL48

; A3.s
; factorial n

AREA AP, CODE, READONLY


ENTRY

START
MOV R0, #1 ; fact = 1
MOV R1, #5 ; n
LOOP

MUL R0, R1, R0 ; n*(n-1)(n-2)...3.2.1


SUBS R1, R1, #1 ; n = n-1
BNE LOOP

LDR R1, =WR


STR R0, [R1] ; mem[R1] = R0

STOP B STOP

AREA RO, DATA, READWRITE


WR DCD 0x0 ; Result stored here

END

CSE Jan, 2020 10 of 75


Microcontroller And Embedded Systems 18CL48

4 A4 Array

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

Objective

1. Sum of numbers in an

array Design

1. Load 32bit numbers in RO memory


2. Set the base as begining of array
3. Get the number at the base and advance the base addresss to next number

4. Stop after reading N numbers


5. Algorithm
sum = 0;
for (i=0; i < n; i++)
{
sum = sum + a[i];
}

ARM Instructions

1. Directives DCD
2. LDR, MOV, ADD, CMP, BNE, LDR, STR

Procedure

1. Create project A4/A4.s for the leagcy device LPC2148 w/o startup.s
2. Translate A4.s and build
3. View RO data in memory window 1 at 0x0000 0000
4. View RW data in memory window 2 at 0x4000 0000
5. Trace the program logic step by step (using say F10)
6. Store the results

CSE Jan, 2020 11 of 75


Microcontroller And Embedded Systems 18CL48

; A4.s
; Find sum of numbers in array

AREA AP, CODE, READONLY


ENTRY
N EQU 5
START
LDR R0, =RD
MOV R1, #0 ; i = 0
MOV R2, #0 ; sum = 0
LOOP
LDRH R3, [R0], #2 ; n = *A++
ADD R2, R2, R3 ; sum = sum + n
ADD R1, R1, #1 ; i = i+1
CMP R1, #N ; i < 5, repeat
BNE LOOP

LDR R3, =WR


STR R2, [R3] ; mem[R3] = R2

STOP B STOP

AREA RO, DATA, READONLY


RD DCW 1, 2, 3, 4, 5 ; Array of numbers

AREA RW, DATA, READWRITE


WR DCD 0x0 ; Result is tored here

END

CSE Jan, 2020 12 of 75


Microcontroller And Embedded Systems 18CL48

5 A5 Table lookup

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

Objective

1. Find square(n) from the lookup table

a[n] Design

1. Read n from RO memory


2. Traverse the table for matching ’n’
3. Read out the associated value
4. Store the result in RW memory
5. Algorithm
sum = 0;
for (i=0; i < n; i++)
{
if (n == a[i]) break;
}

if (i < n) printf("Found");

ARM Instructions

1. Directives DCD
2. LDR, LDMIA, MOV, ADD, CMP, BEQ, BNE, LDR, STR

Procedure

1. Create project A5/A5.s for the leagcy device LPC2148 w/o startup.s
2. Translate A5.s and build
3. View RO data in memory window 1 at 0x0000 0000
4. View RW data in memory window 2 at 0x4000 0000
5. Trace the program logic step by step (using say F10)
6. Store the results as table[n]

CSE Jan, 2020 13 of 75


Microcontroller And Embedded Systems 18CL48

; A4.s
; Find square of a number (1-10) using look up table

AREA AP, CODE, READONLY


ENTRY
N EQU 4
START
LDR R0, =RD ; LB R0 = a
LDR R1, =RD+40 ; UB R1 = ARRAY a + 5*8
MOV R2, #N ; Key n = N
MOV R5, #0 ; 0
LOOP
LDMIA R0!, {R3, R4} ; Get the tuple {n, n^2}, incr i to next tuple
CMP R2, R3 ; Found if n= N, R4 hold the square of n
BEQ DONE ; square(n)
CMP R0, R1 ; End of the array
BNE LOOP
MOV R4, #-1 ; -1 if not found

DONE

LDR R5, =WR


STR R4, [R5] ; mem[R5] = R4

STOP B STOP

AREA RO, DATA, READONLY


RD DCD 1, 1, 2, 4, 3, 9, 4, 16, 5, 25

AREA RW, DATA, READWRITE


WR DCD 0x0 ; Result stored here

END

CSE Jan, 2020 14 of 75


Microcontroller And Embedded Systems 18CL48

6 A6 Min/Max

1. Write a program to find the largest/smallest number in an array of 32 numbers .

Objective

1. Smallest number in the list of numbers

Design

1. Input 32 numbers in RO memory


2. Select first number as smallest
3. Check all other numbers and change smallest if required

4. Store the smallest number in RW memory


5. Algorithm
min = a[0];
for (i=0; i < n; i++)
{
if (min > a[i]) min = a[i];
}

ARM Instructions

1. Directives DCW, DCD


2. LDR[H], MOV[GT], CMP, BNE, LDR, STR

Procedure

1. Create project A6/A6.s for the leagcy device LPC2148 w/o startup.s
2. Translate A6.s and build
3. View RO data in memory window 1 at 0x0000 0000
4. View RW data in memory window 2 at 0x4000 0000

5. Trace the program logic step by step (using say F10)


6. Store the results as e.g min(10, 5, 15) is 5

CSE Jan, 2020 15 of 75


Microcontroller And Embedded Systems 18CL48

; A6.s
; Find smallest/larget of the number in an array of 32 numbers

AREA AP, CODE, READONLY


ENTRY
N EQU 5

START
LDR R0, =RD ; location of a[0] i.e a
LDR R1, =RD+8 ; location of a[N-1] i.e a + (N-1)*2
LDRH R2, [R0] ; min = a[0], i=0

LOOP
LDRH R3, [R0, #2]! ; n = A[++i] i.e i = i + 2 (preindexing)
CMP R2, R3 ; R2 > R3
MOVGT R2, R3 ; min = R3
CMP R0, R1 ; i < N, repeat
BNE LOOP

LDR R3, =WR


STR R2, [R3] ; mem[R3] = R2, minimum value

STOP B STOP

AREA RO, DATA, READONLY


RD DCW 15, 10, 5, 20, 25 ; Array of numbers

AREA RW, DATA, READWRITE


WR DCD 0x0 ; Result stored here

END

CSE Jan, 2020 16 of 75


Microcontroller And Embedded Systems 18CL48

7 A7 Sort

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

order. Objective

1. Sort numbers

Design

1. Input numbers in RO memory


2. Copy them over to RW memory
3. Sort using bubble sort algorithm
4. Results are in RW memory
5. Algorithm

for (int i=0; i < n; i++)


{
for (int j=0; j < n-1-i; j++)
{
if (a[j] > a[j+1])
{
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}

ARM Instructions

1. Directives DCW
2. LDMIA, STMIA, ADD, SUBS, CMP, BGT, BGE, LDR, STR

Procedure

1. Create project A7/A7.s for the leagcy device LPC2148 w/o startup.s
2. Translate A7.s and build
3. View RO data in memory window 1 at 0x0000 0000

CSE Jan, 2020 17 of 75


Microcontroller And Embedded Systems 18CL48

4. View RW data in memory window 2 at 0x4000 0000


5. Trace the program logic step by step (using say F10)
6. Sorted results aare in RW memory

; A7.s
; Sort numbers (bubble sort)
AREA AP, CODE,
READONLY ENTRY
N EQU 5
START
LDR R0, =RD
LDR R1, =WR
LDMIA {R2-R6} ; copy numbers from RO area
R0!, {R2-R6} ; store in RW area
STMIA
R1!,

MOV R1, #0 ; i = 0 LB
MOV R7, #N ; i = n UB
MOV R8, #N
LOOPI
LDR R0, =WR
MOV R2, #0 ; j = 0 LB
SUB R8, #1 ; j = n-1-i
LOOPJ^^I
LDM R0, {R5, R6} ; r5,r6 = A[j], A[j+1]
CMP R5, R6
BLE SKIP
MOV R9, R5 ; R6, R5 seq doesn't work for STM
STM R0, {R6, R9} ; A[j], A[j+1] = R6, R5 (swap)
SKIP
ADD R0, #4 ; j = J+1
; Inner Loop
ADD R2, R2, #1 ; Outer loop
CMP R2, R8 ; j < n-1-i, repeat
BLT LOOPJ

; Outer Loop
ADD R1, R1, #1 ; Outer loop
CMP R1, R7 ; i < n, repeat
BLT LOOPI

STOP B STOP

AREA RO, DATA, READONLY


RD DCD 5, 4, 3, 2, 1 ; numbers

AREA RW, DATA, READWRITE


WR DCD 0, 0, 0, 0, 0 ; RW <- RD and then sort

CSE Jan, 2020 18 of 75


Microcontroller And Embedded Systems 18CL48

END

CSE Jan, 2020 19 of 75


Microcontroller And Embedded Systems 18CL48

8 A8 Count 1s

Write a program to count the number of ones and zeros in two consecutive memory locations.

Objective

1. Count 0s and 1s
2. In two consecutive locations

Design

1. Write a function to count 1s


2. Invoke function to get the count of two 1s in two locations

3. 1s are counted if there is carry on ROR


4. 0s is derved from 1s count i.e by subtracting 1s count from 64
5. Algorithm
unsigned int bitCount (unsigned int value) {
unsigned int count = 0;
while (value > 0) { // until all bits are zero
if ((value & 1) == 1) // check lower bit
count++;
value >>= 1; // shift bits, removing lower bit
}
return count;
}

ARM Instructions

1. Directives DCD

2. LDR, STR, BL, MOV, MOVS, ADDCS, SUBS, BNE, LDMFD, STMFD
3. LDMFD, where FD is Full Descending

Procedure

1. Create project A8/A8.s for the leagcy device LPC2148 w/o startup.s
2. View RO data in memory window 1 at 0x0000 0000

CSE Jan, 2020 20 of 75


Microcontroller And Embedded Systems 18CL48

3. View RW data in memory window 2 at 0x4000 0000


4. Trace the program logic step by step (using say F10)
5. Store the results 0s in word and 1s in another word

; A8.s
; Count 0s and 1 in two consecutive locations

AREA AP, CODE, READONLY


ENTRY
START
LDR SP, =STACK
LDR R2, =RD
STR R2, [SP] ; store R2 before

LDR R0, [R2] ; R0 = number


BL COUNT ; n = count(R0)
MOV R3, R0 ; R3 = R0 returned by count()

LDR R0, [R2, #4] ; R0 = numnber


BL COUNT ; n = count(R0)
ADD R3, R3, R0 ; 1s counted to the previous count of 1s
MOV R4, #64
SUB R4, R4, R3 ; count 0's = 64 - count 1's

LDR R2, =WR


STM R2, {R3, R4} ; mem[2], mem[r2+4] = counts of 0's and 1's

STOP B STOP
NOP

; n = count()
; this function counts the 1s and returns its count

COUNT
STMFD {R2, LR} ; save registers
SP!,
MOV R1, #0x0 ; n = 0
MOV R2, #32 ; i = 32, number of times to rotate

ONES
MOVS R0, R0, ROR #1 ; rotate right R0
ADDCS R1, R1, #1 ; n = n+1 if there is carry
SUBS R2, R2, #1 ; i = i-1
BNE ONES ; i > 32, repeat

MOV R0, R1 ; R0 = n, return value


LDMFD SP!, {R2, PC} ; unsave registers

CSE Jan, 2020 21 of 75


Microcontroller And Embedded Systems 18CL48

AREA RO, DATA, READONLY


RD DCD 0x80808080, 0x40404040 ; numbers in consecutive locations

AREA ARW, DATA, READWRITE


WR DCD 0x0, 0x0 ;result count of 0,s, count of 1's

DCD 0,0,0,0,0 ; stack area, decreasing


STACK

END

CSE Jan, 2020 22 of 75


Microcontroller And Embedded Systems 18CL48

9 B1 UART

Display "Hello World" message using Internal UART.

Design;

For the transmitter operation, the TX has two special registers called Transmit Holding
Register (THR) and Transmit Shift Register (TSR). In order to transmit the data, it is first sent
to THR and then moved to TSR.

For the receiver operation, the RX has two special registers called Receiver Bu ffer Register (RBR)

and Receive Shift Register (RSR). When the data is received, it is first stored in the RSR and
then moved to RBR.

The baudrate is configured using UART0 Divisor Latch Registers. Choose the U0DLL and
U0DLM such that the baudrate is 9600.

PCLK
baudrate =
16 ∗ U
0DLL
U 0DLL PCLK
=
16 ∗ 9600

Where PCLK is 1/4 of the CCLK (60MHz).

CSE Jan, 2020 23 of 75


Microcontroller And Embedded Systems 18CL48

The format of the data transmitted or received is configured using the line control register.
Configure the line with 1 stop bit, no parity and 8bit word length.

CSE Jan, 2020 24 of 75


Microcontroller And Embedded Systems 18CL48

The Line Status Register is used to check TX ready and RX ready condition. Check the THR
is empty (THRE) before writing THR and data ready (DR) before reading RBR.

Hardware

• LPC 2148

• UART 16C550
• USB Cable

• Terminal/PC

Software

• Keil uVision5

• Flash Magic Tool


• Traget options: MicroLIB , HEX file, Memory layout

Procedure

1. Configure the ports P0.0 and P0.1 as Tx and Rx respectively


2. Enable DLAB in LCR to access divisor latch registers
3. Program LCR for 1 stop bit, no parity and word length 8bits
4. Program U0DLL and U0DLM for 9600 baudrate
5. Remove access to DLL by resetting DLAB
6. Transmit data if transmit hold register is empty (THRE)

Simulation

1. Create project B1/B1.c for the leagcy device LPC2148 with startup.s
2. Open output windows View->Serial Window->UART#1
3. Open Peripheral window Pheriperal->UART->UART0
4. Run to main

CSE Jan, 2020 25 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 26 of 75


Microcontroller And Embedded Systems 18CL48

//1. Display "Hello World" message using Internal UART.


#include <LPC214X.h>

// PINSEL

#define TX (0b01 << 0) //bits(1:0) P0.0 as TX


#define RX (0b01 << 2) //bits(3:2) P0.1 as RX

// LCR

#define DLAB (1 << 7) //bit(7)


#define WL (0b11 << 0) //bits(1:0)

// LSR

#define THRE (1 << 5) //bit(5)


#define DR (1 << 0) //bit(0)

void init()
{
PINSEL0 = TX | RX;

U0LCR = DLAB |
WL;
U0DLM = 0x00;
U0DLL = 15000000 / (16*9600);
U0LCR = WL; // Clear DLAB
}

void tx(char ch)


{
while(!(U0LSR & THRE));
U0THR = ch; // Transmit the char
}

int main()
{
char msg[] = "Hello World\r\n";

init();
for (int i=0; msg[i]; i++)
{
tx(msg[i]);
}
}

CSE Jan, 2020 27 of 75


Microcontroller And Embedded Systems 18CL48

10 B2 DC Motor

In terface and Control a DC Motor.

Design

A DC motor is a device that converts electrical energy (direct current system) into mechanical
energy. It is of vital importance for the industry today.

The aim of this project is to demonstrate the DC Motor control using LPC2148. Since motors
draw a significant amount of current, we need a separate IC called Motor Driver IC.

When the system is powered on, the status of the button is read by the MCU. As per the
program, when the button is not pressed, the motor rotates in forward direction.

Whenever the button is pressed, a change in state at the button pin is detected by the MCU
and the motor rotates in reverse directions.

Circuit Diagram

• P0.21 - PWM
• P1.30 - IN1 (driver)
• p1.31 - IN2 (driver)
• Enable 1 - Directly giving 5v

Hardware

• LPC2148
• Motor driver
• Motor
• Powersupply to the driver

Software

• Keil uVision5
• Flash Magic Tool
• Traget options: MicroLIB , HEX file, Memory layout

Procedure

CSE Jan, 2020 28 of 75


Microcontroller And Embedded Systems 18CL48

1. EN Pin High, driver is enabled


2. Forward (general switch SW25 is ON)

• IN1 is High
• IN2 is LOW

Allow motor to run for say 5s

3. Reverse (general switch S25 is OFF)

• IN1 is LOW
• IN2 is HIGH

Allow motor to run for say 5sec


4. Repeat step 2 and 3

Simulation

1. Create project B2/B2.c for the leagcy device LPC2148 with startup.s
2. Open Peripherals->GPIO slow interface->Port0

CSE Jan, 2020 29 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 30 of 75


Microcontroller And Embedded Systems 18CL48

//2. Interface and Control a DC Motor.


#include <lpc214x.h>

#define IN1 (1 << 30) // P1.30


#define IN2 (1 << 31) // P1.31
#define SW1 (1 << 23) // P0.23 Switch

void delay(int n)
{
for(int i=0; i < n; i++)
{
for(int j=0; j < 6000; j++); /* At 60Mhz, the loop delays by 1ms */
}
}

int main()
{
IO1DIR &= ~SW1; //Input port from the switch
IO1DIR = IN1 | IN2; //Output ports IN1 and IN2

// Clockwise or anticlockwise based SW1. SW1 is OFF in forward position,


// ON if backwards.
while (1)
{
IO1SET = IO0PIN & SW1 ? IN1 : IN2;
IO1CLR = IO0PIN & SW1 ? IN2 : IN1;

delay(1000); // stop after 5s


}
}

CSE Jan, 2020 31 of 75


Microcontroller And Embedded Systems 18CL48

11 B3 Stepper Motor

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

Design:

Stepper motor is brushless DC motor, which can be rotated in small angles, these angles are
called steps. We can rotate stepper motor step by step by giving digital pulses to its pins.
Speed of the motor can be controlled by changing frequency of digital pulses.

There are two types of stepper motors available based on the type of stator winding: UNIPOLAR
and BIPOLAR. Here we are using UNIPOLAR stepper motor which is the most commonly
used stepper motor. To rotate the stepper motor we need to energise the coils of the stepper
motor in a sequence. Based on the rotational operation they are classified into two modes:

1. Full Step Mode: (4-Step Sequence)

• One-Phase On Stepping (WAVE STEPPING)


• Two-Phase On Stepping

2. Half Step Mode (8-Step Sequence)

Here we will use FULL STEP: ONE PHASE ON or WAVE STEPPING mode to rotate the
Stepper Motor.

In this method we will energise one coil (one pin of LPC2148) at a time. The sequence of
energising the coils A, B, C and D is shown below.

CSE Jan, 2020 32 of 75


Microcontroller And Embedded Systems 18CL48

Step Coil A Coil B Coil C Coil D


P0.16 P0.17 P0.18 P0.19
1 H L L L
2 L H L L
3 L L H L
4 L L L H

Hardware

• LPC2148
• ULN2803

• STEPPER MOTOR (28BYJ-48)?

Software

• Keil uVision5

• Flash Magic Tool

Procedure

• Connect 4 output ports p0.16-19 to the stepper motor


• Set the IOPIN0 (PORT pins P0.16-19) to HIGH for clockwise operation

• Set the IOPIN0 (PORT pins P0.19-16) to LOW for anticlockwise operation
• Set the delay 10ms (Full speed), 20ms (Gets slow), 30ms (Gets

slower) Simulation

1. Create project B3/B3.c for the leagcy device LPC2148 with startup.s

2. Open Peripherals->GPIO slow interface->Port0

CSE Jan, 2020 33 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 34 of 75


Microcontroller And Embedded Systems 18CL48

//3. Interface a Stepper motor and rotate it in clockwise and anti-clockwise


direction. #include <lpc214x.h>

#define STEPS 556 //550 gives one complete rotation. May choose fewer steps
#define DELAY 10 //10 ms, increased delay cause slowdown in speed

void delay(int n)
{
for(int i=0; i < n; i++)
{
for(int j=0; j < 6000; j++); /* At 60Mhz, the loop delays by 1ms */
}
}

int main()
{
//PINSEL1 = 0x00000000; Setting PORT0 pins
IO0DIR |= 1 << 16 | 1 << 17 | 1 << 18 | 1 << 19; //Output ports P0.16-19

while(1)
{
for (int i=0; i < STEPS; i++)
{
for(int n=16; n <= 19; n++)
{
IOPIN0 = 1 << n; // Set the P0.n value HIGH
delay(DELAY);
}
}

for (int i=0; i < STEPS; i++)


{
for(int n=19; n>=16; n--)
{
IOPIN0 = 1 << n;
delay(DELAY);
}
}
}
}

CSE Jan, 2020 35 of 75


Microcontroller And Embedded Systems 18CL48

12 B4 ADC

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

Design

ADC is used by most of the sensors for e.g. temperature sensor, force sensor, IR sensor, poten-
tiometer, and many more. All these sensors send the data to the microcontroller in analogue
form, which then converts it into digital values.

ADCs in LPC2148 use Successive Approximation technique to convert analog signal into
digital form.

The ADC reference voltage is measured across GND to VREF, meaning it can do the
conversion within this range. Usually, the VREF is connected to VDD. As LPC2148 works on
3.3 volts, this will be the ADC reference voltage.

Now the resolution of ADC = 3.3/(210) = 3.3/1024 =0.003222 = 3.2mV

ADCR A/D Control register, used for configuring the ADC

31:28 27 26:24 23:22 21 20 19:17 16 15:8 7:0


Reserved EDGE START Reserved PDN Reserved CLKS BURST CLCKDIV SEL

1. Bit 7:0 - SEL : Channel Select


These bits are used to select a particular channel for ADC conversion. One bit is allotted
for each channel. Setting the Bit-0 will make the ADC to sample AD0[0] for conversion.
Similary setting bit-7 will do the conversion for AD0[7].

CSE Jan, 2020 36 of 75


Microcontroller And Embedded Systems 18CL48

2. Bit 15:8 - CLCKDIV : Clock Divisor


The APB clock (PCLKADC0) is divided by (this value plus one) to produce the clock for
the A/D converter, which should be less than or equal to 13 MHz.

1. Bit 21 - PDN : Power Down Mode


Setting this bit brings ADC out of power down mode and makes it operational. Clearing
this bit will power down the ADC.
2. Bit 24:26 - START
When the BURST bit is 0, these bits control whether and when an A/D conversion is
started:
000 - Conversion Stopped
001- Start Conversion
Now

ADGDR

31 30 26:24 23:16 15:6 5:0


DONE OVERRUN CHN Reserved RESULT Reserved

1. Bit 15:6 - RESULT


This field contains the 10bit A/D conversion value for the selected channel in ADCR.SEL
The value from this register should be read only after the conversion is complete ie
DONE bit is set.
2. Bit 26:24 - CHN : Channel
These bits contain the channel number for which the A/D conversion is done and the
converted value is available in RESULT bits(e.g. 000 identifies channel 0, 011 channel
3. . . ).

3. Bit 30 - OVERRUN
This bit is set during the BURST mode where the previous conversion data is overwritten
by the new A/D conversion value.
4. Bit 31 - DONE
This bit is set to 1 when an A/D conversion completes. It is cleared when this register is
read and when the ADCR is written. If the ADCR is written while a conversion is still in
progress, this bit is set and a new conversion is started.

Hardware

• LPC2148
• ADC (builtin)

CSE Jan, 2020 37 of 75


Microcontroller And Embedded Systems 18CL48

• LCD (2 x 16 Character LCD Display) P0.10-13

Software

• Keil uVision5

• Flash Magic Tool

Procedure

1. Configure the port P0.30 to function as AD0.3 (bit 2*n-32) PINSEL1 register.
2. Enable the CLock to ADC module.
3. Deselect all the channels and Power on the internal ADC module by setting ADCR.PDN
bit.

4. Select the Particular channel for A/D conversion by setting the corresponding bits in
ADCR.SEL
5. Set the ADCR.START bit for starting the A/D conversion for selected channel.
6. Wait for the conversion to complete, ADGR.DONE bit will be set once conversion is over.
7. Read the 10-bit A/D value from ADGR.RESULT.
8. Use it for further processing or just display on LCD.

Simulation

1. Create project B4/B4.c for the leagcy device LPC2148 with startup.s

2. Open Peripherals->GPIO slow interface->Port0


3. Open Peripherals->A/D converter-> A/D converter 0

CSE Jan, 2020 38 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 39 of 75


Microcontroller And Embedded Systems 18CL48

//4. Determine Digital output for a given Analog input using Internal ADC of ARM controller.
#include <lpc214x.h>
#include <stdio.h>
#include <string.h>

extern void initLcd(void);


extern void lcds(char s[]);
extern void delay(int n);

//PINSEL1
#define ADC3 (0b01 << 28) // 29:28 to AD0.3 (fn=01). P0.30 hint 2n-32

// AD0CR
// | 31:28 | 27 | 26:24 | 23:22 | 21 | 20 | 19:17 | 16 | 15:8 | 7:0 |
// | Reserved | EDGE | START | Reserved | PDN | Reserved | CLKS | BURST | CLCKDIV | SEL |

#define CHNSEL (1 << 3) // Channel 7:0 i.e 7 6 5 3 2 1 0


#define CLKDIV (3 << 8) // Divide the clock by n = 3MHz (<= 13 MHz)
#define PDN (1 << 21) // PDN is 1 i.e operational
#define BURST (1 << 16)
#define START (000 << 24) // Start conversion

// AD0DR3
// | 31 | 30 | 26:24 | 23:16 | 15:6 | 5:0 |
// | DONE | OVERRUN | CHN | Reserved | RESULT | Reserved |

#define DONE (1 << 31) // set bit 31

void initAdc()
{
PINSEL1 = ADC3;
AD0CR = PDN | CLKDIV | CHNSEL | BURST | START; //0x00210308
}

int readAdc()
{
AD0CR |= START;
while (!(AD0DR3 & DONE)); /* Wait till conversion DONE */

return ((AD0DR3 >> 6) & 0x3FF); // 10bit ADC


}

int main(void)
{
int rv;
float voltage;
char s[10];

initAdc();

CSE Jan, 2020 40 of 75


Microcontroller And Embedded Systems 18CL48

initLcd();
lcds("ADC...");
while(1)
{
rv = readAdc();
voltage = (rv/1023.0)*3.3; // Convert ADC value to equivalent voltage
sprintf(s, "%.2f", voltage);
lcds(s);
delay(1000);
}
}

CSE Jan, 2020 41 of 75


Microcontroller And Embedded Systems 18CL48

13 B5 DAC

Interface a DAC and generate Triangular and Square waveforms.

Design

Digital to Analog Converter (DAC) are mostly used to generate analog signals (e.g. sine wave,
triangular wave etc.) from digital values.

LPC2148 has 10 bit DAC and offers changeable analog o/p. LPC2148 has Analog output pin
(AOUT) on chip, where we can get digital value in the form of Analog output voltage.

The Analog voltage on AOUT pin is calculated as ((VALUE/1024) * VREF). Hence, we can
change voltage 0 to 3.3V by changing 10-bit digital value field in DACR (DAC Register).

DACR

Reserved Bias Value Reserved


31:17 16 15:6 5:0

1. Bit 5:0 - RESERVED


2. Bits 15:6 - VALUE This field contains the 10-bit digital value that is to be converted in to
Analog voltage. We can get Analog output voltage on AOUT pin and it is calculated with
the formula (VALUE/1024) * VREF.
3. Bit 16 - BIAS 0 = Maximum settling time of 1ţsec and maximum current is 700µA
1 = Settling time of 2.5ţsec and maximum current is 350
Note that, the settling times are valid for a capacitance loada on the AOUT pin not
exceeding 100 pF. A load impedance value greater than that value will cause settling
time longer than the specified time.
4. Bit 31:17 - RESERVED

Hardware

• LPC2148

CSE Jan, 2020 42 of 75


Microcontroller And Embedded Systems 18CL48

• CRO

Software

• Keil uVision5

• Flash Magic Tool

• Procedure

1. First, configure P0.25/AOUT pin as DAC output using PINSEL Register.


2. Then set settling time using BIAS bit in DACR Register.
3. Now write 10-bit value (which we want to convert into analog form) in VALUE field of
DACR Register.

Simulation

1. Create project B5/B5.c for the leagcy device LPC2148 with startup.s
2. Open Peripherals->D/A converter
3. Setup LA to monitor amplitude of AOUT

CSE Jan, 2020 43 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 44 of 75


Microcontroller And Embedded Systems 18CL48

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


#include <LPC214x.h>

//PINSEL1
#define AOUT (0b10 << 18) // P0.25 as AOUT (fn=10) is selected using PINSEL1 19:18. P0.25 => 2*2

// DACR
// | Reserved | Bias | Value | Reserved |
// | 31:17 | 16 | 15:6 | 5:0 |

#define BIAS (1 << 16) // settling time


#define VALUE(d) ( d << 6) // 10 bit data

void delay(int n)
{
for(int i=0; i < n; i++)
{
for(int j=0; j < 6000; j++); /* At 60Mhz, the loop delays by 1ms */
}
}

void triangle()
{
int d;

d = 0;
while (d != 1023)
{
DACR = VALUE(d) & 0x0001FFC0;
d++;
//delay(1);
}

while (d != 0)
{
DACR = VALUE(d) &
0x0001FFC0; d--;
//delay(1);
}
}

void square()
{
int d;

// Square wave

d = 1023;
DACR = VALUE(d) & 0x0001FFC0;

CSE Jan, 2020 45 of 75


Microcontroller And Embedded Systems 18CL48

delay(1);

d = 0;
DACR = VALUE(d) & 0x0001FFC0;
delay(1);
}

int main()
{
PINSEL1 = AOUT;

while(1)
{
for (int i=0; i <5; i++)
//triangle();

for (int i=0; i <5; i++)


square();
}
}

CSE Jan, 2020 46 of 75


Microcontroller And Embedded Systems 18CL48

14 B6 Keypad

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

Design:

At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU
accesses both rows and columns through ports. When a key is pressed, a row and a column
make a contact. otherwise, there is no connection between rows and columns. So this is the
logic we are going to use.

The status of each keys can be determined by a process called Scanning. The column pins (Col1
- Col4) are connected to the input pins and all the row pins are connected to the output pins of
the microcontroller.

Now we can read the status of each switch through scanning.

A logic LOW is given to Row1 and others (Row2 - Row-4) HIGH. Now each Column is
scanned. If any switch belongs to 1st row is pressed corresponding column will pulled down
(logic LOW) and we can detect the pressed key. This process is repeated for all rows.

Hardware

• LPC2148
• LCD

Software

CSE Jan, 2020 47 of 75


Microcontroller And Embedded Systems 18CL48

• Keil uVision5
• Flash Magic Tool

Procedure

1. Set port directions. Input ports P0.16-19, Output ports P0.20-23


2. Configure LCD for 4-bit mode and turn on using the commands 0x28 and 0x0f
3. Scan columns for each row set to LOW, sequentially.
4. keypad[row][col] is pressed if col output is LOW
5. Display the key on LCD

Simulation

1. Create project B6/B6.c for the leagcy device LPC2148 with startup.s
2. Add scatter file for LCD. Gto Options and press Linker. Select scatter scripting file.
3. Open Peripherals->GPIO slow interface

CSE Jan, 2020 48 of 75


Microcontroller And Embedded Systems 18CL48

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

#include <lpc21xx.h>

extern void initLcd(void);


extern void lcds(char s[]);
extern void lcd(int d, int c);
extern void delay(int n);

#define COL1 (1 << 16)


#define COL2 (1 << 17)
#define COL3 (1 << 18)
#define COL4 (1 << 19)

#define ROW1 (1 << 20)


#define ROW2 (1 << 21)
#define ROW3 (1 << 22)
#define ROW4 (1 << 23)

char keypad[] = // keypad[20:23, 16:19]


{
'0', '4', '8', 'C',
'1', '5', '9', 'D',
'2', '6', 'A', 'E',
'3', '7', 'B', 'F'
};

char keypadscan(void) //Keypad Scan


{
int key;

IOSET1 = COL1 | COL2 | COL3 | COL4; //Set the cols to '1'


IOSET1 &= ~(ROW1 | ROW2 | ROW3 | ROW4);

key = 0;
for (int col=16; col <= 19; col++)
{
IOCLR1 = (1 << col); //Set this col to LOW while other cols at '1'

for (int row=20; row <= 23; row++)


{
if(!(IOPIN1 & (1 << row))) //Scan for key press
{
while(!(IOPIN1 & (1 << row)));
return keypad[key];
}
key++;
}
IOSET1 = (1 << col); //Set this col to HIGH again, before trying next column

CSE Jan, 2020 49 of 75


Microcontroller And Embedded Systems 18CL48

}
return('X');
}

void initKeypad()
{
IO1DIR = COL1 | COL2 | COL3 | COL4; // Output columns
}

int main(void)
{
char c;
char s[2] = {'x', '\0'};

initKeypad();
initLcd();
lcds("Keypad...");

while(1)
{
c = keypadscan(); //Obtaining values from keypad
if (c == 'X')
continue; s[0] = c;
lcds(s);
}
}

CSE Jan, 2020 50 of 75


Microcontroller And Embedded Systems 18CL48

15 B7 EINT

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

Design

Interrupt caused by an external source such as external switch, sensor or monitoring device.
These interrupt are special events that require immediate attention.

When an IRQ arrives to the CPU, it stops executing the current code and start executing the
ISR. After the ISR execution has finished the CPU gets back to where it had stopped.
Interrupts in LPC214x are handled by Vectored Interrupt Controller (VIC).

VIC has 16 VIRQ slots, Slot-0 to Slot-15. Any IRQ configured interrupts can be assigned to any
slot. Priorities are in the order of slot number. Slot-0 has highest priority than slot-15. VIC
registers are:

Hardware

1. LPC2148

CSE Jan, 2020 51 of 75


Microcontroller And Embedded Systems 18CL48

2. Switch
3. LED

Software

• Keil uVision5

• Flash Magic Tool

Procedure

1. Configure Pin Function P0.15 as EINT2, received from switch


2. Configure P1.16 as GPIO connected to LED
3. Write the ISR(), don’t forget to clear EINT2 flag at the end
4. Select the interrupt, slot and enable it for EINT2
5. Choose Signal mode and Polarity
6. Clear EINT Flag

Simulation

1. Create project B7/B7.c for the leagcy device LPC2148 with startup.s
2. Open Peripherals->GPIO slow interface
3. Set break point in ISR
4. Set P0.15 to trigger the interrupt

CSE Jan, 2020 52 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 53 of 75


Microcontroller And Embedded Systems 18CL48

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


#include <LPC214x.h>
#include <stdio.h>

#define EINT2 (0b10 << 30) //Configure P0.15 to receive EINT2 (0b10)
#define IRQ 16 //Interrupt vector number
#define ENVEC (1 << 5) //Enabale interrupt vector
#define MODE 0b100 // Edge sensitive EINT2 (EINT2.1.0)
#define DI 0b100 // EINT2

void delay(int n)
{
for(int i=0; i < n; i++)
{
for(int j=0; j < 6000; j++); /* At 60Mhz, the loop delays by 1ms */
}
}

void togleLed(int n)
{
for (int i=0; i < n; i++)
{
IO1SET = 0x00010000; // Turn ON
delay(100);
IO1CLR = 0x00010000; // Turn OFF
delay(100);
}
}

void initLed()
{
IO1DIR = 1 << 16; // Select P1.16
delay(10);
IO1CLR = 1 << 16; // Clr P1.16
}

void isr() irq


{
togleLed(10);
EXTINT |= DI; // Clear interrupt flag by writing 0b100 i.e EINT2
VICVectAddr=0; // Finished ISR, dummy write
}

void initIsr()
{
PINSEL0 = EINT2; //0x80000000; warnings 16 << 30

EXTMODE = MODE; // Edge sensitive mode on EINT2


EXTPOLAR = 0; // Falling edge sensitive

CSE Jan, 2020 54 of 75


Microcontroller And Embedded Systems 18CL48

VICIntSelect = 0 << IRQ; // default is 0 anyway


VICVectAddr0 = (unsigned long)isr; //Address of isr()
VICVectCntl0 = ENVEC | IRQ; //Assign INTVEC to slot0 and enable it
VICIntEnable = 1 << IRQ; // Enable ENT2 IRQ
}

int main()
{
initIsr();
initLed();

while(1)
{
}
}

Before you start building the program in KEIL goto the Target options and in the
Linker tab check: "Use Memory Layout from the Target Dialog".

CSE Jan, 2020 55 of 75


Microcontroller And Embedded Systems 18CL48

16 B8 7SD

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

Design

7 segment displays are among the simplest display units to display the numbers and
characters. It is generally used to display numbers and has brighter illumination and simpler
construction than dot matrix display. And because of brighter illumination, the output can be
viewed from larger distance than LCD.

It consists of 8 LEDs, each LED used to illuminate one segment of unit and the 8th LED used
to illuminate DOT in 7 segment display. 8th LED is used when two or more 7-segment
modules are used, for example to display (0.1).

The 7-segment display has connections for 8 LEDs. We set logic HIGH to illuminate the
partic- ular and set LOW to turn OFF LED.

CSE Jan, 2020 56 of 75


Microcontroller And Embedded Systems 18CL48

Below table shows the HEX values and corresponding digit according to LPC2148 pins for com-
mon cathode configuration.

8 7 6 5 4 3 2 (P0.8:P0.2);
x=a b c d e f g
0 0 0 0 0 0 0 1
1 1 0 0 1 1 1 1
2 0 0 1 0 0 1 0
3 0 0 0 0 1 1 0 1 = off
4 1 0 0 1 1 0 0
5 0 1 0 0 1 0 0 0 = on
6 0 1 0 0 0 0 0
7 0 0 0 1 1 1 1
8 0 0 0 0 0 0 0
9 0 0 0 0 1 0 0
A 0 0 0 1 0 0 0
B 1 1 0 0 0 0 0
C 0 1 1 0 0 0 1
D 1 0 0 0 0 1 0
E 0 1 1 0 0 0 0
F 0 1 1 1 0 0 0
hex value = 0b11 « 9 | x « 2

Hardware

1. LPC2148
2. SSD

Software

CSE Jan, 2020 57 of 75


Microcontroller And Embedded Systems 18CL48

• Keil uVision5
• Flash Magic Tool

Procedure

1. Generate HEX digits 0-F

2. Set port pins to the digit pattern


3. Dely 1s
4. Clear port pins

Simulation

1. Create project B8/B8.c for the leagcy device LPC2148 with startup.s
2. Open Peripherals->GPIO slow interface

CSE Jan, 2020 58 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 59 of 75


Microcontroller And Embedded Systems 18CL48

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


//appropriate delay in between.

#include <lpc214x.h>

void delay(int n)
{
for(int i=0; i < n; i++)
{
for(int j=0; j < 6000; j++); /* At 60Mhz, the loop delays by 1ms */
}
}

// using (P0.2, P0.3, P0.4, P0.5, P0.6, P0.7, P0.8, and P0.9, P).10)
int a[]=
{
/* 1 = off 0 = on */
/* 8765432 ports */
/*x abcdefg display*/
/*0*/ 0b01 << 9 | 0b0000001 << 2,
/*1*/ 0b01 << 9 | 0b1001111 << 2,
/*2*/ 0b01 << 9 | 0b0010010 << 2,
/*3*/ 0b01 << 9 | 0b0000110 << 2,
/*4*/ 0b01 << 9 | 0b1001100 << 2,
/*5*/ 0b01 << 9 | 0b0100100 << 2,
/*6*/ 0b01 << 9 | 0b0100000 << 2,
/*7*/ 0b01 << 9 | 0b0001111 << 2,
/*8*/ 0b01 << 9 | 0b0000000 << 2,
/*9*/ 0b01 << 9 | 0b0000100 << 2,
/*A*/ 0b01 << 9 | 0b0001000 << 2,
/*B*/ 0b01 << 9 | 0b1100000 << 2,
/*C*/ 0b01 << 9 | 0b0110001 << 2,
/*D*/ 0b01 << 9 | 0b1000010 << 2,
/*E*/ 0b01 << 9 | 0b0110000 << 2,
/*F*/ 0b01 << 9 | 0b0111000 << 2
};

int main()
{
IO0DIR = 0b01 << 9 | 1<<8 | 1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<3 | 1<<2;

while(1)
{
for(int i=0; i < 16; i++)
{
IO0CLR = 0x00000FFF;
IO0SET = a[i];
delay(1000);
}

CSE Jan, 2020 60 of 75


Microcontroller And Embedded Systems 18CL48

}
}

CSE Jan, 2020 61 of 75


Microcontroller And Embedded Systems 18CL48

17 BU LCD

LCD display is common to ADC and Keypad experiments. The code is included into these
projects.

P0.13 DB7 Data bit7


P0.12 DB6 :
P0.11 DB5
P0.10 DB4
P0.09 DB3
DB2 P0.08
P0.07 DB1 :
P0.06 DB0 Data bit0
P0.28 E EN Enable. Falling edge trigger
P0.29 R/W RW Read Write mode; 0-write to LCD, 1-read from LCD
P0.22 RS RS Register select; 0-command register, 1-data register

LCD commands

No. Hex Command to LCD instruction Register


1 01 Clear display screen
11 0F Display on, cursor blinking
16 80 Force cursor to beginning ( 1st line)
16 86 Force cursor to 6th position of first row
17 C0 Force cursor to beginning ( 2nd line)
19 38 2 lines and 57 matrix (8-bit mode)

Hardware

1. LPC2148
2. LCD

CSE Jan, 2020 62 of 75


Microcontroller And Embedded Systems 18CL48

Software

• Keil uVision5

• Flash Magic Tool

Procedure

1. Initialize LCD for 4-bit mode, display on, cursor to home and blinking
2. Write IOPIN0 (c | 0xFFFFFF0F) « 6; IOCLR0=RS if c is a command
3. Delay 100ms before characters

Simulation

1. Create project BU/BU.c for the leagcy device LPC2148 with startup.s

2. Include main code for simulation

CSE Jan, 2020 63 of 75


Microcontroller And Embedded Systems 18CL48

CSE Jan, 2020 64 of 75


Microcontroller And Embedded Systems 18CL48

// LCD
#include <lpc214x.h>
#define W4 10 //13:10
#define W8 6 //13:6
#define RS (1 << 22) //P0.22 -> Register Select. 0:Command, 1: Data
#define RW (1 << 29) //P0.29 -> Read/Write, R/W=0: Write & R/W=1: Read
#define EN (1 << 28) //Enable. Falling edge trigger
#define DATA (0xff << 6) //Data P0.06:13 -> DB0:DB7

void delay(int n)
{
for(int i=0; i < n; i++)
{
for(int j=0; j < 6000; j++); /* At 60Mhz, the loop delays by 1ms */
}
}

void wd(int x)
{
IOCLR0 = RW;
if (x==0) IOCLR0 = RS;
IOSET0 = EN;
delay(100);
IOCLR0 =
EN;
}

void lcd(char d, int x)


{
int n;

n = d | 0xFFFFFF0F;
IOPIN0 = n<<6;
wd(x);

d = d << 4;
n = d | 0xFFFFFF0F;^^I
IOPIN0 = n<<6;
wd(x);

void lcds(char s[])


{
lcd(0x01,0); // clear the display before printing
for (int i=0; s[i]; i++)
{
lcd(s[i],1);
}
}

CSE Jan, 2020 65 of 75


Microcontroller And Embedded Systems 18CL48

void initLcd(void)
{
IODIR0 = EN | RW | RS | DATA ; // output ports for control

lcd(0x28,0); // 4-bit mode


lcd(0x0F,0); // Display on, cursor blinking
lcd(0x80,0); // Cursor to begining
lcd(0x01,0); // Clear display
}

int main()
{
int n = 0;
char s[10];

initLcd();
lcds("Hello...");
delay(1000);
while(1)
{
sprintf(s, "%d", n);
n = n%10;
lcds(s);
delay(10);
n++;
}
}

CSE Jan, 2020 66 of 75


Microcontroller And Embedded Systems 18CL48

18 LPC2148

18.1 ARM IDE

]]

CSE Jan, 2020 67 of 75


Microcontroller And Embedded Systems 18CL48

18.2 ARM development board

CSE Jan, 2020 68 of 75


Microcontroller And Embedded Systems 18CL48

18.3 ARM Microcontroller

CSE Jan, 2020 69 of 75


Microcontroller And Embedded Systems 18CL48

18.4 GPIO

Port number ’n’ cooresponding to,

• PINSEL0[2n+1:2n] if n <= 15
• PINSEL1[2n-32+1:2n-32] if n > 15

Lab Port DIR PINSEL0 00 01 10 11


UART P0.0 -> 1:0 TX
P0.1 3:2 RX
DC P1.30 ->
P1.31 ->
P1.23 <-
SM P0.16 ->
P0.17 ->
P0.18 ->
P0.19 ->
ADC P0.30 29:28 AD0.3
DAC P0.25 19:18 AOUT
KPAD P0.20 ->
P0.21 ->
P0.22 ->
P0.23 ->
P0.16 <-
P0.17 <-
P0.18 <-
P0.19 <-
EINT P0.15 31:30 EIN2
P1.16} ->
SSD P0.2 ->
P0.3 ->
P0.4 ->
P0.5 ->
P0.6 ->
P0.7 ->
P0.8 ->
P0.9 ->
LCD P0.22 ->
P0.28 ->
P0.29 ->
P0.6 ->
:
P0.13 ->
SW p0.23 <-

CSE Jan, 2020 70 of 75


Microcontroller And Embedded Systems 18CL48

18.5 PINSEL

1. PINSEL0

• Configure ports P0.0 to P0.30


• Bits 2n+1:2n, where n is the port number

2. PINSEL1

• Configure ports beyond P0.30


• Bits 2n+1-32:2n-32, where n is the port number

3. GPIO registers

• IOxPIN read the logic value on a I/O pin


• IOxSET set the logic value on a I/O pin to HIGH
• IOxCLR set the logic value on a I/O pin to LOW
• IOxDIR select the input/output function (0 is output)

CSE Jan, 2020 71 of 75


Microcontroller And Embedded Systems 18CL48

18.6 UART Registers

1. U0DLM, U0DLM
Divisor latch registers DLM and DLL are the upper and lower bytes of the word register.

• U0DLM = 0
• U0DLL = pclk
16∗baudrate

2. U0LCR Line control register

|------+-----+--------+-----------+-------------|
| 7 | 6:4 | 3 | 2 | 1:0 |
|------+-----+--------+-----------+-------------|
| DLAB | | Parity | Stop bits | Word length |

U0LCR(DLAB=1«7, 1 stop bit=0, no parity=0, 8bit WL=0b11)

3. U0LSR Line status register

|-----+------+-----+----|
| 7:6 | 5 | 4:1 | 0 |
|-----+------+-----+----|
| | THRE | | DR |

U0LSR(1«5)
4. U0THR Transmit Holding Register
U0THR = data

CSE Jan, 2020 72 of 75


Microcontroller And Embedded Systems 18CL48

18.7 ADC

1. AD0CR A/D Control register

|----------+------+-------+----------+-----+----------+-------+-------+---------+-----|
| 31:28 | 27 | 26:24 | 23:22 | 21 | 20 | 19:17 | 16 | 15:8 | 7:0 |
|----------+------+-------+----------+-----+----------+-------+-------+---------+-----|
| Reserved | EDGE | START | Reserved | PDN | Reserved | CLKS | BURST | CLCKDIV | SEL |

• SEL = 100, select 3rd channerl


• CLKDIV = 3MHz
• PDN = 1, power down mode 1 is operations
• BURST = 1, ADC does repeated conversions at the rate selected by the CLKS field
for the analog inputs selected by SEL field
• START = 000 start conversion

2. AD0DR3

|------+---------+-------+----------+--------+----------|
| 31 | 30 | 26:24 | 23:16 | 15:6 | 5:0 |
|------+---------+-------+----------+--------+----------|
| DONE | OVERRUN | CHN | Reserved | RESULT | Reserved |

• DONE = 1, data is available


• CHN = 000, ADC channel (should be 011?)
• RESULT is ADC data

CSE Jan, 2020 73 of 75


Microcontroller And Embedded Systems 18CL48

18.8 DAC

1. DACR D/A control register

|----------+------+-------+----------|
| 31:17 | 16 | 15:6 | 5:0 |
|----------+------+-------+----------|
| Reserved | Bias | Value | Reserved |

• BIAS=1, setling time is 2.5 µS


• Value = DA value

CSE Jan, 2020 74 of 75


Microcontroller And Embedded Systems 18CL48

18.9 EINT

1. VICIntSelect

|--------+----+----+-----+-------+----+-----+---|
| Bit | 31 | 30 | ... | 16 | 15 | ... | 0 |
|--------+----+----+-----+-------+----+-----+---|
| Symbol | | | | EINT2 | | | |

• EINT2=0 IRQ (default)

2. VICIntEnable

|--------+----+----+-----+-------+----+-----+---|
| Bit | 31 | 30 | ... | 16 | 15 | ... | 0 |
|--------+----+----+-----+-------+----+-----+---|
| Symbol | | | | EINT2 | | | |

• EINT2=1, Enable interrupt


• eg. EINT2 has 1 « 16

3. VICVectCntl0-15
Vector Control registers 0-15. Each of these registers controls one of the 16 vectored IRQ
slots. Slot 0 has the highest priority and slot 15 the lowest.

|----------+------------------+------------|
| 31:6 | 5 | 4:0 |
|----------+------------------+------------|
| Reserved | IRQ slot enabled | IRQ number |

• IRQ slot enabled = 1 e.g 1«5


• IRQ number=16

4. VICVectAddrx

|--------------------|
| 31:0 |
|--------------------|
| IRQ vector address |

• IRQ vector address = isr()

5. EXTINT Register

|-----+-------+-------+-------+-------|
| 8:4 | 3 | 2 | 1 | 0 |
|-----+-------+-------+-------+-------|
| | EINT3 | EINT2 | EINT1 | EINT0 |

CSE Jan, 2020 75 of 75


Microcontroller And Embedded Systems 18CL48

Clear EINTx after the ISR.

• EINT1=1, clear EINT1 e.g EXTINT=2

6. EXTMODE External Interrupt Mode register

|----------+------------|
| 7:4 | 3:0 |
|----------+------------|
| Reserved | EXTMODE0-3 |

• EXTMODE1 = 0 e.g EXTMODE = 4 for edge sensitive

7. EXTPOLAR External Interrupt Polarity register

|----------+-------------|
| 7:4 | 3:0 |
|----------+-------------|
| Reserved | EXTPOLAR0-3 |

• EINT1 = 0 e.g EXTPOLAR=0 falling edge sensitive

CSE Jan, 2020 76 of 75


Microcontroller And Embedded Systems 18CL48

18.10 LCD commands

No. Hex Command to LCD instruction Register


1 01 Clear display screen
2 02 Return home
3 04 Decrement cursor (shift cursor to left)
4 06 Increment cursor (shift cursor to right)
5 05 Shift display right
6 07 Shift display left
7 08 Display off, cursor off
8 0A Display off, cursor on
9 0C Display on, cursor off
10 0E Display on, cursor blinking
11 0F Display on, cursor blinking
12 10 Shift cursor position to left
13 14 Shift cursor position to right
14 18 Shift the entire display to the left
15 1C Shift the entire display to the right
16 80 Force cursor to beginning ( 1st line)
17 C0 Force cursor to beginning ( 2nd line)
18 28 2 lines and 57 matrix (4-bit mode)
19 38 2 lines and 57 matrix (8-bit mode)

CSE Jan, 2020 77 of 75

You might also like