Lab 2 - Interrupts and timer (complete)

Dependencies:   mbed

Fork of FTF2014_lab2_complete by Freescale

Committer:
Kojto
Date:
Mon Apr 07 20:17:34 2014 +0000
Revision:
2:b8ed254b5227
Parent:
0:3561cbaefc3d
mbed-src replaced by mbed lib v82

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:3561cbaefc3d 1 #include "mbed.h"
Kojto 0:3561cbaefc3d 2
Kojto 0:3561cbaefc3d 3 DigitalOut led_red(LED_RED);
Kojto 0:3561cbaefc3d 4 InterruptIn sw2(SW2);
Kojto 0:3561cbaefc3d 5 Ticker ticker;
Kojto 0:3561cbaefc3d 6 float interval;
Kojto 0:3561cbaefc3d 7
Kojto 0:3561cbaefc3d 8 void led_toggle(void)
Kojto 0:3561cbaefc3d 9 {
Kojto 0:3561cbaefc3d 10 led_red = !led_red;
Kojto 0:3561cbaefc3d 11 }
Kojto 0:3561cbaefc3d 12
Kojto 0:3561cbaefc3d 13 void sw2_release(void)
Kojto 0:3561cbaefc3d 14 {
Kojto 0:3561cbaefc3d 15 printf("On-board button SW2 was released.\n");
Kojto 0:3561cbaefc3d 16 }
Kojto 0:3561cbaefc3d 17
Kojto 0:3561cbaefc3d 18 void sw2_press(void)
Kojto 0:3561cbaefc3d 19 {
Kojto 0:3561cbaefc3d 20 printf("On-board button SW2 was pressed.\n");
Kojto 0:3561cbaefc3d 21 interval -= 0.1f;
Kojto 0:3561cbaefc3d 22 ticker.detach();
Kojto 0:3561cbaefc3d 23 ticker.attach(&led_toggle, interval);
Kojto 0:3561cbaefc3d 24 printf("LED toogle interval was set to: %f \n", interval);
Kojto 0:3561cbaefc3d 25 if (interval < 0.5f) {
Kojto 0:3561cbaefc3d 26 interval = 1.1f;
Kojto 0:3561cbaefc3d 27 }
Kojto 0:3561cbaefc3d 28 }
Kojto 0:3561cbaefc3d 29
Kojto 0:3561cbaefc3d 30 int main()
Kojto 0:3561cbaefc3d 31 {
Kojto 0:3561cbaefc3d 32 interval = 1.0f;
Kojto 0:3561cbaefc3d 33 sw2.rise(&sw2_release);
Kojto 0:3561cbaefc3d 34 sw2.fall(&sw2_press);
Kojto 0:3561cbaefc3d 35 ticker.attach(&led_toggle, interval);
Kojto 0:3561cbaefc3d 36 while (true) {
Kojto 0:3561cbaefc3d 37 }
Kojto 0:3561cbaefc3d 38 }