Prints "Hello World!" every 5 seconds, and blinks LED1 at a defined hertz.
Dependencies: mbed
main.cpp@0:543eb4b01cda, 2014-10-14 (annotated)
- Committer:
- Vanger
- Date:
- Tue Oct 14 18:07:09 2014 +0000
- Revision:
- 0:543eb4b01cda
- Child:
- 1:f09b0fb6022a
Created hello world program (Nucleo_blink_led) to demonstrate how to compile and run some test code on the nucleo board.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vanger | 0:543eb4b01cda | 1 | #include "mbed.h" |
Vanger | 0:543eb4b01cda | 2 | |
Vanger | 0:543eb4b01cda | 3 | // A simple and concise explanation of what this program does |
Vanger | 0:543eb4b01cda | 4 | /* Simple Hello World program that outputs "Hello World!" every |
Vanger | 0:543eb4b01cda | 5 | five seconds to the serial debug port, and blinks at the user |
Vanger | 0:543eb4b01cda | 6 | defined hertz |
Vanger | 0:543eb4b01cda | 7 | */ |
Vanger | 0:543eb4b01cda | 8 | |
Vanger | 0:543eb4b01cda | 9 | // LED blink rate: higher -> faster blinking |
Vanger | 0:543eb4b01cda | 10 | #define LED_BLINK_RATE 4 //Hertz |
Vanger | 0:543eb4b01cda | 11 | |
Vanger | 0:543eb4b01cda | 12 | //Define the LED pin output |
Vanger | 0:543eb4b01cda | 13 | DigitalOut led(LED1); |
Vanger | 0:543eb4b01cda | 14 | //Define timers |
Vanger | 0:543eb4b01cda | 15 | Timer print_timer; |
Vanger | 0:543eb4b01cda | 16 | Timer led_timer; |
Vanger | 0:543eb4b01cda | 17 | |
Vanger | 0:543eb4b01cda | 18 | int main() { |
Vanger | 0:543eb4b01cda | 19 | led = 0; //Initialize LED off |
Vanger | 0:543eb4b01cda | 20 | print_timer.start(); //Start timers, will count until stopped |
Vanger | 0:543eb4b01cda | 21 | led_timer.start(); |
Vanger | 0:543eb4b01cda | 22 | |
Vanger | 0:543eb4b01cda | 23 | while (1) { |
Vanger | 0:543eb4b01cda | 24 | if (print_timer.read() >= 5) { //print_timer.read() returns time in seconds |
Vanger | 0:543eb4b01cda | 25 | printf("Hello World!\n"); |
Vanger | 0:543eb4b01cda | 26 | print_timer.reset(); //Resets timer count to 0 |
Vanger | 0:543eb4b01cda | 27 | } |
Vanger | 0:543eb4b01cda | 28 | |
Vanger | 0:543eb4b01cda | 29 | //Calculates interval needed for specified frequency |
Vanger | 0:543eb4b01cda | 30 | if ( led_timer.read_ms() >= (1000.0/(2*LED_BLINK_RATE))) { |
Vanger | 0:543eb4b01cda | 31 | led = !led; //Invert LED output |
Vanger | 0:543eb4b01cda | 32 | led_timer.reset(); //Resets timer count to 0 |
Vanger | 0:543eb4b01cda | 33 | } |
Vanger | 0:543eb4b01cda | 34 | } |
Vanger | 0:543eb4b01cda | 35 | } |