mbed_adxl335_oled_NXP824
Dependencies: Adafruit_GFX mbed
main.cpp@0:cf82471d4484, 2016-11-06 (annotated)
- Committer:
- pythonworld
- Date:
- Sun Nov 06 06:50:33 2016 +0000
- Revision:
- 0:cf82471d4484
- Child:
- 1:427a48756218
NXP824 ADXL335 decline measure.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pythonworld | 0:cf82471d4484 | 1 | #include "mbed.h" |
pythonworld | 0:cf82471d4484 | 2 | #include "Adafruit_SSD1306.h" |
pythonworld | 0:cf82471d4484 | 3 | #include <math.h> /* atan2 */ |
pythonworld | 0:cf82471d4484 | 4 | |
pythonworld | 0:cf82471d4484 | 5 | #define PI 3.14159265 |
pythonworld | 0:cf82471d4484 | 6 | |
pythonworld | 0:cf82471d4484 | 7 | #define DO P0_24 //CLK |
pythonworld | 0:cf82471d4484 | 8 | #define DI P0_26 // MOSI |
pythonworld | 0:cf82471d4484 | 9 | #define CS P0_10 |
pythonworld | 0:cf82471d4484 | 10 | #define DC P0_11 |
pythonworld | 0:cf82471d4484 | 11 | #define RST P0_17 |
pythonworld | 0:cf82471d4484 | 12 | |
pythonworld | 0:cf82471d4484 | 13 | class SPIPreInit : public SPI |
pythonworld | 0:cf82471d4484 | 14 | { |
pythonworld | 0:cf82471d4484 | 15 | public: |
pythonworld | 0:cf82471d4484 | 16 | SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk) { |
pythonworld | 0:cf82471d4484 | 17 | format(8,3); |
pythonworld | 0:cf82471d4484 | 18 | frequency(2000000); |
pythonworld | 0:cf82471d4484 | 19 | }; |
pythonworld | 0:cf82471d4484 | 20 | }; |
pythonworld | 0:cf82471d4484 | 21 | |
pythonworld | 0:cf82471d4484 | 22 | SPIPreInit mySpi(DI,NC,DO); |
pythonworld | 0:cf82471d4484 | 23 | Adafruit_SSD1306_Spi oled(mySpi,DC,RST,CS,64,128); |
pythonworld | 0:cf82471d4484 | 24 | |
pythonworld | 0:cf82471d4484 | 25 | DigitalOut myled1(P0_13); |
pythonworld | 0:cf82471d4484 | 26 | DigitalOut myled2(P0_16); |
pythonworld | 0:cf82471d4484 | 27 | DigitalOut myled3(P0_17); |
pythonworld | 0:cf82471d4484 | 28 | |
pythonworld | 0:cf82471d4484 | 29 | DigitalIn myinput1(P0_6); |
pythonworld | 0:cf82471d4484 | 30 | DigitalIn myinput2(P0_14); |
pythonworld | 0:cf82471d4484 | 31 | DigitalIn myinput3(P0_22); |
pythonworld | 0:cf82471d4484 | 32 | |
pythonworld | 0:cf82471d4484 | 33 | Serial uart(P0_4, P0_0); |
pythonworld | 0:cf82471d4484 | 34 | |
pythonworld | 0:cf82471d4484 | 35 | AnalogIn myadc1(A0); |
pythonworld | 0:cf82471d4484 | 36 | AnalogIn myadc2(A1); |
pythonworld | 0:cf82471d4484 | 37 | AnalogIn myadc3(A3); |
pythonworld | 0:cf82471d4484 | 38 | |
pythonworld | 0:cf82471d4484 | 39 | int main() |
pythonworld | 0:cf82471d4484 | 40 | { |
pythonworld | 0:cf82471d4484 | 41 | |
pythonworld | 0:cf82471d4484 | 42 | double x, y, z, result; |
pythonworld | 0:cf82471d4484 | 43 | myinput1.mode(PullNone); |
pythonworld | 0:cf82471d4484 | 44 | myinput2.mode(PullNone); |
pythonworld | 0:cf82471d4484 | 45 | myinput3.mode(PullNone); |
pythonworld | 0:cf82471d4484 | 46 | |
pythonworld | 0:cf82471d4484 | 47 | oled.clearDisplay(); |
pythonworld | 0:cf82471d4484 | 48 | oled.setTextSize(2); |
pythonworld | 0:cf82471d4484 | 49 | oled.setTextCursor(0,0); |
pythonworld | 0:cf82471d4484 | 50 | |
pythonworld | 0:cf82471d4484 | 51 | while(1) { |
pythonworld | 0:cf82471d4484 | 52 | |
pythonworld | 0:cf82471d4484 | 53 | uart.printf("X: %2.3f V\r\n" ,myadc1.read()*3.3); |
pythonworld | 0:cf82471d4484 | 54 | uart.printf("Y: %2.3f V\r\n" ,myadc2.read()*3.3); |
pythonworld | 0:cf82471d4484 | 55 | uart.printf("Z: %2.3f V\r\n" ,myadc3.read()*3.3); |
pythonworld | 0:cf82471d4484 | 56 | |
pythonworld | 0:cf82471d4484 | 57 | z= (myadc2.read()*3.3-1.625)*3025.72; |
pythonworld | 0:cf82471d4484 | 58 | x = (myadc1.read()*3.3-1.646)*3048.78; |
pythonworld | 0:cf82471d4484 | 59 | y = (myadc3.read()*3.3-1.672)*3067.48; |
pythonworld | 0:cf82471d4484 | 60 | |
pythonworld | 0:cf82471d4484 | 61 | result = atan2 (x,y) * 180 / PI; |
pythonworld | 0:cf82471d4484 | 62 | uart.printf ("The arc tangent for (x=%f, y=%f) is %.4f degrees\r\n", x, y, result ); |
pythonworld | 0:cf82471d4484 | 63 | oled.printf (" %.2f D\r\n",result); |
pythonworld | 0:cf82471d4484 | 64 | oled.printf ("Y: %.0f mg\r\n",z); |
pythonworld | 0:cf82471d4484 | 65 | |
pythonworld | 0:cf82471d4484 | 66 | myled1 = 1; |
pythonworld | 0:cf82471d4484 | 67 | myled2 = 0; |
pythonworld | 0:cf82471d4484 | 68 | myled3 = 1; |
pythonworld | 0:cf82471d4484 | 69 | |
pythonworld | 0:cf82471d4484 | 70 | oled.display(); |
pythonworld | 0:cf82471d4484 | 71 | |
pythonworld | 0:cf82471d4484 | 72 | wait(1); |
pythonworld | 0:cf82471d4484 | 73 | oled.setTextCursor(0,0); |
pythonworld | 0:cf82471d4484 | 74 | oled.clearDisplay(); |
pythonworld | 0:cf82471d4484 | 75 | } |
pythonworld | 0:cf82471d4484 | 76 | } |