LPC 2148 peripherals updated
LPC 2148 peripherals updated
Course handled by
Dr.Saru Meena R.
Assistant Professor
Coimbatore
General purpose input and output (GPIO)
§ It can be either input pin or output pin, whose behavior can be controlled at the run time
§ A group of these pins is called a port (Example, Port 0 of LPC2148 has 32 pins).
§ LPC2148 has two 32-bit General Purpose I/O ports : (a) PORT0 and (b) PORT1
Port 0 :
§ Out of these 32 pins, 28 pins can be configured as either general purpose input or output
§ 3 of these 32 pins (P0.24, P0.26 and P0.27) are reserved. Hence, they are not available for use. Also, these pins
are not mentioned in pin diagram
2
General purpose input and output (GPIO)
§ Almost every pin of these two ports has some alternate function
available
§ For example, P0.0 can be configured as the TXD pin for UART0
§ PINSEL registers are 32-bit registers. These registers are used to select or configure specific pin functionality.
§ In order to select one of the four functions, two corresponding bits of the PINSEL register are needed
4
General purpose input and output (GPIO)
5
General purpose input and output (GPIO)
§ There are two types of GPIO : (a) Slow GPIO and (b) fast GPIO
Slow GPIO :
§ In slow GPIO mode, the access to GPIO registers is done through the VPB (VLSI Peripheral Bus)
§ The VPB clock is typically derived from the CPU clock (CCLK) but can be divided by a factor (default is 4)
§ This division makes GPIO operations slower compared to the fast mode
§ Slow GPIO operations can take more clock cycles due to the VPB divider
6
General purpose input and output (GPIO)
§ The fast GPIO is an enhanced version of GPIO access, introduced to speed up the GPIO operations.
§ In fast GPIO mode, the GPIO registers are accessed directly through the AHB (Advanced High-performance
Bus), which runs at the CPU clock speed (CCLK) without any division.
§ Fast GPIO operations are much faster because the GPIO control is directly connected to the AHB bus.
§ Suitable for time-critical applications where fast response is needed (e.g., toggling pins at high frequency)
Thus, slow GPIO is slower due to clock division, while fast GPIO operates at the system clock speed and is used
7
General purpose input and output (GPIO)
§ IOxDIR (GPIO Port Direction control register) : This is a 32-bit wide register. This register individually controls
the direction of each port pin. Setting a bit to ‘1’ configures the corresponding pin as an output pin. Setting a bit
§ IOxSET (GPIO Port Output Set register) : This is a 32-bit wide register. This register is used to make pins of Port
(PORT0/PORT1) HIGH. Writing one to specific bit makes that pin HIGH. Writing zero has no effect.
§ IOxCLR (GPIO Port Output Clear register) : This is a 32-bit wide register. This register is used to make pins of
Port LOW. Writing one to specific bit makes that pin LOW. Writing zeroes has no effect.
§ IOxPIN (GPIO Port Pin value register): This is a 32-bit wide register. This register is used to read/write the value
on Port (PORT0/PORT1). But care should be taken while writing. Masking should be used to ensure write to the
desired pin
8
General purpose input and output (GPIO)
9
General purpose input and output (GPIO)
10
General purpose input and output (GPIO)
11
General purpose input and output (GPIO)
12
General purpose input and output (GPIO)
13
General purpose input and output (GPIO)
14
General purpose input and output (GPIO)
15
General purpose input and output (GPIO)
16
General purpose input and output (GPIO)
17
Analog to digital converter (ADC)
18
Analog to digital converter (ADC)
19
Analog to digital converter (ADC)
AD0CR :
These bits select ADC0 channel as analog input. In software-controlled mode, only one of these bits should be
1.e.g. bit 7 (10000000) selects AD0.7 channel as analog input
20
Analog to digital converter (ADC)
§ The APB(ARM Peripheral Bus)clock is divided by this value plus one, to produce the clock for ADC
21
Analog to digital converter (ADC)
§ 𝑓!"# = 12 𝑀𝐻𝑧
$$%&
§ CCLK → VPB DIV block → PCLK = '
be 1
22
Analog to digital converter (ADC)
§ The APB(ARM Peripheral Bus)clock is divided by this value plus one, to produce the clock for ADC
Calculation :
§ But to avoid the divide by zero error , LPC 2148 will add 1 to whatever value is being given in CLKDIV . ∴ 8 bit
binary representation of 135 is loaded in CLKDIV ; CLKDIV = 0000 1101
Bit 16 – BURST
Burst mode in the LPC2148 ADC allows for continuous conversions without the need for constant manual
triggering
§ When burst =0 , the ADC waits for the user to tell it to start each time. It takes one reading, then stops.
§ Burst = 0 means the user must manually trigger the ADC to start each conversion, either by software (the user
§ The ADC converts the signal once and then stops. The user must issue another command for the next
conversion.
24
Analog to digital converter (ADC)
Bit 16 – BURST
§ When burst mode is enabled, the ADC automatically samples and converts the selected analog input channels
§ In simple terms, it's like setting the ADC to autopilot mode—once activated, it keeps converting analog signals
into digital values continuously for the selected channels, instead of waiting for the user to say, "start a new
conversion”
§ When Burst = 1, the START bits must be 000, otherwise the conversions will not start
§ In Burst mode, ADC does repeated conversions at the rate selected by the CLKS field for the analog inputs
selected by SEL field
§ When Burst = 1, the START bits must be 000, otherwise the conversions will not start
25
Analog to digital converter (ADC)
Selects the number of clocks used for each conversion in burst mode and the number of bits of accuracy of Result
bits of AD0DR.
e.g. 000 uses 11 clocks for each conversion and provide 10 bits of result in corresponding ADDR register.
Bit 20 – RESERVED
Bit 21 – PDN ; used to decide whether ADC is in power down mode or involved in conversion
1 = ADC is operational
When BURST bit is 0, these bits control whether and when A/D conversion is started
010 = Start conversion when edge selected by bit 27 of this register occurs on CAP0.2/MAT0.2 pin
011= Start conversion when edge selected by bit 27 of this register occurs on CAP0.0/MAT0.0 pin
100 = Start conversion when edge selected by bit 27 of this register occurs on MAT0.1 pin
101 = Start conversion when edge selected by bit 27 of this register occurs on MAT0.3 pin
110 = Start conversion when edge selected by bit 27 of this register occurs on MAT1.0 pin
111 = Start conversion when edge selected by bit 27 of this register occurs on MAT1.1 pin
28
Analog to digital converter (ADC)
Bit 27 – EDGE
This bit is significant only when the Start field contains 010-111. In these cases,
29
Analog to digital converter (ADC)
§ This register contains the ADC’s DONE bit and the result of the most recent A/D conversion
§ When DONE bit is set to 1, this field contains 10-bit ADC result that has a value in the range of 0 (less than or
30
Analog to digital converter (ADC)
Bit 23:16 – RESERVED
§ These bits contain the channel from which ADC value is read.
e.g. 000 identifies that the RESULT field contains ADC value of channel 0.
Bit 30 – Overrun
§ This bit is set to 1 in burst mode if the result of one or more conversions is lost and overwritten before the
conversion that produced the result in the RESULT bits
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
AD0CR is written
§ If AD0CR is written while a conversion is still in progress, this bit is set and new conversion is started
31
Analog to digital converter (ADC)
AD0DR0-AD0DR7 (ADC0 Data Registers)
§ They also include flags that indicate when a conversion has been completed and when a conversion overrun
has occurred
§ When DONE bit is set to 1, this field contains 10-bit ADC result that has a value in the range of 0 (less than or
equal to VSSA) to 1023 (greater than or equal to VREF).
§ This bit is set to 1 in burst mode if the result of one or more conversions is lost and overwritten before the
conversion that produced the result in the RESULT bits
Bit 31 – DONE
§ This bit is set to 1 when an A/D conversion completes. It is cleared when this register is read
33
Analog to digital converter (ADC)
Steps in analog to digital conversion :
§ Configure the ADxCR (ADC Control Register) according to the need of application.
§ Start ADC conversion by writing appropriate value to START bits in ADxCR. (Example, writing 001 to START bits
§ Monitor the DONE bit (bit number 31) of the corresponding ADxDRy (ADC Data Register) till it changes from 0
§ Read the ADC result from the corresponding ADC Data Register ADxDRy. E.g. AD0DR1 contains ADC result of
channel 1 of ADC0.
34
Analog to digital converter (ADC)
Steps in analog to digital conversion :
35
Analog to digital converter (ADC)
Step 1 :
PINSEL 1 register
36
Analog to digital converter (ADC)
Step 2 : Enable the power/clock to ADC0
37
Analog to digital converter (ADC)
Step 3 : Select channel 3 in AD0
38
Analog to digital converter (ADC)
Step 4 : Set the CLKDIV to 13 ADOCR
AD0CR | = 13<<8
19 : 17 = 000 ; this can be done via AND ADC conversion can be started immediately by making bits 26 :
operation between AD0CR and 0xFFF1FFF 24 as 001. Bit 24 is made 1 via. OR operation between AD0CR and
AD0CR & = 0xFFF1FFF 0x01000000. Bits 25 and 26 are made 0 via. AND operation
between AD0CR and 0x F9FFFFFF
Step 6 : Make PDN = 1 to activate the ADC
module AD0CR | = 0x01000000
AD0CR &=0xF9FFFFFF
39
Analog to digital converter (ADC)
Step 8 : Check whether analog to digital ADODR3
conversion is over or not by monitoring the
DONE bit (bit 31) in AD0GDR
AD0DR3. To visualize the result starting to port 1 (P1.16 to P1.25) , IO1PIN = val <<16 (since port 1 has pins
from bit 0 , the data in AD0DR3 has to be only from P1.16 to P1.31)
shifted right by 6. 40
Analog to digital converter (ADC)
while(1) delay(200000);
Complete ADC code :
{ void adc_init(void)
# include <LPC214x.h>
AD0CR | = 0x00000008; {
void adc_init(void);
AD0CR | = 13<<8; PINSEL1& = 0xDFFFFFFF;
void delay(int j);
AD0CR & = 0xFFFEFFFF; PINSEL1|=0x10000000;
unsigned int val;
AD0CR & = 0xFFF1FFFF; PCONP|= (unsigned long) (0x00000001 << 12) ;
int main ()
AD0CR | = 0x00200000; }
{
AD0CR | = 0x01000000 ; void delay (int j)
PINSEL2 = 0x00000000;
AD0CR &=0xF9FFFFFF; {
IO1DIR = 0xFFFFFFFF;
while((AD0GDR & = 0x80000000)==0); for(i=0;i<=j;i++)
adc_init();
val = (AD0DR3>>6) & 0X000003FF; {
§ Each timer module can either work in timer mode or in counter mode
Timer mode :
§ Objective: In Timer Mode, the Timer captures time intervals. It is used to create delays, trigger events after a
specified time, or generate periodic signals
§ Clock source: The clock source for Timer Mode is the PCLK (Peripheral Clock) of the microcontroller.
Operation:
§ The Timer's internal counter is incremented by each pulse from the PCLK, based on the pre-scaler value.
§ It can be configured the timer to generate interrupts after a certain value (e.g., Timer match register).
§ The timer register increments up to a pre-defined value or overflows and resets, creating a periodic event if
set up with interrupts
§ Use Case: Timer Mode is used when you need a precise delay or to generate periodic actions like blinking
an LED or controlling a PWM signal.
42
Timer in LPC 2148
Counter mode :
§ Objective: In Counter Mode, the timer counts the number of external events or pulses. It acts like a digital
§ Clock Source: In this mode, an external input pin acts as the clock source instead of the internal PCLK.
Operation:
§ The counter register increments whenever a pulse is detected at the external clock input.
§ The input signal must be applied at one of the designated Timer/Counter pins (e.g., CAP or MAT pins).
§ This mode is ideal for applications where the need is to measure or count the frequency or occurrence of an
external signal
§ Use case: Counter mode is used in scenarios like measuring rotational speed, counting the number of
43
sensor pulses, or detecting events such as button presses
Timer in LPC 2148
§ The timer module in LPC 2148 supports two advanced functionalities: Capture mode and Match mode
§ There are upto four 32 bit capture channels per timer and four 32 bit match registers
Capture mode :
Objective: In Capture mode, the timer captures the current value of the timer counter when a specific external
Operation :
§ The Timer has dedicated Capture input pins (e.g., CAP0.0, CAP0.1).
§ When an edge (rising or falling) is detected on the Capture pin, the value of the Timer Counter (TC) is copied
Match mode :
Objective: In Match mode, the timer counter value is compared with a predefined value, and specific actions
are performed when they match
Operation:
§ Match Registers (MR0, MR1) are used to specify the value that should be compared against the Timer
Counter (TC)
§ When the Timer Counter matches a value in the match register, several actions can be configured:
Generate an Interrupt: When the counter matches, an interrupt can be generated to perform specific actions
Reset the Timer: The timer can be reset to start counting again.
Stop the Timer: The timer can stop on a match, which is useful for one-shot timing operations
45
Timer in LPC 2148
46
Registers associated with timer
§ It is a 32 bit register
§ When in counter mode, it is used to select the pin and edges for counting.
§ This field selects which rising edges of PCLK can increment Timer’s Prescale Counter (PC), or clear PC and
increment Timer Counter (TC).
§ 01 = Counter Mode (Rising edge): TC is incremented on rising edge on the capture input selected by Bits 3:2.
§ 10 = Counter Mode (falling edge) : TC is incremented on falling edge on the capture input selected by Bits
3:2
§ 11 = Counter Mode (Both rising and falling edge): TC is incremented on both edges on the capture input
selected by Bits 3:2 47
Registers associated with timer
§ These bits determine which capture input to use for the external event counter
§ 00 = CAP0.0
§ 01 = CAP0.1
§ 10 = CAP0.2
§ 11 = CAP0.3
48
Registers associated with timer
§ It is a 32-bit register
§ It is a 32-bit register
§ It controls the division of PCLK by some constant value before it is applied to the Timer Counter
§ When it reaches the value in Prescale Register, the Timer Counter(TC) is incremented by 1 and Prescale
Counter is reset on next PCLK
49
Registers associated with timer
Solution :
1
∴n= = 1000
1×10*+
1) Power Up the Timer Peripheral: Ensure that the Timer module is powered by configuring the Power
Control for Peripherals Register (PCONP)
2) Configure the Prescale Register: The Prescale Register (PR) is used to set the timer resolution. By
default, the timer increments every peripheral clock cycle (PCLK), but the Prescale Register is used to
to slow down the counting
3) Configure Timer Control Register (TCR): The TCR (Timer Control Register) controls enabling, disabling,
and resetting the Timer Counter (TC). To enable the timer, set bit 0 of TCR to 1
4) Wait for desired delay: Use the Timer Counter (TC) value to measure the elapsed time ; loop until the
desired count is reached
52
Steps in timer programming
1) Power Up the Timer Peripheral: Ensure that the Timer module is powered by configuring the Power
Control for Peripherals Register (PCONP)
2) Configure the Prescale Register: The Prescale Register (PR) is used to set the timer resolution. By
default, the timer increments every peripheral clock cycle (PCLK), but the Prescale Register is used to
to slow down the counting
3) Configure Timer Control Register (TCR): The TCR (Timer Control Register) controls enabling, disabling,
and resetting the Timer Counter (TC). To enable the timer, set bit 0 of TCR to 1
4) Wait for desired delay: Use the Timer Counter (TC) value to measure the elapsed time ; loop until the
desired count is reached
53
Steps in timer programming
Write an embedded C code to turn ON / turn OFF of an LED connected to P0.10 for every 1 sec. Use timer
0 module and take TCLK = 1 KHz
#include <lpc214x.h>
int main(void)
55
Steps in timer programming
void initPLL()
{
PLL0CON=0X01;
PLL0CFG=0X04;
PLL0FEED=0XAA;
PLL0FEED=0X55;
while((PLL0STAT&(0X01<<10))==0);
PLL0CON=0X03;
PLL0FEED=0XAA;
PLL0FEED=0X55;
VPBDIV=0X01;
}
56
Programming with timer
Practice tasks :
1) Write an embedded C code that turns ON and OFF LEDs connected to P1.16 to P1.19 for every 100ms.The
delay has to be generated using timer 1. Take TCLK = 1 MHz.
2) Write an embedded C code to turn ON and OFF of LEDs connected to P1.16,P.17 and P1.19 for every 500
ms. Take TCLK = 1KHz. The delay has to be generated using timer 1 module.
57
Timer in LPC 2148
58
Timer in LPC 2148
59