Looking up interrupts from MaxBotix Maxsonar MB73x0 ultrasonic gauge and writing data and timestamp to SD flash card. I'm using MB7380 with STM32F103C8T6 minimal board from Ali (it's perfect MCU board for small projects!).

Dependencies:   SDFileSystem mbed

Revision:
1:5c9bde92518d
Parent:
0:2ce0698a332d
Child:
2:a5ea1929e545
--- a/main.cpp	Mon Mar 14 21:26:39 2016 +0000
+++ b/main.cpp	Tue Mar 15 12:09:16 2016 +0000
@@ -1,11 +1,14 @@
 #include "mbed.h"
+#include "SDFileSystem.h"
+
+SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd");  //mosi, miso, sck, cs
 
 Serial pc(PA_2, PA_3);               // This is USB tx, rx
 Serial sensor_uart(PA_9, PA_10);        // This is USART2 tx, rx
 
-DigitalOut myled(PB_5);             // This is USER LED
-DigitalOut myled2(PA_7);             // This is USER LED
-DigitalIn BlueButton(PB_9);   // This is Blue-Button
+DigitalOut myled(PC_13);             // This is USER LED
+//DigitalOut myled2(PA_7);             // This is USER LED
+DigitalIn Button(PB_12);   // This is Blue-Button
 
 #define Pressed 0
 #define NotPressed 1
@@ -15,7 +18,10 @@
 int n=0;
 int readgauge = 0;
 Timer t;
-char str[5];
+char buf[32];
+char str[32];
+char str2[32];
+uint8_t writeflag = 0;
 long millis = 0;
 
 void PC_INT()  // USART2 used for Debug
@@ -27,20 +33,11 @@
 
 void SENSOR_USART_INT()  // SENSOR_USART
 {
-    // Note: you need to actually read from the serial to clear the RX interrupt
-    //myled = 1;
-    //Serial3.scanf(str);
-    //sensor_uart.scanf("%5s", str);
-    //str = strcat ("   PISKA   ", str);
-    //sprintf(millis, "%d", t.read_ms());
-    //int k = 0;
-    //for (k = 0; k < 10; k++){;}
-    //str = strcat (millis, str);
-    //pc.printf(str);
-    //pc.printf(millis);
     if (sensor_uart.getc() == 'R') {
         myled = (myled)?(0):(1);
-        pc.printf("timestamp: %d; gauge value: %s\r\n", t.read_ms(), sensor_uart.gets(str, 5));
+        sprintf(str, "%d; %s;", t.read_ms(), sensor_uart.gets(buf, 5));
+        writeflag = 1;
+        //pc.printf("%s\r\n", str);
     }
 }
 
@@ -49,21 +46,31 @@
     myled = 0;
 
     // SetUp the baud rate
-    pc.baud(9600);
+    pc.baud(115200);
     sensor_uart.baud(9600);
 
+    mkdir("/sd/ultrasonic", 0777);
+
+    FILE *fp = fopen("/sd/ultrasonic/test.csv", "w");
+    if(fp == NULL) {
+        pc.printf("Could not open file for write\n");
+    }
+    fprintf(fp, "timestamp;value;");
+    //fclose(fp);
+
+    pc.printf("\n\r\n\r|||||START MAIN|||||\n\r");
+    t.start();
     sensor_uart.attach(&SENSOR_USART_INT);
     pc.attach(&PC_INT);
 
-    t.start();
-
-    pc.printf("\n\r\n\r|||||START MAIN|||||\n\r");
-
     while(1) {
-        // Test the Blue Button
-        while(BlueButton == Pressed) {
-            myled2 = BlueButton;
-            pc.printf("Please release the BLUE Button\n\r");
+        if (Button)
+            fclose(fp);
+        if (writeflag) {
+            memcpy(str2, str, 32);
+            fprintf(fp, "%s\n", str2);
+            writeflag = 0;
+            fprintf(pc, "%s\r\n", str2);
         }
     }
 }
\ No newline at end of file