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

Committer:
shtirlitz
Date:
Sat Apr 02 21:37:01 2016 +0000
Revision:
3:66b5c374745e
Parent:
2:a5ea1929e545
Excluded bullshit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shtirlitz 0:2ce0698a332d 1 #include "mbed.h"
shtirlitz 1:5c9bde92518d 2 #include "SDFileSystem.h"
shtirlitz 1:5c9bde92518d 3
shtirlitz 1:5c9bde92518d 4 SDFileSystem sd(PA_7, PA_6, PA_5, PA_4, "sd"); //mosi, miso, sck, cs
shtirlitz 0:2ce0698a332d 5
shtirlitz 3:66b5c374745e 6 Serial pc_uart(PA_2, PA_3); // This is USART1 tx, rx
shtirlitz 3:66b5c374745e 7 Serial sensor_uart(PA_9, PA_10); // This is USART2 tx, rx
shtirlitz 0:2ce0698a332d 8
shtirlitz 3:66b5c374745e 9 DigitalOut led_read(PC_13); // This is reading LED indicator
shtirlitz 3:66b5c374745e 10 DigitalOut led_write(PB_11); // This is writing LED indicator
shtirlitz 3:66b5c374745e 11 DigitalIn button(PB_12); // This is STOP button
shtirlitz 0:2ce0698a332d 12
shtirlitz 0:2ce0698a332d 13 Timer t;
shtirlitz 3:66b5c374745e 14 char buf[5];
shtirlitz 3:66b5c374745e 15 char reading_str[32];
shtirlitz 3:66b5c374745e 16 char writing_str[32];
shtirlitz 1:5c9bde92518d 17 uint8_t writeflag = 0;
shtirlitz 0:2ce0698a332d 18
shtirlitz 3:66b5c374745e 19 void SENSOR_USART_INT() // sensor uart interrupt
shtirlitz 0:2ce0698a332d 20 {
shtirlitz 0:2ce0698a332d 21 if (sensor_uart.getc() == 'R') {
shtirlitz 3:66b5c374745e 22 led_read = 1;
shtirlitz 3:66b5c374745e 23 sprintf(reading_str, "%d; %s;", t.read_ms(), sensor_uart.gets(buf, 5));
shtirlitz 1:5c9bde92518d 24 writeflag = 1;
shtirlitz 0:2ce0698a332d 25 }
shtirlitz 0:2ce0698a332d 26 }
shtirlitz 0:2ce0698a332d 27
shtirlitz 0:2ce0698a332d 28 int main()
shtirlitz 0:2ce0698a332d 29 {
shtirlitz 3:66b5c374745e 30 pc_uart.baud(115200); //set baudrate of pc port
shtirlitz 3:66b5c374745e 31 sensor_uart.baud(9600); //set baudrate of sensorport
shtirlitz 0:2ce0698a332d 32
shtirlitz 1:5c9bde92518d 33 mkdir("/sd/ultrasonic", 0777);
shtirlitz 1:5c9bde92518d 34
shtirlitz 1:5c9bde92518d 35 FILE *fp = fopen("/sd/ultrasonic/test.csv", "w");
shtirlitz 1:5c9bde92518d 36 if(fp == NULL) {
shtirlitz 3:66b5c374745e 37 pc_uart.printf("Could not open file for write\n");
shtirlitz 1:5c9bde92518d 38 }
shtirlitz 2:a5ea1929e545 39 fprintf(fp, "timestamp;value;\n");
shtirlitz 1:5c9bde92518d 40 //fclose(fp);
shtirlitz 1:5c9bde92518d 41
shtirlitz 3:66b5c374745e 42 pc_uart.printf("\n\r\n\r|||||START_MAIN|||||\n\r");
shtirlitz 1:5c9bde92518d 43 t.start();
shtirlitz 0:2ce0698a332d 44 sensor_uart.attach(&SENSOR_USART_INT);
shtirlitz 0:2ce0698a332d 45
shtirlitz 0:2ce0698a332d 46 while(1) {
shtirlitz 3:66b5c374745e 47 if (button)
shtirlitz 1:5c9bde92518d 48 fclose(fp);
shtirlitz 1:5c9bde92518d 49 if (writeflag) {
shtirlitz 3:66b5c374745e 50 led_write = 1;
shtirlitz 3:66b5c374745e 51 memcpy(writing_str, reading_str, 32);
shtirlitz 3:66b5c374745e 52 fprintf(fp, "%s\n", writing_str);
shtirlitz 1:5c9bde92518d 53 writeflag = 0;
shtirlitz 3:66b5c374745e 54 fprintf(pc_uart, "%s\r\n", writing_str);
shtirlitz 3:66b5c374745e 55 }
shtirlitz 3:66b5c374745e 56 else {
shtirlitz 3:66b5c374745e 57 led_write = 1;
shtirlitz 2:a5ea1929e545 58 wait (0.07);
shtirlitz 0:2ce0698a332d 59 }
shtirlitz 3:66b5c374745e 60 led_read = 0;
shtirlitz 3:66b5c374745e 61 led_write = 0;
shtirlitz 0:2ce0698a332d 62 }
shtirlitz 0:2ce0698a332d 63 }