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