Timer Interrupt
Timer Interrupt
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
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)
53
Steps in timer programming
54
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;
}
55
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.
56
UART in LPC 2148
UART0 :
UART1 :
57
UART in LPC 2148
,&'(
§ -
= 𝐵𝑎𝑢𝑑 𝑟𝑎𝑡𝑒 ∗ 16 = 𝐵𝑎𝑢𝑑 𝑟𝑎𝑡𝑒 𝑐𝑙𝑜𝑐𝑘
.)∗$))))))
§ = 9600 ∗ 16 ; 𝑥 = 390.6 = 3910
-
58
UART in LPC 2148
59
UART in LPC 2148
60
Frame structure of UART
61
Registers associated with UART
§ PINSEL register to configure the particular port for UART TX/RX functionality
62
Registers associated with UART
§ DLAB bit has to 0 to access the transmit and receive registers of UART
§ Make DLAB = 1 and generate the required baud rate, then make DLAB =
0 and access transmitter and receiver registers of UART0
64
Registers associated with UART
Bit 5 - Transmitter Holding Register § In the reception mode (LPC 2148 is the receiver and PC is the
Empty transmitter), the data that is received by the LPC 2148 is there
§ 0 = U0THR has valid data in the register U0RBR.In this case , Bit 0 of U0LSR will be 1
§ 1 = U0THR empty
65
Registers associated with UART
U0LSR structure
66
Registers associated with UART
UART0 receive buffer register (U0RBR) :
§ It contains the “oldest” received byte in the receive FIFO U0RBR structure
§ If the character received is less than 8 bits, the unused MSBs are padded
with zeroes.
§ The Divisor Latch Access Bit (DLAB) in U0LCR must be zero in order to
access the U0RBR. (DLAB = 0)
§ The Divisor Latch Access Bit (DLAB) in U0LCR must be zero in order to
67
access theU0THR. (DLAB = 0)
UART transmission
Write an embedded C code to continuously transmit a character from LPC 2148 to PC (UART transmission).Use
UART 0 module and take PCLK = 60 MHz. Required Baud rate = 9600
#include <lpc214x.h>
int main(void)
PINSEL0 | = 0X00000005; // configure P0.0 and P0.1 as TX0 and RX0 pins of UART0
U0DLL = 0x87;
U0DLM = 0x01;
68
UART transmission
while(1)
{
U0THR = ‘a’; //put the data to be transmitted in U0THR register
while ((U0LSR & (0x01<<5)==0); // check the 5th bit in U0LSR register and see if it is zero or 1. If the 5th bit is 1 , it
tells that the data in U0THR is transmitted completely//
}
void initPLL()
{
PLL0CON=0X01;
PLL0CFG=0X04;
PLL0FEED=0XAA;
PLL0FEED=0X55; while((PLL0STAT&(0X01<<10))==0); PLL0CON=0X03;
PLL0FEED=0XAA;
PLL0FEED=0X55;
VPBDIV=0X01;
69
}
UART reception
Write an embedded C code to continuously receive a character from PC to LPC 2148 (UART reception).Use UART 0
module and take PCLK = 60 MHz. Required Baud rate = 9600. LPC 2148 will receive a character through PC
terminal and will transmit the same character back to the PC terminal.
#include <lpc214x.h>
int main(void)
PINSEL0 | = 0X00000005; // configure P0.0 and P0.1 as TX0 and RX0 pins of UART0
U0DLL = 0x87;
U0DLM = 0x01;
70
U0LCR = 0x03 ; // make DLAB = 0 to access U0THR and U0RBR registers
UART reception
while(1)
{
while((U0LSR & 0x01)==0); //check if the reception is over or not
mydata = U0RBR ;
//transmit the received data back to PC so that the received data can be visualized in the terminal
U0THR = mydata; //put the data to be transmitted in U0THR register
while ((U0LSR & (0x01<<5)==0);
}
void initPLL()
{
PLL0CON=0X01;
PLL0CFG=0X04;
PLL0FEED=0XAA;
PLL0FEED=0X55; while((PLL0STAT&(0X01<<10))==0); PLL0CON=0X03;
PLL0FEED=0XAA;
PLL0FEED=0X55;
VPBDIV=0X01;
} 71
Digital to analog converter (DAC)
§ Digital to Analog Converter (DAC) are mostly used to generate
analog signals (e.g. sine wave, triangular wave etc.) from digital
values
§ It is a read-write register
Bit 16 – BIAS
§ Write the 10-bit digital value (which has to be converted into analog form) in
VALUE field of DACR register
74
Digital to analog converter (DAC)
1) Write an embedded C code to generate a triangular waveform using
DAC of LPC 2148
for (i=1023 ; i>=0;i--)
#include <lpc214x.h>
{
void delay () ;
DACR = i<<6;
int main(void)
delay();
{
}
int i,j;
}
PINSEL1 |= (1<<19); //Select AOUT function for P0.25. bits[19:18] = [1 0]
}
DACR = 0;
void delay ()
while(1)
{
{
for (j=0;j<=50000;j++)
for (i=0 ; i< 1024 ; i++)
}
{
DACR = i<<6;
delay();
75
}
Digital to analog converter (DAC)
2) Write an embedded C code to generate a sawtooth waveform of frequency 50
Hz with 100 samples using DAC of LPC 2148. Take TCLK = 1 MHz and PCLK =
One
60MHz. Use timer 0 for delay generation sawtooth
cycle
Solution :
7
Given that f = 50 Hz ⟹ 𝑇 = = 20 𝑚𝑠
8
20ms
Duration of one sawtooth cycle = 20 ms and this one sawtooth cycle is sliced into
100 samples
9:×7:!"
100 samples = 20 ms ⟹ 1 sample = = 200𝜇𝑠𝑒𝑐
7::
§ In the code, the for loop should start from 0 and end in 99 since the no.of
samples =100 ; In order to use the full bandwidth of 0 – 1023 , each digital
value is multiplied by 10 so that 100 samples will cover the entire 0 – 1023
76
span
Digital to analog converter (DAC)
#include <lpc214x.h>
void initPLL(void); // Setup PLL and Clock Frequency
void init_timer(void);
int main(void)
{
initPLL(); // to set the PCLK = 60 MHz for timer
PINSEL1 |= (1<<19); //Select AOUT function for P0.25. bits[19:18] = [1 0]
DACR = 0;
unsigned int step = 1023/100 ;
while(1)
{
for (i=0 ; i< 100 ; i++)
{
DACR = (i*step) <<6;
init_timer(); 77
Digital to analog converter (DAC)
DACR = 0; // Set DAC output to 0 while (T0TC < 200); // Wait for 200 𝜇𝑠𝑒𝑐 delay
init_timer(); // Small delay for the falling edge TOTCR &=~(0x00000001) ; // disabling the timer
} }
} void initPLL()
void init_timer(void) {
{ PLL0CON=0X01;
T0CTCR = 0x0; //Set Timer 0 into Timer Mode PLL0CFG=0X04;
T0PR = 59; //Increment T0TC at every 6 clock PLL0FEED=0XAA;
cycles
PLL0FEED=0X55;
T0TCR |=0x00000001 ; //enable the timer
while((PLL0STAT&(0X01<<10))==0);
T0TC =0x00000000 ; //reset the TC register
PLL0CON=0X03;
manually
PLL0FEED=0XAA;
TOPC = 0x00000000; //reset the PC register
manually PLL0FEED=0X55;
VPBDIV=0X01;
78
}
Digital to analog converter (DAC)
3) Write an embedded C code to generate a triangular waveform of frequency 50 Hz
with 100 samples using DAC of LPC 2148. Take TCLK = 1 MHz and PCLK = 60MHz. Use
One
timer 0 for delay generation 10ms triangular
wave cycle
Solution :
10ms
7
Given that f = 50 Hz ⟹ 𝑇 = = 20 𝑚𝑠
8
20ms
Duration of one triangular cycle = 20 ms and this one cycle is sliced into 100
§ Out of the total 100
samples
samples for one cycle , 50
§ Out of the total 100 samples for one cycle , 50 samples are used for generating the samples are used for
generating the rising edge
rising edge and 50 samples are used for generating the falling edge of the and 50 samples are used
for generating the falling
triangular wave
edge of the triangular
§ Rising edge duration = falling edge duration = Duration of one cycle /2 ⟹ Rising wave
Solution :
7
Given that f = 50 Hz ⟹ 𝑇 = = 20 𝑚𝑠
8
Duration of one cycle = 20 ms and this one cycle is sliced into 100 samples (50 samples for 𝑇<= and 50
samples for 𝑇<>> )
7:×7:!"
50 samples = 10 ms ⟹ 1 sample = = 200𝜇𝑠𝑒𝑐
?:
82
Digital to analog converter (DAC)
#include <lpc214x.h>
void initPLL(void); // Setup PLL and Clock Frequency
void init_timer(void);
int main(void)
{
initPLL(); // to set the PCLK = 60 MHz for timer
PINSEL1 |= (1<<19); //Select AOUT function for P0.25. bits[19:18] = [1 0]
DACR = 0;
while(1)
{
DACR = 0<<6;
init_timer();
83
Digital to analog converter (DAC)
DACR = 1023 <<6; while (T0TC < 200); // Wait for 200 𝜇𝑠𝑒𝑐 delay
init_timer(); TOTCR &=~(0x00000001) ; // disabling the timer
} }
} void initPLL()
void init_timer(void) {
{ PLL0CON=0X01;
T0CTCR = 0x0; //Set Timer 0 into Timer Mode PLL0CFG=0X04;
T0PR = 59; //Increment T0TC at every 6 clock PLL0FEED=0XAA;
cycles
PLL0FEED=0X55;
T0TCR |=0x00000001 ; //enable the timer
while((PLL0STAT&(0X01<<10))==0);
T0TC =0x00000000 ; //reset the TC register
PLL0CON=0X03;
manually
PLL0FEED=0XAA;
TOPC = 0x00000000; //reset the PC register
manually PLL0FEED=0X55;
VPBDIV=0X01;
84
}
Interrupts in LPC 2148
§ An interrupt is a signal sent to the CPU which indicates
that a system event has a occurred which needs
immediate attention“
§ After the ISR execution has finished the CPU gets back
to where it had stopped
85
Interrupts in LPC 2148
How the interrupt works ?
§ Whenever any device needs service of microcontroller, the device notifies the microcontroller by sending
interrupt signal
§ Upon receiving an interrupt signal, the microcontroller stops or interrupt main program flow and saves
the address of the next instruction (PC) on the stack pointer (SP)
§ It jumps to a fixed location in memory, called interrupt vector table that hold the address of the ISR
(Interrupt Service Routine). Each interrupt has its own ISR. The microcontroller gets the address of the ISR
from the interrupt vector table and jump to it.
§ It starts to execute the Interrupt Service Routine until it reaches the last instruction of the subroutine
§ Upon executing last instruction in Interrupt Service Routine the microcontroller returns to the place where
it left off or interrupted previously
§ First, it gets the program counter (PC) address from the stack pointer by popping the top 4 bytes of the
stack into the PC
§ Then it starts to execute from that address and continue executing main program
86
Interrupts in LPC 2148
§ There are two types of interrupts : (a) FastIRQs or FIQs (b) Normal
IRQs or IRQs. Normal IRQs can be further classified as : (a)
Vectored IRQ and (b) Non-Vectored IRQ
87
Interrupts in LPC 2148
§ Vectored means that the CPU is aware of the address of the ISR when the interrupt occurs
§ Non-Vectored means that CPU doesn’t know the address of the ISR (nor) the source of the IRQ when the
interrupt occurs
§ For Non – Vectored interrupts , CPU needs to be supplied by the ISR address
§ For the Vectored interrupts , the system internally maintains a table called IVT or Interrupt Vector Table
which contains the information about Interrupts sources and their corresponding ISR address
§ The CPU needs to be supplied with the address of the ‘default’ or a ‘common’ ISR that needs to be executed
when the interrupt occurs
§ The user must assign the address of the default ISR to this register for handling Non-Vectored IRQs
88
Interrupts in LPC 2148
§ Vectored means that the CPU is aware of the address of the ISR when the interrupt occurs
§ Non-Vectored means that CPU doesn’t know the address of the ISR (nor) the source of the IRQ when the
interrupt occurs
§ For Non – Vectored interrupts , CPU needs to be supplied by the ISR address
§ For the Vectored interrupts , the system internally maintains a table called IVT or Interrupt Vector Table
which contains the information about Interrupts sources and their corresponding ISR address
§ The CPU needs to be supplied with the address of the ‘default’ or a ‘common’ ISR that needs to be executed
when the interrupt occurs
§ The user must assign the address of the default ISR to this register for handling Non-Vectored IRQs
89
Interrupts in LPC 2148
VICIntSelect (R/W) :
90
Interrupts in LPC 2148
VICIntEnable (R/W) :
§ Writing a 1 at a given bit location will make the corresponding interrupt Enabled
VICIntEnClr (R/W) :
§ This is similar to VICIntEnable expect writing a 1 here will disabled the corresponding Interrupt
§ This has an effect on VICIntEnable since writing at a specific bit location will clear the corresponding bit in
the VICIntEnable register
91
Interrupts in LPC 2148
VICIRQStatus (R) :
§ This register is used for reading the current status of the enabled IRQ interrupts
§ If a bit location is read as 1 then it means that the corresponding interrupt is enabled and active
VICFIQStatus (R) :
§ This register is used for reading the current status of the enabled FIQ interrupts
§ If a bit location is read as 1 then it means that the corresponding interrupt is enabled and active
VICSoftInt :
§ This register is used to generate interrupts using software i.e manually generating interrupts using code
§ If the value written to any bit location is 1, then, the correspoding interrupt is triggered i.e. it forces the
interrupt to occur
VICSoftIntClear:
§ This register is used to clear the interrupt request that was triggered(forced) using VICSoftInt.
§ Vector Address Registers are used to hold the addresses of the ISR of the 16 Vectored IRQ slots
§ If Slot 0 (VICVectCntl0) is assigned to TIMER0 IRQ, then the address for the interrupt function must be
assigned to VICVectAddr0.
§ Default Vector Address Register holds the address of the ISR for Non – Vectored IRQs
§ This address acts as the default or common ISR address when a Non – Vectored IRQ occurs
§ This is a different and single register and must not be confused with Vector Address Registers
(VICVectAddr0 – VICVectAddr15)
§ It is a read / write register and when the data is read from this register, it returns address of ISR
§ Writing a value to this register indicates the VIC that the execution of the current interrupt is finished.
Hence, this register is written with a dummy value at the end of the ISR to indicate the end of execution.
93
Interrupts in LPC 2148
Vectored Interrupt Controller (VIC) Vectored Interrupt Controller (VIC)
§ There are 22 Interrupt Sources in LPC2148
94
Interrupts in LPC 2148
95
Interrupts in LPC 2148
96
Interrupts in LPC 2148
How to write the ISR ?
97
Interrupts in LPC 2148
Three steps process to enable vectored IRQ :
98
Interrupts in LPC 2148
Assign TIMER0 IRQ and the corresponding ISR to Slot 0.
Based on the above steps, the following assignments must be made to setup the interrupt.
99
Interrupts in LPC 2148
Match registers :
§ The match registers (like T0MR0, T0MR1, etc.) specify the value at which Timer 0 generates an event.
§ When the timer counter (T0TC) reaches a match value (such as in T0MR0), it triggers a specific action like
setting a flag, generating an interrupt, or resetting the timer counter.
§ To use a match register for delay, set T0MR0 to the calculated value for the desired delay based on PCLK
and the prescaler
100
Interrupts in LPC 2148
T0IR (Timer 0 interrupt register)
include <lpc214x.h>
void initClocks(void);
void initTimer0(void);
__irq void timer0ISR(void);
int main(void)
{
initClocks(); // Initialize PLL to setup clocks
initTimer0(); // Initialize Timer0
IO0DIR = (1<<10); // Configure pin P0.10 as Output
IO0PIN = (1<<10);
T0TCR = (1<<0); // Enable timer
while(1); // Infinite Idle Loop
}
103
Interrupts in LPC 2148
void initTimer0(void)
{
T0CTCR = 0x0; //Set Timer Mode
T0PR = 60000-1; //Increment T0TC at every 60000 clock cycles
//60000 clock cycles @60Mhz = 1 mS
T0MR0 = 500-1; //Zero Indexed Count-hence subtracting 1
T0MCR = (1<<0) | (1<<1);//Set bit0 & bit1 to Interrupt & Reset TC on MR0
VICVectAddr4 = (unsigned )timer0ISR; //Pointer Interrupt Function (ISR)
VICVectCntl4 = (1<<5) | 4; //(bit 5 = 1)->to enable Vectored IRQ slot
//bit[4:0]) -> this the source number
VICIntEnable = (1<<4); // Enable timer0 interrupt
T0TCR = (1<<1); // Reset Timer
}
104
Interrupts in LPC 2148
Task2 : Generate a square wave at 1KHz @ P1.25 using Timer0 Match Interrupt
106