test

Dependencies:   mbed MMA8452Q

Fork of HelloWorld by Simon Ford

Revision:
25:16a041dd21db
Parent:
24:0227ae0ad260
Child:
26:523ac79471d8
--- a/main.cpp	Mon Sep 04 17:49:13 2017 +0000
+++ b/main.cpp	Tue Sep 05 20:18:59 2017 +0000
@@ -1,11 +1,13 @@
+/**** Vincent Labbé - labv2507  *****/
+/**** Karan Kalsi -   *****/
 #include "mbed.h"    
 #include "MMA8452Q.h"
 
 Serial pc(USBTX, USBRX); // tx, rx
-//SPI accel(p11,p12,p13);
-//DigitalOut cs(p14);
+SPI spi(p11,p12,p13);
+DigitalOut cs(p14);
 // Communication I2C
-//I2C comI2C(p9,p10); // sda, scl
+//I2C i2c(p9,p10); // sda, scl
 
 
 void UARTInit()
@@ -26,13 +28,11 @@
  // Word select 8-bit character length and set DLAB
  LPC_UART3->LCR  =   0x83;  // 0000 0000 1000 0011
 
+//Baud rate calculation
  usFdiv = (SystemCoreClock / (16*9600));
  LPC_UART3->DLM  =  usFdiv / 256;
  LPC_UART3->DLL  =   usFdiv % 256;
 
- //LPC_UART3->DLM  = ((SystemCoreClock / (16*9600))/256);
- //LPC_UART3->DLL  = ((SystemCoreClock / (16*9600))%256);
-
  // Enable and reset UART3 FIFOs.
  LPC_UART3->FCR       = 0x7;
  
@@ -45,7 +45,6 @@
  // Clear DLAB
  LPC_UART3->LCR &= ~(1 << 7);
  
- //pc.printf("system clock : %d", SystemCoreClock);
 }
 
 char UART3Transmit(int out)
