Download Note - Engenuics
Transcript
MPG LEVEL 1
Check out the code in utilities.c to understand the usage of the function – it will likely come in handy in
some future applications you write. The button application that will be written in the next section will
make use of IsTimeUp() to help with button debouncing.
9.5 Button Application
All of the services are ready to let us build a button application that can be used for the remainder of the
course firmware. Just like the LED API that was defined in Chapter 8, we will write a button API and the
associated functions to provide key services for the buttons. The button app will be a bit more involved
than the LED application, and will make use of many of the concepts and tools you have learned in the
last two chapters.
If you are wondering what sort of services are required beyond what the button interrupts provide, take
a moment to consider what additional features would be handy.
1. Debouncing – how are you going to time out a debounce period and verify that a button has
been pressed or released?
2. Button held – the ISR tells you when a button is first pressed, but what if you need to determine
if the button is being held down? What if you have special (secret?) functionality that only
works if a button is held for 10 seconds or some other period of time?
3. Button history – if you have a long state in your program or just a really fast button press, it
would be nice to be able to check if a button had been pressed even if it is no longer pressed.
While you can manage a lot of those features directly with flag bits, timers and various polling
techniques, having an application running that automatically handles all of that is infinitely beneficial
(try writing code for a project where you are managing the buttons manually and you will quickly see!).
It would also be nice to write a fairly generic and modular button application that could be ported to
other systems in the future.
9.5.1 ButtonConfigType
Parameters of buttons are very much like parameters of LEDs. Buttons are at various GPIO locations and
could be either active high or active low. For now we will assume that they are all on the same port,
though they could be at any bit location. It is also assumed that all buttons are interrupt sources. At
any given time, a button could be pressed or released, and we want to be able to time how long a
button is held for. So to start off, we will create a structure type in buttons.h that will hold all of the
information necessary for a particular button.
typedef struct
{
u32 u32Location;
DeviceActiveType eActiveState;
u32 u32IntFlagLocation;
ButtonStateType eCurrentState;
ButtonStateType eNewState;
u32 u32HoldTimeStart;
} ButtonConfigType;
notes_mpgl1_chapter9.docx
/*
/*
/*
/*
/*
/*
Bit location of the button on Port 0 */
Active-high or active-low */
Bit location of the button's interrupt flag */
Current state (RELEASED/PRESSED) of the button */
New state of the button after a debounce period */
Time stamp of the button press */
Release 1.0
Page 18 of 23