ほぼ関数かしたLIS3DH

Dependencies:   mbed

LIS3DHnew.cpp

Committer:
tknara
Date:
2015-02-16
Revision:
0:8c85bd2e8177

File content as of revision 0:8c85bd2e8177:

#include "mbed.h"
 
I2C i2c(p9,p10);//sda,scl
Serial pc(USBTX,USBRX);
//DigitalOut led(LED1);
// addr
#define WRITE_ADDR 0x30
#define READ_ADDR 0x31
//   LIS3DH ID
#define I_AM_LIS3DH         0x33
 
//  Register's definition
#define LIS3DH_STATUS_REG_AUX  0x07
#define LIS3DH_OUT_ADC1_L      0x08
#define LIS3DH_OUT_ADC1_H      0x09
#define LIS3DH_OUT_ADC2_L      0x0a
#define LIS3DH_OUT_ADC2_H      0x0b
#define LIS3DH_OUT_ADC3_L      0x0c
#define LIS3DH_OUT_ADC3_H      0x0d
#define LIS3DH_INT_COUNTER_REG 0x0e
#define LIS3DH_WHO_AM_I        0x0f
#define LIS3DH_TEMP_CFG_REG    0x1f
#define LIS3DH_CTRL_REG1       0x20
#define LIS3DH_CTRL_REG2       0x21
#define LIS3DH_CTRL_REG3       0x22
#define LIS3DH_CTRL_REG4       0x23
#define LIS3DH_CTRL_REG5       0x24
#define LIS3DH_CTRL_REG6       0x25
#define LIS3DH_REFERENCE       0x26
#define LIS3DH_STATUS_REG2     0x27
#define LIS3DH_OUT_X_L         0x28
#define LIS3DH_OUT_X_H         0x29
#define LIS3DH_OUT_Y_L         0x2a
#define LIS3DH_OUT_Y_H         0x2b
#define LIS3DH_OUT_Z_L         0x2c
#define LIS3DH_OUT_Z_H         0x2d
#define LIS3DH_FIFO_CTRL_REG   0x2e
#define LIS3DH_FIFO_SRC_REG    0x2f
#define LIS3DH_INT1_CFG        0x30
#define LIS3DH_INT1_SOURCE     0x31
#define LIS3DH_INT1_THS        0x32
#define LIS3DH_INT1_DURATION   0x33
#define LIS3DH_CLICK_CFG       0x38
#define LIS3DH_CLICK_SRC       0x39
#define LIS3DH_CLICK_THS       0x3a
#define LIS3DH_TIME_LIMIT      0x3b
#define LIS3DH_TIME_LATENCY    0x3c
#define LIS3DH_TIME_WINDOW     0x3d
 
// definition for Nomalization
#define LIS3DH_SENSITIVITY_2G  (0.001F)
#define LIS3DH_SENSITIVITY_4G  (0.002F)
#define LIS3DH_SENSITIVITY_8G  (0.004F)
#define LIS3DH_SENSITIVITY_16G (0.012F)
char dbf[2],addr;
int check;
float dt[3];

int che(void);
int cot(void);   
int main(){
    int check;
    check = che();
    if (check == 1) {
        while(1) {
            cot();
            wait(1);
            }
        }
    }
int che(void){
            addr = WRITE_ADDR;
            dbf[0] = LIS3DH_WHO_AM_I;
            i2c.write(addr, dbf,1);
            i2c.read(addr | 1, dbf, 1);
            if  (dbf[0] == I_AM_LIS3DH) {
                return 1;
            }
            else {
                return 0;
            }
}
int cot(void) {
            char data[6];
            dbf[0] = LIS3DH_CTRL_REG1;
            dbf[1] = 0x7f;
            i2c.write(addr , dbf , 2);         
            dbf[0] = LIS3DH_OUT_X_L | 0x80;
            i2c.write(addr, dbf, 1);
            i2c.read(addr | 1, data, 6);
            dt[0] = float(short((data[1]<<8) | data[0])) *LIS3DH_SENSITIVITY_2G  / 15;
            dt[1] = float(short((data[3]<<8) | data[2])) *LIS3DH_SENSITIVITY_2G  / 15;
            dt[2] = float(short((data[5]<<8) | data[4])) *LIS3DH_SENSITIVITY_2G  / 15;
            pc.printf("X=%f __Y=%f __Z=%f \n",dt[0],dt[1],dt[2]);
            return 0;
        }