![](/media/cache/profiles/cc38c8ffd23cef2e3e5c21d20c1db636.jpg.50x50_q85.png)
all publish
main.cpp@0:4c5353013c5f, 2019-05-06 (annotated)
- Committer:
- daikinect
- Date:
- Mon May 06 00:20:57 2019 +0000
- Revision:
- 0:4c5353013c5f
all publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daikinect | 0:4c5353013c5f | 1 | #include "mbed.h" |
daikinect | 0:4c5353013c5f | 2 | #include <ADXL345.h> |
daikinect | 0:4c5353013c5f | 3 | |
daikinect | 0:4c5353013c5f | 4 | #define MOSI D11 |
daikinect | 0:4c5353013c5f | 5 | #define MISO D12 |
daikinect | 0:4c5353013c5f | 6 | #define SCK D13 |
daikinect | 0:4c5353013c5f | 7 | #define CS D10 |
daikinect | 0:4c5353013c5f | 8 | |
daikinect | 0:4c5353013c5f | 9 | DigitalOut led_1(A2); |
daikinect | 0:4c5353013c5f | 10 | DigitalOut led_2(A3); |
daikinect | 0:4c5353013c5f | 11 | ADXL345 aMeter(MOSI,MISO,SCK,CS); |
daikinect | 0:4c5353013c5f | 12 | Serial pc(USBTX, USBRX,230400); |
daikinect | 0:4c5353013c5f | 13 | |
daikinect | 0:4c5353013c5f | 14 | void aSetup() |
daikinect | 0:4c5353013c5f | 15 | { |
daikinect | 0:4c5353013c5f | 16 | //pc.printf("Device ID is: 0x%02x\n", aMeter.getDevId()); |
daikinect | 0:4c5353013c5f | 17 | aMeter.setPowerControl(0x00);//スタンバイモードに移行してデバイスを設定します |
daikinect | 0:4c5353013c5f | 18 | aMeter.setDataFormatControl(0x0B);//フル解像度、+/- 16g、4mg / LSB。 |
daikinect | 0:4c5353013c5f | 19 | aMeter.setDataRate(ADXL345_3200HZ);// 3.2kHzのデータレート。 |
daikinect | 0:4c5353013c5f | 20 | aMeter.setPowerControl(0x08);//測定モード。 |
daikinect | 0:4c5353013c5f | 21 | } |
daikinect | 0:4c5353013c5f | 22 | |
daikinect | 0:4c5353013c5f | 23 | int main() |
daikinect | 0:4c5353013c5f | 24 | { |
daikinect | 0:4c5353013c5f | 25 | aSetup(); |
daikinect | 0:4c5353013c5f | 26 | led_1=1; |
daikinect | 0:4c5353013c5f | 27 | int max=0,n=0,xyz[3]= {0,0,0};//読み取りXYZ |
daikinect | 0:4c5353013c5f | 28 | while (1) { |
daikinect | 0:4c5353013c5f | 29 | //wait(0.05); |
daikinect | 0:4c5353013c5f | 30 | aMeter.getOutput(xyz);//xyz取得 |
daikinect | 0:4c5353013c5f | 31 | max=0; |
daikinect | 0:4c5353013c5f | 32 | for(int i=0; i<100; i++) { |
daikinect | 0:4c5353013c5f | 33 | n=abs(xyz[2]-240); |
daikinect | 0:4c5353013c5f | 34 | if(n>max)max=n; |
daikinect | 0:4c5353013c5f | 35 | wait(0.001); |
daikinect | 0:4c5353013c5f | 36 | } |
daikinect | 0:4c5353013c5f | 37 | if(max>80) { |
daikinect | 0:4c5353013c5f | 38 | led_1=0; |
daikinect | 0:4c5353013c5f | 39 | } |
daikinect | 0:4c5353013c5f | 40 | if(max>20) { |
daikinect | 0:4c5353013c5f | 41 | led_2=1; |
daikinect | 0:4c5353013c5f | 42 | } else { |
daikinect | 0:4c5353013c5f | 43 | led_2=0; |
daikinect | 0:4c5353013c5f | 44 | led_1=1; |
daikinect | 0:4c5353013c5f | 45 | } |
daikinect | 0:4c5353013c5f | 46 | pc.printf("%i,200\n",(int16_t)xyz[2]-240);//xyz表示 |
daikinect | 0:4c5353013c5f | 47 | } |
daikinect | 0:4c5353013c5f | 48 | } |