First Revision of BMA180 accelerometer to read X, Y, and Z acceleration
Dependents: BMA180_1 BMA180_2 BMA180_3 BMA180_4
BMA180.cpp
00001 #include "mbed.h" 00002 #include "BMA180.h" 00003 00004 Serial pc(USBTX, USBRX); // tx, rx 00005 00006 BMA180::BMA180(PinName sdi, PinName sdo, PinName sck, PinName csb, PinName interrupt) : _csb(csb), _interrupt(interrupt), spi(sdi, sdo, sck){ 00007 // mosi, miso, sclk 00008 _csb = 1; 00009 _interrupt = 0; 00010 } 00011 00012 int BMA180::validate(char id, char ver) { 00013 char readID; 00014 int value = 0; 00015 00016 readID = readReg(id); 00017 00018 if(readID == 3) { 00019 value = 1; 00020 } else { 00021 pc.printf("Lost Connection"); 00022 value = -1; 00023 } 00024 return value; 00025 } 00026 00027 void BMA180::initBMA180(void) { 00028 char readVersion, readID; 00029 char byte; 00030 00031 readID = readReg(CHIPID); 00032 readVersion = readReg(VERSION); 00033 00034 if(readID == 3) { 00035 pc.printf("Connected to BMA180\n\r"); 00036 pc.printf("BMA180 Version: %d\n\r", readVersion); 00037 00038 reset(); 00039 00040 byte = readReg(CTRL_REG0); 00041 byte |= 0x10; // Set bit4 to 1 to enable self test and unlock image writing 00042 writeReg(CTRL_REG0, byte); 00043 00044 byte = readReg(DIS_I2C); // Read register 00045 //pc.printf("\n\rDisable I2C Before: %0.3X ", byte); 00046 byte |= 0x01; // Set bit0 to 1 for only SPI 00047 writeReg(DIS_I2C, byte); // Set SPI and disable I2C as per pg 31 of datasheet 00048 byte = readReg(RANGE); 00049 //pc.printf("After: %0.3X", byte); 00050 00051 byte = readReg(RANGE); // Read register 00052 //pc.printf("\n\rRange Before: %0.3X ", byte); 00053 byte &= 0xFB; // Set bit3,2,1 to 010 for 2g 00054 byte |= 0x02; 00055 writeReg(RANGE, byte); 00056 byte = readReg(RANGE); 00057 //pc.printf("After: %0.3X", byte); 00058 00059 byte = readReg(CTRL_REG3); 00060 //pc.printf("\n\rRegister 3 Before: %0.3X ", byte); 00061 byte |= 0x02; // Set bit1 to 1 to enable new data interrupts 00062 byte |= 0x40; // Set bit6 to 1 to enable slope mode 00063 byte |= 0x80; // Set bit6 to 1 to enable slope alert 00064 writeReg(CTRL_REG3, byte); 00065 //pc.printf("After: %0.3X", byte); 00066 pc.printf("Interrupt is enabled."); 00067 00068 byte = readReg(CTRL_REG0); 00069 byte &= 0xEF; // Set bit4 to 0 to disable self test and lock image writing 00070 writeReg(CTRL_REG0, byte); 00071 00072 pc.printf("\n\rBMA initialization complete.\n\r"); 00073 00074 } else { 00075 pc.printf("Not connected to BMA180! ReadVersion: %d ReadID: %d\n\r", readVersion, readID); 00076 } 00077 } 00078 00079 void BMA180::reset(void) { 00080 writeReg(RESET, 0xB6); 00081 wait_ms(10); 00082 pc.printf("Soft Reset, EEPROM Copied\n\r"); 00083 } 00084 00085 void BMA180::readAxis(void) { 00086 int x_msb, y_msb, z_msb; 00087 char x_lsb, y_lsb, z_lsb; 00088 short ax, ay, az; 00089 float afx, afy, afz; 00090 00091 x_lsb = readReg(ACCXLSB); // Read X LSB register 00092 x_msb = readReg(ACCXMSB); // Read X MSB register 00093 ax = (x_msb << 8) | x_lsb; // Concatinate X MSB and LSB 00094 ax = ax >> 2; // Remove unused first 2 LSB (16 bits to 14 bits) 00095 afx = (float)ax*3/16384; 00096 00097 y_lsb = readReg(ACCYLSB); // Read Y LSB register 00098 y_msb = readReg(ACCYMSB); // Read Y MSB register 00099 ay = (y_msb << 8) | y_lsb; // Concatinate Y MSB and LSB 00100 ay = ay >> 2; // Remove unused first 2 LSB 00101 afy = (float)ay*3/16384; 00102 00103 z_lsb = readReg(ACCZLSB); // Read Z LSB register 00104 z_msb = readReg(ACCZMSB); // Read Z MSB register 00105 az = (z_msb << 8) | z_lsb; // Concatinate Z MSB and LSB 00106 az = az >> 2; // Remove unused first 2 LSB 00107 afz = (float)az*3/16384; 00108 00109 00110 pc.printf("\n\rX: %05f Y: %05f Z: %05f", afx, afy, afz); 00111 } 00112 00113 void BMA180::writeReg(uint8_t address, char data) { 00114 address &= 0x7F; // Set bit7 to 0 for write mode 00115 _csb = 0; // Select the device 00116 wait_us(2); 00117 spi.write(address); // Send to data register location 00118 wait_us(2); 00119 spi.write(data); // Send value to register 00120 wait_us(2); 00121 _csb = 1; // Deselect the device 00122 } 00123 00124 char BMA180::readReg(uint8_t address) { 00125 char byte; 00126 00127 address |= 0x80; // Set bit7 to 1 for read mode 00128 _csb = 0; // Select the device 00129 wait_us(2); 00130 spi.write(address); // Send to data register location 00131 wait_us(2); 00132 byte = spi.write(address); // Get data 00133 wait_us(2); 00134 _csb = 1; // Deselect the device 00135 wait_us(2); 00136 return byte; 00137 } 00138 00139 void BMA180::disInt(void) { 00140 char byte; 00141 00142 byte = readReg(CTRL_REG0); 00143 byte |= 0x10; // Set bit4 to 1 to enable self test and unlock image writing 00144 writeReg(CTRL_REG0, byte); 00145 00146 byte = readReg(CTRL_REG3); // Unlock image writing 00147 byte &= 0xFD; // Set bit1 to 0 to disable interrupt 00148 writeReg(CTRL_REG3, byte); 00149 00150 byte = readReg(CTRL_REG0); 00151 byte &= 0xEF; // Set bit4 to 0 to disable self test and lock image writing 00152 writeReg(CTRL_REG0, byte); 00153 }
Generated on Sun Jul 17 2022 01:40:16 by 1.7.2