ほぼ関数かしたLIS3DH

Dependencies:   mbed

Committer:
tknara
Date:
Mon Feb 16 11:10:28 2015 +0000
Revision:
0:8c85bd2e8177
LIS3DH

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tknara 0:8c85bd2e8177 1 #include "mbed.h"
tknara 0:8c85bd2e8177 2
tknara 0:8c85bd2e8177 3 I2C i2c(p9,p10);//sda,scl
tknara 0:8c85bd2e8177 4 Serial pc(USBTX,USBRX);
tknara 0:8c85bd2e8177 5 //DigitalOut led(LED1);
tknara 0:8c85bd2e8177 6 // addr
tknara 0:8c85bd2e8177 7 #define WRITE_ADDR 0x30
tknara 0:8c85bd2e8177 8 #define READ_ADDR 0x31
tknara 0:8c85bd2e8177 9 // LIS3DH ID
tknara 0:8c85bd2e8177 10 #define I_AM_LIS3DH 0x33
tknara 0:8c85bd2e8177 11
tknara 0:8c85bd2e8177 12 // Register's definition
tknara 0:8c85bd2e8177 13 #define LIS3DH_STATUS_REG_AUX 0x07
tknara 0:8c85bd2e8177 14 #define LIS3DH_OUT_ADC1_L 0x08
tknara 0:8c85bd2e8177 15 #define LIS3DH_OUT_ADC1_H 0x09
tknara 0:8c85bd2e8177 16 #define LIS3DH_OUT_ADC2_L 0x0a
tknara 0:8c85bd2e8177 17 #define LIS3DH_OUT_ADC2_H 0x0b
tknara 0:8c85bd2e8177 18 #define LIS3DH_OUT_ADC3_L 0x0c
tknara 0:8c85bd2e8177 19 #define LIS3DH_OUT_ADC3_H 0x0d
tknara 0:8c85bd2e8177 20 #define LIS3DH_INT_COUNTER_REG 0x0e
tknara 0:8c85bd2e8177 21 #define LIS3DH_WHO_AM_I 0x0f
tknara 0:8c85bd2e8177 22 #define LIS3DH_TEMP_CFG_REG 0x1f
tknara 0:8c85bd2e8177 23 #define LIS3DH_CTRL_REG1 0x20
tknara 0:8c85bd2e8177 24 #define LIS3DH_CTRL_REG2 0x21
tknara 0:8c85bd2e8177 25 #define LIS3DH_CTRL_REG3 0x22
tknara 0:8c85bd2e8177 26 #define LIS3DH_CTRL_REG4 0x23
tknara 0:8c85bd2e8177 27 #define LIS3DH_CTRL_REG5 0x24
tknara 0:8c85bd2e8177 28 #define LIS3DH_CTRL_REG6 0x25
tknara 0:8c85bd2e8177 29 #define LIS3DH_REFERENCE 0x26
tknara 0:8c85bd2e8177 30 #define LIS3DH_STATUS_REG2 0x27
tknara 0:8c85bd2e8177 31 #define LIS3DH_OUT_X_L 0x28
tknara 0:8c85bd2e8177 32 #define LIS3DH_OUT_X_H 0x29
tknara 0:8c85bd2e8177 33 #define LIS3DH_OUT_Y_L 0x2a
tknara 0:8c85bd2e8177 34 #define LIS3DH_OUT_Y_H 0x2b
tknara 0:8c85bd2e8177 35 #define LIS3DH_OUT_Z_L 0x2c
tknara 0:8c85bd2e8177 36 #define LIS3DH_OUT_Z_H 0x2d
tknara 0:8c85bd2e8177 37 #define LIS3DH_FIFO_CTRL_REG 0x2e
tknara 0:8c85bd2e8177 38 #define LIS3DH_FIFO_SRC_REG 0x2f
tknara 0:8c85bd2e8177 39 #define LIS3DH_INT1_CFG 0x30
tknara 0:8c85bd2e8177 40 #define LIS3DH_INT1_SOURCE 0x31
tknara 0:8c85bd2e8177 41 #define LIS3DH_INT1_THS 0x32
tknara 0:8c85bd2e8177 42 #define LIS3DH_INT1_DURATION 0x33
tknara 0:8c85bd2e8177 43 #define LIS3DH_CLICK_CFG 0x38
tknara 0:8c85bd2e8177 44 #define LIS3DH_CLICK_SRC 0x39
tknara 0:8c85bd2e8177 45 #define LIS3DH_CLICK_THS 0x3a
tknara 0:8c85bd2e8177 46 #define LIS3DH_TIME_LIMIT 0x3b
tknara 0:8c85bd2e8177 47 #define LIS3DH_TIME_LATENCY 0x3c
tknara 0:8c85bd2e8177 48 #define LIS3DH_TIME_WINDOW 0x3d
tknara 0:8c85bd2e8177 49
tknara 0:8c85bd2e8177 50 // definition for Nomalization
tknara 0:8c85bd2e8177 51 #define LIS3DH_SENSITIVITY_2G (0.001F)
tknara 0:8c85bd2e8177 52 #define LIS3DH_SENSITIVITY_4G (0.002F)
tknara 0:8c85bd2e8177 53 #define LIS3DH_SENSITIVITY_8G (0.004F)
tknara 0:8c85bd2e8177 54 #define LIS3DH_SENSITIVITY_16G (0.012F)
tknara 0:8c85bd2e8177 55 char dbf[2],addr;
tknara 0:8c85bd2e8177 56 int check;
tknara 0:8c85bd2e8177 57 float dt[3];
tknara 0:8c85bd2e8177 58
tknara 0:8c85bd2e8177 59 int che(void);
tknara 0:8c85bd2e8177 60 int cot(void);
tknara 0:8c85bd2e8177 61 int main(){
tknara 0:8c85bd2e8177 62 int check;
tknara 0:8c85bd2e8177 63 check = che();
tknara 0:8c85bd2e8177 64 if (check == 1) {
tknara 0:8c85bd2e8177 65 while(1) {
tknara 0:8c85bd2e8177 66 cot();
tknara 0:8c85bd2e8177 67 wait(1);
tknara 0:8c85bd2e8177 68 }
tknara 0:8c85bd2e8177 69 }
tknara 0:8c85bd2e8177 70 }
tknara 0:8c85bd2e8177 71 int che(void){
tknara 0:8c85bd2e8177 72 addr = WRITE_ADDR;
tknara 0:8c85bd2e8177 73 dbf[0] = LIS3DH_WHO_AM_I;
tknara 0:8c85bd2e8177 74 i2c.write(addr, dbf,1);
tknara 0:8c85bd2e8177 75 i2c.read(addr | 1, dbf, 1);
tknara 0:8c85bd2e8177 76 if (dbf[0] == I_AM_LIS3DH) {
tknara 0:8c85bd2e8177 77 return 1;
tknara 0:8c85bd2e8177 78 }
tknara 0:8c85bd2e8177 79 else {
tknara 0:8c85bd2e8177 80 return 0;
tknara 0:8c85bd2e8177 81 }
tknara 0:8c85bd2e8177 82 }
tknara 0:8c85bd2e8177 83 int cot(void) {
tknara 0:8c85bd2e8177 84 char data[6];
tknara 0:8c85bd2e8177 85 dbf[0] = LIS3DH_CTRL_REG1;
tknara 0:8c85bd2e8177 86 dbf[1] = 0x7f;
tknara 0:8c85bd2e8177 87 i2c.write(addr , dbf , 2);
tknara 0:8c85bd2e8177 88 dbf[0] = LIS3DH_OUT_X_L | 0x80;
tknara 0:8c85bd2e8177 89 i2c.write(addr, dbf, 1);
tknara 0:8c85bd2e8177 90 i2c.read(addr | 1, data, 6);
tknara 0:8c85bd2e8177 91 dt[0] = float(short((data[1]<<8) | data[0])) *LIS3DH_SENSITIVITY_2G / 15;
tknara 0:8c85bd2e8177 92 dt[1] = float(short((data[3]<<8) | data[2])) *LIS3DH_SENSITIVITY_2G / 15;
tknara 0:8c85bd2e8177 93 dt[2] = float(short((data[5]<<8) | data[4])) *LIS3DH_SENSITIVITY_2G / 15;
tknara 0:8c85bd2e8177 94 pc.printf("X=%f __Y=%f __Z=%f \n",dt[0],dt[1],dt[2]);
tknara 0:8c85bd2e8177 95 return 0;
tknara 0:8c85bd2e8177 96 }