User manual

LPCXpresso Experiment Kit - User’s Guide
Page 90
Copyright 2013 © Embedded Artists AB
7.13.2 Lab 12b: Timer IRQ
In this experiment an interrupt will be generated from a timer. In Lab 1c: Delay Function LED
Flashing a simple for-loop was used to create exact a delay function. Recreate the experiment and
flash with a LED. Start with a fixed flash pattern; say 5 Hz. Keep the breadboard setup from the
previous experiment (see Figure 13, page 39). This experiment is also an extension to Lab 9a: Create
Exact Delay Function, where a 32-bit timer was user to create exact delay functions.
The code below illustrated a suitable framework to start from.
/* Define Interrupt Service Routine for 32-bit timer #1 */
void TIMER32_1_IRQHandler(void) //name of function is predefined
{
/* toggle LED on PIO0_2 and clear timer interrupt before exiting ISR */
...
}
/*****************************************************************************
** Function name: main
** Descriptions: The main function
** Parameters: None
** Returned value: None
**
*****************************************************************************/
void main (void)
{
/* initialize GPIO as needed */
...
/* setup 32-bit timer #1 to generate continuous interrupts every 200 ms = 5 Hz */
...
/* enable 32-bit timer #1 interrupt */
NVIC_EnableIRQ(TIMER_32_1_IRQn);
/* enter forever loop let interrupt handle processing */
while(1)
;
}
Now, expand the functionality of the program and design a program that flash with the LED 50 ms
(milli seconds) on, 150 ms off, 50 ms on and finally and 750 ms off. Continuously repeat this 1000 ms
cycle. The suggested program structure is to set the timer interrupt rate high, for example 1000 Hz.
That is 1 ms between every interrupt. Check which state the LED should have inside the timer ISR.
/* Define Interrupt Service Routine for 32-bit timer #1 */
void TIMER32_1_IRQHandler (void) //name of function is predefined
{
/* increment millisecond counter */
msCnt++;
/* keep counter at one second resolution */
if (msCnt >= 1000)
msCnt = 0;
/* set LED state based on millisecond counter */
if (...)
{
/* set LED */
...
}
else if (...)
{
/* set LED */
...
}
//etc
...
//clear timer interrupt
}