IC 045 Embedded Sys Design PPT 5
IC 045 Embedded Sys Design PPT 5
IC 045 Embedded Sys Design PPT 5
LPC2148 Microcontroller
Dr. N. Mathivanan
Visiting Professor,
Department of Instrumentation & Control Engineering
National Institute of Technology
Trichy
LPC2148
• Provided by NXP Semiconductor Ltd.
• Based on ARM v4T architecture version,
• Based on ARM7-TDMI processor
• Integrated Real-Time Emulation and Embedded Trace support.
• Topics discussed:
o Specification list
o LPC2148 – Internal architecture diagram
o Pin diagram, Pin connect block,
o Memory map
o GPIO, PLL
o Interrupt structure, Vectored Interrupt Controller (VIC)
o Peripherals: ADC, DAC, Timer, PWM,
o Serial Communication Interfaces:
Dr. N. Mathivanan
• LPC2148 - Specifications
Architecture : ARM v4T
Processor : ARM7-TDMI-S
Processor
Instructions : 32-bit ARM and 16-bit Thumb
Debug support : RT EmbeddedICE, Embedded Trace interface
Static RAM Size : 32 KB
(On-chip) Additional : 8 KB for USB DMA
Interrupts : Four
External
No. of pins : Nine
Interrupts
Sensitivity : Rising/falling edge or low/high level sensitive
Dr. N. Mathivanan
Pins & Signals
LQFP64 package,
64 (physical) pins,
Dr. N. Mathivanan
Pin Connect Block
• 64 pins are attached to two 32-bit I/O ports, Port-0 & Port-1.
• Port-0, Port-1 pins are designated as P0.0 – P0.31 & P1.0 - P1.31.
• Pins P0.24, P0.26, P0.27, P1.0-P1.15 are unavailable.
• Pin functions are multiplexed, up to 4 functions assigned to each pin.
o Port-0 pins multiplex peripheral pin, & comm. interface pin functions
o Port-1 pins multiplex JTAG interface, Trace function
o Advantages: keeps size small, adds more functionalities to devices
o Disadvantages: if functions not carefully selected, some can’t be availed
• Questions:
How are LPC2148 pins configured for the following?
(i) PINSEL0 = 0x0000 0000; (Ans. I/O pins P0.0 to P0.15 are configured for GPIO function)
(ii) (a) PINSEL0 = 0x0001 0000; (b) PINSEL0 = 0x0002 0000; (c) PINSEL0 = 0x0003 0000;
(iii) PINSEL1 &= ~(0x03); PINSEL1 |= (0x03);
Dr. N. Mathivanan
Memory Map
Dr. N. Mathivanan
No. Peripheral Base Address No. Peripheral Base Address
Watchdog
0 0xE000 0000 12 Not used 0xE003 0000
Timer
1 Timer0 0xE000 4000 13 ADC0 0xE003 4000
0xE003 8000 –
2 Timer1 0xE000 8000 14 – 22 Not used
0xE005 8000
3 UART0 0xE000 C000 23 I2C1 0xE005 C000
4 UART1 0xE001 0000 24 ADC1 0xE006 0000
5 PWM 0xE001 4000 25 Not used 0xE006 4000
6 Not used 0xE001 8000 26 SSP 0xE006 8000
7 I2C0 0xE001 C000 27 DAC 0xE006 C000
0xE007 0000 –
8 SPI0 0xE002 0000 28 – 35 Not used
0xE008 C000
9 RTC 0xE002 4000 36 USB 0xE009 0000
0xE009 4000 –
10 GPIO 0xE002 8000 37 – 126 Not used
0xE01F 8000
• GPIO registers
• IOxPIN – To get logic value on a I/O pin
• IOxSET – To set an output configured pin (by writing 1 in corresponding bit)
• IOxCLR - To reset an output configured pin (by writing 1 in corresponding bit)
• IOxDIR – To select input /output function (by placing 0/1) for an I/O pin
(x = 0/1, i.e. Port-0 or Port-1)
Dr. N. Mathivanan
• Examples: Configuring & initializing GPIO pins
(Demo: ‘C’ Program to illustrate configuring, setting / resetting I/O pins using
Keil mVision - Debugger/Simulator)
Dr. N. Mathivanan
Phase Locked Loop (PLL)
• LPC214x has two PLLs: PLL0 and PLL1
• PLL1 is used for USB.
• PLL0 is used to generate clock for CPU (CCLK) & peripherals (PCLK)
• Uses crystal oscillator input frequency (FOSC) 10–25 MHz and
generates output up to 60 MHz
• Parameters m & p determine output frequency, CCLK = m x FOSC
• In the feedback path of PLL there is current controlled osc (CCO)
whose frequency should be kept within 156 – 320 MHz.
Computing m and p values for generating
CCLK of 60 MHz from FOSC 12 MHz
m = CCLK/FOSC = 60/12 = 5
If p is chosen to be 2, CCO (= 2 x p x 60) is within
156 – 320 MHz.
Dr. N. Mathivanan
• PLL Registers
PLLxCON – Controls PLL activation; used to enable and connect PLL
PLLxCFG – Multiplier value (bits [4:0]) and divider value (bits [6:5] are configured
PLLxSTAT – Shows current enable & connect status, m & p values, lock status
PLLxFEED – Writing feed sequence, i.e. ‘0xAA’ and ‘0x55’ in sequence, to this register
– It enables changes made to PLLCON, PLLCFG registers to take effect
APBDIV – Sets PCLK freq to 1/4, same or 1/2 of CCLK (by writing 0x00, 0x01, 0x02),
Dr. N. Mathivanan
Checking PLL0 lock status & waiting till PLL0 locks onto the target
frequency:
PLOCK, the bit [10] of PLL0STAT register, is polled and checked for lock status.
PLOCK = 0x40;
while(!(PLLSTAT & PLOCK)); // poll lock status & wait till PLL0 locks
Value 0x00, 0x01 or 0x02 in APBDIV register sets the PCLK frequency to one-
fourth, same as or half of CCLK frequency.
Dr. N. Mathivanan
Interrupt Structure
• ARM7 Processor hardware interrupt inputs: 2, (FIQ. IRQ)
Dr. N. Mathivanan
Vectored Interrupt Controller (VIC)
• 32 interrupt request inputs, LPC2148 uses 22 of 32 interrupts
Interrupt
request logic
NVFIQ logic
Vectored IRQ
blocks
NVIRQ logic
Priority block
VICFIQ,
VICIRQ
External
interrupt inputs
Many 32-bit
registers
Dr. N. Mathivanan
• Bit allocation for sources of interrupts in VIC registers
Dr. N. Mathivanan
• VIC Registers (not all listed)
VICSoftInt: Generates any interrupt by software. High bit generates corresponding interrupt
VICIRQStatus: A high bit indicates corresponding IRQ classified, enabled IRQ interrupt is active
VICFIQStatus: A high bit indicates corresponding FIQ classified, enabled IRQ interrupt is active
VICVectAddr: Holds ISR addr of active interrupt. Writing any value indicates End of Interrupt
Dr. N. Mathivanan
• Handling IRQ interrupts:
o On interrupt, processor executes branch instruction from interrupt vector
table at 0x 00000018 and branches to IRQ handler routine
o Reads VICVectAddr reg that holds address of highest priority pending VIRQ
Slot-x interrupt. If no slot is assigned, it holds address of default vect address
o Branches to handler routine.
o Reads interrupt register of the peripheral, identifies actual source, executes
codes respective of the interrupt.
o Clears interrupt flags set by peripherals in their interrupt registers.
o Writes a dummy word into VICVectAddr register to indicate EoI, to clear
respective interrupt in VIC interrupt priority hardware.
o Returns back to interrupted program, re-enables interrupts.
Dr. N. Mathivanan
Programming VIC registers: Examples
Dr. N. Mathivanan
• Questions:
iv. Program VICIntEnable register to enable TIMER1, ADC0, UART1 and SPI
interrupts.
Dr. N. Mathivanan
External interrupt inputs
• Interrupt inputs: EINT0, EINT1, EINT2 (2 pins to each), EINT3 (3 pins)
• Registers: 4 – Bit allocation Bit[0] – EINT0, bit[1] – EINT1, ………….
EXTINT: Interrupts set flags, writing ‘1’ in respective bits clears interrupts
INTWAKE: Interrupts enabled by this reg wakesup CPU from power down mode
EXTMODE: Sets edge/level sensitivity to interrupt inputs
EXTPOLAR: Sets polarity for edge/level sensitivity
*Port @PinConfiguration
Pin
Block Symbol Description I/O I/O PSR Bits Val
No
No.
P0.1 0 3:2 11 21
EINT0 External interrupt input 0 I
P0.16 1 1:0 01 46
P0.3 0 7:6 11 26
EINT1 External interrupt input 1 I
P0.14 0 29:28 10 41
External
P0.7 0 15:14 11 31
Interrupts EINT2 External interrupt input 2 I
P0.15 0 31:30 10 45
P0.30 1 29:28 10 15
EINT3 External interrupt input 3 I P0.9 0 19:18 11 34
Dr. N. Mathivanan
P0.20 1 9:8 11 55
Programming EINT registers: Examples
2. EXTMODE &= 0 << 3; // set level sensitivity to external interrupt EINT3 pin
Dr. N. Mathivanan
Timer
• No. of Timers : 2, (Timer0 and Timer1)
• Applications
• PWM, data acquisition, measurements of freq, speed, velocity, position
Dr. N. Mathivanan
• Timer Operation:
o Uses PCLK clock
o Prescale Counter (PC) increments on each PCLK clock input pulse
o Prescale Register (PR) holds 32-bit value
o When PC = PR, increments Timer/Counter (TC) and resets PC
o Hence, TC counting rate is controlled by PR
o Each timer has 4 capture channels, 4 capture inputs (CAPx.0-CAPx.3)
A transition on capture input, copies TC value into Capture Reg (CR)
Can optionally generate interrupt also
CCR configures capturing event (rising, falling or both edges)
o Each timer has 4 match channels, 4 match outputs (MATx.0-MATx.3)
o For each match channel, there is a MR that holds 32-bit value
o When TC = MR, an event (reset or stop TC, generate int) is triggered
o MCR configures type of event to generate
Dr. N. Mathivanan
• Timer Registers
Timer Counter (TC): Incremented when PC = PR
Timer Control Register (TCR): Enables / disables, resets TC
Prescale Register (PR): Holds max value for PC to count up to. Controls counting rate
Prescale Counter (PC): Increments on every PCLK clock
Capture Registers 0 – 3: Transition on cap inputs copy TC into respective reg
Capture Control Register (CCR): Prescribes active event (rising/falling edge), int
Match Registers 0 – 3 (MR): Holds a value, matched with TC, matching triggers event
Match Control Register (MCR): Prescribes action (enabling/ disabling interrupt
generation, resetting/disabling TC, or stopping TC and PC) to be triggered on match
Count Control Register (CTCR): Selects Timer or Counter mode;
Interrupt Register (IR): Has 4 bits for match interrupt, 4 bits for capture interrupt.
When interrupt occurs corresponding bit is set. Writing high in a bit resets
corresponding interrupt
Dr. N. Mathivanan
• Programming Timer Registers:
The registers for a typical application are configured in the following order
Dr. N. Mathivanan
PWM
• Reduces power delivered to load without power loss
• Extensively used in industrial control (e.g. DC motor control), volume
control of audio amplifiers, brightness control, power control.
• It is special type of Timer
• PWM signals – pulses with variable widths,
• Two parameters – period of pulse and duty cycle
• Examples of 10 kHz PWM signal with 25%, 50%, 75% duty cycles
Dr. N. Mathivanan
• PWM peripheral in LPC2148
o Has a Timer Counter, Prescale register and 7 match registers
o 6 PWM outputs and 6 corresponding match registers (MR1-MR6)
o MR0 is used by all PWM channels, used to control period of PWM cycle
o MR1-MR6 hold values that control pulse width (ON period)
o PWM outputs are set HIGH when a PWM cycle begins.
o When TC matches with a MRx corresponding PWM output goes LOW
and remains low till a next PWM cycle begins.
Dr. N. Mathivanan
• PWM registers:
PWMPC: Prescale Counter. Counts on every PCLK clock pulse. When PC = PR, PC is
reset and TC increments.
PWMTCR: Timer Control Register. Controls Timer Bit[0] enables/disables (1/0) TC,
bit[1] resets both PC and TC.
Dr. N. Mathivanan
PWMPCR: Selects PWM channel type as single or double edge controlled
Dr. N. Mathivanan
• Functional Block Diagram
Dr. N. Mathivanan
ADC
• ADC0 (6 Ch), ADC1 (8 Ch)
Dr. N. Mathivanan
• Pin configuration
Dr. N. Mathivanan
• Block diagram
Dr. N. Mathivanan
• ADC Registers
o Control Register:
Selects channel, clock freq, resolution, conversion mode, method of
issue of SoC, edge for conversion
o Global Start Register:
Used to initiate simultaneous conversion on both ADCs
o Status Register:
Allows simultaneous checking of status of all A/D channels,
Contains done, overrun, interrupt flags
o Data Registers (DR0 – DR7):
Contains most recent converted data and EoC status on respected ch,
o Global Data Register:
Contains done bit, most converted data, ch number
o Interrupt Enable Register:
Enables interrupt on Eoc by
Dr. N.each channel
Mathivanan
• Programming ADC registers – Examples (Construction of control words)
Select ADC-0, Channel-1, Clock frequency 4 MHz (let PCLK is 60 MHz), Software
controlled mode and 10-bit resolution. Power-up ADC and issue start of conversion.
Dr. N. Mathivanan
Serial Communication Interfaces
UART
I2C
SPI
USB
Dr. N. Mathivanan
Review Questions
1. Illustrate pin function selection in LPC2148 with an example
2. How are the LPC2148 pins configured for “PINSEL0=0x00000000;”?
3. Write down the GPIO registers and their function.
4. Write a ‘C’ program fragment to select GPIO function for I/O pin P0.0,
configure the pin as output port and set the pin to HIGH.
5. Write down the range of memory space allocated for APB & AHP peripherals
6. Describe the PLL configuration sequence.
7. How do you program PLL0CFG register for generating 60 MHz CCLK from 12
MHz FOSC?
8. How do you issue PLL feed sequence?
9. Discuss in detail the interrupt processing by LPC2148 with ARM PrimeCell
Vectored Interrupt Controller.
10. Describe the operation of ARM Primecell VIC.
11. List various VIC registers and outline their functions.
Dr. N. Mathivanan
12. Program VICVectCntl0 register to assign ADC0 interrupt (Channel -18) to
VIRQ Slot-0.
13. Illustrate programming of VIC registers in a sequence for a typical interrupt
application.
14. Discuss External Interrupt Inputs control block and processing of external
interrupts in LPC2148.
15. What are the applications for the Timer peripheral?
16. Draw the functional block diagram of a Timer peripheral in LPC214x
microcontroller and explain the functionalities supported by the Timer.
17. List various registers of a Timer peripheral and mention their functions.
18. Explain the sequence of programming of Timer registers for typical
applications.
19. Write a C program fragment for programming PR of Timer0 for incrementing
the TC on every one ms and for initializing MR0 and MCR for generating
interrupt at regular interval of 1 s. (Assume PCLK is 30 MHz. )
Dr. N. Mathivanan
20. Write down typical applications of PWM peripheral.
21. List registers of PWM peripheral and mention about their functions.
22. Illustrate with suitable example, the programming sequence of PWM
registers.
23. Draw the functional block diagram of a ADC peripheral in LPC2148.
24. Write down the specifications of ADC peripherals in LPC2148.
25. List various ADC registers and mention their functions.
26. Initiate software controlled conversion of analog signal applied to ADC-0
Channel–1 and monitor end of conversion. On completion, read converted
data. Set ADC clock frequency to 4 MHz with 24 MHz PCLK.
27. Write a C program fragment to generate a triangular waveform on Aout pin
using on-chip DAC peripheral.
Dr. N. Mathivanan
Textbooks & Reference books
• Andrew N. Sloss, Dominic Symes, and Chris Wright, ARM System Developer’s
Guide: Designing and Optimizing System Software, Morgan Kaufmann Publishers,
2004.
• Reference Books:
• Cortex-M3 Devices Generic User Guide: ARM DUI, 0552A, ID 121610, 2010