
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
Diff: main.cpp
- Revision:
- 6:d1435e38c1b4
- Parent:
- 5:ff77acf7d21d
- Child:
- 7:05f4f9d3c168
--- a/main.cpp Sun Dec 13 06:25:45 2015 +0000 +++ b/main.cpp Sun Dec 13 10:43:17 2015 +0000 @@ -12,6 +12,19 @@ static uint32_t uiCnt=0; static DigitalOut myled(LED1); + +static DigitalOut trigDist(PB_9); +static DigitalIn echoDist(PA_6); + +static Timer timer; + +static struct { + uint32_t timerTicks; + float distance; +} dist_data = { 10000, 10.0f }; + + + static void initDS1620Temp() { ds1620Sensor.setSerialClockFrequency(freq500k); if ((ds1620Sensor.readConfig() & 0x03) != 0x03) { @@ -37,7 +50,7 @@ void disp_thread(void const *args) { while (1) { lcd.cls(); - lcd.printf("Raw: %u", temp_raw); + lcd.printf("Raw:%3u % 5.0f mm", temp_raw, dist_data.distance); uiCnt += 2; lcd.locate(0, 1); lcd.printf("Float: %3.2f%cC", temperature, 0xdf); @@ -46,6 +59,46 @@ } } +void dist_thread(void const *args) { + + while (1) { + // Dej puls na trig + trigDist = 1; + Thread::wait(1); + trigDist = 0; + + if (echoDist != 0) { + dist_data.timerTicks = 0; + dist_data.distance = 0.0f; + } else { + timer.stop(); + timer.reset(); + + // Cekej na hranu na Echo + while (echoDist != 1) + ; + + // Zacni merit cas + timer.start(); + + // Cekej na hranu na Echo + while (echoDist != 0) + ; + + // Zastav mereni + timer.stop(); + + dist_data.timerTicks = timer.read_us(); + + // 340 ms-1 + dist_data.distance = (timer.read()/2.0f - 0.0f)*340.0f*1000.0f; + } + + Thread::wait(500); + } +} + + int main() { Thread::wait(1000); @@ -54,6 +107,7 @@ Thread dispThread(disp_thread, NULL, osPriorityNormal); Thread tempThread(temp_thread, NULL, osPriorityNormal); + Thread distThread(dist_thread, NULL, osPriorityNormal); Thread::wait(osWaitForever); }