NA
Fork of BMA180 by
BMA180.cpp@1:c525baa503a0, 2017-11-16 (annotated)
- Committer:
- tedparrott6
- Date:
- Thu Nov 16 14:59:22 2017 +0000
- Revision:
- 1:c525baa503a0
- Parent:
- 0:ff755cb08068
NA
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jsermita | 0:ff755cb08068 | 1 | #include "mbed.h" |
jsermita | 0:ff755cb08068 | 2 | #include "BMA180.h" |
jsermita | 0:ff755cb08068 | 3 | |
tedparrott6 | 1:c525baa503a0 | 4 | //Serial pc(USBTX, USBRX); // tx, rx |
jsermita | 0:ff755cb08068 | 5 | |
jsermita | 0:ff755cb08068 | 6 | BMA180::BMA180(PinName sdi, PinName sdo, PinName sck, PinName csb, PinName interrupt) : _csb(csb), _interrupt(interrupt), spi(sdi, sdo, sck){ |
jsermita | 0:ff755cb08068 | 7 | // mosi, miso, sclk |
jsermita | 0:ff755cb08068 | 8 | _csb = 1; |
jsermita | 0:ff755cb08068 | 9 | _interrupt = 0; |
jsermita | 0:ff755cb08068 | 10 | } |
jsermita | 0:ff755cb08068 | 11 | |
jsermita | 0:ff755cb08068 | 12 | int BMA180::validate(char id, char ver) { |
jsermita | 0:ff755cb08068 | 13 | char readID; |
jsermita | 0:ff755cb08068 | 14 | int value = 0; |
jsermita | 0:ff755cb08068 | 15 | |
jsermita | 0:ff755cb08068 | 16 | readID = readReg(id); |
jsermita | 0:ff755cb08068 | 17 | |
jsermita | 0:ff755cb08068 | 18 | if(readID == 3) { |
jsermita | 0:ff755cb08068 | 19 | value = 1; |
jsermita | 0:ff755cb08068 | 20 | } else { |
tedparrott6 | 1:c525baa503a0 | 21 | //pc.printf("Lost Connection"); |
jsermita | 0:ff755cb08068 | 22 | value = -1; |
jsermita | 0:ff755cb08068 | 23 | } |
jsermita | 0:ff755cb08068 | 24 | return value; |
jsermita | 0:ff755cb08068 | 25 | } |
jsermita | 0:ff755cb08068 | 26 | |
jsermita | 0:ff755cb08068 | 27 | void BMA180::initBMA180(void) { |
jsermita | 0:ff755cb08068 | 28 | char readVersion, readID; |
jsermita | 0:ff755cb08068 | 29 | char byte; |
jsermita | 0:ff755cb08068 | 30 | |
jsermita | 0:ff755cb08068 | 31 | readID = readReg(CHIPID); |
jsermita | 0:ff755cb08068 | 32 | readVersion = readReg(VERSION); |
jsermita | 0:ff755cb08068 | 33 | |
jsermita | 0:ff755cb08068 | 34 | if(readID == 3) { |
tedparrott6 | 1:c525baa503a0 | 35 | //pc.printf("Connected to BMA180\n\r"); |
tedparrott6 | 1:c525baa503a0 | 36 | //pc.printf("BMA180 Version: %d\n\r", readVersion); |
jsermita | 0:ff755cb08068 | 37 | |
jsermita | 0:ff755cb08068 | 38 | reset(); |
jsermita | 0:ff755cb08068 | 39 | |
jsermita | 0:ff755cb08068 | 40 | byte = readReg(CTRL_REG0); |
jsermita | 0:ff755cb08068 | 41 | byte |= 0x10; // Set bit4 to 1 to enable self test and unlock image writing |
jsermita | 0:ff755cb08068 | 42 | writeReg(CTRL_REG0, byte); |
jsermita | 0:ff755cb08068 | 43 | |
jsermita | 0:ff755cb08068 | 44 | byte = readReg(DIS_I2C); // Read register |
jsermita | 0:ff755cb08068 | 45 | //pc.printf("\n\rDisable I2C Before: %0.3X ", byte); |
jsermita | 0:ff755cb08068 | 46 | byte |= 0x01; // Set bit0 to 1 for only SPI |
jsermita | 0:ff755cb08068 | 47 | writeReg(DIS_I2C, byte); // Set SPI and disable I2C as per pg 31 of datasheet |
jsermita | 0:ff755cb08068 | 48 | byte = readReg(RANGE); |
jsermita | 0:ff755cb08068 | 49 | //pc.printf("After: %0.3X", byte); |
jsermita | 0:ff755cb08068 | 50 | |
jsermita | 0:ff755cb08068 | 51 | byte = readReg(RANGE); // Read register |
jsermita | 0:ff755cb08068 | 52 | //pc.printf("\n\rRange Before: %0.3X ", byte); |
jsermita | 0:ff755cb08068 | 53 | byte &= 0xFB; // Set bit3,2,1 to 010 for 2g |
jsermita | 0:ff755cb08068 | 54 | byte |= 0x02; |
jsermita | 0:ff755cb08068 | 55 | writeReg(RANGE, byte); |
jsermita | 0:ff755cb08068 | 56 | byte = readReg(RANGE); |
jsermita | 0:ff755cb08068 | 57 | //pc.printf("After: %0.3X", byte); |
jsermita | 0:ff755cb08068 | 58 | |
jsermita | 0:ff755cb08068 | 59 | byte = readReg(CTRL_REG3); |
jsermita | 0:ff755cb08068 | 60 | //pc.printf("\n\rRegister 3 Before: %0.3X ", byte); |
jsermita | 0:ff755cb08068 | 61 | byte |= 0x02; // Set bit1 to 1 to enable new data interrupts |
jsermita | 0:ff755cb08068 | 62 | byte |= 0x40; // Set bit6 to 1 to enable slope mode |
jsermita | 0:ff755cb08068 | 63 | byte |= 0x80; // Set bit6 to 1 to enable slope alert |
jsermita | 0:ff755cb08068 | 64 | writeReg(CTRL_REG3, byte); |
jsermita | 0:ff755cb08068 | 65 | //pc.printf("After: %0.3X", byte); |
tedparrott6 | 1:c525baa503a0 | 66 | //pc.printf("Interrupt is enabled."); |
jsermita | 0:ff755cb08068 | 67 | |
jsermita | 0:ff755cb08068 | 68 | byte = readReg(CTRL_REG0); |
jsermita | 0:ff755cb08068 | 69 | byte &= 0xEF; // Set bit4 to 0 to disable self test and lock image writing |
jsermita | 0:ff755cb08068 | 70 | writeReg(CTRL_REG0, byte); |
jsermita | 0:ff755cb08068 | 71 | |
tedparrott6 | 1:c525baa503a0 | 72 | //pc.printf("\n\rBMA initialization complete.\n\r"); |
jsermita | 0:ff755cb08068 | 73 | |
jsermita | 0:ff755cb08068 | 74 | } else { |
tedparrott6 | 1:c525baa503a0 | 75 | //pc.printf("Not connected to BMA180! ReadVersion: %d ReadID: %d\n\r", readVersion, readID); |
jsermita | 0:ff755cb08068 | 76 | } |
jsermita | 0:ff755cb08068 | 77 | } |
jsermita | 0:ff755cb08068 | 78 | |
jsermita | 0:ff755cb08068 | 79 | void BMA180::reset(void) { |
jsermita | 0:ff755cb08068 | 80 | writeReg(RESET, 0xB6); |
jsermita | 0:ff755cb08068 | 81 | wait_ms(10); |
tedparrott6 | 1:c525baa503a0 | 82 | //pc.printf("Soft Reset, EEPROM Copied\n\r"); |
jsermita | 0:ff755cb08068 | 83 | } |
jsermita | 0:ff755cb08068 | 84 | |
jsermita | 0:ff755cb08068 | 85 | void BMA180::readAxis(void) { |
jsermita | 0:ff755cb08068 | 86 | int x_msb, y_msb, z_msb; |
jsermita | 0:ff755cb08068 | 87 | char x_lsb, y_lsb, z_lsb; |
jsermita | 0:ff755cb08068 | 88 | short ax, ay, az; |
jsermita | 0:ff755cb08068 | 89 | float afx, afy, afz; |
jsermita | 0:ff755cb08068 | 90 | |
jsermita | 0:ff755cb08068 | 91 | x_lsb = readReg(ACCXLSB); // Read X LSB register |
jsermita | 0:ff755cb08068 | 92 | x_msb = readReg(ACCXMSB); // Read X MSB register |
jsermita | 0:ff755cb08068 | 93 | ax = (x_msb << 8) | x_lsb; // Concatinate X MSB and LSB |
jsermita | 0:ff755cb08068 | 94 | ax = ax >> 2; // Remove unused first 2 LSB (16 bits to 14 bits) |
jsermita | 0:ff755cb08068 | 95 | afx = (float)ax*3/16384; |
jsermita | 0:ff755cb08068 | 96 | |
jsermita | 0:ff755cb08068 | 97 | y_lsb = readReg(ACCYLSB); // Read Y LSB register |
jsermita | 0:ff755cb08068 | 98 | y_msb = readReg(ACCYMSB); // Read Y MSB register |
jsermita | 0:ff755cb08068 | 99 | ay = (y_msb << 8) | y_lsb; // Concatinate Y MSB and LSB |
jsermita | 0:ff755cb08068 | 100 | ay = ay >> 2; // Remove unused first 2 LSB |
jsermita | 0:ff755cb08068 | 101 | afy = (float)ay*3/16384; |
jsermita | 0:ff755cb08068 | 102 | |
jsermita | 0:ff755cb08068 | 103 | z_lsb = readReg(ACCZLSB); // Read Z LSB register |
jsermita | 0:ff755cb08068 | 104 | z_msb = readReg(ACCZMSB); // Read Z MSB register |
jsermita | 0:ff755cb08068 | 105 | az = (z_msb << 8) | z_lsb; // Concatinate Z MSB and LSB |
jsermita | 0:ff755cb08068 | 106 | az = az >> 2; // Remove unused first 2 LSB |
tedparrott6 | 1:c525baa503a0 | 107 | afz = (float)az*3/16384; |
jsermita | 0:ff755cb08068 | 108 | |
tedparrott6 | 1:c525baa503a0 | 109 | |
tedparrott6 | 1:c525baa503a0 | 110 | //pc.printf("\n\rX: %05f Y: %05f Z: %05f", afx, afy, afz); |
jsermita | 0:ff755cb08068 | 111 | } |
jsermita | 0:ff755cb08068 | 112 | |
jsermita | 0:ff755cb08068 | 113 | void BMA180::writeReg(uint8_t address, char data) { |
jsermita | 0:ff755cb08068 | 114 | address &= 0x7F; // Set bit7 to 0 for write mode |
jsermita | 0:ff755cb08068 | 115 | _csb = 0; // Select the device |
jsermita | 0:ff755cb08068 | 116 | wait_us(2); |
jsermita | 0:ff755cb08068 | 117 | spi.write(address); // Send to data register location |
jsermita | 0:ff755cb08068 | 118 | wait_us(2); |
jsermita | 0:ff755cb08068 | 119 | spi.write(data); // Send value to register |
jsermita | 0:ff755cb08068 | 120 | wait_us(2); |
jsermita | 0:ff755cb08068 | 121 | _csb = 1; // Deselect the device |
jsermita | 0:ff755cb08068 | 122 | } |
jsermita | 0:ff755cb08068 | 123 | |
jsermita | 0:ff755cb08068 | 124 | char BMA180::readReg(uint8_t address) { |
jsermita | 0:ff755cb08068 | 125 | char byte; |
jsermita | 0:ff755cb08068 | 126 | |
jsermita | 0:ff755cb08068 | 127 | address |= 0x80; // Set bit7 to 1 for read mode |
jsermita | 0:ff755cb08068 | 128 | _csb = 0; // Select the device |
jsermita | 0:ff755cb08068 | 129 | wait_us(2); |
jsermita | 0:ff755cb08068 | 130 | spi.write(address); // Send to data register location |
jsermita | 0:ff755cb08068 | 131 | wait_us(2); |
jsermita | 0:ff755cb08068 | 132 | byte = spi.write(address); // Get data |
jsermita | 0:ff755cb08068 | 133 | wait_us(2); |
jsermita | 0:ff755cb08068 | 134 | _csb = 1; // Deselect the device |
jsermita | 0:ff755cb08068 | 135 | wait_us(2); |
jsermita | 0:ff755cb08068 | 136 | return byte; |
jsermita | 0:ff755cb08068 | 137 | } |
jsermita | 0:ff755cb08068 | 138 | |
jsermita | 0:ff755cb08068 | 139 | void BMA180::disInt(void) { |
jsermita | 0:ff755cb08068 | 140 | char byte; |
jsermita | 0:ff755cb08068 | 141 | |
jsermita | 0:ff755cb08068 | 142 | byte = readReg(CTRL_REG0); |
jsermita | 0:ff755cb08068 | 143 | byte |= 0x10; // Set bit4 to 1 to enable self test and unlock image writing |
jsermita | 0:ff755cb08068 | 144 | writeReg(CTRL_REG0, byte); |
jsermita | 0:ff755cb08068 | 145 | |
jsermita | 0:ff755cb08068 | 146 | byte = readReg(CTRL_REG3); // Unlock image writing |
jsermita | 0:ff755cb08068 | 147 | byte &= 0xFD; // Set bit1 to 0 to disable interrupt |
jsermita | 0:ff755cb08068 | 148 | writeReg(CTRL_REG3, byte); |
jsermita | 0:ff755cb08068 | 149 | |
jsermita | 0:ff755cb08068 | 150 | byte = readReg(CTRL_REG0); |
jsermita | 0:ff755cb08068 | 151 | byte &= 0xEF; // Set bit4 to 0 to disable self test and lock image writing |
jsermita | 0:ff755cb08068 | 152 | writeReg(CTRL_REG0, byte); |
jsermita | 0:ff755cb08068 | 153 | } |