Configuring sensors dynamically using serial bus from the host computer.

Dependencies:   MAG3110 mbed

Committer:
mja054
Date:
Sat Feb 15 00:46:41 2014 +0000
Revision:
2:a422a41dbea1
Parent:
0:1efeb3fc4ba6
Send the time delay

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mja054 0:1efeb3fc4ba6 1 #include "mbed.h"
mja054 0:1efeb3fc4ba6 2 #include "MMA8451Q.h"
mja054 0:1efeb3fc4ba6 3 #include "MAG3110.h"
mja054 0:1efeb3fc4ba6 4 #include "TSISensor.h"
mja054 0:1efeb3fc4ba6 5
mja054 0:1efeb3fc4ba6 6 #define I2C0_SDA PTE25
mja054 0:1efeb3fc4ba6 7 #define I2C0_SCL PTE24
mja054 0:1efeb3fc4ba6 8 #define MMA8451_I2C_ADDRESS (0x1d<<1)
mja054 0:1efeb3fc4ba6 9 #define MAG3110_I2C_ADDRESS (0x0E<<1)
mja054 0:1efeb3fc4ba6 10 #define LIGHT_SENSOR_PIN PTE22
mja054 0:1efeb3fc4ba6 11 #define COUNT 1000
mja054 0:1efeb3fc4ba6 12
mja054 0:1efeb3fc4ba6 13 Serial pc_sr(USBTX, USBRX);
mja054 0:1efeb3fc4ba6 14 MMA8451Q acc_sr(I2C0_SDA, I2C0_SCL, MMA8451_I2C_ADDRESS);
mja054 0:1efeb3fc4ba6 15 MAG3110 mag_sr(I2C0_SDA, I2C0_SCL, MAG3110_I2C_ADDRESS);
mja054 0:1efeb3fc4ba6 16 Timer timer_sr;
mja054 0:1efeb3fc4ba6 17 TSISensor tsi_sr;
mja054 0:1efeb3fc4ba6 18 AnalogIn lightSensor_sr(PTE22);
mja054 0:1efeb3fc4ba6 19
mja054 0:1efeb3fc4ba6 20 void light(int count)
mja054 0:1efeb3fc4ba6 21 {
mja054 0:1efeb3fc4ba6 22 int i = 0;
mja054 0:1efeb3fc4ba6 23
mja054 0:1efeb3fc4ba6 24 while (i < count) {
mja054 0:1efeb3fc4ba6 25 lightSensor_sr.read();
mja054 0:1efeb3fc4ba6 26 i++;
mja054 0:1efeb3fc4ba6 27 }
mja054 0:1efeb3fc4ba6 28 }
mja054 0:1efeb3fc4ba6 29
mja054 0:1efeb3fc4ba6 30 void touch(int count)
mja054 0:1efeb3fc4ba6 31 {
mja054 0:1efeb3fc4ba6 32 int i = 0;
mja054 0:1efeb3fc4ba6 33 while(i < count) {
mja054 0:1efeb3fc4ba6 34 tsi_sr.readPercentage();
mja054 0:1efeb3fc4ba6 35 i++;
mja054 0:1efeb3fc4ba6 36 }
mja054 0:1efeb3fc4ba6 37 }
mja054 0:1efeb3fc4ba6 38
mja054 0:1efeb3fc4ba6 39 void magnetometer(int count)
mja054 0:1efeb3fc4ba6 40 {
mja054 0:1efeb3fc4ba6 41 int i = 0;
mja054 0:1efeb3fc4ba6 42
mja054 0:1efeb3fc4ba6 43 while(i < count) {
mja054 0:1efeb3fc4ba6 44 mag_sr.readVal(MAG_OUT_X_MSB);
mja054 0:1efeb3fc4ba6 45 mag_sr.readVal(MAG_OUT_Y_MSB);
mja054 0:1efeb3fc4ba6 46 mag_sr.readVal(MAG_OUT_Z_MSB);
mja054 0:1efeb3fc4ba6 47 i++;
mja054 0:1efeb3fc4ba6 48 }
mja054 0:1efeb3fc4ba6 49 }
mja054 0:1efeb3fc4ba6 50
mja054 0:1efeb3fc4ba6 51 void accelerometer(int count)
mja054 0:1efeb3fc4ba6 52 {
mja054 0:1efeb3fc4ba6 53 int i = 0;
mja054 0:1efeb3fc4ba6 54
mja054 0:1efeb3fc4ba6 55 while (i < 1000) {
mja054 0:1efeb3fc4ba6 56 acc_sr.getAccX();
mja054 0:1efeb3fc4ba6 57 acc_sr.getAccY();
mja054 0:1efeb3fc4ba6 58 acc_sr.getAccZ();
mja054 0:1efeb3fc4ba6 59 i++;
mja054 0:1efeb3fc4ba6 60 }
mja054 0:1efeb3fc4ba6 61 }
mja054 0:1efeb3fc4ba6 62
mja054 0:1efeb3fc4ba6 63 void max_sample_rate(int count)
mja054 0:1efeb3fc4ba6 64 {
mja054 0:1efeb3fc4ba6 65 int i = 0;
mja054 0:1efeb3fc4ba6 66
mja054 0:1efeb3fc4ba6 67 while (i < count) {
mja054 0:1efeb3fc4ba6 68 acc_sr.getAccX();
mja054 0:1efeb3fc4ba6 69 acc_sr.getAccY();
mja054 0:1efeb3fc4ba6 70 acc_sr.getAccZ();
mja054 0:1efeb3fc4ba6 71
mja054 0:1efeb3fc4ba6 72 mag_sr.readVal(MAG_OUT_X_MSB);
mja054 0:1efeb3fc4ba6 73 mag_sr.readVal(MAG_OUT_Y_MSB);
mja054 0:1efeb3fc4ba6 74 mag_sr.readVal(MAG_OUT_Z_MSB);
mja054 0:1efeb3fc4ba6 75
mja054 0:1efeb3fc4ba6 76 lightSensor_sr.read();
mja054 0:1efeb3fc4ba6 77
mja054 0:1efeb3fc4ba6 78 tsi_sr.readPercentage();
mja054 0:1efeb3fc4ba6 79
mja054 0:1efeb3fc4ba6 80 i++;
mja054 0:1efeb3fc4ba6 81 }
mja054 0:1efeb3fc4ba6 82 }
mja054 0:1efeb3fc4ba6 83 /*
mja054 0:1efeb3fc4ba6 84 int main() {
mja054 0:1efeb3fc4ba6 85 int t1, t2;
mja054 0:1efeb3fc4ba6 86
mja054 0:1efeb3fc4ba6 87 while (true) {
mja054 0:1efeb3fc4ba6 88 timer_sr.reset();
mja054 0:1efeb3fc4ba6 89
mja054 0:1efeb3fc4ba6 90 // Accelerometer
mja054 0:1efeb3fc4ba6 91 timer_sr.start();
mja054 0:1efeb3fc4ba6 92 t1 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 93 accelerometer(COUNT);
mja054 0:1efeb3fc4ba6 94 t2 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 95 timer_sr.stop();
mja054 0:1efeb3fc4ba6 96 pc_sr.printf("Time to sample acceleromter sensor 1000times = %dms\n", t2 - t1);
mja054 0:1efeb3fc4ba6 97
mja054 0:1efeb3fc4ba6 98 timer_sr.reset();
mja054 0:1efeb3fc4ba6 99
mja054 0:1efeb3fc4ba6 100 // magnetometer
mja054 0:1efeb3fc4ba6 101 timer_sr.start();
mja054 0:1efeb3fc4ba6 102 t1 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 103 magnetometer(COUNT);
mja054 0:1efeb3fc4ba6 104 t2 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 105 timer_sr.stop();
mja054 0:1efeb3fc4ba6 106 pc_sr.printf("Time to sample magnetometer sensor 1000times = %dms\n", t2 - t1);
mja054 0:1efeb3fc4ba6 107
mja054 0:1efeb3fc4ba6 108 timer_sr.reset();
mja054 0:1efeb3fc4ba6 109
mja054 0:1efeb3fc4ba6 110 // light
mja054 0:1efeb3fc4ba6 111 timer_sr.start();
mja054 0:1efeb3fc4ba6 112 t1 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 113 light(COUNT);
mja054 0:1efeb3fc4ba6 114 t2 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 115 timer_sr.stop();
mja054 0:1efeb3fc4ba6 116 pc_sr.printf("Time to sample light sensor 1000times = %dms\n", t2 - t1);
mja054 0:1efeb3fc4ba6 117
mja054 0:1efeb3fc4ba6 118 timer_sr.reset();
mja054 0:1efeb3fc4ba6 119
mja054 0:1efeb3fc4ba6 120 // touch
mja054 0:1efeb3fc4ba6 121 timer_sr.start();
mja054 0:1efeb3fc4ba6 122 t1 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 123 touch(COUNT);
mja054 0:1efeb3fc4ba6 124 t2 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 125 timer_sr.stop();
mja054 0:1efeb3fc4ba6 126 pc_sr.printf("Time to sample touch sensor 1000times = %dms\n", t2 - t1);
mja054 0:1efeb3fc4ba6 127
mja054 0:1efeb3fc4ba6 128 timer_sr.reset();
mja054 0:1efeb3fc4ba6 129
mja054 0:1efeb3fc4ba6 130 // All
mja054 0:1efeb3fc4ba6 131 timer_sr.start();
mja054 0:1efeb3fc4ba6 132 t1 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 133 max_sample_rate(COUNT);
mja054 0:1efeb3fc4ba6 134 t2 = timer_sr.read_ms();
mja054 0:1efeb3fc4ba6 135 timer_sr.stop();
mja054 0:1efeb3fc4ba6 136 pc_sr.printf("Time to sample all sensors 1000times = %dms\n", t2 - t1);
mja054 0:1efeb3fc4ba6 137 wait(3);
mja054 0:1efeb3fc4ba6 138 }
mja054 0:1efeb3fc4ba6 139
mja054 0:1efeb3fc4ba6 140 }
mja054 0:1efeb3fc4ba6 141 */