Use serial port, not SD for datalogging.
Dependencies: MMA8451Q_tb SDFileSystem mbed
Fork of StepLogger by
main.cpp@0:75c161b9fc38, 2014-04-30 (annotated)
- Committer:
- donoman
- Date:
- Wed Apr 30 00:44:56 2014 +0000
- Revision:
- 0:75c161b9fc38
- Child:
- 1:03f0a25c5a86
First pass at datalogger --- still problems when writing to SD card due to buffer full causing delay.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donoman | 0:75c161b9fc38 | 1 | #include "mbed.h" |
donoman | 0:75c161b9fc38 | 2 | #include "MMA8451Q_tb.h" |
donoman | 0:75c161b9fc38 | 3 | #include "SDFileSystem.h" |
donoman | 0:75c161b9fc38 | 4 | |
donoman | 0:75c161b9fc38 | 5 | |
donoman | 0:75c161b9fc38 | 6 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
donoman | 0:75c161b9fc38 | 7 | float acc_all[3]; |
donoman | 0:75c161b9fc38 | 8 | DigitalOut pinout(PTD4); |
donoman | 0:75c161b9fc38 | 9 | Timer timer; |
donoman | 0:75c161b9fc38 | 10 | Serial pc(USBTX, USBRX); |
donoman | 0:75c161b9fc38 | 11 | SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // (mosi, miso, sclok, cs, name) |
donoman | 0:75c161b9fc38 | 12 | |
donoman | 0:75c161b9fc38 | 13 | int main(void) { |
donoman | 0:75c161b9fc38 | 14 | |
donoman | 0:75c161b9fc38 | 15 | //Initialize |
donoman | 0:75c161b9fc38 | 16 | int now; //used for timestamping |
donoman | 0:75c161b9fc38 | 17 | timer.start(); |
donoman | 0:75c161b9fc38 | 18 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
donoman | 0:75c161b9fc38 | 19 | printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
donoman | 0:75c161b9fc38 | 20 | |
donoman | 0:75c161b9fc38 | 21 | //Open File |
donoman | 0:75c161b9fc38 | 22 | mkdir("/sd/mydir", 0777); |
donoman | 0:75c161b9fc38 | 23 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "a"); |
donoman | 0:75c161b9fc38 | 24 | if(fp == NULL) { |
donoman | 0:75c161b9fc38 | 25 | error("Could not open file for write\n"); |
donoman | 0:75c161b9fc38 | 26 | } |
donoman | 0:75c161b9fc38 | 27 | |
donoman | 0:75c161b9fc38 | 28 | for(int i=0; i<2000; i++) { |
donoman | 0:75c161b9fc38 | 29 | pinout = !pinout; |
donoman | 0:75c161b9fc38 | 30 | now = timer.read_ms(); |
donoman | 0:75c161b9fc38 | 31 | acc.fastRead(&acc_all[0]); |
donoman | 0:75c161b9fc38 | 32 | |
donoman | 0:75c161b9fc38 | 33 | //Print to file |
donoman | 0:75c161b9fc38 | 34 | fprintf(fp, "%d, %f,%f,%f\n",now, acc_all[0],acc_all[1],acc_all[2]); |
donoman | 0:75c161b9fc38 | 35 | |
donoman | 0:75c161b9fc38 | 36 | } |
donoman | 0:75c161b9fc38 | 37 | fclose(fp); |
donoman | 0:75c161b9fc38 | 38 | } |