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

main.cpp

Committer:
shtirlitz
Date:
2016-03-15
Revision:
1:5c9bde92518d
Parent:
0:2ce0698a332d
Child:
2:a5ea1929e545

File content as of revision 1:5c9bde92518d:

#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(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

int USBByte='\0';
int GaugeByte='\0';
int n=0;
int readgauge = 0;
Timer t;
char buf[32];
char str[32];
char str2[32];
uint8_t writeflag = 0;
long millis = 0;

void PC_INT()  // USART2 used for Debug
{
    // Note: you need to actually read from the serial to clear the RX interrupt
    pc.putc(pc.getc());
    myled = (myled)?(0):(1);
}

void SENSOR_USART_INT()  // SENSOR_USART
{
    if (sensor_uart.getc() == 'R') {
        myled = (myled)?(0):(1);
        sprintf(str, "%d; %s;", t.read_ms(), sensor_uart.gets(buf, 5));
        writeflag = 1;
        //pc.printf("%s\r\n", str);
    }
}

int main()
{
    myled = 0;

    // SetUp the baud rate
    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);

    while(1) {
        if (Button)
            fclose(fp);
        if (writeflag) {
            memcpy(str2, str, 32);
            fprintf(fp, "%s\n", str2);
            writeflag = 0;
            fprintf(pc, "%s\r\n", str2);
        }
    }
}