NUCLEO-F042K6 Simple demo writing Timer time to UART periodically using Ticker
Dependencies: mbed
main.cpp@4:fd43e0e4fe7d, 2018-02-10 (annotated)
- Committer:
- vodsejak
- Date:
- Sat Feb 10 16:00:26 2018 +0000
- Revision:
- 4:fd43e0e4fe7d
- Parent:
- 3:906776a3481e
v2.3;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vodsejak | 0:a1bcdb6060ae | 1 | #include "mbed.h" |
vodsejak | 0:a1bcdb6060ae | 2 | |
vodsejak | 0:a1bcdb6060ae | 3 | /******************************************************************************* |
vodsejak | 0:a1bcdb6060ae | 4 | |
vodsejak | 0:a1bcdb6060ae | 5 | EXAMPLE DESCRIPTION |
vodsejak | 0:a1bcdb6060ae | 6 | |
vodsejak | 0:a1bcdb6060ae | 7 | Initialize Timer and Ticker. Timer is started and Ticker then calls funtion |
vodsejak | 0:a1bcdb6060ae | 8 | that prints timer value to UART. |
vodsejak | 0:a1bcdb6060ae | 9 | |
vodsejak | 0:a1bcdb6060ae | 10 | *******************************************************************************/ |
vodsejak | 0:a1bcdb6060ae | 11 | |
vodsejak | 0:a1bcdb6060ae | 12 | Timer tim; // Timer definition |
vodsejak | 0:a1bcdb6060ae | 13 | Ticker tick; // Ticker definition |
vodsejak | 0:a1bcdb6060ae | 14 | |
vodsejak | 0:a1bcdb6060ae | 15 | // returns Timer value |
vodsejak | 0:a1bcdb6060ae | 16 | int getTimerVal(Timer *tim) { |
vodsejak | 1:3b8cd8e8ce53 | 17 | int time = tim->read_us(); |
vodsejak | 2:e80bdfa0212c | 18 | // reset when overflow |
vodsejak | 1:3b8cd8e8ce53 | 19 | if (time<0){ |
vodsejak | 1:3b8cd8e8ce53 | 20 | tim->reset(); |
vodsejak | 1:3b8cd8e8ce53 | 21 | time = tim->read_us(); |
vodsejak | 1:3b8cd8e8ce53 | 22 | } |
vodsejak | 4:fd43e0e4fe7d | 23 | |
vodsejak | 3:906776a3481e | 24 | return time; |
vodsejak | 0:a1bcdb6060ae | 25 | } |
vodsejak | 0:a1bcdb6060ae | 26 | |
vodsejak | 0:a1bcdb6060ae | 27 | // prints Timer value to UART (default UART->USB) Bd 9600, no parity, 1 stopbit |
vodsejak | 0:a1bcdb6060ae | 28 | void printTimerVal(){ |
vodsejak | 0:a1bcdb6060ae | 29 | printf("Timer is running %i us.\n", getTimerVal(&tim)); |
vodsejak | 0:a1bcdb6060ae | 30 | } |
vodsejak | 0:a1bcdb6060ae | 31 | |
vodsejak | 0:a1bcdb6060ae | 32 | int main() |
vodsejak | 0:a1bcdb6060ae | 33 | { |
vodsejak | 0:a1bcdb6060ae | 34 | tim.start(); // start Timer |
vodsejak | 0:a1bcdb6060ae | 35 | tick.attach(&printTimerVal, 0.1); // Init the ticker with the address of the |
vodsejak | 0:a1bcdb6060ae | 36 | // function to be attached and |
vodsejak | 0:a1bcdb6060ae | 37 | // the interval (100 ms) |
vodsejak | 0:a1bcdb6060ae | 38 | while (true) { |
vodsejak | 0:a1bcdb6060ae | 39 | // main programm loop - can do other things |
vodsejak | 0:a1bcdb6060ae | 40 | } |
vodsejak | 0:a1bcdb6060ae | 41 | } |