Slight Mod

Dependencies:   mbed-dsp mbed

Fork of Hat_Board_v5 by John Scharf

Files at this revision

API Documentation at this revision

Comitter:
drnow
Date:
Thu Mar 20 09:49:36 2014 +0000
Parent:
1:2efeed26d93a
Child:
3:8334f137c151
Commit message:
Cleaned up, sends 2 light and 3 accel, with timing data.

Changed in this revision

SI_LIS.cpp Show annotated file Show diff for this revision Revisions of this file
SI_LIS.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SI_LIS.cpp	Thu Mar 20 09:03:12 2014 +0000
+++ b/SI_LIS.cpp	Thu Mar 20 09:49:36 2014 +0000
@@ -123,13 +123,29 @@
     i2c.write((IR_ADDRESS << 1) & 0xFE, tx, 2);
 }
 
-void Init_Accel (char Reg_Num, char Reg_Val)
+void Init_Accel ()
 {
     char        data[2];
+    char        Reg_Num;
+    char        Reg_Val;
+    
+    Reg_Num = 0x20; // CTRL_REG1
+    Reg_Val = 0x57; // Nib 1 of 0101: Normal mode, 100 Hz;  Nib 2 of 0111: Normal mode, XYZ enabled
+
     data[0]   = Reg_Num;     //register to be written to
     data[1]   = Reg_Val;     //data
+    i2c.write((LIS_Addr << 1) & 0xFE, data, 2);
 
+    wait(0.050);    // Delay needed, or 2nd write doesn't work
+
+    Reg_Num = 0x21; // CTRL_REG2
+    Reg_Val = 0xA0; // 7-6 10: Normal filter mode;  5-4 10: Cut off freq; 3-0 0000: bypass interrupts
+    // High-pass filter bits 5-4: @ 100 Hz:  00 - 2Hz   01 - 1Hz   10- 0.5Hz   11- 0.2Hz
+    data[0]   = Reg_Num;     //register to be written to
+    data[1]   = Reg_Val;     //data
     i2c.write((LIS_Addr << 1) & 0xFE, data, 2);
+
+    wait(0.050);    // Delay needed?
 }
 
 void Get_Accel_Register (char Reg_Num)
@@ -144,29 +160,12 @@
     accel_data[0] = data;
 }
 
-void Get_Accel_Register_2 (char Reg_Num) // Read 2 registers
-/*
-In order to read multiple bytes, it is necessary to assert the most significant bit of the subaddress
-field. In other words, SUB(7) must be equal to 1 while SUB(6-0) represents the
-address of first register to be read.
-*/
-{
-    char  reg;
-    reg = Reg_Num | 0x80; // set bit 7 high
-    i2c.write((LIS_Addr << 1) & 0xFE, &reg, 1);
-    i2c.read((LIS_Addr << 1) | 0x01, accel_2_data, 2);
-}
-
 void Get_Accel_Reg_6 (char Reg_Num) // Read 6 registers
 {
     char  reg;
-    reg = Reg_Num | 0x80; // set bit 7 high
+    reg = Reg_Num | 0x80; // set bit 7 high to enable burst read
     i2c.write((LIS_Addr << 1) & 0xFE, &reg, 1);
     i2c.read((LIS_Addr << 1) | 0x01, accel_data, 6);
 }
 
-void i2c_start()  // also set by photodiode setup/reset
-{
-    i2c.frequency(400000);
-}
 
--- a/SI_LIS.h	Thu Mar 20 09:03:12 2014 +0000
+++ b/SI_LIS.h	Thu Mar 20 09:49:36 2014 +0000
@@ -149,17 +149,13 @@
 // Read 4 registers in a row
 char read_reg2(/*unsigned*/ char address);
 
-// Write to a register on the device.
+// Write to a register on Si1142.
 void write_reg(char address, char num_data);
 
-void Init_Accel (char Reg_Num, char Reg_Val);
+void Init_Accel ();
 
 void Get_Accel_Register (char Reg_Num);
 
-void Get_Accel_Register_2 (char Reg_Num); // Read 2 registers
-
 void Get_Accel_Reg_6 (char Reg_Num); // Read 6 registers
 
-void i2c_start();
-
 #endif
\ No newline at end of file
--- a/main.cpp	Thu Mar 20 09:03:12 2014 +0000
+++ b/main.cpp	Thu Mar 20 09:49:36 2014 +0000
@@ -16,94 +16,65 @@
 
 int         reading_IR,reading_660,LSB,MSB;
 char        rx_data[4];
-char        accel_2_data[2];
 char        accel_data[6];
 char        temp_val;
 short int   temp_dataX;     // short int: 16 bits.  This allows easy negative results
 short int   temp_dataY;     // short int: 16 bits.  This allows easy negative results
 short int   temp_dataZ;     // short int: 16 bits.  This allows easy negative results
