Module 1 root square

Dependencies:   mbed MMA8451Q

Files at this revision

API Documentation at this revision

Comitter:
nmaududi
Date:
Thu Sep 19 06:16:00 2019 +0000
Commit message:
Revision 1 of root square for Module 1

Changed in this revision

MMA8451Q.lib 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
rootsquare.c Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8451Q.lib	Thu Sep 19 06:16:00 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/emilmont/code/MMA8451Q/#c4d879a39775
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 19 06:16:00 2019 +0000
@@ -0,0 +1,39 @@
+#include "mbed.h"
+#include "MMA8451Q.h"
+
+#if   defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+  PinName const SDA = PTE25;
+  PinName const SCL = PTE24;
+#elif defined (TARGET_KL05Z)
+  PinName const SDA = PTB4;
+  PinName const SCL = PTB3;
+#elif defined (TARGET_K20D50M)
+  PinName const SDA = PTB1;
+  PinName const SCL = PTB0;
+#else
+  #error TARGET NOT DEFINED
+#endif
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+int main(void)
+{
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+    PwmOut rled(LED1);
+    PwmOut gled(LED2);
+    PwmOut bled(LED3);
+
+    printf("MMA8451 ID: %d\n", acc.getWhoAmI());
+
+    while (true) {
+        float x, y, z;
+        x = abs(acc.getAccX());
+        y = abs(acc.getAccY());
+        z = abs(acc.getAccZ());
+        rled = 1.0f - x;
+        gled = 1.0f - y;
+        bled = 1.0f - z;
+        wait(0.1f);
+        printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 19 06:16:00 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rootsquare.c	Thu Sep 19 06:16:00 2019 +0000
@@ -0,0 +1,107 @@
+/************************************************************************//**
+* \file main.c
+* \brief LAB EXERCISE 5.2 - SQUARE ROOT APPROXIMATION
+*
+* Write an assembly code subroutine to approximate the square root of an 
+* argument using the bisection method. All math is done with integers, so the 
+* resulting square root will also be an integer
+******************************************************************************
+* GOOD LUCK!
+ ****************************************************************************/
+
+ #include "stdint.h"
+ 
+ 
+ /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
+ #include "string.h"
+ /** @endcond */
+ 
+ /**
+ * @brief 5863 Write a brief description of the function here
+ *
+ * Detailed description of the function after one line gap
+ *
+ * @note You can also add a note this way
+ *
+ * @param[in] 
+ *  You can also start writing on a new line. 
+ *  And continue on the next line. This is how you describe an input parameter
+ *
+ * @return
+ *  This is how you describe the return value
+ * 
+ */
+__asm int my_sqrt(int x){
+    
+    MOVS r1, #0; r1 will be the register to store a, initialized to 0
+    MOVS r2,#200; r2 is for variable b, store the largest squaure root round[sqrt(2^31-1)] (46341)
+    MOVS r3,#1;  r3 is for variable c, it has been define and intialized
+  NEGS r3,r3;
+    MOVS r4,#0; r4 is for variable done
+    
+loop_while  
+    
+    MOVS r5,r3; r5 is allocated for c old within the code
+    ADDS r3, r1, r2; stores into c = a+b
+    LSRS r3,r3,#1; multiply c by 0.5 to get c= (a+b)/2, completed a shift to complete division
+  MOVS r6,r3; r6 is allocated for c within the code
+    MULS r6,r6,r6; r6 now contains x*x as implemented int code
+    
+    CMP   r6,r0;   Compare c*c and x, start of the if loop
+    BEQ sqrt_loop_done
+    BLT sqrt_elseif
+    
+  MOVS r2,r3; b is loaded with the value of c, b <--c
+    
+    B end_of_sqrtloop
+sqrt_elseif
+    MOVS r1,r3; a is loaded with the value of c, a <--c
+    B end_of_sqrtloop
+    
+sqrt_loop_done
+    
+    MOVS r4,#1; store 1 into the variable done
+    
+end_of_sqrtloop
+    MVNS r6,r4;     r6 ==!done, was c*c above
+    CMP r5, r3;     compare cold and c
+    BNE skip
+    MOVS r7,#0;      set r7 equal to 0 if cold and c are equal
+    B skip_over
+skip
+    MOVS r7,#1;
+    NEGS r7,r7;
+skip_over
+    CMP r6,r7;
+    BEQ loop_while
+    
+  BLX    r3             ; Else return from subroutine with link and returning the variable c
+    //CMP 
+    
+    //Write your code here
+
+}
+
+/*----------------------------------------------------------------------------
+ MAIN function
+ *----------------------------------------------------------------------------*/
+ /**
+ * @brief Main function
+ *
+ * Detailed description of the main
+ */
+int main(void){
+    volatile int r1,r2,r3, j=0;
+    int i;                  
+  r1 = my_sqrt(25);     // should be 0
+  r2 = my_sqrt(25);    // should be 5
+    r3 = my_sqrt(133);  // should be 11
+  for (i=0; i<10000; i++){
+        r = my_sqrt(i);
+    j+=r;
+  }
+    while(1)
+        ;
+}
+
+// *******************************ARM University Program Copyright © ARM Ltd 2014*************************************/