User manual

LPCXpresso Experiment Kit - User’s Guide
Page 92
Copyright 2013 © Embedded Artists AB
/*****************************************************************************
** Function name: main
** Descriptions: The main function
** Parameters: None
** Returned value: None
**
*****************************************************************************/
void main (void)
{
/* initialize GPIO as needed */
...
/* enter forever loop let interrupt handle processing */
while(1)
{
/* register callback (to toggle LED) in 200ms */
registerCbAndDelay(200, &toggleLED);
/* wait until callback has been called */
while (pCB != NULL)
;
}
}
Place the callback related functions in file timerCB.c.
The structure above is not perfect since the caller must wait until the callback has been executed
before the next callback can be registered and started. To make it more user friendly, extend the
callback timer functionality to also support repeated calls. When registering a new callback one
parameter/flag tells if it is a one-time callback or a repeated callback. A new function is then also
needed to stop a repeated callback. It would be good programming practice to let the function return
an error if there is already an active callback in the system.
A much more flexible and robust framework would allow multiple callbacks to be registered and
handled accordingly. Such a framework is however a lot more work and out of the scope for this
experiment.
7.13.4 Lab 12d: Nested Interrupts
In this experiment the effect of nested interrupts will be investigated. This experiment is a little
combination of Lab 12a and Lab 12b. Use the same breadboard setup as in these experiments, one
LED and one push-button.
Setup a repeated timer interrupt to toggle the LED with 5Hz rate. Whenever the push-button is pressed
enter a 2 second delay loop (of the old type) in the interrupt service routine. First observe the LED
flashing.
Press the push-button. What happens? _______________________________________________
Yes, the LED will stop toggle for 2 seconds whenever the push-button is pressed. It is exactly what can
be expected since the push-button (port #1) ISR will block the timer interrupt. This is an excellent
illustration that time spent in an ISR should be kept to a minimum in order not to block other ISR:s from
being executed.
Now explicitly set the priority of both ISR:s. Set the priority of the timer ISR higher than for the GPIO
ISR. Remember that a lower number (range is 0-3) means higher priority.
Verify that the LED now continues flashing whenever the push-button is pressed.
What is the default priority for all interrupts? __________________________
There is a function call for reading the priority also. Search in the file core_cm0.h after this
function.