2020/08/26
Dependents: STM32_MR_NOT_IM_REV2 STM32_MR_NOT_IM_REV2 STM32_MR_NOT_IM_REV2_829 STM32_MR_include_IM_rev2
BMX055.cpp@2:7e049ebd96ab, 2020-08-25 (annotated)
- Committer:
- falconsyunya
- Date:
- Tue Aug 25 17:28:06 2020 +0000
- Revision:
- 2:7e049ebd96ab
- Parent:
- 0:d56d821617b6
- Child:
- 3:58349cbf9ed9
2020/08/26;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
makidia | 0:d56d821617b6 | 1 | #include "mbed.h" |
makidia | 0:d56d821617b6 | 2 | #include "BMX055.h" |
makidia | 0:d56d821617b6 | 3 | |
makidia | 0:d56d821617b6 | 4 | BMX055::BMX055(PinName SDA, PinName SCL) : bmx055(SDA,SCL){ |
makidia | 0:d56d821617b6 | 5 | bmx055.frequency(100000); |
makidia | 0:d56d821617b6 | 6 | bmx_init(); |
makidia | 0:d56d821617b6 | 7 | } |
makidia | 0:d56d821617b6 | 8 | |
makidia | 0:d56d821617b6 | 9 | void BMX055::bmx_init(void) |
makidia | 0:d56d821617b6 | 10 | { |
makidia | 0:d56d821617b6 | 11 | char buf[2]={0}; |
makidia | 0:d56d821617b6 | 12 | printf("accel setting\r\n"); |
makidia | 0:d56d821617b6 | 13 | buf[0]=0x14; buf[1]=0xB6; |
makidia | 0:d56d821617b6 | 14 | bmx055.write(ACC,buf,2); wait_ms(2); |
makidia | 0:d56d821617b6 | 15 | buf[0]=0x0F; buf[1]=0x05; |
makidia | 0:d56d821617b6 | 16 | buf[0]=0x10; buf[1]=0x08; |
makidia | 0:d56d821617b6 | 17 | bmx055.write(ACC,buf,2); |
makidia | 0:d56d821617b6 | 18 | buf[0]=0x11; buf[1]=0x00; |
makidia | 0:d56d821617b6 | 19 | bmx055.write(ACC,buf,2); wait_ms(2); |
makidia | 0:d56d821617b6 | 20 | |
makidia | 0:d56d821617b6 | 21 | printf("gyroscope setting\r\n"); |
makidia | 0:d56d821617b6 | 22 | buf[0]=0x0F; buf[1]=0x04; |
makidia | 0:d56d821617b6 | 23 | bmx055.write(GYRO,buf,2); wait(0.1); |
makidia | 0:d56d821617b6 | 24 | buf[0]=0x10; buf[1]=0x07; |
makidia | 0:d56d821617b6 | 25 | bmx055.write(GYRO,buf,2); wait(0.1); |
makidia | 0:d56d821617b6 | 26 | buf[0]=0x11; buf[1]=0x00; |
makidia | 0:d56d821617b6 | 27 | bmx055.write(GYRO,buf,2); wait(0.1); |
makidia | 0:d56d821617b6 | 28 | |
makidia | 0:d56d821617b6 | 29 | printf("magnet setting\r\n"); |
makidia | 0:d56d821617b6 | 30 | buf[0]=0x4B; buf[1]=0x82; |
makidia | 0:d56d821617b6 | 31 | bmx055.write(MAG,buf,2); wait(0.1); |
makidia | 0:d56d821617b6 | 32 | buf[0]=0x4B; buf[1]=0x01; |
makidia | 0:d56d821617b6 | 33 | bmx055.write(MAG,buf,2); wait(0.1); |
makidia | 0:d56d821617b6 | 34 | buf[0]=0x4C; buf[1]=0x00; |
makidia | 0:d56d821617b6 | 35 | bmx055.write(MAG,buf,2); |
makidia | 0:d56d821617b6 | 36 | buf[0]=0x4E; buf[1]=0x84; |
makidia | 0:d56d821617b6 | 37 | bmx055.write(MAG,buf,2); |
makidia | 0:d56d821617b6 | 38 | buf[0]=0x51; buf[1]=0x04; |
makidia | 0:d56d821617b6 | 39 | bmx055.write(MAG,buf,2); |
makidia | 0:d56d821617b6 | 40 | buf[0]=0x52; buf[1]=0x16; |
makidia | 0:d56d821617b6 | 41 | bmx055.write(MAG,buf,2); |
makidia | 0:d56d821617b6 | 42 | wait(0.1); |
makidia | 0:d56d821617b6 | 43 | |
makidia | 0:d56d821617b6 | 44 | buf[0]=0x00; |
makidia | 0:d56d821617b6 | 45 | bmx055.write(MAG,buf,1,1); |
makidia | 0:d56d821617b6 | 46 | bmx055.read(MAG,buf,1); |
makidia | 0:d56d821617b6 | 47 | printf("read:0x%02x\r\n",buf[0]); |
makidia | 0:d56d821617b6 | 48 | } |
makidia | 0:d56d821617b6 | 49 | |
makidia | 0:d56d821617b6 | 50 | void BMX055::getAcc(void){ |
makidia | 0:d56d821617b6 | 51 | uint8_t data[6]={0}; |
makidia | 0:d56d821617b6 | 52 | char send[1], get[1]; |
makidia | 0:d56d821617b6 | 53 | char temp; |
makidia | 0:d56d821617b6 | 54 | |
makidia | 0:d56d821617b6 | 55 | send[0]=(char)(2); |
makidia | 0:d56d821617b6 | 56 | bmx055.write(ACC,send,1,true); |
makidia | 0:d56d821617b6 | 57 | bmx055.read(ACC,(char*)data,6); |
makidia | 0:d56d821617b6 | 58 | |
makidia | 0:d56d821617b6 | 59 | for(int i=0;i<3;i++){ |
makidia | 0:d56d821617b6 | 60 | accel[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 4; |
falconsyunya | 2:7e049ebd96ab | 61 | //if(accel[i]>2047)accel[i]-=4096; |
makidia | 0:d56d821617b6 | 62 | } |
makidia | 0:d56d821617b6 | 63 | } |
makidia | 0:d56d821617b6 | 64 | |
makidia | 0:d56d821617b6 | 65 | void BMX055::getGyro(void){ |
makidia | 0:d56d821617b6 | 66 | int data[6]={0}; |
makidia | 0:d56d821617b6 | 67 | char send[1],get[1]; |
makidia | 0:d56d821617b6 | 68 | char temp; |
makidia | 0:d56d821617b6 | 69 | |
makidia | 0:d56d821617b6 | 70 | for(int i=0;i<6;i++){ |
makidia | 0:d56d821617b6 | 71 | send[0]=(char)(2+i); |
makidia | 0:d56d821617b6 | 72 | bmx055.write(GYRO,send,1); |
makidia | 0:d56d821617b6 | 73 | bmx055.read(GYRO,get,1); |
makidia | 0:d56d821617b6 | 74 | temp=get[0]; |
makidia | 0:d56d821617b6 | 75 | data[i]=temp; |
makidia | 0:d56d821617b6 | 76 | } |
makidia | 0:d56d821617b6 | 77 | |
makidia | 0:d56d821617b6 | 78 | for(int i=0;i<3;i++){ |
makidia | 0:d56d821617b6 | 79 | gyroscope[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 4; |
makidia | 0:d56d821617b6 | 80 | if(gyroscope[i]>32767)gyroscope[i]-=65536; |
makidia | 0:d56d821617b6 | 81 | // gyroscope[i]=gyroscope[i]*0.0038; |
makidia | 0:d56d821617b6 | 82 | } |
makidia | 0:d56d821617b6 | 83 | } |
makidia | 0:d56d821617b6 | 84 | |
makidia | 0:d56d821617b6 | 85 | void BMX055::getMag(void){ |
makidia | 0:d56d821617b6 | 86 | int data[8]={0}; |
makidia | 0:d56d821617b6 | 87 | char send[1],get[1]; |
makidia | 0:d56d821617b6 | 88 | char temp; |
makidia | 0:d56d821617b6 | 89 | |
makidia | 0:d56d821617b6 | 90 | for(int i=0;i<8;i++){ |
makidia | 0:d56d821617b6 | 91 | send[0]=(char)(0x42+i); |
makidia | 0:d56d821617b6 | 92 | bmx055.write(MAG,send,1); |
makidia | 0:d56d821617b6 | 93 | bmx055.read(MAG,get,1); |
makidia | 0:d56d821617b6 | 94 | // printf("%02X ",get[0]); |
makidia | 0:d56d821617b6 | 95 | temp=get[0]; |
makidia | 0:d56d821617b6 | 96 | data[i]=temp; |
makidia | 0:d56d821617b6 | 97 | } |
makidia | 0:d56d821617b6 | 98 | |
makidia | 0:d56d821617b6 | 99 | for(int i=0;i<3;i++){ |
makidia | 0:d56d821617b6 | 100 | if(i!=2)magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 3; |
makidia | 0:d56d821617b6 | 101 | else magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 1; |
makidia | 0:d56d821617b6 | 102 | if(i==2 && magnet[i]>16383)magnet[i]-=32768; |
makidia | 0:d56d821617b6 | 103 | else if(i!=2 && magnet[i]>4095)magnet[i]-=8092; |
makidia | 0:d56d821617b6 | 104 | } |
makidia | 0:d56d821617b6 | 105 | } |