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: mbed
Revision 1:e9a1cf8774e0, committed 2018-02-02
- Comitter:
- rschiano
- Date:
- Fri Feb 02 13:52:00 2018 +0000
- Parent:
- 0:f0ae885dd281
- Commit message:
- I2C Ottimizzato Ver2.0
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Feb 01 14:33:45 2018 +0000
+++ b/main.cpp Fri Feb 02 13:52:00 2018 +0000
@@ -5,31 +5,37 @@
#define CTRL1X 0x10
#define ODR416 0x60
#define CTRL2G 0x11
+#define CTRL8X 0x17
#define OUTG 0x22
#define OUTX 0x28
#define OUTT 0x20
-
+#define LPF2X 0xC8
#define MAG_W 0x3C
#define ACC_M 0x32
#define CFG_REG_A_M 0x60
#define ODR100 0x8C
#define OUTM 0x68
+#define SERIAL
+//#define DEBUG
+//#define FILTERING
I2C i2c(D14, D15);
DigitalOut myled(LED1);
int16_t buffer[10];
+Timer timer;
+#ifdef SERIAL
char bufUart[20];
+#endif
RawSerial pc(USBTX, USBRX, 921600);
-Timer timer;
// volatile char TempCelsiusDisplay[] = "+abc.d C";
-
+int32_t nSam;
int main()
{
char data_write[2];
- char data_read[6];
+ char data_read[14];
/* Configure the Temperature sensor device STLM75:
- Thermostat mode Interrupt
@@ -56,6 +62,12 @@
data_write[0] = CTRL1X;
data_write[1] = ODR416;
i2c.write(ACC_W, data_write, 2, 0);
+#ifdef FILTERING
+ // Imposto LPF ODR/9
+ data_write[0] = CTRL8X;
+ data_write[1] = LPF2X;
+ i2c.write(MAG_W, data_write, 2, 0);
+#endif
// Imposto ODR 416 Hz su gyro
data_write[0] = CTRL2G;
data_write[1] = ODR416;
@@ -64,70 +76,70 @@
data_write[0] = CFG_REG_A_M;
data_write[1] = ODR100;
i2c.write(MAG_W, data_write, 2, 0);
- while(1){
- //timer.reset();
- //timer.start();
- char app = '.';
- //pc.printf("Avvio...\r\n");
- do{
- app = pc.getc();
- }while(app!='k');
- data_write[0] = OUTG;
+ nSam = 0;
+ char app = '.';
+ do{
+ app = pc.getc();
+ if ((app>=0x30)&&(app<=0x39))
+ nSam = nSam*10 + app-0x30;
+ }while(app!='k');
+ int i = 0;
+ while(i<nSam){
+ i++;
+ timer.reset();
+ timer.start();
+#ifdef DEBUG
+ timer.reset();
+ timer.start();
+ pc.printf("Avvio...\r\n");
+#endif
+ data_write[0] = OUTT;
i2c.write(ACC_W, data_write, 1, 0);
- i2c.read(ACC_W, data_read, 6, 0);
- for(int i=0; i<3; i++)
+ i2c.read(ACC_W, data_read, 14, 0);
+ for(int i=0; i<7; i++)
buffer[i] = (int16_t)((uint16_t)data_read[2*i+1]*256+data_read[2*i]);
- for(int i=0; i<6; i++){
+#ifdef SERIAL
+ for(int i=0; i<14; i++){
bufUart[i] = data_read[i];
}
- //timer.stop();
- //pc.printf("%d\t%d\t%d\r\n",(int16_t)((uint16_t)data_read[1]*256+data_read[0]),(int16_t)((uint16_t)data_read[3]*256+data_read[2]),(int16_t)((uint16_t)data_read[5]*256+data_read[4]));
- //pc.printf("Elapsed time %d [us]\r\n",timer.read_us());
- //timer.reset();
- //timer.start();
- data_write[0] = OUTX;
- i2c.write(ACC_W, data_write, 1, 0);
- i2c.read(ACC_W, data_read, 6, 0);
- for(int i=0; i<3; i++)
- buffer[i+3] = (int16_t)((uint16_t)data_read[2*i+1]*256+data_read[2*i]);
- for(int i=0; i<6; i++){
- bufUart[i+6] = data_read[i];
- }
- //timer.stop();
- //pc.printf("%d\t%d\t%d\r\n",(int16_t)((uint16_t)data_read[1]*256+data_read[0]),(int16_t)((uint16_t)data_read[3]*256+data_read[2]),(int16_t)((uint16_t)data_read[5]*256+data_read[4]));
- //pc.printf("Elapsed time %d [us]\r\n",timer.read_us());
- //timer.reset();
- //timer.start();
- data_write[0] = OUTT;
- i2c.write(ACC_W, data_write, 1, 0);
- i2c.read(ACC_W, data_read, 2, 0);
- for(int i=0; i<1; i++)
- buffer[i+6] = (int16_t)((uint16_t)data_read[2*i+1]*256+data_read[2*i]);
- for(int i=0; i<2; i++){
- bufUart[i+12] = data_read[i];
- }
- //timer.stop();
- //pc.printf("%f\t%d\t%d\r\n",(float)((int16_t)(((int16_t)data_read[1])*256+data_read[0]))/256.0 + 25.0,data_read[1],data_read[0]);
- //pc.printf("Elapsed time %d [us]\r\n",timer.read_us());
- //timer.reset();
- //timer.start();
+#endif
+#ifdef DEBUG
+ timer.stop();
+ pc.printf("Elapsed time %d [us]\r\n",timer.read_us());
+ timer.reset();
+ timer.start();
+#endif
data_write[0] = OUTM;
i2c.write(MAG_W, data_write, 1, 0);
i2c.read(MAG_W, data_read, 6, 0);
for(int i=0; i<3; i++)
buffer[i+7] = (int16_t)((uint16_t)data_read[2*i+1]*256+data_read[2*i]);
+#ifdef SERIAL
for(int i=0; i<6; i++){
bufUart[i+14] = data_read[i];
}
- //timer.stop();
- //pc.printf("%d\t%d\t%d\r\n",(int16_t)((uint16_t)data_read[1]*256+data_read[0]),(int16_t)((uint16_t)data_read[3]*256+data_read[2]),(int16_t)((uint16_t)data_read[5]*256+data_read[4]));
- //pc.printf("Elapsed time %d [us]\r\n",timer.read_us());
- //wait_ms(1000);
- //pc.printf("Invio dati...\r\n");
+#endif
+#ifdef DEBUG
+ timer.stop();
+ pc.printf("%d\t%d\t%d\r\n",(int16_t)((uint16_t)data_read[1]*256+data_read[0]),(int16_t)((uint16_t)data_read[3]*256+data_read[2]),(int16_t)((uint16_t)data_read[5]*256+data_read[4]));
+ pc.printf("Elapsed time %d [us]\r\n",timer.read_us());
+ pc.printf("Invio dati...\r\n");
+#endif
+#ifdef DEBUG
+ timer.reset();
+ timer.start();
+#endif
+#ifdef SERIAL
for(int i=0; i<20; i++){
pc.putc(bufUart[i]);
}
+#endif
+ timer.stop();
+ wait_us(10000-timer.read_us());
+#ifdef DEBUG
+ timer.stop();
+ pc.printf("Elapsed time %d [us]\r\n",timer.read_us());
+#endif
}
-}
-
+}
\ No newline at end of file