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@2:a5ea1929e545, 2016-03-17 (annotated)
- Committer:
- shtirlitz
- Date:
- Thu Mar 17 22:32:39 2016 +0000
- Revision:
- 2:a5ea1929e545
- Parent:
- 1:5c9bde92518d
- Child:
- 3:66b5c374745e
working good
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:2ce0698a332d | 6 | Serial pc(PA_2, PA_3); // This is USB tx, rx |
shtirlitz | 0:2ce0698a332d | 7 | Serial sensor_uart(PA_9, PA_10); // This is USART2 tx, rx |
shtirlitz | 0:2ce0698a332d | 8 | |
shtirlitz | 1:5c9bde92518d | 9 | DigitalOut myled(PC_13); // This is USER LED |
shtirlitz | 2:a5ea1929e545 | 10 | DigitalOut myled2(PB_11); // This is USER LED |
shtirlitz | 1:5c9bde92518d | 11 | DigitalIn Button(PB_12); // This is Blue-Button |
shtirlitz | 0:2ce0698a332d | 12 | |
shtirlitz | 0:2ce0698a332d | 13 | #define Pressed 0 |
shtirlitz | 0:2ce0698a332d | 14 | #define NotPressed 1 |
shtirlitz | 0:2ce0698a332d | 15 | |
shtirlitz | 0:2ce0698a332d | 16 | int USBByte='\0'; |
shtirlitz | 0:2ce0698a332d | 17 | int GaugeByte='\0'; |
shtirlitz | 0:2ce0698a332d | 18 | int n=0; |
shtirlitz | 0:2ce0698a332d | 19 | int readgauge = 0; |
shtirlitz | 0:2ce0698a332d | 20 | Timer t; |
shtirlitz | 1:5c9bde92518d | 21 | char buf[32]; |
shtirlitz | 1:5c9bde92518d | 22 | char str[32]; |
shtirlitz | 1:5c9bde92518d | 23 | char str2[32]; |
shtirlitz | 1:5c9bde92518d | 24 | uint8_t writeflag = 0; |
shtirlitz | 0:2ce0698a332d | 25 | long millis = 0; |
shtirlitz | 0:2ce0698a332d | 26 | |
shtirlitz | 0:2ce0698a332d | 27 | void PC_INT() // USART2 used for Debug |
shtirlitz | 0:2ce0698a332d | 28 | { |
shtirlitz | 0:2ce0698a332d | 29 | // Note: you need to actually read from the serial to clear the RX interrupt |
shtirlitz | 0:2ce0698a332d | 30 | pc.putc(pc.getc()); |
shtirlitz | 0:2ce0698a332d | 31 | myled = (myled)?(0):(1); |
shtirlitz | 0:2ce0698a332d | 32 | } |
shtirlitz | 0:2ce0698a332d | 33 | |
shtirlitz | 0:2ce0698a332d | 34 | void SENSOR_USART_INT() // SENSOR_USART |
shtirlitz | 0:2ce0698a332d | 35 | { |
shtirlitz | 0:2ce0698a332d | 36 | if (sensor_uart.getc() == 'R') { |
shtirlitz | 0:2ce0698a332d | 37 | myled = (myled)?(0):(1); |
shtirlitz | 1:5c9bde92518d | 38 | sprintf(str, "%d; %s;", t.read_ms(), sensor_uart.gets(buf, 5)); |
shtirlitz | 1:5c9bde92518d | 39 | writeflag = 1; |
shtirlitz | 1:5c9bde92518d | 40 | //pc.printf("%s\r\n", str); |
shtirlitz | 0:2ce0698a332d | 41 | } |
shtirlitz | 0:2ce0698a332d | 42 | } |
shtirlitz | 0:2ce0698a332d | 43 | |
shtirlitz | 0:2ce0698a332d | 44 | int main() |
shtirlitz | 0:2ce0698a332d | 45 | { |
shtirlitz | 0:2ce0698a332d | 46 | myled = 0; |
shtirlitz | 0:2ce0698a332d | 47 | |
shtirlitz | 0:2ce0698a332d | 48 | // SetUp the baud rate |
shtirlitz | 1:5c9bde92518d | 49 | pc.baud(115200); |
shtirlitz | 0:2ce0698a332d | 50 | sensor_uart.baud(9600); |
shtirlitz | 0:2ce0698a332d | 51 | |
shtirlitz | 1:5c9bde92518d | 52 | mkdir("/sd/ultrasonic", 0777); |
shtirlitz | 1:5c9bde92518d | 53 | |
shtirlitz | 1:5c9bde92518d | 54 | FILE *fp = fopen("/sd/ultrasonic/test.csv", "w"); |
shtirlitz | 1:5c9bde92518d | 55 | if(fp == NULL) { |
shtirlitz | 1:5c9bde92518d | 56 | pc.printf("Could not open file for write\n"); |
shtirlitz | 1:5c9bde92518d | 57 | } |
shtirlitz | 2:a5ea1929e545 | 58 | fprintf(fp, "timestamp;value;\n"); |
shtirlitz | 1:5c9bde92518d | 59 | //fclose(fp); |
shtirlitz | 1:5c9bde92518d | 60 | |
shtirlitz | 1:5c9bde92518d | 61 | pc.printf("\n\r\n\r|||||START MAIN|||||\n\r"); |
shtirlitz | 1:5c9bde92518d | 62 | t.start(); |
shtirlitz | 0:2ce0698a332d | 63 | sensor_uart.attach(&SENSOR_USART_INT); |
shtirlitz | 0:2ce0698a332d | 64 | pc.attach(&PC_INT); |
shtirlitz | 0:2ce0698a332d | 65 | |
shtirlitz | 0:2ce0698a332d | 66 | while(1) { |
shtirlitz | 1:5c9bde92518d | 67 | if (Button) |
shtirlitz | 1:5c9bde92518d | 68 | fclose(fp); |
shtirlitz | 1:5c9bde92518d | 69 | if (writeflag) { |
shtirlitz | 2:a5ea1929e545 | 70 | myled2 = (myled2)?(0):(1); |
shtirlitz | 1:5c9bde92518d | 71 | memcpy(str2, str, 32); |
shtirlitz | 1:5c9bde92518d | 72 | fprintf(fp, "%s\n", str2); |
shtirlitz | 1:5c9bde92518d | 73 | writeflag = 0; |
shtirlitz | 1:5c9bde92518d | 74 | fprintf(pc, "%s\r\n", str2); |
shtirlitz | 2:a5ea1929e545 | 75 | } |
shtirlitz | 2:a5ea1929e545 | 76 | else |
shtirlitz | 2:a5ea1929e545 | 77 | { |
shtirlitz | 2:a5ea1929e545 | 78 | myled2 = (myled2)?(0):(1); |
shtirlitz | 2:a5ea1929e545 | 79 | wait (0.07); |
shtirlitz | 0:2ce0698a332d | 80 | } |
shtirlitz | 0:2ce0698a332d | 81 | } |
shtirlitz | 0:2ce0698a332d | 82 | } |