![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
3Axis gyro sensor sample. target sensor: L3GD20(ST)
L3GD20.cpp@0:a1d1374eea97, 2018-12-17 (annotated)
- Committer:
- satoshi1204
- Date:
- Mon Dec 17 12:47:04 2018 +0000
- Revision:
- 0:a1d1374eea97
- Child:
- 1:191e963655c2
Implementation sample program for gyro sensor L3GD20
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
satoshi1204 | 0:a1d1374eea97 | 1 | #include "L3GD20.h" |
satoshi1204 | 0:a1d1374eea97 | 2 | |
satoshi1204 | 0:a1d1374eea97 | 3 | L3GD20::L3GD20(PinName sda, PinName scl) : i2c(sda, scl), address(0xD6) |
satoshi1204 | 0:a1d1374eea97 | 4 | { |
satoshi1204 | 0:a1d1374eea97 | 5 | writeByte(0x20, 0x0F); |
satoshi1204 | 0:a1d1374eea97 | 6 | } |
satoshi1204 | 0:a1d1374eea97 | 7 | |
satoshi1204 | 0:a1d1374eea97 | 8 | L3GD20::~L3GD20() |
satoshi1204 | 0:a1d1374eea97 | 9 | { |
satoshi1204 | 0:a1d1374eea97 | 10 | } |
satoshi1204 | 0:a1d1374eea97 | 11 | |
satoshi1204 | 0:a1d1374eea97 | 12 | void L3GD20::readData(double &x, double &y, double &z) |
satoshi1204 | 0:a1d1374eea97 | 13 | { |
satoshi1204 | 0:a1d1374eea97 | 14 | static const double rate = 0.00875; |
satoshi1204 | 0:a1d1374eea97 | 15 | char h, l; |
satoshi1204 | 0:a1d1374eea97 | 16 | l = readByte(0x28); |
satoshi1204 | 0:a1d1374eea97 | 17 | h = readByte(0x29); |
satoshi1204 | 0:a1d1374eea97 | 18 | x = (short)(h << 8 | l) * rate; |
satoshi1204 | 0:a1d1374eea97 | 19 | l = readByte(0x2A); |
satoshi1204 | 0:a1d1374eea97 | 20 | h = readByte(0x2B); |
satoshi1204 | 0:a1d1374eea97 | 21 | y = (short)(h << 8 | l) * rate; |
satoshi1204 | 0:a1d1374eea97 | 22 | l = readByte(0x2C); |
satoshi1204 | 0:a1d1374eea97 | 23 | h = readByte(0x2D); |
satoshi1204 | 0:a1d1374eea97 | 24 | z = (short)(h << 8 | l) * rate; |
satoshi1204 | 0:a1d1374eea97 | 25 | } |
satoshi1204 | 0:a1d1374eea97 | 26 | |
satoshi1204 | 0:a1d1374eea97 | 27 | char L3GD20::readByte(char reg) |
satoshi1204 | 0:a1d1374eea97 | 28 | { |
satoshi1204 | 0:a1d1374eea97 | 29 | i2c.write(address, ®, 1); |
satoshi1204 | 0:a1d1374eea97 | 30 | char data; |
satoshi1204 | 0:a1d1374eea97 | 31 | i2c.read(address, &data, 1); |
satoshi1204 | 0:a1d1374eea97 | 32 | return data; |
satoshi1204 | 0:a1d1374eea97 | 33 | } |
satoshi1204 | 0:a1d1374eea97 | 34 | |
satoshi1204 | 0:a1d1374eea97 | 35 | void L3GD20::writeByte(char reg, char data) |
satoshi1204 | 0:a1d1374eea97 | 36 | { |
satoshi1204 | 0:a1d1374eea97 | 37 | char buf[2]; |
satoshi1204 | 0:a1d1374eea97 | 38 | buf[0] = reg; |
satoshi1204 | 0:a1d1374eea97 | 39 | buf[1] = data; |
satoshi1204 | 0:a1d1374eea97 | 40 | i2c.write(address, buf, 2); |
satoshi1204 | 0:a1d1374eea97 | 41 | } |