-
-/* From AccelWaveForms
-In Loop:
-*/
+float       t_msec;
 
 int main()
 {
     unsigned char LowB_IR,HighB_IR,LowB_660,HighB_660;
-    char    Reg_Val = 0x00;
-    char    Reg_Num;
 
     myled4 = 0;  // ODD: if this line not included, there is a compiler "internal error"
     pc.baud(230400);
-    i2c_start();        //sets i2c bus at 400,000.  Also set by photodiode setup/reset
-
-    Reg_Num = 0x20; // CTRL_REG1
-    Reg_Val = 0x57; // Nib 1 of 0101: Normal mode, 100 Hz;  Nib 2 of 0111: Normal mode, XYZ enabled
-    Init_Accel (Reg_Num, Reg_Val);
-
-    wait(0.050);    // Delay needed, or 2nd write doesn't work
+    Init_Accel();   // starts LIS3DH
+    restart();      // starts Si1142
 
-    Reg_Num = 0x21; // CTRL_REG2
-    Reg_Val = 0xA0; // 7-6 10: Normal filter mode;  5-4 10: Cut off freq; 3-0 0000: bypass interrupts
-    // High-pass filter bits 5-4: @ 100 Hz:  00 - 2Hz   01 - 1Hz   10- 0.5Hz   11- 0.2Hz
-    Init_Accel (Reg_Num, Reg_Val);
-    wait(0.050);    // Delay needed?
+    wait_ms(30);
+    command (PS_AUTO);   //start measuring
+    wait (0.5);
 
-    pc.printf ("Hello\n");
+    while(1) {
+        if(!int_pin) {
 
-    Get_Accel_Register (0x0F);
-    pc.printf ("I am: %x",accel_data[0]);  //in hex
+            myled3 = !myled3;      // LED on mbed, to follow along
 
-    while (1) {
+            t.reset();
+            t.start();
 
-        Get_Accel_Reg_6 (0x28);
-        LSB = accel_data[0];
-        MSB = accel_data[1];
-        temp_dataX = (MSB * 256) + LSB;
-        pc.printf ("%d, ",temp_dataX);
+            write_reg(IRQ_STATUS,0x04);  // clear the interrupt.
 
-        LSB = accel_data[2];
-        MSB = accel_data[3];
-        temp_dataY = (MSB * 256) + LSB;
-        pc.printf ("%d, ",temp_dataY);
+            read_reg2(PS1_DATA0);
+            LowB_IR = rx_data[0];
+            HighB_IR = rx_data[1];
 
-        LSB = accel_data[4];
-        MSB = accel_data[5];
-        temp_dataZ = (MSB * 256) + LSB;
-        pc.printf ("%d\n",temp_dataZ);
+            LowB_660 = rx_data[2];
+            HighB_660 = rx_data[3];
 
+            reading_IR = (HighB_IR * 256) + LowB_IR;
+            reading_660 = (HighB_660 * 256) + LowB_660;
 
-        wait(0.02);
-    }
-    /*
-        restart();
-        wait_ms(30);
-        command (PS_AUTO);   //start measuring
-        wait (0.5);
+            pc.printf ("%d, %d, ", reading_IR,reading_660);
 
-        while(1) {
-            if(!int_pin) {
+            Get_Accel_Reg_6 (0x28);
 
-                myled3 = !myled3;      // LED on mbed, to follow along
+            temp_dataX = (accel_data[1] * 256) + accel_data[0];
+            pc.printf ("%d, ",temp_dataX);
 
-    //        t.reset();
-    //        t.start();
-                write_reg(IRQ_STATUS,0x04);  // clear the interrupt.
+            temp_dataY = (accel_data[3] * 256) + accel_data[2];
+            pc.printf ("%d, ",temp_dataY);
 
-                read_reg2(PS1_DATA0);
-                LowB_IR = rx_data[0];
-                HighB_IR = rx_data[1];
+            temp_dataZ = (accel_data[5] * 256) + accel_data[4];
+            pc.printf ("%d, ",temp_dataZ);
 
-                LowB_660 = rx_data[2];
-                HighB_660 = rx_data[3];
+            t.stop();
+            t_msec = t.read() * 1000;
+            printf("%4.1f msec\n", t_msec);
+        }
+    }
 
-                reading_IR = (HighB_IR * 256) + LowB_IR;
-                reading_660 = (HighB_660 * 256) + LowB_660;
-
-                pc.printf ("%d,%d\n", reading_IR,reading_660);
-            }
-        }
-    */
 
 }
 /*