Library for LIS2DH12 acc.

Dependents:   acnSensa_LIS aconnoCellularGnss Lizzy

Committer:
jurica238814
Date:
Fri Dec 29 17:40:50 2017 +0000
Revision:
0:cc5d477d5dbe
Child:
1:d89f4b12116b
Init commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:cc5d477d5dbe 1 /*
jurica238814 0:cc5d477d5dbe 2 *
jurica238814 0:cc5d477d5dbe 3 * LIS2DH12 MEMS digital output motion sensor
jurica238814 0:cc5d477d5dbe 4 * More info @ aconno.de
jurica238814 0:cc5d477d5dbe 5 * Made by Jurica Resetar
jurica238814 0:cc5d477d5dbe 6 * jurica_resetar@yahoo.com
jurica238814 0:cc5d477d5dbe 7 *
jurica238814 0:cc5d477d5dbe 8 */
jurica238814 0:cc5d477d5dbe 9
jurica238814 0:cc5d477d5dbe 10 #include "Lis2dh12.h"
jurica238814 0:cc5d477d5dbe 11 #include "Lis2dh12_regs.h"
jurica238814 0:cc5d477d5dbe 12
jurica238814 0:cc5d477d5dbe 13 Lis2dh12::Lis2dh12(I2C *i2c_, char address) : i2c(i2c_, address){
jurica238814 0:cc5d477d5dbe 14 }
jurica238814 0:cc5d477d5dbe 15
jurica238814 0:cc5d477d5dbe 16 uint8_t Lis2dh12::whoIAm(){
jurica238814 0:cc5d477d5dbe 17 char regAddr = (char)WHO_AM_I;
jurica238814 0:cc5d477d5dbe 18 char regData;
jurica238814 0:cc5d477d5dbe 19 i2c.readFromReg(regAddr, &regData, 1);
jurica238814 0:cc5d477d5dbe 20 return (uint8_t)regData;
jurica238814 0:cc5d477d5dbe 21 }
jurica238814 0:cc5d477d5dbe 22
jurica238814 0:cc5d477d5dbe 23 uint8_t Lis2dh12::setMode(Mode mode){
jurica238814 0:cc5d477d5dbe 24 char ctrl1Copy;
jurica238814 0:cc5d477d5dbe 25 char ctrl4Copy;
jurica238814 0:cc5d477d5dbe 26 uint8_t success;
jurica238814 0:cc5d477d5dbe 27
jurica238814 0:cc5d477d5dbe 28 i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 29 i2c.readFromReg((char)CTRL_REG4, &ctrl4Copy, 1);
jurica238814 0:cc5d477d5dbe 30
jurica238814 0:cc5d477d5dbe 31 switch(mode){
jurica238814 0:cc5d477d5dbe 32 case HIGH_RES:
jurica238814 0:cc5d477d5dbe 33 ctrl1Copy &= 0xF7;
jurica238814 0:cc5d477d5dbe 34 ctrl4Copy |= 0x08;
jurica238814 0:cc5d477d5dbe 35 break;
jurica238814 0:cc5d477d5dbe 36 case NORMAL:
jurica238814 0:cc5d477d5dbe 37 ctrl1Copy &= 0xF7;
jurica238814 0:cc5d477d5dbe 38 ctrl4Copy &= 0xF7;
jurica238814 0:cc5d477d5dbe 39 break;
jurica238814 0:cc5d477d5dbe 40 case LOW_POWER:
jurica238814 0:cc5d477d5dbe 41 ctrl1Copy |= 0x08;
jurica238814 0:cc5d477d5dbe 42 ctrl4Copy &= 0xF7;
jurica238814 0:cc5d477d5dbe 43 break;
jurica238814 0:cc5d477d5dbe 44 default:
jurica238814 0:cc5d477d5dbe 45 return 0;
jurica238814 0:cc5d477d5dbe 46 }
jurica238814 0:cc5d477d5dbe 47 i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 48 success = i2c.writeToReg((char)CTRL_REG4, &ctrl4Copy, 1);
jurica238814 0:cc5d477d5dbe 49 return success;
jurica238814 0:cc5d477d5dbe 50 }
jurica238814 0:cc5d477d5dbe 51
jurica238814 0:cc5d477d5dbe 52 uint8_t Lis2dh12::enableAxes(Axis axis){
jurica238814 0:cc5d477d5dbe 53 char ctrl1Copy;
jurica238814 0:cc5d477d5dbe 54 i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 55 ctrl1Copy |= axis;
jurica238814 0:cc5d477d5dbe 56 i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 57 return 0;
jurica238814 0:cc5d477d5dbe 58 }
jurica238814 0:cc5d477d5dbe 59
jurica238814 0:cc5d477d5dbe 60 uint8_t Lis2dh12::disableAxes(Axis axis){
jurica238814 0:cc5d477d5dbe 61 char ctrl1Copy;
jurica238814 0:cc5d477d5dbe 62 i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 63 ctrl1Copy &= ~(1 << axis);
jurica238814 0:cc5d477d5dbe 64 i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 65 return 0;
jurica238814 0:cc5d477d5dbe 66 }
jurica238814 0:cc5d477d5dbe 67
jurica238814 0:cc5d477d5dbe 68 int16_t Lis2dh12::readXAxis(){
jurica238814 0:cc5d477d5dbe 69 int16_t rawData;
jurica238814 0:cc5d477d5dbe 70 char tempData;
jurica238814 0:cc5d477d5dbe 71 i2c.readFromReg((char)OUT_X_H, &tempData, 1);
jurica238814 0:cc5d477d5dbe 72 rawData = (int8_t)tempData << 8;
jurica238814 0:cc5d477d5dbe 73 i2c.readFromReg((char)OUT_X_L, &tempData, 1);
jurica238814 0:cc5d477d5dbe 74 rawData |= (int8_t)tempData;
jurica238814 0:cc5d477d5dbe 75 return rawData;
jurica238814 0:cc5d477d5dbe 76 }
jurica238814 0:cc5d477d5dbe 77
jurica238814 0:cc5d477d5dbe 78 int16_t Lis2dh12::readYAxis(){
jurica238814 0:cc5d477d5dbe 79 int16_t rawData;
jurica238814 0:cc5d477d5dbe 80 char tempData;
jurica238814 0:cc5d477d5dbe 81 i2c.readFromReg((char)OUT_Y_H, &tempData, 1);
jurica238814 0:cc5d477d5dbe 82 rawData = (int8_t)tempData << 8;
jurica238814 0:cc5d477d5dbe 83 i2c.readFromReg((char)OUT_Y_L, &tempData, 1);
jurica238814 0:cc5d477d5dbe 84 rawData |= (int8_t)tempData;
jurica238814 0:cc5d477d5dbe 85 return rawData;
jurica238814 0:cc5d477d5dbe 86 }
jurica238814 0:cc5d477d5dbe 87
jurica238814 0:cc5d477d5dbe 88 int16_t Lis2dh12::readZAxis(){
jurica238814 0:cc5d477d5dbe 89 int16_t rawData;
jurica238814 0:cc5d477d5dbe 90 char tempData;
jurica238814 0:cc5d477d5dbe 91 i2c.readFromReg((char)OUT_Z_H, &tempData, 1);
jurica238814 0:cc5d477d5dbe 92 rawData = (int8_t)tempData << 8;
jurica238814 0:cc5d477d5dbe 93 i2c.readFromReg((char)OUT_Z_L, &tempData, 1);
jurica238814 0:cc5d477d5dbe 94 rawData |= (int8_t)tempData;
jurica238814 0:cc5d477d5dbe 95 return rawData;
jurica238814 0:cc5d477d5dbe 96 }
jurica238814 0:cc5d477d5dbe 97
jurica238814 0:cc5d477d5dbe 98 uint8_t Lis2dh12::setODR(Odr odr){
jurica238814 0:cc5d477d5dbe 99 char ctrl1Copy;
jurica238814 0:cc5d477d5dbe 100 i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 101 ctrl1Copy |= (odr << 4);
jurica238814 0:cc5d477d5dbe 102 i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1);
jurica238814 0:cc5d477d5dbe 103 return 0;
jurica238814 0:cc5d477d5dbe 104 }
jurica238814 0:cc5d477d5dbe 105
jurica238814 0:cc5d477d5dbe 106 uint8_t Lis2dh12::setScale(Scale scale){
jurica238814 0:cc5d477d5dbe 107 char ctrl4Copy;
jurica238814 0:cc5d477d5dbe 108 i2c.readFromReg((char)CTRL_REG4, &ctrl4Copy, 1);
jurica238814 0:cc5d477d5dbe 109 ctrl4Copy |= (scale << 4);
jurica238814 0:cc5d477d5dbe 110 i2c.writeToReg((char)CTRL_REG4, &ctrl4Copy, 1);
jurica238814 0:cc5d477d5dbe 111 return 0;
jurica238814 0:cc5d477d5dbe 112 }
jurica238814 0:cc5d477d5dbe 113
jurica238814 0:cc5d477d5dbe 114
jurica238814 0:cc5d477d5dbe 115