0% found this document useful (0 votes)
118 views13 pages

Electronic Engineering Laboratory IV BEE31101 Instruction Sheet BEJ40401

This document provides instructions for Lab 6 on interrupts, timers, and PWM in an ARM Cortex-M3 microcontroller. The lab objectives are to configure and debug interrupt, timer, and PWM applications using the LPC1768 microcontroller in Keil MDK. Guidelines are provided on lab group formation, pre-lab submission, in-lab activities, demonstration, and report submission. The pre-lab questions cover interrupt handling steps, counter maximum values, and the number of PWM outputs on the LPC1768. The document then provides details on interrupt concepts and configuration using the LPC1768's GPIO interrupts as an example application.

Uploaded by

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

Electronic Engineering Laboratory IV BEE31101 Instruction Sheet BEJ40401

This document provides instructions for Lab 6 on interrupts, timers, and PWM in an ARM Cortex-M3 microcontroller. The lab objectives are to configure and debug interrupt, timer, and PWM applications using the LPC1768 microcontroller in Keil MDK. Guidelines are provided on lab group formation, pre-lab submission, in-lab activities, demonstration, and report submission. The pre-lab questions cover interrupt handling steps, counter maximum values, and the number of PWM outputs on the LPC1768. The document then provides details on interrupt concepts and configuration using the LPC1768's GPIO interrupts as an example application.

Uploaded by

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

Faculty of Electric and Electronic Engineering

Electronic Engineering Laboratory IV


BEE31101
BEJ40401
Instruction Sheet

Lab No. 6
Lab Title Interrupt, Timer and PWM in ARM Cortex-M3
Microcontroller (LPC1768)
Semester 01
Session 2021/22
Lab Durations 4 Hours (2 weeks)
Independent Studies 2 Hours
Electronic Engineering Laboratory IV (BEE31101) 1
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

Outcomes

After completing this module, student should be able to:


1. Configure, apply, and debug interrupt applications for the LPC1768 microcontroller in Keil MDK
Vision.
2. Configure, apply, and debug the timer applications for the LPC1768 microcontroller in Keil MDK
Vision.
3. Configure, apply, and debug the PWM applications for the LPC1768 microcontroller in Keil
MDK Vision
4. Organize time management in group effectively according to the given tasks.
5. Utilize the microprocessor/microcontroller user manual in order to use the specific peripheral.

Guidelines

1. Grouping: Lab group is not predetermined and consists with at most FIVE team members.

2. Pre-Lab: The pre-lab must be submitted to the instructor at the beginning of lab session.
Verified by the instructor and returned to the students at the end of lab session. The verified
pre-lab will be attached with the final report for submission.

3. Lab Activities: All lab activities such as sample code, examples and lab assignments must be
held in the respective lab location and completed within the given times.

4. Demonstration: Student must demonstrate the successful sample code, examples, and lab
assignments to the respective instructor. Verification only will be given upon completion of all
lab activities and initialized by the instructor on the cover page.

5. Report Organization: Report must be organized according to given report template.

6. Report Submission: Report must be received by respective technical staff (at respective lab)
before 4.00pm; not later than three (7) days upon completion of lab session.

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 2
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

Pre-Lab (5%)

1. List the most significant actions that a CPU takes when it responds to an enabled interrupt.
(3 marks)

2. What is the maximum value, in decimal, that a 12-bit and a 24-bit counter can count up to?
(1 mark)

3. The LPC1768 has how many PWM outputs?


(1 mark)

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 3
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

6.0 Interrupt in LPC1768 Microcontroller


Interrupts are used to achieve rapid response with minimal overhead to infrequent, random, or
unexpected events. The alternative to interrupts is polling. With polling the firmware continually
tests the status of input-output devices for a service request. The disadvantages of polling are: (1)
The code overhead to continually test the status, and (2) The response time that for worse case
can be as long as the time between the tests.

Relative to polling the advantages of interrupts are the minimal overhead and very short response
time. The disadvantages of interrupts are that it is difficult to anticipate and test for all
contingencies. The general response to an interrupt request is illustrated in Figure 6.1.

Figure 6.1: A typical microprocessor interrupt response

