MBA180 sensor SPI (four wire connection) with interrupt demo. Fifty samples are read, stored in a array and presented.

Dependencies:   mbed

Committer:
GerritPathuis
Date:
Sat Dec 18 11:13:09 2010 +0000
Revision:
0:8ce1608bb6f8
Revision 0.0 (16-12-2010)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GerritPathuis 0:8ce1608bb6f8 1 #include "mbed.h"
GerritPathuis 0:8ce1608bb6f8 2 #define BMA180_ID 0x00
GerritPathuis 0:8ce1608bb6f8 3 #define VERSION 0x01
GerritPathuis 0:8ce1608bb6f8 4 #define ACCXLSB 0x02
GerritPathuis 0:8ce1608bb6f8 5 #define ACCXMSB 0x03
GerritPathuis 0:8ce1608bb6f8 6 #define ACCYLSB 0x04
GerritPathuis 0:8ce1608bb6f8 7 #define ACCYMSB 0x05
GerritPathuis 0:8ce1608bb6f8 8 #define ACCZLSB 0x06
GerritPathuis 0:8ce1608bb6f8 9 #define ACCZMSB 0x07
GerritPathuis 0:8ce1608bb6f8 10 #define CTRL_REG0 0x0D
GerritPathuis 0:8ce1608bb6f8 11 #define DIS_I2C 0x27 // bit 0 must be 1 for SPI
GerritPathuis 0:8ce1608bb6f8 12 #define CTRL_REG3 0x21 // bit 1= new_data_int
GerritPathuis 0:8ce1608bb6f8 13 #define RESET 0x10 // soft reset
GerritPathuis 0:8ce1608bb6f8 14
GerritPathuis 0:8ce1608bb6f8 15 /*-------------Hardware connections-------------
GerritPathuis 0:8ce1608bb6f8 16 By Gerrit Pathuis (gpa@quicknet.nl)
GerritPathuis 0:8ce1608bb6f8 17 MBA180 breakour board (sparkfun) is used
GerritPathuis 0:8ce1608bb6f8 18 VIO----- mbed Vout (3.3 Volt)
GerritPathuis 0:8ce1608bb6f8 19 SDI----- mbed mosi (p5)
GerritPathuis 0:8ce1608bb6f8 20 SDO----- mbed miso (p6)
GerritPathuis 0:8ce1608bb6f8 21 SCK----- mbed sck (p7)
GerritPathuis 0:8ce1608bb6f8 22 CS------ mbed (p8) // chip select
GerritPathuis 0:8ce1608bb6f8 23 INT----- mbed (p9) // MBA give a interrupt when ready
GerritPathuis 0:8ce1608bb6f8 24 GND----- mbed GND (0 Volt)
GerritPathuis 0:8ce1608bb6f8 25 VDO----- mbed Vout (3.3 Volt)
GerritPathuis 0:8ce1608bb6f8 26 there are no additional external components used
GerritPathuis 0:8ce1608bb6f8 27 //----------------------------------------------*/
GerritPathuis 0:8ce1608bb6f8 28
GerritPathuis 0:8ce1608bb6f8 29 SPI spi(p5,p6,p7); //mosi, miso, sclk
GerritPathuis 0:8ce1608bb6f8 30 DigitalOut cs(p8);
GerritPathuis 0:8ce1608bb6f8 31 InterruptIn event(p9);
GerritPathuis 0:8ce1608bb6f8 32
GerritPathuis 0:8ce1608bb6f8 33 Serial pc(USBTX, USBRX); // tx, rx
GerritPathuis 0:8ce1608bb6f8 34 void init_SPI_BMA180(void);
GerritPathuis 0:8ce1608bb6f8 35 void soft_reset(void);
GerritPathuis 0:8ce1608bb6f8 36 void trigger(void);
GerritPathuis 0:8ce1608bb6f8 37 void new_data(void);
GerritPathuis 0:8ce1608bb6f8 38 void write_bits(char u);
GerritPathuis 0:8ce1608bb6f8 39 void write_reg(uint8_t address, char data);
GerritPathuis 0:8ce1608bb6f8 40 char read_reg(uint8_t address);
GerritPathuis 0:8ce1608bb6f8 41 void disable_int(void);
GerritPathuis 0:8ce1608bb6f8 42
GerritPathuis 0:8ce1608bb6f8 43 struct sample { // reserve spave for 1000 samples
GerritPathuis 0:8ce1608bb6f8 44 signed short x;
GerritPathuis 0:8ce1608bb6f8 45 signed short y;
GerritPathuis 0:8ce1608bb6f8 46 signed short z;
GerritPathuis 0:8ce1608bb6f8 47 signed short t;
GerritPathuis 0:8ce1608bb6f8 48 } vib[1000+1];
GerritPathuis 0:8ce1608bb6f8 49
GerritPathuis 0:8ce1608bb6f8 50 int sample_cnt;
GerritPathuis 0:8ce1608bb6f8 51
GerritPathuis 0:8ce1608bb6f8 52 int main() {
GerritPathuis 0:8ce1608bb6f8 53 int pp;
GerritPathuis 0:8ce1608bb6f8 54 float xx, yy, zz;
GerritPathuis 0:8ce1608bb6f8 55
GerritPathuis 0:8ce1608bb6f8 56 init_SPI_BMA180(); // init the sensor
GerritPathuis 0:8ce1608bb6f8 57 sample_cnt =0; // start the counter
GerritPathuis 0:8ce1608bb6f8 58 pc.printf("Start lurking ");
GerritPathuis 0:8ce1608bb6f8 59 while (sample_cnt <50) {
GerritPathuis 0:8ce1608bb6f8 60 event.rise(&trigger);
GerritPathuis 0:8ce1608bb6f8 61 }
GerritPathuis 0:8ce1608bb6f8 62 disable_int(); // switch the BMA180 off
GerritPathuis 0:8ce1608bb6f8 63 pc.printf("\n\r\nPresent the data\n\r");
GerritPathuis 0:8ce1608bb6f8 64 for (pp=0; pp<50; pp +=1) {
GerritPathuis 0:8ce1608bb6f8 65 // 2^14 = 16384
GerritPathuis 0:8ce1608bb6f8 66 // Range is +/1 2.0 G versnelling
GerritPathuis 0:8ce1608bb6f8 67 xx = vib[pp].x/16384.0*4.0;
GerritPathuis 0:8ce1608bb6f8 68 yy = vib[pp].y/16384.0*4.0;
GerritPathuis 0:8ce1608bb6f8 69 zz = vib[pp].z/16384.0*4.0;
GerritPathuis 0:8ce1608bb6f8 70 pc.printf("Sample %2d, Acc X= %0.4f, Y= %0.4f, Z= %0.4f \n\r", pp, xx, yy, zz);
GerritPathuis 0:8ce1608bb6f8 71 }
GerritPathuis 0:8ce1608bb6f8 72 }
GerritPathuis 0:8ce1608bb6f8 73
GerritPathuis 0:8ce1608bb6f8 74 void init_SPI_BMA180(void) {
GerritPathuis 0:8ce1608bb6f8 75 char readVersion, readID;
GerritPathuis 0:8ce1608bb6f8 76 char byte;
GerritPathuis 0:8ce1608bb6f8 77 // Setup the spi for 8 bit data, high steady state clock,
GerritPathuis 0:8ce1608bb6f8 78 // second edge capture, with a 10MHz clock rate
GerritPathuis 0:8ce1608bb6f8 79
GerritPathuis 0:8ce1608bb6f8 80 spi.frequency(10000000); // 10 mHz page 59 of the BMA180 datasheet
GerritPathuis 0:8ce1608bb6f8 81 spi.format(8,3); // Not conform !!! page 58 of the BMA180 datasheet)
GerritPathuis 0:8ce1608bb6f8 82 wait_ms(100);
GerritPathuis 0:8ce1608bb6f8 83 readID = read_reg(BMA180_ID);
GerritPathuis 0:8ce1608bb6f8 84 readVersion = read_reg(VERSION);
GerritPathuis 0:8ce1608bb6f8 85 pc.printf("\n\r");
GerritPathuis 0:8ce1608bb6f8 86 if (readID == 3) {
GerritPathuis 0:8ce1608bb6f8 87 pc.printf("Connected to BMA180\n\r");
GerritPathuis 0:8ce1608bb6f8 88 pc.printf("BMA180 Version %d\n\r", readVersion);
GerritPathuis 0:8ce1608bb6f8 89 } else
GerritPathuis 0:8ce1608bb6f8 90 pc.printf("Sorry not connected to BMA180 !!!\n\r", readID);
GerritPathuis 0:8ce1608bb6f8 91
GerritPathuis 0:8ce1608bb6f8 92 soft_reset(); // to copy EEprom into volatile area
GerritPathuis 0:8ce1608bb6f8 93
GerritPathuis 0:8ce1608bb6f8 94 //---------------------------
GerritPathuis 0:8ce1608bb6f8 95 byte = read_reg(CTRL_REG0); // Unlock image writing
GerritPathuis 0:8ce1608bb6f8 96 byte |= 0x10; // Set bit 4
GerritPathuis 0:8ce1608bb6f8 97 write_reg(CTRL_REG0,byte); // Have to set ee_w to
GerritPathuis 0:8ce1608bb6f8 98 //------------------------
GerritPathuis 0:8ce1608bb6f8 99 byte= read_reg(DIS_I2C); // read
GerritPathuis 0:8ce1608bb6f8 100 byte |= 0x01; // set bit0 to 1, SPI only
GerritPathuis 0:8ce1608bb6f8 101 write_reg(DIS_I2C, byte); // Set spi, disable i2c, page 31
GerritPathuis 0:8ce1608bb6f8 102 //-------------------------
GerritPathuis 0:8ce1608bb6f8 103 byte = read_reg(CTRL_REG3);
GerritPathuis 0:8ce1608bb6f8 104 byte |= 0x02; // set bit 1 enable interrupt
GerritPathuis 0:8ce1608bb6f8 105 byte |= 0x40; // set bit 6 slope mode
GerritPathuis 0:8ce1608bb6f8 106 byte |= 0x80; // set bit 6 slope alert
GerritPathuis 0:8ce1608bb6f8 107 write_reg(CTRL_REG3,byte); //
GerritPathuis 0:8ce1608bb6f8 108 pc.printf("Enable interrupt bis is set ");
GerritPathuis 0:8ce1608bb6f8 109 //------------------------------
GerritPathuis 0:8ce1608bb6f8 110 byte = read_reg(CTRL_REG0); // Lock image writing
GerritPathuis 0:8ce1608bb6f8 111 byte &= 0xEF; // REset bit 4
GerritPathuis 0:8ce1608bb6f8 112 write_reg(CTRL_REG0,byte); // Have to set ee_w to
GerritPathuis 0:8ce1608bb6f8 113 //-------------------------------------------------------------------------------------
GerritPathuis 0:8ce1608bb6f8 114 pc.printf("\n\rBMA init done \n\r");
GerritPathuis 0:8ce1608bb6f8 115 }
GerritPathuis 0:8ce1608bb6f8 116
GerritPathuis 0:8ce1608bb6f8 117 void soft_reset(void) {
GerritPathuis 0:8ce1608bb6f8 118 // Write a soft reset
GerritPathuis 0:8ce1608bb6f8 119 // to copy EEprom into volatile area, see mid page 22
GerritPathuis 0:8ce1608bb6f8 120 write_reg(RESET, 0xB6); // page 48
GerritPathuis 0:8ce1608bb6f8 121 wait_ms(10); // wait 10 ms, see page 49
GerritPathuis 0:8ce1608bb6f8 122 pc.printf("Soft reset, EEPROM copied \n\r");
GerritPathuis 0:8ce1608bb6f8 123 }
GerritPathuis 0:8ce1608bb6f8 124
GerritPathuis 0:8ce1608bb6f8 125 //-----------------READ OUT the X-Y-Z values---------------
GerritPathuis 0:8ce1608bb6f8 126 void trigger(void) {
GerritPathuis 0:8ce1608bb6f8 127 char x_lsb, x_msb;
GerritPathuis 0:8ce1608bb6f8 128 char y_lsb, y_msb;
GerritPathuis 0:8ce1608bb6f8 129 char z_lsb, z_msb;
GerritPathuis 0:8ce1608bb6f8 130 signed short ax, ay, az;
GerritPathuis 0:8ce1608bb6f8 131
GerritPathuis 0:8ce1608bb6f8 132 //------------X----------------
GerritPathuis 0:8ce1608bb6f8 133 x_lsb = read_reg(ACCXLSB);
GerritPathuis 0:8ce1608bb6f8 134 x_msb = read_reg(ACCXMSB);
GerritPathuis 0:8ce1608bb6f8 135 ax = (x_msb << 8) | x_lsb ; // combineer msb en lsb
GerritPathuis 0:8ce1608bb6f8 136 ax = ax >> 2; // Get rid of two non-value bits in LSB
GerritPathuis 0:8ce1608bb6f8 137 //------------Y----------------
GerritPathuis 0:8ce1608bb6f8 138 y_lsb = read_reg(ACCYLSB);
GerritPathuis 0:8ce1608bb6f8 139 y_msb = read_reg(ACCYMSB);
GerritPathuis 0:8ce1608bb6f8 140 ay = (y_msb << 8) | y_lsb; // combineer msb en lsb
GerritPathuis 0:8ce1608bb6f8 141 ay = ay >> 2; // Get rid of two non-value bits in LSB
GerritPathuis 0:8ce1608bb6f8 142 //------------Z----------------
GerritPathuis 0:8ce1608bb6f8 143 z_lsb = read_reg(ACCZLSB);
GerritPathuis 0:8ce1608bb6f8 144 z_msb = read_reg(ACCZMSB);
GerritPathuis 0:8ce1608bb6f8 145 az = (z_msb << 8) | z_lsb; // combineer msb en lsb
GerritPathuis 0:8ce1608bb6f8 146 az = az >> 2; // Get rid of two non-value bits in LSB
GerritPathuis 0:8ce1608bb6f8 147
GerritPathuis 0:8ce1608bb6f8 148 //----------shift into the array---------------------
GerritPathuis 0:8ce1608bb6f8 149 vib[sample_cnt].x= ax;
GerritPathuis 0:8ce1608bb6f8 150 vib[sample_cnt].y= ay;
GerritPathuis 0:8ce1608bb6f8 151 vib[sample_cnt].z= az;
GerritPathuis 0:8ce1608bb6f8 152 //---------counter------------
GerritPathuis 0:8ce1608bb6f8 153 sample_cnt += 1;
GerritPathuis 0:8ce1608bb6f8 154 }
GerritPathuis 0:8ce1608bb6f8 155
GerritPathuis 0:8ce1608bb6f8 156 void write_reg(uint8_t address, char data) {
GerritPathuis 0:8ce1608bb6f8 157 address &= 0x7F; //Force a write (bit 7=0)
GerritPathuis 0:8ce1608bb6f8 158 cs=0; //Select SPI device
GerritPathuis 0:8ce1608bb6f8 159 wait_us(2);
GerritPathuis 0:8ce1608bb6f8 160 spi.write(address); //Send register location
GerritPathuis 0:8ce1608bb6f8 161 wait_us(2);
GerritPathuis 0:8ce1608bb6f8 162 spi.write(data); //Send value to record into register
GerritPathuis 0:8ce1608bb6f8 163 wait_us(2);
GerritPathuis 0:8ce1608bb6f8 164 cs=1;
GerritPathuis 0:8ce1608bb6f8 165 wait_us(2);
GerritPathuis 0:8ce1608bb6f8 166 }
GerritPathuis 0:8ce1608bb6f8 167
GerritPathuis 0:8ce1608bb6f8 168 char read_reg(uint8_t address) {
GerritPathuis 0:8ce1608bb6f8 169 char byte;
GerritPathuis 0:8ce1608bb6f8 170 address |= 0x80; //Force a read (bit 7=1)
GerritPathuis 0:8ce1608bb6f8 171 cs=0;
GerritPathuis 0:8ce1608bb6f8 172 wait_us(2); //Select SPI device
GerritPathuis 0:8ce1608bb6f8 173 spi.write(address); //Send register location
GerritPathuis 0:8ce1608bb6f8 174 wait_us(2);
GerritPathuis 0:8ce1608bb6f8 175 byte=spi.write(0xFF); //Get the data
GerritPathuis 0:8ce1608bb6f8 176 wait_us(2);
GerritPathuis 0:8ce1608bb6f8 177 cs=1;
GerritPathuis 0:8ce1608bb6f8 178 wait_us(2);
GerritPathuis 0:8ce1608bb6f8 179 return byte;
GerritPathuis 0:8ce1608bb6f8 180 }
GerritPathuis 0:8ce1608bb6f8 181
GerritPathuis 0:8ce1608bb6f8 182 void disable_int(void) {
GerritPathuis 0:8ce1608bb6f8 183 char byte;
GerritPathuis 0:8ce1608bb6f8 184 byte = read_reg(CTRL_REG0); // Unlock image writing
GerritPathuis 0:8ce1608bb6f8 185 byte |= 0x10; // Set bit 4
GerritPathuis 0:8ce1608bb6f8 186 write_reg(CTRL_REG0,byte); // Have to set ee_w to
GerritPathuis 0:8ce1608bb6f8 187 //-------------------------
GerritPathuis 0:8ce1608bb6f8 188 byte = read_reg(CTRL_REG3);
GerritPathuis 0:8ce1608bb6f8 189 byte &= 0xFD; // REset bit 1 enable interrupt
GerritPathuis 0:8ce1608bb6f8 190 write_reg(CTRL_REG3,byte); //
GerritPathuis 0:8ce1608bb6f8 191 pc.printf("\n\rDisable interrupt bis is set ");
GerritPathuis 0:8ce1608bb6f8 192 //------------------------------
GerritPathuis 0:8ce1608bb6f8 193 byte = read_reg(CTRL_REG0); // Lock image writing
GerritPathuis 0:8ce1608bb6f8 194 byte &= 0xEF; // REset bit 4
GerritPathuis 0:8ce1608bb6f8 195 write_reg(CTRL_REG0,byte); // Have to set ee_w to
GerritPathuis 0:8ce1608bb6f8 196 pc.printf("\n\rMBA180 in now switched off ");
GerritPathuis 0:8ce1608bb6f8 197 }
GerritPathuis 0:8ce1608bb6f8 198