@@ -58,125 +57,129 @@
 
 int main() {
     
- UARTInit();
-    
+ //UARTInit();
+
      float x, y, z ;
- 
+    
      MMA8452Q acc(p9,p10,0x1d);     // acceleration object
- 
-     while (true) {       
-         x = acc.getAccX() ;
+    //i2c.frequency(100000);
+    //int cmd[0] = 0x0D;
+    //12c.write(addr,cmd,1,true);
+    //i2c.read(addr,cmd,1);
+     
+        //Clear display SPI
+         //cs = 0;
+         //wait(0.1);
+         //spi.write(0x76); // Clear display
+         //cs = 1;
+         //wait(0.1);
+         //cs = 1;
+         
+         //clear display UART
+         //UART3Transmit(0x76); // Clear display
+    // while (true) {       
+         x = acc.getAccX() ; 
          y = acc.getAccY() ;
          z = acc.getAccZ() ;
          printf("X[%.3f] Y[%.3f] Z[%.3f]\n",x, y, z) ;
          wait(0.1);
 
-        int X = x * 1000;
+        int X = x * 1000; //Pour enlever floating point et garder une précision
         int Y = y * 1000;
         int Z = z * 1000;
 
-        int angle = (1000-(((2*X*X+2*Y*Y)*1000/(X*X+Y*Y+Z*Z))));
+        int angle = (1000-(((2*X*X+2*Y*Y)*1000/(X*X+Y*Y+Z*Z)))); //Calcul vectoriel pour calculer l'angle à l'horizontal
         
         pc.printf("avant acos = %d", angle);
-        //float resultat = ((3.14/2.0) -( angle + (angle *angle * angle)/6.0 + (3*angle*angle*angle*angle*angle)/40.0))/2;
-        float angleAcos = angle/1000.0;
         
-        int resultat = 500*acos((angleAcos));
-        pc.printf("valeur rad new= %d", resultat);
-        int degree = resultat * 18000/31400;
+        float ratioZaccel = angle/1000.0; //Remettre en floating point pour acos
+        int resultatRAD = 500*acos((ratioZaccel)); //Reponse en RAD
+        
+        pc.printf("valeur rad new= %d", resultatRAD);
+        int degree = resultatRAD * 18000/31400; //Tranfo degree
         pc.printf("valeur deg new = %d", degree);
-        //degree = degree * 10;
-        int degreInt = degree;
-        pc.printf("valeur deg new = %d", degreInt);
-         
-        int rep;
-         // extracting digits
+        //int degreInt = degree;
+        pc.printf("valeur deg new = %d", degree);
+        
+        // extracting digits
+        int digit;
         int digits[4] = {0,0,0,0};
         int i = 0;
-        while(degreInt > 0) {
-        rep = degreInt % 10; //to get the right most digit
-        digits[i]=rep;
-        pc.printf("digit %d = %d, degree int: %d", i, digits[i], degreInt);
-        degreInt /= 10;              //reduce the number by one digit
+        while(degree > 0) {
+        digit = degree % 10; //to get the right most digit
+        digits[i]=digit;
+        pc.printf("digit %d = %d, degree int: %d", i, digits[i], degree);
+        degree /= 10;              //reduce the number by one digit
         ++i;
         }
          
         // Pour la communication UART
-         UART3Transmit(0x76); // Clear display
-         wait(0.1);
-         UART3Transmit(0x77); // Decimal control command
-         UART3Transmit(0x04);// Turn on decimal
-         wait(0.1);
-         UART3Transmit(digits[3]);
-         wait(0.1);
-         UART3Transmit(digits[2]);
-         wait(0.1);
-         UART3Transmit(digits[1]);
-         wait(0.1);
-         UART3Transmit(digits[0]);
-         wait(0.1);
+    
+         //UART3Transmit(0x77); // Decimal control command
+         //UART3Transmit(0x04);// Turn on decimal
+         
+         
+         //UART3Transmit(digits[3]);
+         //UART3Transmit(digits[2]);
+         //UART3Transmit(digits[1]);
+         //UART3Transmit(digits[0]);
+         //UART3Transmit(0xA5);
          
          // Pour la communication SPI
-         /*
+        
          cs = 0;
-         accel.write(0x76); // Clear display
+        /* spi.write(0x77); // Decimal control command
          cs = 1;
-         wait(0.1);
+         wait(0.01);
          cs = 0;
-         accel.write(0x77); // Decimal control command
-         accel.write(0x04);// Turn on decimal
+         spi.write(0x04);// Turn on decimal
          cs = 1;
          wait(0.01);
          cs = 0;
-         accel.write(digits[3]);
+         spi.write(digits[3]);
          cs = 1;
          wait(0.01);
          cs = 0;
-         accel.write(digits[2]);
-         cs = 1;
-         wait(0.01);
-         cs = 0;
-         accel.write(digits[1]);
+         spi.write(digits[2]);
          cs = 1;
          wait(0.01);
          cs = 0;
-         accel.write(digits[0]);
+         spi.write(digits[1]);
          cs = 1;
+         wait(0.01);
+         cs = 0;
+         spi.write(digits[0]);
+         cs = 1; 
+         */
          wait(0.1);
-         */
+         spi.write(0x5);
+         cs = 1;
          
-        }
+    //    }
         
-}
-
-
-
-
-
+} 
 
 
 
 
 /* communication SPI 
 Serial pc(USBTX, USBRX); // tx, rx
-SPI acc(p11,p12,p13);
-DigitalOut cs(p14);
+I2C i2c(p9,p10);   
+
 int main() {
-    while(1){
-    int nombre = 0;
-    pc.printf("Entrez un nombre de 4 chiffres : ");
-    pc.scanf("%d", &nombre);
-    pc.printf("Votre numero entrez est le : %d", nombre);
-  
-    cs = 0;
-    acc.write(nombre);
-    cs = 1;
-    wait(0.2);
-    cs = 0;
-    wait(1);
+    i2c.frequency(100000);
+    char cmd[2] = {0,0};
+    int addr = 0x5A;
+    int data = 0xA4;
+    cmd[0] = addr;
+    cmd[1] = data;
+    
+while(1){
+    i2c.write(0x3A,cmd,2);
+    wait_ms(5);
     }
-}*/
- 
+}
+ */
  /*
 Serial pc(USBTX, USBRX); // tx, rx
 Serial mc(p9,p10);