Library to communicate with a ST LIS331DLH

Committer:
atommota
Date:
Wed Nov 17 18:34:25 2010 +0000
Revision:
3:147d95b7a525
Parent:
2:d4b810a888b5
Child:
4:c3df518a938e
Changed WHO_AM_I reg name to play nicer with itg3200 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atommota 2:d4b810a888b5 1 /**
atommota 2:d4b810a888b5 2 * @section LICENSE
atommota 2:d4b810a888b5 3 *
atommota 2:d4b810a888b5 4 *
atommota 2:d4b810a888b5 5 * @section DESCRIPTION
atommota 2:d4b810a888b5 6 *
atommota 2:d4b810a888b5 7 * LIS331 triple axis, digital interface, accelerometer.
atommota 2:d4b810a888b5 8 * Based off Aaron Berk's ITG3200 Gyro Library
atommota 2:d4b810a888b5 9 *
atommota 2:d4b810a888b5 10 * Datasheet:
atommota 2:d4b810a888b5 11 *
atommota 2:d4b810a888b5 12 * http://www.st.com/stonline/products/literature/ds/13951.pdf
atommota 2:d4b810a888b5 13 */
atommota 2:d4b810a888b5 14
atommota 2:d4b810a888b5 15 /**
atommota 2:d4b810a888b5 16 * Includes
atommota 2:d4b810a888b5 17 */
atommota 2:d4b810a888b5 18
atommota 2:d4b810a888b5 19 #include "LIS331.h"
atommota 2:d4b810a888b5 20
atommota 2:d4b810a888b5 21 LIS331::LIS331(PinName sda, PinName scl) : i2c_(sda, scl) {
atommota 2:d4b810a888b5 22
atommota 2:d4b810a888b5 23 //400kHz, fast mode.
atommota 2:d4b810a888b5 24 i2c_.frequency(400000);
atommota 2:d4b810a888b5 25
atommota 2:d4b810a888b5 26
atommota 2:d4b810a888b5 27 //Power Up Device, Set Output data rate, Enable All 3 Axis
atommota 2:d4b810a888b5 28 //See datasheet for details.
atommota 2:d4b810a888b5 29 char tx[2];
atommota 2:d4b810a888b5 30 //char tx2[2];
atommota 2:d4b810a888b5 31 //char rx[1];
atommota 2:d4b810a888b5 32 tx[0] = CTRL_REG_1;
atommota 2:d4b810a888b5 33 //CTRL_REG_1 [00111111] / [0x3F] to power up, set output rate to 1000Hz, and enable all 3 axis.
atommota 2:d4b810a888b5 34 tx[1] = 0x3F;
atommota 2:d4b810a888b5 35 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 2:d4b810a888b5 36
atommota 2:d4b810a888b5 37
atommota 2:d4b810a888b5 38 // Set Big endian bit
atommota 2:d4b810a888b5 39 //tx2[0] = CTRL_REG_4;
atommota 2:d4b810a888b5 40 //i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx2, 1); // Request control reg 4
atommota 2:d4b810a888b5 41 //i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 1); //Read control reg 4
atommota 2:d4b810a888b5 42 //tx2[1] = rx[0] | 1<<7; // Set bit 7
atommota 2:d4b810a888b5 43 //i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx2, 2);
atommota 2:d4b810a888b5 44
atommota 2:d4b810a888b5 45 }
atommota 2:d4b810a888b5 46
atommota 2:d4b810a888b5 47 char LIS331::getWhoAmI(void){
atommota 2:d4b810a888b5 48
atommota 2:d4b810a888b5 49 //WhoAmI Register address.
atommota 3:147d95b7a525 50 char tx = WHO_AM_I_REG_LIS331;
atommota 2:d4b810a888b5 51 char rx;
atommota 2:d4b810a888b5 52
atommota 2:d4b810a888b5 53 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 54
atommota 2:d4b810a888b5 55 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 56
atommota 2:d4b810a888b5 57 return rx;
atommota 2:d4b810a888b5 58
atommota 2:d4b810a888b5 59 }
atommota 2:d4b810a888b5 60
atommota 2:d4b810a888b5 61
atommota 2:d4b810a888b5 62
atommota 2:d4b810a888b5 63
atommota 2:d4b810a888b5 64 void LIS331::setPowerMode(char power_mode){
atommota 2:d4b810a888b5 65 // Currently sets all 3 axis to enabled. Will be set to preserve existing status in future
atommota 2:d4b810a888b5 66 char tx[2];
atommota 2:d4b810a888b5 67 tx[0] = CTRL_REG_1;
atommota 2:d4b810a888b5 68 tx[1] = power_mode;
atommota 2:d4b810a888b5 69
atommota 2:d4b810a888b5 70 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, tx, 2);
atommota 2:d4b810a888b5 71
atommota 2:d4b810a888b5 72 }
atommota 2:d4b810a888b5 73
atommota 2:d4b810a888b5 74 char LIS331::getPowerMode(void){
atommota 2:d4b810a888b5 75
atommota 2:d4b810a888b5 76 char tx = CTRL_REG_1;
atommota 2:d4b810a888b5 77 char rx;
atommota 2:d4b810a888b5 78
atommota 2:d4b810a888b5 79 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 80
atommota 2:d4b810a888b5 81 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 82
atommota 2:d4b810a888b5 83
atommota 2:d4b810a888b5 84 return rx;
atommota 2:d4b810a888b5 85
atommota 2:d4b810a888b5 86 }
atommota 2:d4b810a888b5 87
atommota 2:d4b810a888b5 88
atommota 2:d4b810a888b5 89
atommota 2:d4b810a888b5 90 char LIS331::getInterruptConfiguration(void){
atommota 2:d4b810a888b5 91
atommota 2:d4b810a888b5 92 char tx = CTRL_REG_3;
atommota 2:d4b810a888b5 93 char rx;
atommota 2:d4b810a888b5 94
atommota 2:d4b810a888b5 95 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 96
atommota 2:d4b810a888b5 97 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 98
atommota 2:d4b810a888b5 99 return rx;
atommota 2:d4b810a888b5 100
atommota 2:d4b810a888b5 101 }
atommota 2:d4b810a888b5 102
atommota 2:d4b810a888b5 103
atommota 2:d4b810a888b5 104
atommota 2:d4b810a888b5 105 char LIS331::getAccelStatus(void){
atommota 2:d4b810a888b5 106
atommota 2:d4b810a888b5 107 char tx = STATUS_REG;
atommota 2:d4b810a888b5 108 char rx;
atommota 2:d4b810a888b5 109
atommota 2:d4b810a888b5 110 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 111
atommota 2:d4b810a888b5 112 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, &rx, 1);
atommota 2:d4b810a888b5 113
atommota 2:d4b810a888b5 114 return rx;
atommota 2:d4b810a888b5 115 }
atommota 2:d4b810a888b5 116
atommota 2:d4b810a888b5 117
atommota 2:d4b810a888b5 118
atommota 2:d4b810a888b5 119 int LIS331::getAccelX(void){
atommota 2:d4b810a888b5 120
atommota 2:d4b810a888b5 121 char tx = ACCEL_XOUT_H_REG;
atommota 2:d4b810a888b5 122 char rx[2];
atommota 2:d4b810a888b5 123
atommota 2:d4b810a888b5 124 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 125
atommota 2:d4b810a888b5 126 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 127
atommota 2:d4b810a888b5 128 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 2:d4b810a888b5 129
atommota 2:d4b810a888b5 130 return output;
atommota 2:d4b810a888b5 131
atommota 2:d4b810a888b5 132 }
atommota 2:d4b810a888b5 133
atommota 2:d4b810a888b5 134 int LIS331::getAccelY(void){
atommota 2:d4b810a888b5 135
atommota 2:d4b810a888b5 136 char tx = ACCEL_YOUT_H_REG;
atommota 2:d4b810a888b5 137 char rx[2];
atommota 2:d4b810a888b5 138
atommota 2:d4b810a888b5 139 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 140
atommota 2:d4b810a888b5 141 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 142
atommota 2:d4b810a888b5 143 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 2:d4b810a888b5 144
atommota 2:d4b810a888b5 145 return output;
atommota 2:d4b810a888b5 146
atommota 2:d4b810a888b5 147 }
atommota 2:d4b810a888b5 148
atommota 2:d4b810a888b5 149 int LIS331::getAccelZ(void){
atommota 2:d4b810a888b5 150
atommota 2:d4b810a888b5 151 char tx = ACCEL_ZOUT_H_REG;
atommota 2:d4b810a888b5 152 char rx[2];
atommota 2:d4b810a888b5 153
atommota 2:d4b810a888b5 154 i2c_.write((LIS331_I2C_ADDRESS << 1) & 0xFE, &tx, 1);
atommota 2:d4b810a888b5 155
atommota 2:d4b810a888b5 156 i2c_.read((LIS331_I2C_ADDRESS << 1) | 0x01, rx, 2);
atommota 2:d4b810a888b5 157
atommota 2:d4b810a888b5 158 int16_t output = ((int) rx[0] << 8) | ((int) rx[1]);
atommota 2:d4b810a888b5 159
atommota 2:d4b810a888b5 160 return output;
atommota 1:02c1f5bb1c90 161 }