All interrupts including the core exceptions are managed by the NVIC (Nested Vector Interrupt
Controller). Core exceptions are non-maskable.

External interrupts are maskable. That is when an event occurs the peripheral will issue an interrupt
request. If the interrupt has been enabled in the NVIC it will be accepted and the microcontroller
will proceed to the appropriate vector and interrupt service routine. If the interrupt has not been
enabled at the NVIC no further action will occur. A simplified diagram of the logic is given in Figure
6.2.

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 4
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

Figure 6.2: Simplified NVIC logic

With many interrupt sources the Interrupt Set Enable Register is actually several registers or an
array. ISER[0] will handle interrupt request numbers from 0 to 31, ISER[1] will handle interrupts
requests 32 through 63 etc. See list of interrupt requests numbers (IRQn) under individual
microcontrollers in LPC1768 user manual. The Cortex Microcontroller Software Interface Standard
(CMSIS) can be used where the compiler has defined functions NVIC_EnableIRQ(IRQn) and
NVIC_DisableIRQ(IRQn) where IRQn is the interrupt number.

Upon interrupt the ARM will jump to a location in low memory where it expects to find the starting
address for the interrupt service routine (ISR). In the case of the Keil MDK the start-up code will
initialize all vectors with the address of a default interrupt handler (see the startup_PLC17xx.s file).
The Interrupt Service Routine (ISR) should be given the same label as defined by the start-up
code. i.e., the ISR for the ADC is ADC_IRQHandler.

Activity 1: Setup the GPIO interrupt

In advanced microcontroller applications, the polling method of testing the hardware should be
minimized. For example, if we need to test the input at the GPIO, instead of using polling in the
code, we can generate the interrupt from the GPIO such that the microprocessor will only check
the state of the GPIO when only there is a change in the GPIO state. The interrupt can be
generated from the GPIO by enabling the appropriate Interrupt Set Enable register in the NVIC.

Port 0 and Port 2 can provide a single interrupt for any combination of port pins. Each port pin can
be programmed to generate an interrupt on a rising edge, a falling edge, or both. GPIO0 and
GPIO2 interrupts share the same position in the NVIC with External Interrupt 3 (EINT3). Steps for
initializing the GPIO interrupt are as follows:

1. All maskable interrupts are disable on reset (power on). As stated above, GPIO interrupts
share the same position in the NVIC with the EINT3, therefore, to enable the GPIO0 or
GPIO2 interrupts, the EINT3 interrupt must be enabled as follow:
NVIC_EnableIRQ(EINT3_IRQn);
FKEE, Sem01 Session 2021/22
Electronic Engineering Laboratory IV (BEE31101) 5
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

2. Enable GPIO interrupts in IO0/2IntEnR for interrupt on rising edge, or IO0/2IntEnF for
interrupt on falling edge of the GPIO pin. Note that, the corresponding GPIO pin must be
initialized as an input pin.
LPC_GPIOINT->IO0IntEnR |= (1<<0); //Enable falling edge interrupt at P0.0

3. When writing the ISR, the name of the ISR must be the same as the IRQ name in the
startup code.
void EINT3_IRQHandler(void) //ISR for GPIO int

4. Program in Listing 6.1 shows a simple application to demonstrate the use of GPIO interrupt
in the LPC1768 microcontroller. Build the program and run the debug session.

#include <lpc17xx.h>
void EINT3_IRQHandler(void);
void delay(void);

int main(void)
{
NVIC_EnableIRQ(EINT3_IRQn); //Enable the EINT3 interrupt
LPC_GPIOINT->IO0IntEnF |= (1<<0); //Enable falling edge interrupt
//at P0.0
LPC_GPIO2->FIODIR |=(3<<0); //P2.0 and P2.1 as output

while(1)
{
LPC_GPIO2->FIOSET |=(1<<1); //flashing LED at P2.1
delay();
LPC_GPIO2->FIOCLR |=(1<<1);
delay();
}
}
void EINT3_IRQHandler(void)
{ LPC_GPIO2->FIOPIN ^=(1<<0); //Toggle P2.0
LPC_GPIOINT->IO0IntClr |= (1<<0); //Clear the interrupt
}
void delay(void) //delay function
{ unsigned int count,i=0;
for(count=0; count < 0x1FFFF/*000*/; count++)
{
i++;
}
}

