Needed for FXAS21000 NXP gyroscope

Fork of FXAS21000 by Jim Carver

Committer:
JimCarver
Date:
Sat Apr 19 00:10:49 2014 +0000
Revision:
0:4cfc774d6d85
Child:
2:cd21ef326977
Improved version, added getWhoAmI() and readXYXraw()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:4cfc774d6d85 1 #include "FXAS21000.h"
JimCarver 0:4cfc774d6d85 2
JimCarver 0:4cfc774d6d85 3 FXAS21000::FXAS21000(PinName sda, PinName scl) : _i2c(sda, scl) {
JimCarver 0:4cfc774d6d85 4
JimCarver 0:4cfc774d6d85 5 begin();
JimCarver 0:4cfc774d6d85 6 }
JimCarver 0:4cfc774d6d85 7
JimCarver 0:4cfc774d6d85 8 void FXAS21000::RegRead( char reg, char * d, int len)
JimCarver 0:4cfc774d6d85 9 {
JimCarver 0:4cfc774d6d85 10 char cmd[1];
JimCarver 0:4cfc774d6d85 11 cmd[0] = reg;
JimCarver 0:4cfc774d6d85 12 char i2c_addr = FXAS21000_SLAVE_ADDR;
JimCarver 0:4cfc774d6d85 13 _i2c.write( i2c_addr, cmd, 1, true);
JimCarver 0:4cfc774d6d85 14 _i2c.read ( i2c_addr, d, len);
JimCarver 0:4cfc774d6d85 15 }
JimCarver 0:4cfc774d6d85 16
JimCarver 0:4cfc774d6d85 17 void FXAS21000::begin(void)
JimCarver 0:4cfc774d6d85 18 {
JimCarver 0:4cfc774d6d85 19 char data[2];
JimCarver 0:4cfc774d6d85 20 // write 0000 1000 = 0x08 to gyro control register 1 to place FXAS21000 into
JimCarver 0:4cfc774d6d85 21 // standby
JimCarver 0:4cfc774d6d85 22 // [7-1] = 0000 000
JimCarver 0:4cfc774d6d85 23 // [0]: active=0
JimCarver 0:4cfc774d6d85 24 data[0] = FXAS21000_CTRL_REG1;
JimCarver 0:4cfc774d6d85 25 data[1] = 0x08;
JimCarver 0:4cfc774d6d85 26 _i2c.write( FXAS21000_SLAVE_ADDR, data, 2);
JimCarver 0:4cfc774d6d85 27
JimCarver 0:4cfc774d6d85 28 // write 0001 1011 to CRTL_REG0 register
JimCarver 0:4cfc774d6d85 29
JimCarver 0:4cfc774d6d85 30 data[0] = FXAS21000_CTRL_REG0;
JimCarver 0:4cfc774d6d85 31 data[1] = 0x1B;
JimCarver 0:4cfc774d6d85 32 _i2c.write( FXAS21000_SLAVE_ADDR, data, 2);
JimCarver 0:4cfc774d6d85 33
JimCarver 0:4cfc774d6d85 34 // write 0000 1001 to gyro control register 1
JimCarver 0:4cfc774d6d85 35
JimCarver 0:4cfc774d6d85 36 data[0] = FXAS21000_CTRL_REG1;
JimCarver 0:4cfc774d6d85 37 data[1] = 0x0A;
JimCarver 0:4cfc774d6d85 38 _i2c.write( FXAS21000_SLAVE_ADDR, data, 2);
JimCarver 0:4cfc774d6d85 39 }
JimCarver 0:4cfc774d6d85 40
JimCarver 0:4cfc774d6d85 41 char FXAS21000::getWhoAmI(void)
JimCarver 0:4cfc774d6d85 42 {
JimCarver 0:4cfc774d6d85 43 char d;
JimCarver 0:4cfc774d6d85 44 RegRead( FXAS21000_WHOAMI, &d, 1);
JimCarver 0:4cfc774d6d85 45 return(d);
JimCarver 0:4cfc774d6d85 46 }
JimCarver 0:4cfc774d6d85 47
JimCarver 0:4cfc774d6d85 48 void FXAS21000::ReadXYZ(float * a)
JimCarver 0:4cfc774d6d85 49 {
JimCarver 0:4cfc774d6d85 50 char d[7];
JimCarver 0:4cfc774d6d85 51 int16_t t[6];
JimCarver 0:4cfc774d6d85 52
JimCarver 0:4cfc774d6d85 53 RegRead( FXAS21000_STATUS, d, 7);
JimCarver 0:4cfc774d6d85 54 t[0] = ((d[1] * 256) + ((unsigned short) d[2]));
JimCarver 0:4cfc774d6d85 55 t[1] = ((d[3] * 256) + ((unsigned short) d[4]));
JimCarver 0:4cfc774d6d85 56 t[2] = ((d[5] * 256) + ((unsigned short) d[6]));
JimCarver 0:4cfc774d6d85 57 //printf("%X\r\n", (int) d[0]);
JimCarver 0:4cfc774d6d85 58
JimCarver 0:4cfc774d6d85 59 a[0] = (float) t[0] * 0.003125;
JimCarver 0:4cfc774d6d85 60 a[1] = (float) t[1] * 0.003125;
JimCarver 0:4cfc774d6d85 61 a[2] = (float) t[2] * 0.003125;
JimCarver 0:4cfc774d6d85 62
JimCarver 0:4cfc774d6d85 63 }
JimCarver 0:4cfc774d6d85 64
JimCarver 0:4cfc774d6d85 65 void FXAS21000::ReadXYZraw(int16_t * t)
JimCarver 0:4cfc774d6d85 66 {
JimCarver 0:4cfc774d6d85 67 char d[7];
JimCarver 0:4cfc774d6d85 68
JimCarver 0:4cfc774d6d85 69 RegRead( FXAS21000_STATUS, d, 7);
JimCarver 0:4cfc774d6d85 70 t[0] = ((d[1] * 256) + ((unsigned short) d[2]));
JimCarver 0:4cfc774d6d85 71 t[1] = ((d[3] * 256) + ((unsigned short) d[4]));
JimCarver 0:4cfc774d6d85 72 t[2] = ((d[5] * 256) + ((unsigned short) d[6]));
JimCarver 0:4cfc774d6d85 73 }