Simple test of two speed flashing LED toggled by button press with output to the serial monitor and monitoring time using the onboard RTC.
nucleoBlink.cpp@0:cde87f156401, 2014-03-04 (annotated)
- Committer:
- patthoyts
- Date:
- Tue Mar 04 13:18:29 2014 +0000
- Revision:
- 0:cde87f156401
Initial version testing programming the Nucleo F401 from Windows and Linux.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
patthoyts | 0:cde87f156401 | 1 | #include "mbed.h" |
patthoyts | 0:cde87f156401 | 2 | |
patthoyts | 0:cde87f156401 | 3 | DigitalOut led(LED1); |
patthoyts | 0:cde87f156401 | 4 | InterruptIn btn(USER_BUTTON); |
patthoyts | 0:cde87f156401 | 5 | Serial serial(SERIAL_TX, SERIAL_RX); |
patthoyts | 0:cde87f156401 | 6 | |
patthoyts | 0:cde87f156401 | 7 | enum {DELAY_SLOW = 500000, DELAY_FAST = 100000, DELAY_FLASH=50000 }; |
patthoyts | 0:cde87f156401 | 8 | long delay = DELAY_FAST; |
patthoyts | 0:cde87f156401 | 9 | bool pressed = false; |
patthoyts | 0:cde87f156401 | 10 | long counter = 0; |
patthoyts | 0:cde87f156401 | 11 | |
patthoyts | 0:cde87f156401 | 12 | static void flash() |
patthoyts | 0:cde87f156401 | 13 | { |
patthoyts | 0:cde87f156401 | 14 | for (int n = 0; n < 12; ++n) { |
patthoyts | 0:cde87f156401 | 15 | led = !led; |
patthoyts | 0:cde87f156401 | 16 | wait_us(DELAY_FLASH); |
patthoyts | 0:cde87f156401 | 17 | } |
patthoyts | 0:cde87f156401 | 18 | } |
patthoyts | 0:cde87f156401 | 19 | static void onButtonPress() |
patthoyts | 0:cde87f156401 | 20 | { |
patthoyts | 0:cde87f156401 | 21 | delay = (delay == DELAY_SLOW) ? DELAY_FAST : DELAY_SLOW; |
patthoyts | 0:cde87f156401 | 22 | pressed = true; |
patthoyts | 0:cde87f156401 | 23 | } |
patthoyts | 0:cde87f156401 | 24 | int |
patthoyts | 0:cde87f156401 | 25 | main() |
patthoyts | 0:cde87f156401 | 26 | { |
patthoyts | 0:cde87f156401 | 27 | serial.baud(115200); |
patthoyts | 0:cde87f156401 | 28 | btn.fall(&onButtonPress); |
patthoyts | 0:cde87f156401 | 29 | flash(); |
patthoyts | 0:cde87f156401 | 30 | set_time(1393936792); // set the onboard RTC |
patthoyts | 0:cde87f156401 | 31 | printf("MBED Nucleo Blink Test (v1.0)\n"); |
patthoyts | 0:cde87f156401 | 32 | while (true) |
patthoyts | 0:cde87f156401 | 33 | { |
patthoyts | 0:cde87f156401 | 34 | led = !led; |
patthoyts | 0:cde87f156401 | 35 | ++counter; |
patthoyts | 0:cde87f156401 | 36 | if ((counter % 10) == 0) { |
patthoyts | 0:cde87f156401 | 37 | time_t seconds = time(NULL); |
patthoyts | 0:cde87f156401 | 38 | printf("%d %d %s", counter, seconds, ctime(&seconds)); |
patthoyts | 0:cde87f156401 | 39 | } |
patthoyts | 0:cde87f156401 | 40 | if (pressed) { pressed = false; printf("# button pressed\n"); } |
patthoyts | 0:cde87f156401 | 41 | wait_us(delay); |
patthoyts | 0:cde87f156401 | 42 | } |
patthoyts | 0:cde87f156401 | 43 | } |
patthoyts | 0:cde87f156401 | 44 |