Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #define BMA180_ID 0x00 00003 #define VERSION 0x01 00004 #define ACCXLSB 0x02 00005 #define ACCXMSB 0x03 00006 #define ACCYLSB 0x04 00007 #define ACCYMSB 0x05 00008 #define ACCZLSB 0x06 00009 #define ACCZMSB 0x07 00010 #define CTRL_REG0 0x0D 00011 #define DIS_I2C 0x27 // bit 0 must be 1 for SPI 00012 #define CTRL_REG3 0x21 // bit 1= new_data_int 00013 #define RESET 0x10 // soft reset 00014 00015 /*-------------Hardware connections------------- 00016 By Gerrit Pathuis (gpa@quicknet.nl) 00017 MBA180 breakour board (sparkfun) is used 00018 VIO----- mbed Vout (3.3 Volt) 00019 SDI----- mbed mosi (p5) 00020 SDO----- mbed miso (p6) 00021 SCK----- mbed sck (p7) 00022 CS------ mbed (p8) // chip select 00023 INT----- mbed (p9) // MBA give a interrupt when ready 00024 GND----- mbed GND (0 Volt) 00025 VDO----- mbed Vout (3.3 Volt) 00026 there are no additional external components used 00027 //----------------------------------------------*/ 00028 00029 SPI spi(p5,p6,p7); //mosi, miso, sclk 00030 DigitalOut cs(p8); 00031 InterruptIn event(p9); 00032 00033 Serial pc(USBTX, USBRX); // tx, rx 00034 void init_SPI_BMA180(void); 00035 void soft_reset(void); 00036 void trigger(void); 00037 void new_data(void); 00038 void write_bits(char u); 00039 void write_reg(uint8_t address, char data); 00040 char read_reg(uint8_t address); 00041 void disable_int(void); 00042 00043 struct sample { // reserve spave for 1000 samples 00044 signed short x; 00045 signed short y; 00046 signed short z; 00047 signed short t; 00048 } vib[1000+1]; 00049 00050 int sample_cnt; 00051 00052 int main() { 00053 int pp; 00054 float xx, yy, zz; 00055 00056 init_SPI_BMA180(); // init the sensor 00057 sample_cnt =0; // start the counter 00058 pc.printf("Start lurking "); 00059 while (sample_cnt <50) { 00060 event.rise(&trigger); 00061 } 00062 disable_int(); // switch the BMA180 off 00063 pc.printf("\n\r\nPresent the data\n\r"); 00064 for (pp=0; pp<50; pp +=1) { 00065 // 2^14 = 16384 00066 // Range is +/1 2.0 G versnelling 00067 xx = vib[pp].x/16384.0*4.0; 00068 yy = vib[pp].y/16384.0*4.0; 00069 zz = vib[pp].z/16384.0*4.0; 00070 pc.printf("Sample %2d, Acc X= %0.4f, Y= %0.4f, Z= %0.4f \n\r", pp, xx, yy, zz); 00071 } 00072 } 00073 00074 void init_SPI_BMA180(void) { 00075 char readVersion, readID; 00076 char byte; 00077 // Setup the spi for 8 bit data, high steady state clock, 00078 // second edge capture, with a 10MHz clock rate 00079 00080 spi.frequency(10000000); // 10 mHz page 59 of the BMA180 datasheet 00081 spi.format(8,3); // Not conform !!! page 58 of the BMA180 datasheet) 00082 wait_ms(100); 00083 readID = read_reg(BMA180_ID); 00084 readVersion = read_reg(VERSION); 00085 pc.printf("\n\r"); 00086 if (readID == 3) { 00087 pc.printf("Connected to BMA180\n\r"); 00088 pc.printf("BMA180 Version %d\n\r", readVersion); 00089 } else 00090 pc.printf("Sorry not connected to BMA180 !!!\n\r", readID); 00091 00092 soft_reset(); // to copy EEprom into volatile area 00093 00094 //--------------------------- 00095 byte = read_reg(CTRL_REG0); // Unlock image writing 00096 byte |= 0x10; // Set bit 4 00097 write_reg(CTRL_REG0,byte); // Have to set ee_w to 00098 //------------------------ 00099 byte= read_reg(DIS_I2C); // read 00100 byte |= 0x01; // set bit0 to 1, SPI only 00101 write_reg(DIS_I2C, byte); // Set spi, disable i2c, page 31 00102 //------------------------- 00103 byte = read_reg(CTRL_REG3); 00104 byte |= 0x02; // set bit 1 enable interrupt 00105 byte |= 0x40; // set bit 6 slope mode 00106 byte |= 0x80; // set bit 6 slope alert 00107 write_reg(CTRL_REG3,byte); // 00108 pc.printf("Enable interrupt bis is set "); 00109 //------------------------------ 00110 byte = read_reg(CTRL_REG0); // Lock image writing 00111 byte &= 0xEF; // REset bit 4 00112 write_reg(CTRL_REG0,byte); // Have to set ee_w to 00113 //------------------------------------------------------------------------------------- 00114 pc.printf("\n\rBMA init done \n\r"); 00115 } 00116 00117 void soft_reset(void) { 00118 // Write a soft reset 00119 // to copy EEprom into volatile area, see mid page 22 00120 write_reg(RESET, 0xB6); // page 48 00121 wait_ms(10); // wait 10 ms, see page 49 00122 pc.printf("Soft reset, EEPROM copied \n\r"); 00123 } 00124 00125 //-----------------READ OUT the X-Y-Z values--------------- 00126 void trigger(void) { 00127 char x_lsb, x_msb; 00128 char y_lsb, y_msb; 00129 char z_lsb, z_msb; 00130 signed short ax, ay, az; 00131 00132 //------------X---------------- 00133 x_lsb = read_reg(ACCXLSB); 00134 x_msb = read_reg(ACCXMSB); 00135 ax = (x_msb << 8) | x_lsb ; // combineer msb en lsb 00136 ax = ax >> 2; // Get rid of two non-value bits in LSB 00137 //------------Y---------------- 00138 y_lsb = read_reg(ACCYLSB); 00139 y_msb = read_reg(ACCYMSB); 00140 ay = (y_msb << 8) | y_lsb; // combineer msb en lsb 00141 ay = ay >> 2; // Get rid of two non-value bits in LSB 00142 //------------Z---------------- 00143 z_lsb = read_reg(ACCZLSB); 00144 z_msb = read_reg(ACCZMSB); 00145 az = (z_msb << 8) | z_lsb; // combineer msb en lsb 00146 az = az >> 2; // Get rid of two non-value bits in LSB 00147 00148 //----------shift into the array--------------------- 00149 vib[sample_cnt].x= ax; 00150 vib[sample_cnt].y= ay; 00151 vib[sample_cnt].z= az; 00152 //---------counter------------ 00153 sample_cnt += 1; 00154 } 00155 00156 void write_reg(uint8_t address, char data) { 00157 address &= 0x7F; //Force a write (bit 7=0) 00158 cs=0; //Select SPI device 00159 wait_us(2); 00160 spi.write(address); //Send register location 00161 wait_us(2); 00162 spi.write(data); //Send value to record into register 00163 wait_us(2); 00164 cs=1; 00165 wait_us(2); 00166 } 00167 00168 char read_reg(uint8_t address) { 00169 char byte; 00170 address |= 0x80; //Force a read (bit 7=1) 00171 cs=0; 00172 wait_us(2); //Select SPI device 00173 spi.write(address); //Send register location 00174 wait_us(2); 00175 byte=spi.write(0xFF); //Get the data 00176 wait_us(2); 00177 cs=1; 00178 wait_us(2); 00179 return byte; 00180 } 00181 00182 void disable_int(void) { 00183 char byte; 00184 byte = read_reg(CTRL_REG0); // Unlock image writing 00185 byte |= 0x10; // Set bit 4 00186 write_reg(CTRL_REG0,byte); // Have to set ee_w to 00187 //------------------------- 00188 byte = read_reg(CTRL_REG3); 00189 byte &= 0xFD; // REset bit 1 enable interrupt 00190 write_reg(CTRL_REG3,byte); // 00191 pc.printf("\n\rDisable interrupt bis is set "); 00192 //------------------------------ 00193 byte = read_reg(CTRL_REG0); // Lock image writing 00194 byte &= 0xEF; // REset bit 4 00195 write_reg(CTRL_REG0,byte); // Have to set ee_w to 00196 pc.printf("\n\rMBA180 in now switched off "); 00197 } 00198
Generated on Wed Jul 13 2022 18:01:48 by
1.7.2