Listing 6.1: Simple GPIO interrupt

5. Open the GPIO and Logic Analyzer windows. Run the program and to change the state of
P0.0 and observe the output at P2.0, P2.1, and the signals on the logic analyzer.

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 6
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

6. Remove the “clear the interrupt code” in the ISR. Rebuild and debug the program. Observe
the results.

Lab Assignment 6.1

Modify the program such that the interrupt is now at the P2.3, and it is triggered at the rising edge
of P2.3.

6.1: Timer in the LPC1768 microcontroller

Timers are one of the important peripherals of modern microcontrollers (also known as hardware
timer). They are used to generate periodic interrupts (in ‘Timer’ mode) and also count events (in
‘Counter’ mode). With hardware timers, we can generate precise delays, which are non-blocking
delays. By referring to Listing 6.1 (or in our previous programs), where we use the blocking delay
function to generate the delay. The disadvantages of the blocking delay are (1) very hard to code
to achieve precise delay, and (2) the microcontroller needs to finish the delay function before it can
be used for other tasks. However, with the hardware timer, we can now create a very accurate
timing (delay), and this process can execute independently from the main program. In other words,
after we start the timer, we can now use the microcontroller to perform other tasks, i.e., read analog
inputs, switches etc.

There are four identical Timer peripherals in LPC1768 namely Timer0, Timer1, Timer2 and Timer3.
All four timers are 32-bit timers with programmable 32-bit prescaler. They are designed to count
cycles of the peripheral clock or an external clock signal and optionally generate interrupts (or other
actions) at specified timer values (based on four match registers).

There are many Timer registers in LPC1768 (Please refer to the LPC1768 user manual), the Timer
Counter (TC) and Prescale Register (PR) are particularly the most important as they are directly
responsible for the Timer’s functionality. After a Timer peripheral is reset and enabled, its TC
register is initialized to 0 and is incremented by 1 for every PR+1 clock cycles. After reaching its
maximum, the value in TC is reset to 0 and the counting restarts.

PR register controls the resolution of the timer. For example, when PR is set to 0, then TC
increments at every cycle of the peripheral clock. Similarly, if PR is set to 1, then TC increments at
2 cycles of the peripheral clock. Using these two registers we can generate precise delays.

Another important register associated with Timers is the Match Register. There are four match
registers for each timer peripheral. Users can load the values into these match registers and for
every TC increment, the value in TC is compared to the value in match register. If the values are
a match, then the match register can perform any of the following action:
FKEE, Sem01 Session 2021/22
Electronic Engineering Laboratory IV (BEE31101) 7
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

1. An interrupt is generated.

2. The TC will be reset

3. TC and PR will be stopped and corresponding TCR will be set to 0.

Additionally, when the values in Match Register and TC matches, we can also control the External
Match Register (EMR) to set an External Match Output pin as LOW, HIGH or Toggle. Timers 0, 1
and 3 have two External Match Outputs each while Timer 2 has four External Match Outputs. Using
this functionality, we can toggle an LED connected to one of the Match Output Pins without
intervention from the processor or any GPIO functionality.

Lab Activity 6.2: Creating a precise delay in LPC1768 microcontroller

1. First, enable the timer in PCONP register. On reset, both TIM0 and TIM1 are enabled.
Therefore, no need to enable TIM0 and TIM1 in order to use them. However, TIM2 and
TIM3 are disabled. Hence, need to enable as follow:

LPC_SC → PCONP |= (1<<23); //enable POWER to TIM3

2. Set the PCLK (timer clock). By default, PCLK = CCLK / 4. For example, if we want to have
the timer clock to be the same as a system clock, we need to code as follow:

LPC_SC → PCLKSEL1 = (1<< 14); //Timer clock = system clk

3. Set to use the timer mode in CTCR register

LPC_TIM0 → CTCR = 0x0; //Timer mode selected

