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

Files at this revision

API Documentation at this revision

Comitter:
dzoni
Date:
Sun Dec 13 11:12:19 2015 +0000
Parent:
6:d1435e38c1b4
Child:
8:812c2b49251f
Commit message:
Release 2.0 (RTOS + rewritten to separate tasks). Remaining:; - split tasks to separate source files; - program FSM(s) tisked by RTOS timer instead of Thread::wait() in tasks

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Dec 13 10:43:17 2015 +0000
+++ b/main.cpp	Sun Dec 13 11:12:19 2015 +0000
@@ -3,15 +3,14 @@
 #include "DS1620.h"
 #include "TextLCD.h"
 
-TextLCD lcd(PA_8, PA_7, PA_9, PA_1, PB_5, PA_10, TextLCD::LCD16x2);
+static TextLCD lcd(PA_8, PA_7, PA_9, PA_1, PB_5, PA_10, TextLCD::LCD16x2);
     
-DS1620 ds1620Sensor(PB_4, PB_10, PB_3);
+static  DS1620 ds1620Sensor(PB_4, PB_10, PB_3);
 
-static float temperature = 0.0f;
-static uint32_t temp_raw = 0;
 static uint32_t uiCnt=0;
-static DigitalOut myled(LED1);
 
+static DigitalOut  led2(LED2);
+static DigitalIn   button1(USER_BUTTON);
 
 static DigitalOut  trigDist(PB_9);
 static DigitalIn   echoDist(PA_6);
@@ -23,6 +22,16 @@
     float distance;
 } dist_data = { 10000, 10.0f };
 
+static struct {
+    uint32_t    temp_raw;
+    float       temperature;
+} temp_data = { 0, 10.0f };
+
+static struct {
+    uint32_t on_time;
+    uint32_t off_time;
+} flash_times = { 100, 2500 };
+
 
 
 static void initDS1620Temp() {
@@ -32,9 +41,36 @@
     }
 }
 
+void led2_thread(void const *args) {
+
+    while (true) {
+        led2 = 1;
+        Thread::wait(flash_times.on_time);
+                 
+        led2 = 0;
+        Thread::wait(flash_times.off_time);
+    }
+}
+
+void ubutton_thread(void const *args) {
+    static uint32_t last_state = button1;
+    uint32_t temp;
+            
+    while (true) {
+        if (button1 != last_state) {
+            temp = flash_times.on_time;
+            flash_times.on_time = flash_times.off_time;
+            flash_times.off_time = temp;
+        }
+        
+        Thread::wait(250);
+    }
+}
+
+
 void temp_thread(void const *args) {
-    while (1) {
-        myled = myled ^ 1; 
+
+    while (true) {
         ds1620Sensor.startConversion();
     
         // Wait for conversion completion (Tconv = 750 ms typ)
@@ -42,18 +78,19 @@
         while (!(ds1620Sensor.readConfig() & 0x80))
             Thread::wait(10);
                 
-        temperature = ds1620Sensor.getHighResolutionTemperature();
-        temp_raw = ds1620Sensor.readTemperatureRaw();
+        temp_data.temperature = ds1620Sensor.getHighResolutionTemperature();
+        temp_data.temp_raw = ds1620Sensor.readTemperatureRaw();
     }
 }
 
 void disp_thread(void const *args) {
-    while (1) {
+
+    while (true) {
         lcd.cls();
-        lcd.printf("Raw:%3u % 5.0f mm", temp_raw, dist_data.distance);
+        lcd.printf("Raw:%3u % 5.0f mm", temp_data.temp_raw, dist_data.distance);
         uiCnt += 2;
         lcd.locate(0, 1);    
-        lcd.printf("Float: %3.2f%cC", temperature, 0xdf);
+        lcd.printf("Float: %3.2f%cC", temp_data.temperature, 0xdf);
         
         Thread::wait(1900);
     }
@@ -61,7 +98,7 @@
 
 void dist_thread(void const *args) {
     
-    while (1) {    
+    while (true) {    
         // Dej puls na trig
         trigDist = 1;
         Thread::wait(1);
@@ -105,9 +142,11 @@
 
     initDS1620Temp();
     
-    Thread dispThread(disp_thread, NULL, osPriorityNormal);
-    Thread tempThread(temp_thread, NULL, osPriorityNormal);
-    Thread distThread(dist_thread, NULL, osPriorityNormal);
+    Thread dispThread(disp_thread, NULL, osPriorityLow);
+    Thread tempThread(temp_thread, NULL, osPriorityLow);
+    Thread distThread(dist_thread, NULL, osPriorityRealtime);
+    Thread led2Thread(led2_thread, NULL, osPriorityNormal);
+    Thread ubuttonThread(ubutton_thread, NULL, osPriorityAboveNormal);
 
     Thread::wait(osWaitForever);
 }