Simple test program to get familiar with functionality of MBED RTOS on ST Nucleo-F411RE. Tasks for LED blinking, user button, temperature measurement with DS1620, temperature measurement with internal temperature sensor of ST32F411RE, ultrasonic distance measurement and displaying result on 16x2 TextLCD.
Dependencies: DS1620_improved TextLCD_improved mbed-rtos mbed
tsk_dist.cpp@17:94c385ff2641, 2015-12-18 (annotated)
- Committer:
- dzoni
- Date:
- Fri Dec 18 15:23:21 2015 +0000
- Revision:
- 17:94c385ff2641
- Parent:
- 15:a627638edd9c
- Child:
- 18:be0130c42925
Bugfixes. Works but freezes after some time. Button task requires rewrite (InterruptIn).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dzoni | 9:645f0e517017 | 1 | #include "mbed.h" |
dzoni | 9:645f0e517017 | 2 | #include "rtos.h" |
dzoni | 9:645f0e517017 | 3 | |
dzoni | 17:94c385ff2641 | 4 | #include "tsk_main.h" |
dzoni | 9:645f0e517017 | 5 | #include "tsk_dist.h" |
dzoni | 9:645f0e517017 | 6 | |
dzoni | 9:645f0e517017 | 7 | |
dzoni | 17:94c385ff2641 | 8 | struct dist_data_struct dist_data; |
dzoni | 9:645f0e517017 | 9 | |
dzoni | 9:645f0e517017 | 10 | static DigitalOut trigDist(PB_9); |
dzoni | 15:a627638edd9c | 11 | static InterruptIn echoDist(PA_6); |
dzoni | 9:645f0e517017 | 12 | |
dzoni | 17:94c385ff2641 | 13 | static Timer timer; |
dzoni | 17:94c385ff2641 | 14 | |
dzoni | 17:94c385ff2641 | 15 | uint32_t initDist(void const *args) { |
dzoni | 17:94c385ff2641 | 16 | |
dzoni | 17:94c385ff2641 | 17 | dist_data.distance = 0.0f; |
dzoni | 17:94c385ff2641 | 18 | dist_data.timerTicks = 0; |
dzoni | 17:94c385ff2641 | 19 | |
dzoni | 17:94c385ff2641 | 20 | trigDist = 0; |
dzoni | 17:94c385ff2641 | 21 | |
dzoni | 17:94c385ff2641 | 22 | return 1; |
dzoni | 17:94c385ff2641 | 23 | } |
dzoni | 9:645f0e517017 | 24 | |
dzoni | 15:a627638edd9c | 25 | static void rising(void) { |
dzoni | 15:a627638edd9c | 26 | timer.start(); |
dzoni | 15:a627638edd9c | 27 | } |
dzoni | 15:a627638edd9c | 28 | |
dzoni | 15:a627638edd9c | 29 | static void falling(void) { |
dzoni | 15:a627638edd9c | 30 | timer.stop(); |
dzoni | 15:a627638edd9c | 31 | echoDist.rise(0); |
dzoni | 15:a627638edd9c | 32 | echoDist.fall(0); |
dzoni | 15:a627638edd9c | 33 | |
dzoni | 17:94c385ff2641 | 34 | // mutexDist.lock(); |
dzoni | 15:a627638edd9c | 35 | dist_data.timerTicks = timer.read_us(); |
dzoni | 15:a627638edd9c | 36 | |
dzoni | 15:a627638edd9c | 37 | // 340 ms-1 |
dzoni | 15:a627638edd9c | 38 | dist_data.distance = (timer.read()/2.0f - 0.0f)*340.0f*1000.0f; |
dzoni | 17:94c385ff2641 | 39 | // mutexDist.unlock(); |
dzoni | 15:a627638edd9c | 40 | |
dzoni | 15:a627638edd9c | 41 | // Vynuluj timer |
dzoni | 15:a627638edd9c | 42 | timer.reset(); |
dzoni | 15:a627638edd9c | 43 | } |
dzoni | 9:645f0e517017 | 44 | |
dzoni | 9:645f0e517017 | 45 | void dist_thread(void const *args) { |
dzoni | 9:645f0e517017 | 46 | |
dzoni | 9:645f0e517017 | 47 | while (true) { |
dzoni | 9:645f0e517017 | 48 | // Dej puls na trig |
dzoni | 9:645f0e517017 | 49 | trigDist = 1; |
dzoni | 9:645f0e517017 | 50 | Thread::wait(1); |
dzoni | 9:645f0e517017 | 51 | trigDist = 0; |
dzoni | 9:645f0e517017 | 52 | |
dzoni | 9:645f0e517017 | 53 | if (echoDist != 0) { |
dzoni | 17:94c385ff2641 | 54 | mutexDist.lock(); |
dzoni | 9:645f0e517017 | 55 | dist_data.timerTicks = 0; |
dzoni | 9:645f0e517017 | 56 | dist_data.distance = 0.0f; |
dzoni | 17:94c385ff2641 | 57 | mutexDist.unlock(); |
dzoni | 9:645f0e517017 | 58 | } else { |
dzoni | 15:a627638edd9c | 59 | // Vynuluj timer |
dzoni | 9:645f0e517017 | 60 | timer.stop(); |
dzoni | 9:645f0e517017 | 61 | timer.reset(); |
dzoni | 9:645f0e517017 | 62 | |
dzoni | 15:a627638edd9c | 63 | // Cekej na hrany na Echo |
dzoni | 15:a627638edd9c | 64 | echoDist.rise(&rising); |
dzoni | 15:a627638edd9c | 65 | echoDist.fall(&falling); |
dzoni | 9:645f0e517017 | 66 | } |
dzoni | 9:645f0e517017 | 67 | |
dzoni | 17:94c385ff2641 | 68 | Thread::wait(1000); |
dzoni | 9:645f0e517017 | 69 | } |
dzoni | 9:645f0e517017 | 70 | } |