4. To generate the precise timing, we need to determine the value that need to be in the
prescaler register (PR) such that the timer will count at our desired time interval (also known
as timer resolution given by

PR + 1
TRES =
PCLK

For example, if we want to produce 1 ms resolution, hence the value in PR must be

PR = PCLK × TRES − 1

In our case, the CCLK is clocked at 100 MHz, and let’s use the default setting where PCLK
= CCLK / 4 = 100 MHz/4 = 25 MHz.

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 8
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

Therefore, PR = 25 MHz × 1 ms – 1 = 24999

5. Program in Listing 3.2 demonstrates a simple application of a hardware Timer0 where, it is


used to produce a delay routine with 1 ms resolution. The program will flash the LED
connected to P1.0 every 10 ms.

6. Build the program and run the debug session. Run the program and observe the output in
GPIO and logic analyzer windows.

#include <lpc17xx.h>

void Init_Wait(void);
void wait_ms(int t);

int main(void)
{
Init_Wait();
LPC_GPIO1->FIODIR |= (1<<0);
while(1)
{
LPC_GPIO1->FIOSET |= (1<<0); //set P1.0
wait_ms(10);
LPC_GPIO1->FIOCLR |= (1<<0); //clear P1.0
wait_ms(10);
}
}
void Init_Wait(void)
{
LPC_TIM0->CTCR = 0; //timer mode
LPC_TIM0->PR = 24999; //for 1ms resolution
}
void wait_ms(int t)
{
LPC_TIM0->TCR = 1<<1; //reset timer counter
LPC_TIM0->TCR = 1; //enable timer counter
while (LPC_TIM0->TC < t); //wait if conter < t
}

Listing 6.2: Simple program to demonstrate the application of hardware timer

Lab Assignment 2:

Modify the program in Listing 3.2 to use Timer3 instead of Timer0, create a timing resolution of 1
s and use PCLK = CCLK/2. Name the delay routine as wait_us( ). In the code, flash the P1.0
every 100 s.

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 9
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

Pulse Width Modulation

PWM represents a neat and remarkably simple way of getting a rectangular digital waveform to
control an analog variable, usually voltage or current. Although we can create the PWM signal
using software, however, it will add load to the processor. In other words, when we create the PWM
signal in software, the CPU will use all the CPU time for PWM, hence, it is difficult to use the same
processor to do other job. Hence, most of the latest microcontroller has built-in PWM (hardware).
PWM control is used in a variety of applications, ranging from telecommunications to robotic
control. Figure 3.3 illustrates a PWM signal, where the period of the PWM is measured from a
pulse’s positive edge to the next positive edge.

Figure 6.3: PWM signal

The period is normally kept constant, and the pulse width, or ‘on’ time, is varied, hence the name.
The duty cycle is the proportion of time that the pulse is ‘on’ or ‘high’, and is expressed as a
percentage given by

"𝑜𝑛" 𝑡𝑖𝑚𝑒
𝐷𝑢𝑡𝑦 𝑐𝑦𝑐𝑙𝑒 = × 100%
𝑃𝑒𝑟𝑖𝑜𝑑

The LPC1678 has six PWM outputs called PWM1.1 to PWM1.6. Please refer to the LPC1768 user
manual for the register associated with PWM in LPC1768 microcontroller.

Lab Activity 6.3: PWM initialization in LPC1768 microcontroller

1. To generate PWM, first the PWM pin must be selected using the PINSEL register

LPC_PINCON → PINSEL4 |= (1<<0) //select PWM1.1 on P2.0

2. Enable the PWM output and select the PWM mode whether single edge or double edge
mode. By default, it is a single edge mode.

LPC_PWM1 → PCR = (1<<9); //PWM1 output enabled, single edge mode

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 10
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

3. Determine the prescaler value that need to put in the PR register. The prescaler value will
determine the time resolution for the PWM counter, just like the timer counter. The time
resolution is given by

𝑃𝑅 + 1
𝑇𝑅𝐸𝑆 =
𝑃𝐶𝐿𝐾

Which by default, the PCLK = CLCK/4 = 25 MHz.

For example, if we wish to get 1 s, therefore PR = TRES × PCLK – 1 = 24. Hence, the code

LPC_PWM1 → PR = 24; //Prescaler for 1us resolution

4. Reset the MR0 when it value matches the value with the value in the TC.

LPC_PWM1 → MCR |= (1<<1); //Reset on PWM MR0, reset TC if it matches


//MR0

5. Now, we need to set the period and duty cycle of the PWM signal. The PWM1.1 in the
LPC1768 is created as illustrated in Figure 6.4.

Figure 6.4: PWM1.1 signal generated by the LPC1768

Therefore, to create PWM signal with 10 ms period and 70% duty cycle at PWM1.1 the
values in MR0 and MR1 are set as follows:

LPC_PWM1 → MR0 = 10000; //set PWM period

LPC_PWM1 → MR1 = 7000; //Set 70% Duty Cycle

6. Now, the value in MR0 and MR1 need to be latched using the LER register by

LPC_PWM1 → LER = (1<<1) | (1<<0); //update values in MR1 and MR0

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 11
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

#include <LPC17xx.h>

void Init_Wait(void);
void wait_ms(int t);

int main(void)
{
Init_Wait();
LPC_PINCON->PINSEL4 |= (1<<0); //Set pin function to PWM1.1, P2.0
LPC_PWM1->PCR = (1<<9); //PWM1 output enabled
LPC_PWM1->PR = 24;//Prescaler 1us resolution
LPC_PWM1->MCR |= (1<<1); //Reset on PWM MR0 & TC if it matches MR0
LPC_PWM1->MR0 = 10000; //set PWM cycle to 10 ms
LPC_PWM1->MR1 = 1000; //Set 10% Duty Cycle
LPC_PWM1->LER = (1<<1) | (1<<0); //latch values in MR1 and MR0
LPC_PWM1->TCR = (1<<0) | (1<<3); //enable counters and PWM Mode

unsigned int i;

while(1)
{
for(i=1000;i<10000;i=i+500)
{
LPC_PWM1->MR1 = i;
LPC_PWM1->LER = (1<<1); //latch value in MR1
wait_ms(10);
}
for(i=9500;i>1000;i=i-500)
{
LPC_PWM1->MR1 = i;
LPC_PWM1->LER = (1<<1); //latch value in MR1
wait_ms(10);
}
}
}
void Init_Wait(void)
{
LPC_TIM0->CTCR = 0; //timer mode
LPC_TIM0->PR = 24999; //for 1ms resolution
}
void wait_ms(int t)
{
LPC_TIM0->TCR = 1<<1; //reset timer counter
LPC_TIM0->TCR = 1; //enable timer counter
while (LPC_TIM0->TC < t); //wait if conter < t
}

Listing 6.3: PWM program sample

FKEE, Sem01 Session 2021/22


Electronic Engineering Laboratory IV (BEE31101) 12
Lab 6: Interrupt, Timer and PWM in ARM Cortex-M3 Microcontroller (LPC1768)

7. Finally enable the counter and PWM mode by

LPC_PWM1->TCR = (1<<0) | (1<<3); //enable counters and PWM Mode

8. Listing 6.3 shows the program to vary the PWM duty cycle from 10% to 95% and from 95%
to 10% continuously. Build the program and run the debugging session. Observe the output
of the PWM on the Logic Analyzer.

Lab Assignment 6.3:

Create PWM signals with different duty cycles on PWM1.1, PWM1.2 and PWM1.3. i.e., PWM1.1
= 20% duty cycle, PWM1.2 = 50% duty cycle, and PWM1.3 = 80% duty cycle. Observe the outputs
on the Logic Analyzer.

Lab Assignment 6.4:

Create a PWM signal that uses an analog signal on AIN0 to vary the PWM duty cycle where 0 V
will produce 0% duty cycle and 3 V will produce 100% duty cycle.

Questions

6.1 By referring to the “startup_PLC17xx.s” file, give the interrupt number for Timer0 to
Timer3, and GPIO, respectively.
(4 marks)

6.2 Explain why in the interrupt handler, the interrupt must be cleared?
(4 marks)

6.3 Determine the value in the prescale register (PR) if the timer resolution of 5 ms is
required.
(4 marks)

6.4 Explain the function of the prescale counter (PC) and its relation to the PR.
(3 marks)

FKEE, Sem01 Session 2021/22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy