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 "AnalogOut.h"
mja054 0:1efeb3fc4ba6 3 #include "AnalogIn.h"
mja054 0:1efeb3fc4ba6 4 #include <math.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 PI 3.141f
mja054 0:1efeb3fc4ba6 12
mja054 0:1efeb3fc4ba6 13 Serial pc_sine(USBTX, USBRX);
mja054 0:1efeb3fc4ba6 14 Timer timer;
mja054 0:1efeb3fc4ba6 15
mja054 0:1efeb3fc4ba6 16
mja054 0:1efeb3fc4ba6 17 void sine_wave()
mja054 0:1efeb3fc4ba6 18 {
mja054 0:1efeb3fc4ba6 19 int t = 0;
mja054 0:1efeb3fc4ba6 20 float s;
mja054 0:1efeb3fc4ba6 21 AnalogOut a(PTE30);
mja054 0:1efeb3fc4ba6 22 while (1) { // sin(2 * PI * f * t / Fs)
mja054 0:1efeb3fc4ba6 23 s = sin(2 * PI * 1 * t / 100.0);
mja054 0:1efeb3fc4ba6 24 s = s/2 + .5;
mja054 0:1efeb3fc4ba6 25 a.write(s);
mja054 0:1efeb3fc4ba6 26 pc_sine.printf ("%f\n", s);
mja054 0:1efeb3fc4ba6 27 if (t == 100)
mja054 0:1efeb3fc4ba6 28 t = 0;
mja054 0:1efeb3fc4ba6 29 else
mja054 0:1efeb3fc4ba6 30 t++;
mja054 0:1efeb3fc4ba6 31 wait(0.01);
mja054 0:1efeb3fc4ba6 32 }
mja054 0:1efeb3fc4ba6 33 }
mja054 0:1efeb3fc4ba6 34
mja054 0:1efeb3fc4ba6 35 void sine_wave_read()
mja054 0:1efeb3fc4ba6 36 {
mja054 0:1efeb3fc4ba6 37 AnalogIn a(PTB0);
mja054 0:1efeb3fc4ba6 38 while(1) {
mja054 0:1efeb3fc4ba6 39 pc_sine.printf("%f\n", a.read());
mja054 0:1efeb3fc4ba6 40 wait(0.01);
mja054 0:1efeb3fc4ba6 41 }
mja054 0:1efeb3fc4ba6 42 }
mja054 0:1efeb3fc4ba6 43
mja054 0:1efeb3fc4ba6 44 /*
mja054 0:1efeb3fc4ba6 45 int main() {
mja054 0:1efeb3fc4ba6 46 sine_wave();
mja054 0:1efeb3fc4ba6 47
mja054 0:1efeb3fc4ba6 48 char c = pc_sine.getc();
mja054 0:1efeb3fc4ba6 49
mja054 0:1efeb3fc4ba6 50 switch (c) {
mja054 0:1efeb3fc4ba6 51 case '1':sine_wave();
mja054 0:1efeb3fc4ba6 52 break;
mja054 0:1efeb3fc4ba6 53 case '2': sine_wave_read();
mja054 0:1efeb3fc4ba6 54 break;
mja054 0:1efeb3fc4ba6 55 }
mja054 0:1efeb3fc4ba6 56 }
mja054 0:1efeb3fc4ba6 57 */