mbed_adxl335_oled_NXP824

Dependencies:   Adafruit_GFX mbed

Revision:
0:cf82471d4484
Child:
1:427a48756218
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Nov 06 06:50:33 2016 +0000
@@ -0,0 +1,76 @@
+#include "mbed.h"
+#include "Adafruit_SSD1306.h"
+#include <math.h>       /* atan2 */
+
+#define PI 3.14159265
+
+#define DO P0_24    //CLK
+#define DI P0_26    // MOSI
+#define CS P0_10
+#define DC P0_11
+#define RST P0_17
+
+class SPIPreInit : public SPI
+{
+public:
+    SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk) {
+        format(8,3);
+        frequency(2000000);
+    };
+};
+
+SPIPreInit mySpi(DI,NC,DO);
+Adafruit_SSD1306_Spi oled(mySpi,DC,RST,CS,64,128);
+
+DigitalOut myled1(P0_13);
+DigitalOut myled2(P0_16);
+DigitalOut myled3(P0_17);
+
+DigitalIn myinput1(P0_6);
+DigitalIn myinput2(P0_14);
+DigitalIn myinput3(P0_22);
+
+Serial uart(P0_4, P0_0);
+
+AnalogIn myadc1(A0);
+AnalogIn myadc2(A1);
+AnalogIn myadc3(A3);
+
+int main()
+{
+
+    double x, y, z, result;
+    myinput1.mode(PullNone);
+    myinput2.mode(PullNone);
+    myinput3.mode(PullNone);
+
+    oled.clearDisplay();
+    oled.setTextSize(2);
+    oled.setTextCursor(0,0);
+
+    while(1) {
+
+        uart.printf("X: %2.3f V\r\n" ,myadc1.read()*3.3);
+        uart.printf("Y: %2.3f V\r\n" ,myadc2.read()*3.3);
+        uart.printf("Z: %2.3f V\r\n" ,myadc3.read()*3.3);
+
+        z= (myadc2.read()*3.3-1.625)*3025.72;
+        x = (myadc1.read()*3.3-1.646)*3048.78;
+        y = (myadc3.read()*3.3-1.672)*3067.48;
+
+        result = atan2 (x,y) * 180 / PI;
+        uart.printf ("The arc tangent for (x=%f, y=%f) is %.4f degrees\r\n", x, y, result );
+        oled.printf (" %.2f D\r\n",result);
+        oled.printf ("Y: %.0f mg\r\n",z);
+
+        myled1 = 1;
+        myled2 = 0;
+        myled3 = 1;
+
+        oled.display();
+
+        wait(1);
+        oled.setTextCursor(0,0);
+        oled.clearDisplay();
+    }
+}