mbed_adxl335_oled_NXP824
Dependencies: Adafruit_GFX mbed
main.cpp@3:e6eb75606acf, 2017-03-15 (annotated)
- Committer:
- pythonworld
- Date:
- Wed Mar 15 11:20:57 2017 +0000
- Revision:
- 3:e6eb75606acf
- Parent:
- 2:2c53f4db30f9
little change
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 | 1:427a48756218 | 22 | |
pythonworld | 0:cf82471d4484 | 23 | SPIPreInit mySpi(DI,NC,DO); |
pythonworld | 0:cf82471d4484 | 24 | Adafruit_SSD1306_Spi oled(mySpi,DC,RST,CS,64,128); |
pythonworld | 0:cf82471d4484 | 25 | |
pythonworld | 0:cf82471d4484 | 26 | DigitalOut myled1(P0_13); |
pythonworld | 0:cf82471d4484 | 27 | DigitalOut myled2(P0_16); |
pythonworld | 0:cf82471d4484 | 28 | DigitalOut myled3(P0_17); |
pythonworld | 0:cf82471d4484 | 29 | |
pythonworld | 0:cf82471d4484 | 30 | DigitalIn myinput1(P0_6); |
pythonworld | 0:cf82471d4484 | 31 | DigitalIn myinput2(P0_14); |
pythonworld | 0:cf82471d4484 | 32 | DigitalIn myinput3(P0_22); |
pythonworld | 0:cf82471d4484 | 33 | |
pythonworld | 1:427a48756218 | 34 | InterruptIn my_button(P0_1); |
pythonworld | 1:427a48756218 | 35 | DigitalIn my_buttoninput(P0_1); |
pythonworld | 1:427a48756218 | 36 | |
pythonworld | 0:cf82471d4484 | 37 | Serial uart(P0_4, P0_0); |
pythonworld | 0:cf82471d4484 | 38 | |
pythonworld | 0:cf82471d4484 | 39 | AnalogIn myadc1(A0); |
pythonworld | 0:cf82471d4484 | 40 | AnalogIn myadc2(A1); |
pythonworld | 0:cf82471d4484 | 41 | AnalogIn myadc3(A3); |
pythonworld | 0:cf82471d4484 | 42 | |
pythonworld | 1:427a48756218 | 43 | int mode=0; |
pythonworld | 1:427a48756218 | 44 | |
pythonworld | 1:427a48756218 | 45 | void pressed() |
pythonworld | 1:427a48756218 | 46 | { |
pythonworld | 1:427a48756218 | 47 | wait_ms(20); |
pythonworld | 1:427a48756218 | 48 | if(my_buttoninput==0) { |
pythonworld | 1:427a48756218 | 49 | if(mode==0) |
pythonworld | 1:427a48756218 | 50 | mode=1; |
pythonworld | 1:427a48756218 | 51 | else mode =0; |
pythonworld | 1:427a48756218 | 52 | } |
pythonworld | 1:427a48756218 | 53 | |
pythonworld | 1:427a48756218 | 54 | |
pythonworld | 1:427a48756218 | 55 | } |
pythonworld | 1:427a48756218 | 56 | |
pythonworld | 0:cf82471d4484 | 57 | int main() |
pythonworld | 0:cf82471d4484 | 58 | { |
pythonworld | 0:cf82471d4484 | 59 | double x, y, z, result; |
pythonworld | 0:cf82471d4484 | 60 | myinput1.mode(PullNone); |
pythonworld | 0:cf82471d4484 | 61 | myinput2.mode(PullNone); |
pythonworld | 0:cf82471d4484 | 62 | myinput3.mode(PullNone); |
pythonworld | 0:cf82471d4484 | 63 | |
pythonworld | 1:427a48756218 | 64 | my_button.fall(&pressed); |
pythonworld | 1:427a48756218 | 65 | |
pythonworld | 0:cf82471d4484 | 66 | oled.clearDisplay(); |
pythonworld | 0:cf82471d4484 | 67 | oled.setTextSize(2); |
pythonworld | 0:cf82471d4484 | 68 | oled.setTextCursor(0,0); |
pythonworld | 0:cf82471d4484 | 69 | |
pythonworld | 0:cf82471d4484 | 70 | while(1) { |
pythonworld | 0:cf82471d4484 | 71 | |
pythonworld | 0:cf82471d4484 | 72 | uart.printf("X: %2.3f V\r\n" ,myadc1.read()*3.3); |
pythonworld | 0:cf82471d4484 | 73 | uart.printf("Y: %2.3f V\r\n" ,myadc2.read()*3.3); |
pythonworld | 0:cf82471d4484 | 74 | uart.printf("Z: %2.3f V\r\n" ,myadc3.read()*3.3); |
pythonworld | 0:cf82471d4484 | 75 | x = (myadc1.read()*3.3-1.646)*3048.78; |
pythonworld | 1:427a48756218 | 76 | y = (myadc2.read()*3.3-1.642)*3025.72; |
pythonworld | 1:427a48756218 | 77 | z = (myadc3.read()*3.3-1.672)*3067.48; |
pythonworld | 1:427a48756218 | 78 | if(mode==0) { |
pythonworld | 1:427a48756218 | 79 | result = atan2(x,sqrt(z*z+y*y))*180/PI; |
pythonworld | 1:427a48756218 | 80 | uart.printf("Alpha: %2.3f degrees\r\n" ,result); |
pythonworld | 1:427a48756218 | 81 | oled.printf ("x %.1f d\r\n",result); |
pythonworld | 1:427a48756218 | 82 | result = atan2(y,sqrt(z*z+x*x))*180/PI; |
pythonworld | 1:427a48756218 | 83 | uart.printf("Beta: %2.3f degrees\r\n" ,result); |
pythonworld | 1:427a48756218 | 84 | oled.printf ("y %.1f d\r\n",result); |
pythonworld | 1:427a48756218 | 85 | result = atan2(z,sqrt(x*x+y*y))*180/PI; |
pythonworld | 1:427a48756218 | 86 | uart.printf("Gamma: %2.3f degrees\r\n" ,result); |
pythonworld | 1:427a48756218 | 87 | oled.printf ("z %.1f d\r\n",result); |
pythonworld | 1:427a48756218 | 88 | oled.printf (" Mode A\r\n"); |
pythonworld | 0:cf82471d4484 | 89 | |
pythonworld | 1:427a48756218 | 90 | } else { |
pythonworld | 1:427a48756218 | 91 | result = atan2 (x,z) * 180 / PI; |
pythonworld | 1:427a48756218 | 92 | uart.printf ("The arc tangent for (x=%f, z=%f) is %.4f degrees \r\n", x, z, result ); |
pythonworld | 1:427a48756218 | 93 | oled.printf ("X %.1f D\r\n",result); |
pythonworld | 1:427a48756218 | 94 | oled.printf ("Y: %.0f mg\r\n",y); |
pythonworld | 1:427a48756218 | 95 | // result = atan2 (y,z) * 180 / PI; |
pythonworld | 1:427a48756218 | 96 | // uart.printf ("The arc tangent for (y=%f, z=%f) is %.4f degrees\r\n", y, z, result ); |
pythonworld | 1:427a48756218 | 97 | // oled.printf ("Y %.1f D\r\n",result); |
pythonworld | 1:427a48756218 | 98 | // oled.printf ("Y: %.0f mg\r\n",y); |
pythonworld | 1:427a48756218 | 99 | oled.printf (" Mode B\r\n"); |
pythonworld | 1:427a48756218 | 100 | } |
pythonworld | 0:cf82471d4484 | 101 | myled1 = 1; |
pythonworld | 0:cf82471d4484 | 102 | myled2 = 0; |
pythonworld | 0:cf82471d4484 | 103 | myled3 = 1; |
pythonworld | 0:cf82471d4484 | 104 | |
pythonworld | 0:cf82471d4484 | 105 | oled.display(); |
pythonworld | 0:cf82471d4484 | 106 | |
pythonworld | 1:427a48756218 | 107 | wait(1.5); |
pythonworld | 0:cf82471d4484 | 108 | oled.setTextCursor(0,0); |
pythonworld | 0:cf82471d4484 | 109 | oled.clearDisplay(); |
pythonworld | 0:cf82471d4484 | 110 | } |
pythonworld | 0:cf82471d4484 | 111 | } |