MES Lab Manual
MES Lab Manual
18CL48
1
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
1 A1 Multiply
Objective
Design
ARM Instructions
Procedure
1. Create project A1/A1.s for the leagcy device LPC2148 w/o startup.s
2. Translate A1.s and build
4 3 2 1 1 2 3 4
0x00 00 01 00 -> 00 01 00 00
; A1.s
; Multiplication of 16bit data
START
STOP B STOP
END
2 A2 Sum
Objectives
numbers Design
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
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
STOP B STOP
END
3 A3 Factorial
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
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
; A3.s
; factorial n
START
MOV R0, #1 ; fact = 1
MOV R1, #5 ; n
LOOP
STOP B STOP
END
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
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
; A4.s
; Find sum of numbers in array
STOP B STOP
END
5 A5 Table lookup
Write a program to find the square of a number (1 to 10) using look-up table.
Objective
a[n] Design
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]
; A4.s
; Find square of a number (1-10) using look up table
DONE
STOP B STOP
END
6 A6 Min/Max
Objective
Design
ARM Instructions
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
; A6.s
; Find smallest/larget of the number in an array of 32 numbers
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
STOP B STOP
END
7 A7 Sort
order. Objective
1. Sort numbers
Design
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
; 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
END
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
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
; A8.s
; Count 0s and 1 in two consecutive locations
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
END
9 B1 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
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.
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
Procedure
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
// PINSEL
// LCR
// LSR
void init()
{
PINSEL0 = TX | RX;
U0LCR = DLAB |
WL;
U0DLM = 0x00;
U0DLL = 15000000 / (16*9600);
U0LCR = WL; // Clear DLAB
}
int main()
{
char msg[] = "Hello World\r\n";
init();
for (int i=0; msg[i]; i++)
{
tx(msg[i]);
}
}
10 B2 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
• IN1 is High
• IN2 is LOW
• IN1 is LOW
• IN2 is HIGH
Simulation
1. Create project B2/B2.c for the leagcy device LPC2148 with startup.s
2. Open Peripherals->GPIO slow interface->Port0
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
11 B3 Stepper Motor
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:
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.
Hardware
• LPC2148
• ULN2803
Software
• Keil uVision5
Procedure
• 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
#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);
}
}
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.
ADGDR
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)
Software
• Keil uVision5
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
//4. Determine Digital output for a given Analog input using Internal ADC of ARM controller.
#include <lpc214x.h>
#include <stdio.h>
#include <string.h>
//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 |
// AD0DR3
// | 31 | 30 | 26:24 | 23:16 | 15:6 | 5:0 |
// | DONE | OVERRUN | CHN | Reserved | RESULT | Reserved |
void initAdc()
{
PINSEL1 = ADC3;
AD0CR = PDN | CLKDIV | CHNSEL | BURST | START; //0x00210308
}
int readAdc()
{
AD0CR |= START;
while (!(AD0DR3 & DONE)); /* Wait till conversion DONE */
int main(void)
{
int rv;
float voltage;
char s[10];
initAdc();
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);
}
}
13 B5 DAC
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
Hardware
• LPC2148
• CRO
Software
• Keil uVision5
• Procedure
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
//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 |
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;
delay(1);
d = 0;
DACR = VALUE(d) & 0x0001FFC0;
delay(1);
}
int main()
{
PINSEL1 = AOUT;
while(1)
{
for (int i=0; i <5; i++)
//triangle();
14 B6 Keypad
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.
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
• Keil uVision5
• Flash Magic Tool
Procedure
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
//6. Interface a 4x4 keyboard and display the key code on an LCD.
#include <lpc21xx.h>
key = 0;
for (int col=16; col <= 19; col++)
{
IOCLR1 = (1 << col); //Set this col to LOW while other cols at '1'
}
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);
}
}
15 B7 EINT
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
2. Switch
3. LED
Software
• Keil uVision5
Procedure
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
#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 initIsr()
{
PINSEL0 = EINT2; //0x80000000; warnings 16 << 30
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".
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.
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
• Keil uVision5
• Flash Magic Tool
Procedure
Simulation
1. Create project B8/B8.c for the leagcy device LPC2148 with startup.s
2. Open Peripherals->GPIO slow interface
#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);
}
}
}
17 BU LCD
LCD display is common to ADC and Keypad experiments. The code is included into these
projects.
LCD commands
Hardware
1. LPC2148
2. LCD
Software
• Keil uVision5
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
// 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;
}
n = d | 0xFFFFFF0F;
IOPIN0 = n<<6;
wd(x);
d = d << 4;
n = d | 0xFFFFFF0F;^^I
IOPIN0 = n<<6;
wd(x);
void initLcd(void)
{
IODIR0 = EN | RW | RS | DATA ; // output ports for control
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++;
}
}
18 LPC2148
]]
18.4 GPIO
• PINSEL0[2n+1:2n] if n <= 15
• PINSEL1[2n-32+1:2n-32] if n > 15
18.5 PINSEL
1. PINSEL0
2. PINSEL1
3. GPIO 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
|------+-----+--------+-----------+-------------|
| 7 | 6:4 | 3 | 2 | 1:0 |
|------+-----+--------+-----------+-------------|
| DLAB | | Parity | Stop bits | Word length |
|-----+------+-----+----|
| 7:6 | 5 | 4:1 | 0 |
|-----+------+-----+----|
| | THRE | | DR |
U0LSR(1«5)
4. U0THR Transmit Holding Register
U0THR = data
18.7 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 |
2. AD0DR3
|------+---------+-------+----------+--------+----------|
| 31 | 30 | 26:24 | 23:16 | 15:6 | 5:0 |
|------+---------+-------+----------+--------+----------|
| DONE | OVERRUN | CHN | Reserved | RESULT | Reserved |
18.8 DAC
|----------+------+-------+----------|
| 31:17 | 16 | 15:6 | 5:0 |
|----------+------+-------+----------|
| Reserved | Bias | Value | Reserved |
18.9 EINT
1. VICIntSelect
|--------+----+----+-----+-------+----+-----+---|
| Bit | 31 | 30 | ... | 16 | 15 | ... | 0 |
|--------+----+----+-----+-------+----+-----+---|
| Symbol | | | | EINT2 | | | |
2. VICIntEnable
|--------+----+----+-----+-------+----+-----+---|
| Bit | 31 | 30 | ... | 16 | 15 | ... | 0 |
|--------+----+----+-----+-------+----+-----+---|
| Symbol | | | | EINT2 | | | |
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 |
4. VICVectAddrx
|--------------------|
| 31:0 |
|--------------------|
| IRQ vector address |
5. EXTINT Register
|-----+-------+-------+-------+-------|
| 8:4 | 3 | 2 | 1 | 0 |
|-----+-------+-------+-------+-------|
| | EINT3 | EINT2 | EINT1 | EINT0 |
|----------+------------|
| 7:4 | 3:0 |
|----------+------------|
| Reserved | EXTMODE0-3 |
|----------+-------------|
| 7:4 | 3:0 |
|----------+-------------|
| Reserved | EXTPOLAR0-3 |