Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed
main.cpp@1:5c9bde92518d, 2016-03-15 (annotated)
- Committer:
- shtirlitz
- Date:
- Tue Mar 15 12:09:16 2016 +0000
- Revision:
- 1:5c9bde92518d
- Parent:
- 0:2ce0698a332d
- Child:
- 2:a5ea1929e545
working reading gauge, writing to flash, writing to serial
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 | 1:5c9bde92518d | 10 | //DigitalOut myled2(PA_7); // 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 | 1:5c9bde92518d | 58 | fprintf(fp, "timestamp;value;"); |
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 | 1:5c9bde92518d | 70 | memcpy(str2, str, 32); |
shtirlitz | 1:5c9bde92518d | 71 | fprintf(fp, "%s\n", str2); |
shtirlitz | 1:5c9bde92518d | 72 | writeflag = 0; |
shtirlitz | 1:5c9bde92518d | 73 | fprintf(pc, "%s\r\n", str2); |
shtirlitz | 0:2ce0698a332d | 74 | } |
shtirlitz | 0:2ce0698a332d | 75 | } |
shtirlitz | 0:2ce0698a332d | 76 | } |