consider a push button being connected to p16 on the mbed bo
consider a push button being connected to p16 on the mbed board so that p16 will be set to 1 when the push button is pushed down and 0 when the push button is released. Write a program to receive the push down of the push button as an interrupt event, and to respond to it with the starting of a ticker to flash the LED4 on the mbed board to flash every 10 seconds (on for 5 seconds and off for 5 seconds). Please do not use any wait(), wait_ms() or wait_us() functions.
The following are the API information:
InterruptIn API:
InterruptIn (PinName pin)
Create an InterruptIn connected to the specified pin.
void rise (void(*fptr)(void))
Attach a function to call when a rising edge occurs on the input.
void fall (void(*fptr)(void))
Attach a function to call when a falling edge occurs on the input.
void mode (PinMode pull)
Set the input pin mode.
----------------------------------------------------------------------------
Timer API
void start ()
Start the timer.
void stop ()
Stop the timer.
void reset ()
Reset the timer to 0.
float read ()
Get the time passed in seconds.
int read_ms ()
Get the time passed in mili-seconds.
int read_us ()
Get the time passed in micro-seconds.
--------------------------------------------------------------------------
Timeout API
void attach (void(*fptr)(void), float t)
Attach a function to be called by the Ticker , specifiying the interval in seconds.
void attach_us (void(*fptr)(void), timestamp_t t)
Attach a function to be called by the Ticker , specifiying the interval in micro-seconds.
void detach ()
Detach the function.
--------------------------------------------------------------------------
Ticker API
void attach (void(*fptr)(void), float t)
Attach a function to be called by the Ticker , specifiying the interval in seconds.
void attach_us (void(*fptr)(void), timestamp_t t)
Attach a function to be called by the Ticker , specifiying the interval in micro-seconds.
void detach ()
Detach the function.
Solution
#include \"mbed.h\"
InterruptIn button(P1_16); //interrupt pin
Ticker flicker;//ticker function init
DigitalOut flash(LED4); //led 4 as OUTPUT
void blink()//timer isr
{
flash=!flash;//led state change
}
//interrupt isr
void flip() {
flicker.attached (&blink,5);//timer for 5 sec
}
int main() { button.rise(&flip); // attach the address of the flip function to the rising edge while(1) { // wait around, interrupts will interrupt this!
}
}

