BMX055に4線式SPIモードで接続するためのライブラリです
BMX055.cpp@1:fed25406cc14, 2018-02-13 (annotated)
- Committer:
- kim1212
- Date:
- Tue Feb 13 04:13:42 2018 +0000
- Revision:
- 1:fed25406cc14
- Parent:
- 0:c317a0f96763
??????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kim1212 | 0:c317a0f96763 | 1 | |
kim1212 | 0:c317a0f96763 | 2 | #include "BMX055.h" |
kim1212 | 0:c317a0f96763 | 3 | |
kim1212 | 0:c317a0f96763 | 4 | BMX055::BMX055(PinName mosi,PinName miso,PinName sck,PinName _accss,PinName _gyrss,PinName _magss):bmx(mosi,miso,sck),accss(_accss),gyrss(_gyrss),magss(_magss) |
kim1212 | 0:c317a0f96763 | 5 | { |
kim1212 | 0:c317a0f96763 | 6 | accss=1; |
kim1212 | 0:c317a0f96763 | 7 | gyrss=1; |
kim1212 | 0:c317a0f96763 | 8 | magss=1; |
kim1212 | 0:c317a0f96763 | 9 | wait(0.1); |
kim1212 | 1:fed25406cc14 | 10 | bmx.frequency(10000000); |
kim1212 | 0:c317a0f96763 | 11 | bmx.format(8,3); |
kim1212 | 1:fed25406cc14 | 12 | init_acc(RANGE_2G,BW_250); |
kim1212 | 1:fed25406cc14 | 13 | init_gyr(RANGE_250,ODR_BW_2000_230); |
kim1212 | 1:fed25406cc14 | 14 | init_mag(0,0); |
kim1212 | 0:c317a0f96763 | 15 | |
kim1212 | 0:c317a0f96763 | 16 | |
kim1212 | 0:c317a0f96763 | 17 | |
kim1212 | 0:c317a0f96763 | 18 | } |
kim1212 | 0:c317a0f96763 | 19 | |
kim1212 | 0:c317a0f96763 | 20 | char BMX055::readbyte_acc(char adr){ |
kim1212 | 0:c317a0f96763 | 21 | accss=0; |
kim1212 | 0:c317a0f96763 | 22 | adr=adr|0b10000000; |
kim1212 | 0:c317a0f96763 | 23 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 24 | char val=bmx.write(0xFF); |
kim1212 | 0:c317a0f96763 | 25 | accss=1; |
kim1212 | 0:c317a0f96763 | 26 | return val; |
kim1212 | 0:c317a0f96763 | 27 | |
kim1212 | 0:c317a0f96763 | 28 | } |
kim1212 | 0:c317a0f96763 | 29 | |
kim1212 | 0:c317a0f96763 | 30 | void BMX055::readnbyte_acc(char adr,char *data,char num){//numは配列の数 |
kim1212 | 0:c317a0f96763 | 31 | accss=0; |
kim1212 | 0:c317a0f96763 | 32 | adr=adr|0b10000000; |
kim1212 | 0:c317a0f96763 | 33 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 34 | for(int i=0;i<num;i++)data[i]=bmx.write(0xFF); |
kim1212 | 0:c317a0f96763 | 35 | accss=1; |
kim1212 | 0:c317a0f96763 | 36 | } |
kim1212 | 0:c317a0f96763 | 37 | void BMX055::writebyte_acc(char adr,char data){ |
kim1212 | 0:c317a0f96763 | 38 | accss=0; |
kim1212 | 0:c317a0f96763 | 39 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 40 | bmx.write(data); |
kim1212 | 0:c317a0f96763 | 41 | accss=1; |
kim1212 | 0:c317a0f96763 | 42 | } |
kim1212 | 1:fed25406cc14 | 43 | bool BMX055::read_acc(short *acc){ |
kim1212 | 0:c317a0f96763 | 44 | char data[6]; |
kim1212 | 0:c317a0f96763 | 45 | readnbyte_acc(0x02,data,6); |
kim1212 | 0:c317a0f96763 | 46 | unsigned short buf[3]; |
kim1212 | 0:c317a0f96763 | 47 | for(int i=0;i<3;i++){ |
kim1212 | 0:c317a0f96763 | 48 | buf[i]=(data[2*i]>>4)|(data[2*i+1]<<4); |
kim1212 | 1:fed25406cc14 | 49 | if(buf[i]>0x800)acc[i]=(short)-1*((~buf[i]+1)&0xfff); |
kim1212 | 1:fed25406cc14 | 50 | else acc[i]=(short)buf[i]; |
kim1212 | 0:c317a0f96763 | 51 | } |
kim1212 | 0:c317a0f96763 | 52 | return true; |
kim1212 | 0:c317a0f96763 | 53 | } |
kim1212 | 0:c317a0f96763 | 54 | char BMX055::read_temp(){ |
kim1212 | 0:c317a0f96763 | 55 | return readbyte_acc(0x08); |
kim1212 | 0:c317a0f96763 | 56 | } |
kim1212 | 0:c317a0f96763 | 57 | void BMX055::calib_acc(){ |
kim1212 | 0:c317a0f96763 | 58 | //sensor offset |
kim1212 | 1:fed25406cc14 | 59 | writebyte_acc(0x37,0b01111110); |
kim1212 | 0:c317a0f96763 | 60 | |
kim1212 | 1:fed25406cc14 | 61 | wait_ms(10); |
kim1212 | 0:c317a0f96763 | 62 | while(((readbyte_acc(0x36)&0b00010000)>>4)!=1); |
kim1212 | 0:c317a0f96763 | 63 | writebyte_acc(0x36,0b00100000); |
kim1212 | 0:c317a0f96763 | 64 | while(((readbyte_acc(0x36)&0b00010000)>>4)!=1); |
kim1212 | 0:c317a0f96763 | 65 | writebyte_acc(0x36,0b01000000); |
kim1212 | 0:c317a0f96763 | 66 | while(((readbyte_acc(0x36)&0b00010000)>>4)!=1); |
kim1212 | 0:c317a0f96763 | 67 | writebyte_acc(0x36,0b01100000); |
kim1212 | 0:c317a0f96763 | 68 | while(((readbyte_acc(0x36)&0b00010000)>>4)!=1); |
kim1212 | 0:c317a0f96763 | 69 | } |
kim1212 | 0:c317a0f96763 | 70 | void BMX055::init_acc(char range,char bw){ |
kim1212 | 0:c317a0f96763 | 71 | accss=1; |
kim1212 | 0:c317a0f96763 | 72 | writebyte_acc(0x14,0xb6);//softwarereset |
kim1212 | 0:c317a0f96763 | 73 | |
kim1212 | 0:c317a0f96763 | 74 | writebyte_acc(0x0f,range); |
kim1212 | 0:c317a0f96763 | 75 | writebyte_acc(0x10,bw); |
kim1212 | 0:c317a0f96763 | 76 | |
kim1212 | 0:c317a0f96763 | 77 | wait(0.5); |
kim1212 | 0:c317a0f96763 | 78 | calib_acc(); |
kim1212 | 0:c317a0f96763 | 79 | } |
kim1212 | 0:c317a0f96763 | 80 | |
kim1212 | 0:c317a0f96763 | 81 | |
kim1212 | 0:c317a0f96763 | 82 | char BMX055::readbyte_gyr(char adr){ |
kim1212 | 0:c317a0f96763 | 83 | gyrss=0; |
kim1212 | 0:c317a0f96763 | 84 | adr=adr|0b10000000; |
kim1212 | 0:c317a0f96763 | 85 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 86 | char val=bmx.write(0xFF); |
kim1212 | 0:c317a0f96763 | 87 | gyrss=1; |
kim1212 | 0:c317a0f96763 | 88 | return val; |
kim1212 | 0:c317a0f96763 | 89 | } |
kim1212 | 0:c317a0f96763 | 90 | void BMX055::readnbyte_gyr(char adr,char *data,char num){//numは配列の数 |
kim1212 | 0:c317a0f96763 | 91 | gyrss=0; |
kim1212 | 0:c317a0f96763 | 92 | adr=adr|0b10000000; |
kim1212 | 0:c317a0f96763 | 93 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 94 | for(int i=0;i<num;i++)data[i]=bmx.write(0xFF); |
kim1212 | 0:c317a0f96763 | 95 | gyrss=1; |
kim1212 | 0:c317a0f96763 | 96 | } |
kim1212 | 0:c317a0f96763 | 97 | void BMX055::writebyte_gyr(char adr,char data){ |
kim1212 | 0:c317a0f96763 | 98 | gyrss=0; |
kim1212 | 0:c317a0f96763 | 99 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 100 | bmx.write(data); |
kim1212 | 0:c317a0f96763 | 101 | gyrss=1; |
kim1212 | 0:c317a0f96763 | 102 | } |
kim1212 | 1:fed25406cc14 | 103 | bool BMX055::read_gyr(short *gyr){ |
kim1212 | 0:c317a0f96763 | 104 | char data[6]; |
kim1212 | 0:c317a0f96763 | 105 | readnbyte_gyr(0x02,data,6); |
kim1212 | 0:c317a0f96763 | 106 | short buf[3]; |
kim1212 | 0:c317a0f96763 | 107 | for(int i=0;i<3;i++){ |
kim1212 | 0:c317a0f96763 | 108 | buf[i]=data[2*i]|(data[2*i+1]<<8); |
kim1212 | 1:fed25406cc14 | 109 | gyr[i]=buf[i]; |
kim1212 | 0:c317a0f96763 | 110 | } |
kim1212 | 0:c317a0f96763 | 111 | |
kim1212 | 0:c317a0f96763 | 112 | return true; |
kim1212 | 0:c317a0f96763 | 113 | } |
kim1212 | 0:c317a0f96763 | 114 | void BMX055::calib_gyr(){ |
kim1212 | 0:c317a0f96763 | 115 | writebyte_gyr(0x32,0b00111111); |
kim1212 | 0:c317a0f96763 | 116 | } |
kim1212 | 0:c317a0f96763 | 117 | void BMX055::init_gyr(char range,char odr_bw){ |
kim1212 | 0:c317a0f96763 | 118 | |
kim1212 | 0:c317a0f96763 | 119 | gyrss=1; |
kim1212 | 0:c317a0f96763 | 120 | writebyte_gyr(0x14,0xb6);//soft reset |
kim1212 | 0:c317a0f96763 | 121 | writebyte_gyr(0x0f,range); |
kim1212 | 0:c317a0f96763 | 122 | writebyte_gyr(0x10,odr_bw);//bandwidth&ODR |
kim1212 | 0:c317a0f96763 | 123 | calib_gyr(); |
kim1212 | 0:c317a0f96763 | 124 | } |
kim1212 | 0:c317a0f96763 | 125 | |
kim1212 | 0:c317a0f96763 | 126 | char BMX055::readbyte_mag(char adr){ |
kim1212 | 0:c317a0f96763 | 127 | magss=0; |
kim1212 | 0:c317a0f96763 | 128 | adr=adr|0b10000000; |
kim1212 | 0:c317a0f96763 | 129 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 130 | char val=bmx.write(0xFF); |
kim1212 | 0:c317a0f96763 | 131 | magss=1; |
kim1212 | 0:c317a0f96763 | 132 | return val; |
kim1212 | 0:c317a0f96763 | 133 | } |
kim1212 | 0:c317a0f96763 | 134 | void BMX055::writebyte_mag(char adr,char data){ |
kim1212 | 0:c317a0f96763 | 135 | magss=0; |
kim1212 | 0:c317a0f96763 | 136 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 137 | bmx.write(data); |
kim1212 | 0:c317a0f96763 | 138 | magss=1; |
kim1212 | 0:c317a0f96763 | 139 | } |
kim1212 | 0:c317a0f96763 | 140 | void BMX055::readnbyte_mag(char adr,char *data,char num){//numは配列の数 |
kim1212 | 0:c317a0f96763 | 141 | magss=0; |
kim1212 | 0:c317a0f96763 | 142 | adr=adr|0b10000000; |
kim1212 | 0:c317a0f96763 | 143 | bmx.write(adr); |
kim1212 | 0:c317a0f96763 | 144 | for(int i=0;i<num;i++)data[i]=bmx.write(0xFF); |
kim1212 | 0:c317a0f96763 | 145 | magss=1; |
kim1212 | 0:c317a0f96763 | 146 | } |
kim1212 | 1:fed25406cc14 | 147 | bool BMX055::read_mag(short *mag){ |
kim1212 | 0:c317a0f96763 | 148 | if((readbyte_mag(0x48)&0x01)==1){ |
kim1212 | 0:c317a0f96763 | 149 | char data[8]; |
kim1212 | 0:c317a0f96763 | 150 | readnbyte_mag(0x42,data,8); |
kim1212 | 0:c317a0f96763 | 151 | short buf[4]; |
kim1212 | 0:c317a0f96763 | 152 | |
kim1212 | 0:c317a0f96763 | 153 | buf[0]=(data[0]>>3)|(data[1]<<5); |
kim1212 | 1:fed25406cc14 | 154 | if(buf[0]>0x1000)mag[0]=-1*((~buf[0]+1)&0xfff); |
kim1212 | 1:fed25406cc14 | 155 | else mag[0]=buf[0]; |
kim1212 | 0:c317a0f96763 | 156 | |
kim1212 | 0:c317a0f96763 | 157 | buf[1]=(data[2]>>3)|(data[3]<<5); |
kim1212 | 1:fed25406cc14 | 158 | if(buf[1]>0x1000)mag[1]=-1*((~buf[1]+1)&0xfff); |
kim1212 | 1:fed25406cc14 | 159 | else mag[1]=buf[1]; |
kim1212 | 0:c317a0f96763 | 160 | |
kim1212 | 0:c317a0f96763 | 161 | buf[2]=((short)data[4]>>1)|((short)data[5]<<7); |
kim1212 | 1:fed25406cc14 | 162 | if(buf[2]>0x4000)mag[2]=-1*((~buf[2]+1)&0x7fff); |
kim1212 | 1:fed25406cc14 | 163 | else mag[2]=buf[2]; |
kim1212 | 0:c317a0f96763 | 164 | |
kim1212 | 0:c317a0f96763 | 165 | buf[3]=((short)data[6]>>2)|((short)data[7]<<6); |
kim1212 | 1:fed25406cc14 | 166 | if(buf[3]>0x2000)mag[3]=-1*((~buf[3]+1)&0x3fff); |
kim1212 | 1:fed25406cc14 | 167 | else mag[3]=buf[3]; |
kim1212 | 1:fed25406cc14 | 168 | |
kim1212 | 1:fed25406cc14 | 169 | writebyte_mag(0x4c,0b00000010);//forced mode trigger |
kim1212 | 1:fed25406cc14 | 170 | |
kim1212 | 0:c317a0f96763 | 171 | |
kim1212 | 0:c317a0f96763 | 172 | return true; |
kim1212 | 0:c317a0f96763 | 173 | |
kim1212 | 0:c317a0f96763 | 174 | }else return false; |
kim1212 | 0:c317a0f96763 | 175 | } |
kim1212 | 1:fed25406cc14 | 176 | void BMX055::init_mag(unsigned char REPXY,unsigned char REPZ){ |
kim1212 | 0:c317a0f96763 | 177 | magss=1; |
kim1212 | 1:fed25406cc14 | 178 | writebyte_mag(0x4b,0b00000001);//go sleep mode |
kim1212 | 1:fed25406cc14 | 179 | wait_ms(50); |
kim1212 | 1:fed25406cc14 | 180 | writebyte_mag(0x4b,0b10000011);//soft reset&go sleep mode |
kim1212 | 0:c317a0f96763 | 181 | wait_ms(50); |
kim1212 | 1:fed25406cc14 | 182 | |
kim1212 | 1:fed25406cc14 | 183 | writebyte_mag(0x51,REPXY);//set REPXY |
kim1212 | 1:fed25406cc14 | 184 | //1+REPXY |
kim1212 | 1:fed25406cc14 | 185 | wait_ms(50); |
kim1212 | 1:fed25406cc14 | 186 | |
kim1212 | 1:fed25406cc14 | 187 | writebyte_mag(0x52,REPZ);//set REPZ |
kim1212 | 1:fed25406cc14 | 188 | //1+REPZ |
kim1212 | 1:fed25406cc14 | 189 | wait_ms(50); |
kim1212 | 1:fed25406cc14 | 190 | |
kim1212 | 1:fed25406cc14 | 191 | writebyte_mag(0x4c,0b00000010);//forced modetrigger |
kim1212 | 0:c317a0f96763 | 192 | } |
kim1212 | 0:c317a0f96763 | 193 | |
kim1212 | 0:c317a0f96763 | 194 | |
kim1212 | 0:c317a0f96763 | 195 | |
kim1212 | 0:c317a0f96763 | 196 | void BMX055::offset(){ |
kim1212 | 0:c317a0f96763 | 197 | calib_acc(); |
kim1212 | 0:c317a0f96763 | 198 | calib_gyr(); |
kim1212 | 0:c317a0f96763 | 199 | } |
kim1212 | 0:c317a0f96763 | 200 |