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.
main.cpp@1:2c0d72027a69, 2013-03-12 (annotated)
- Committer:
- RMWorks
- Date:
- Tue Mar 12 07:55:13 2013 +0000
- Revision:
- 1:2c0d72027a69
- Parent:
- 0:0a596069bd38
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RMWorks | 0:0a596069bd38 | 1 | #include "mbed.h" |
RMWorks | 0:0a596069bd38 | 2 | #include "ADXL345.h" |
RMWorks | 0:0a596069bd38 | 3 | #include "SDFileSystem.h" |
RMWorks | 0:0a596069bd38 | 4 | #include "SCP1000.h" |
RMWorks | 0:0a596069bd38 | 5 | |
RMWorks | 0:0a596069bd38 | 6 | DigitalOut myled(LED1); |
RMWorks | 0:0a596069bd38 | 7 | ADXL345 adxl345(p5, p6, p7, p9); |
RMWorks | 0:0a596069bd38 | 8 | SCP1000 scp1000(p5, p6, p7, p8); |
RMWorks | 0:0a596069bd38 | 9 | SDFileSystem sd(p5, p6, p7, p10, "sd"); |
RMWorks | 0:0a596069bd38 | 10 | I2C i2c(p28, p27); |
RMWorks | 0:0a596069bd38 | 11 | LocalFileSystem local("local"); |
RMWorks | 0:0a596069bd38 | 12 | Serial pc(USBTX, USBRX); |
RMWorks | 0:0a596069bd38 | 13 | |
RMWorks | 0:0a596069bd38 | 14 | float toG(float value) { |
RMWorks | 0:0a596069bd38 | 15 | return value / 256; |
RMWorks | 0:0a596069bd38 | 16 | } //ADXL345 |
RMWorks | 0:0a596069bd38 | 17 | |
RMWorks | 1:2c0d72027a69 | 18 | short int toDeg_Sec(short int value) { |
RMWorks | 0:0a596069bd38 | 19 | //return (value - ((scp1000.readTemperature() + 13200.0) / 280.0 + 35.0)) / 16.4; |
RMWorks | 1:2c0d72027a69 | 20 | //return (value - ((20 + 13200.0) / 280.0 + 35.0)) / 16.4; //at 20degC |
RMWorks | 1:2c0d72027a69 | 21 | return value / 16.4; |
RMWorks | 0:0a596069bd38 | 22 | } //IMU3000 |
RMWorks | 0:0a596069bd38 | 23 | |
RMWorks | 0:0a596069bd38 | 24 | short int read_x_2byte; |
RMWorks | 0:0a596069bd38 | 25 | short int read_y_2byte; |
RMWorks | 0:0a596069bd38 | 26 | short int read_z_2byte; |
RMWorks | 0:0a596069bd38 | 27 | |
RMWorks | 0:0a596069bd38 | 28 | Ticker tick; //Timer IMU3000 |
RMWorks | 0:0a596069bd38 | 29 | double interval = 10; //[ms] IMU3000 |
RMWorks | 0:0a596069bd38 | 30 | const int adrIMU3000 = 0xD0;//8-bit write IMU3000 |
RMWorks | 0:0a596069bd38 | 31 | |
RMWorks | 0:0a596069bd38 | 32 | |
RMWorks | 0:0a596069bd38 | 33 | int main() { |
RMWorks | 0:0a596069bd38 | 34 | |
RMWorks | 0:0a596069bd38 | 35 | /*FILE *fp = fopen("/sd/Data.txt","a"); |
RMWorks | 0:0a596069bd38 | 36 | fprintf(fp, "X, Y, Z, Xw, Yw, Zw, Pa\n"); |
RMWorks | 0:0a596069bd38 | 37 | fclose(fp);*/ |
RMWorks | 0:0a596069bd38 | 38 | |
RMWorks | 0:0a596069bd38 | 39 | int readings[3] = {0, 0, 0}; |
RMWorks | 0:0a596069bd38 | 40 | |
RMWorks | 0:0a596069bd38 | 41 | adxl345.setPowerControl(0x00); //Go into standby mode to configure the device. |
RMWorks | 0:0a596069bd38 | 42 | adxl345.setDataFormatControl(0x0B); //Full resolution, +/-16g, 4mg/LSB. |
RMWorks | 0:0a596069bd38 | 43 | adxl345.setDataRate(ADXL345_3200HZ); //3.2kHz data rate. |
RMWorks | 0:0a596069bd38 | 44 | adxl345.setPowerControl(0x08); //Measurement mode. |
RMWorks | 0:0a596069bd38 | 45 | |
RMWorks | 0:0a596069bd38 | 46 | i2c.frequency(400000); //set 400kHz |
RMWorks | 0:0a596069bd38 | 47 | |
RMWorks | 0:0a596069bd38 | 48 | //pc.baud(115200); |
RMWorks | 0:0a596069bd38 | 49 | |
RMWorks | 0:0a596069bd38 | 50 | char PWR_M[2] = {0x3E, 0x80}; |
RMWorks | 0:0a596069bd38 | 51 | i2c.write(adrIMU3000, PWR_M, 2, true); |
RMWorks | 0:0a596069bd38 | 52 | char SMPL[2] = {0x15, 0x00}; |
RMWorks | 0:0a596069bd38 | 53 | i2c.write(adrIMU3000, SMPL, 2, true); |
RMWorks | 0:0a596069bd38 | 54 | char DLPF[2] = {0x16, 0x18}; |
RMWorks | 0:0a596069bd38 | 55 | i2c.write(adrIMU3000, DLPF, 2, true); |
RMWorks | 0:0a596069bd38 | 56 | char INT_C[2] = {0x17, 0x05}; |
RMWorks | 0:0a596069bd38 | 57 | i2c.write(adrIMU3000, INT_C, 2, true); |
RMWorks | 0:0a596069bd38 | 58 | char PWR_M2[2] = {0x3E, 0x00}; |
RMWorks | 0:0a596069bd38 | 59 | i2c.write(adrIMU3000, PWR_M2, 2, true); |
RMWorks | 0:0a596069bd38 | 60 | |
RMWorks | 0:0a596069bd38 | 61 | char adrIMU3000_y[1]; |
RMWorks | 0:0a596069bd38 | 62 | char readIMU3000[8]; |
RMWorks | 0:0a596069bd38 | 63 | |
RMWorks | 1:2c0d72027a69 | 64 | read_x_2byte = 0; |
RMWorks | 1:2c0d72027a69 | 65 | read_y_2byte = 0; |
RMWorks | 1:2c0d72027a69 | 66 | read_z_2byte = 0; |
RMWorks | 1:2c0d72027a69 | 67 | |
RMWorks | 0:0a596069bd38 | 68 | |
RMWorks | 0:0a596069bd38 | 69 | while (1) { |
RMWorks | 0:0a596069bd38 | 70 | |
RMWorks | 0:0a596069bd38 | 71 | adxl345.getOutput(readings); |
RMWorks | 0:0a596069bd38 | 72 | float accelX = toG((int16_t)readings[0] ); //13-bit, sign extended values. |
RMWorks | 0:0a596069bd38 | 73 | float accelY = toG((int16_t)readings[1] ); |
RMWorks | 0:0a596069bd38 | 74 | float accelZ = toG((int16_t)readings[2] ); |
RMWorks | 0:0a596069bd38 | 75 | |
RMWorks | 0:0a596069bd38 | 76 | |
RMWorks | 0:0a596069bd38 | 77 | adrIMU3000_y[0] = 0x1D; //register GYRO_XOUT_H |
RMWorks | 0:0a596069bd38 | 78 | |
RMWorks | 0:0a596069bd38 | 79 | i2c.write(adrIMU3000, adrIMU3000_y , 1); |
RMWorks | 0:0a596069bd38 | 80 | i2c.read(adrIMU3000, readIMU3000, 8); |
RMWorks | 0:0a596069bd38 | 81 | |
RMWorks | 0:0a596069bd38 | 82 | read_x_2byte = ((readIMU3000[0] << 8) + readIMU3000[1]); |
RMWorks | 0:0a596069bd38 | 83 | read_y_2byte = ((readIMU3000[2] << 8) + readIMU3000[3]); |
RMWorks | 0:0a596069bd38 | 84 | read_z_2byte = ((readIMU3000[4] << 8) + readIMU3000[5]); |
RMWorks | 1:2c0d72027a69 | 85 | short int Xw = toDeg_Sec(read_x_2byte ); |
RMWorks | 1:2c0d72027a69 | 86 | short int Yw = toDeg_Sec(read_y_2byte ); |
RMWorks | 1:2c0d72027a69 | 87 | short int Zw = toDeg_Sec(read_z_2byte ); |
RMWorks | 1:2c0d72027a69 | 88 | |
RMWorks | 1:2c0d72027a69 | 89 | scp1000.readPressure(); |
RMWorks | 1:2c0d72027a69 | 90 | scp1000.readTemperature(); |
RMWorks | 0:0a596069bd38 | 91 | |
RMWorks | 1:2c0d72027a69 | 92 | int Pa = scp1000.readPressure(); |
RMWorks | 1:2c0d72027a69 | 93 | int Dc = scp1000.readTemperature(); |
RMWorks | 0:0a596069bd38 | 94 | |
RMWorks | 1:2c0d72027a69 | 95 | FILE *fp = fopen("/local/Data.txt","a"); |
RMWorks | 1:2c0d72027a69 | 96 | myled = 1; |
RMWorks | 1:2c0d72027a69 | 97 | //fprintf(fp, "%f, %f, %f, %d, %d, %d, %f, %f\n", accelX, accelY, accelZ, Xw, Yw, Zw, Pa, Dc); |
RMWorks | 1:2c0d72027a69 | 98 | //fprintf(fp, "%f, %f, %f", accelX, accelY, accelZ); |
RMWorks | 1:2c0d72027a69 | 99 | fprintf(fp, "%d, %d, %d", Xw, Yw, Zw); |
RMWorks | 1:2c0d72027a69 | 100 | //fprintf(fp, "%f, %f\n", Pa, Dc); |
RMWorks | 0:0a596069bd38 | 101 | fclose(fp); |
RMWorks | 0:0a596069bd38 | 102 | myled = 0; |
RMWorks | 0:0a596069bd38 | 103 | |
RMWorks | 0:0a596069bd38 | 104 | wait(1); |
RMWorks | 0:0a596069bd38 | 105 | |
RMWorks | 0:0a596069bd38 | 106 | } |
RMWorks | 0:0a596069bd38 | 107 | } |