Thundersoft
/
FRDM_FXS_MULTI_B_for_TT_Mxx
Create a project for TT_Mxx.
FRDM_FXS_MULTI_B/MMA8652.cpp@0:369a1b265ddb, 2019-03-21 (annotated)
- Committer:
- ThunderSoft
- Date:
- Thu Mar 21 09:03:32 2019 +0000
- Revision:
- 0:369a1b265ddb
Add code for FRDM_FXS_MULTI_B
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThunderSoft | 0:369a1b265ddb | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
ThunderSoft | 0:369a1b265ddb | 2 | * |
ThunderSoft | 0:369a1b265ddb | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ThunderSoft | 0:369a1b265ddb | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
ThunderSoft | 0:369a1b265ddb | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
ThunderSoft | 0:369a1b265ddb | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
ThunderSoft | 0:369a1b265ddb | 7 | * Software is furnished to do so, subject to the following conditions: |
ThunderSoft | 0:369a1b265ddb | 8 | * |
ThunderSoft | 0:369a1b265ddb | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ThunderSoft | 0:369a1b265ddb | 10 | * substantial portions of the Software. |
ThunderSoft | 0:369a1b265ddb | 11 | * |
ThunderSoft | 0:369a1b265ddb | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ThunderSoft | 0:369a1b265ddb | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ThunderSoft | 0:369a1b265ddb | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ThunderSoft | 0:369a1b265ddb | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ThunderSoft | 0:369a1b265ddb | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ThunderSoft | 0:369a1b265ddb | 17 | */ |
ThunderSoft | 0:369a1b265ddb | 18 | |
ThunderSoft | 0:369a1b265ddb | 19 | #include "MMA8652.h" |
ThunderSoft | 0:369a1b265ddb | 20 | #include "common_define.h" |
ThunderSoft | 0:369a1b265ddb | 21 | #define UINT14_MAX 16383 |
ThunderSoft | 0:369a1b265ddb | 22 | |
ThunderSoft | 0:369a1b265ddb | 23 | MMA8652::MMA8652(PinName sda, PinName scl) : _i2c(sda, scl) { |
ThunderSoft | 0:369a1b265ddb | 24 | |
ThunderSoft | 0:369a1b265ddb | 25 | begin(); |
ThunderSoft | 0:369a1b265ddb | 26 | } |
ThunderSoft | 0:369a1b265ddb | 27 | |
ThunderSoft | 0:369a1b265ddb | 28 | |
ThunderSoft | 0:369a1b265ddb | 29 | MMA8652::~MMA8652() { } |
ThunderSoft | 0:369a1b265ddb | 30 | |
ThunderSoft | 0:369a1b265ddb | 31 | |
ThunderSoft | 0:369a1b265ddb | 32 | void MMA8652::RegRead( char reg, char * d, int len) |
ThunderSoft | 0:369a1b265ddb | 33 | { |
ThunderSoft | 0:369a1b265ddb | 34 | // char cmd = reg; |
ThunderSoft | 0:369a1b265ddb | 35 | // char i2c_addr = MMA8652_SLAVE_ADDR; |
ThunderSoft | 0:369a1b265ddb | 36 | // int status; |
ThunderSoft | 0:369a1b265ddb | 37 | // printf("i2c_addr = 0x%x \r\n",i2c_addr); |
ThunderSoft | 0:369a1b265ddb | 38 | // status = _i2c.write( i2c_addr, &cmd, 1); |
ThunderSoft | 0:369a1b265ddb | 39 | // status = _i2c.read (i2c_addr, d, len); |
ThunderSoft | 0:369a1b265ddb | 40 | // if(status == 0) |
ThunderSoft | 0:369a1b265ddb | 41 | // { |
ThunderSoft | 0:369a1b265ddb | 42 | // printf("success \r\n"); |
ThunderSoft | 0:369a1b265ddb | 43 | // } |
ThunderSoft | 0:369a1b265ddb | 44 | // else |
ThunderSoft | 0:369a1b265ddb | 45 | // { |
ThunderSoft | 0:369a1b265ddb | 46 | // printf("failer \r\n"); |
ThunderSoft | 0:369a1b265ddb | 47 | // } |
ThunderSoft | 0:369a1b265ddb | 48 | i2cReadForFXS_MUTIL(&_i2c,MMA8652_SLAVE_ADDR,(uint8_t*)d,reg,len); |
ThunderSoft | 0:369a1b265ddb | 49 | |
ThunderSoft | 0:369a1b265ddb | 50 | } |
ThunderSoft | 0:369a1b265ddb | 51 | |
ThunderSoft | 0:369a1b265ddb | 52 | void MMA8652::begin(void) |
ThunderSoft | 0:369a1b265ddb | 53 | { |
ThunderSoft | 0:369a1b265ddb | 54 | char data[2]; |
ThunderSoft | 0:369a1b265ddb | 55 | // write 0000 0000 = 0x00 to accelerometer control register 1 to place MMA8652 into |
ThunderSoft | 0:369a1b265ddb | 56 | // standby |
ThunderSoft | 0:369a1b265ddb | 57 | // [7-1] = 0000 000 |
ThunderSoft | 0:369a1b265ddb | 58 | // [0]: active=0 |
ThunderSoft | 0:369a1b265ddb | 59 | data[0] = MMA8652_CTRL_REG1; |
ThunderSoft | 0:369a1b265ddb | 60 | data[1] = 0x00; |
ThunderSoft | 0:369a1b265ddb | 61 | _i2c.write( MMA8652_SLAVE_ADDR, data, 2); |
ThunderSoft | 0:369a1b265ddb | 62 | |
ThunderSoft | 0:369a1b265ddb | 63 | // write 0000 0001= 0x01 to XYZ_DATA_CFG register |
ThunderSoft | 0:369a1b265ddb | 64 | // [7]: reserved |
ThunderSoft | 0:369a1b265ddb | 65 | // [6]: reserved |
ThunderSoft | 0:369a1b265ddb | 66 | // [5]: reserved |
ThunderSoft | 0:369a1b265ddb | 67 | // [4]: hpf_out=0 |
ThunderSoft | 0:369a1b265ddb | 68 | // [3]: reserved |
ThunderSoft | 0:369a1b265ddb | 69 | // [2]: reserved |
ThunderSoft | 0:369a1b265ddb | 70 | // [1-0]: fs=00 for accelerometer range of +/-2g range with 0.244mg/LSB |
ThunderSoft | 0:369a1b265ddb | 71 | data[0] = MMA8652_XYZ_DATA_CFG; |
ThunderSoft | 0:369a1b265ddb | 72 | data[1] = 0x00; |
ThunderSoft | 0:369a1b265ddb | 73 | _i2c.write( MMA8652_SLAVE_ADDR, data, 2); |
ThunderSoft | 0:369a1b265ddb | 74 | |
ThunderSoft | 0:369a1b265ddb | 75 | // write 0000 1101 = 0x0D to accelerometer control register 1 |
ThunderSoft | 0:369a1b265ddb | 76 | // [7-6]: aslp_rate=00 |
ThunderSoft | 0:369a1b265ddb | 77 | // [5-3]: dr=100 for 50Hz data rate |
ThunderSoft | 0:369a1b265ddb | 78 | // [2]: 0 |
ThunderSoft | 0:369a1b265ddb | 79 | // [1]: 0 |
ThunderSoft | 0:369a1b265ddb | 80 | // [0]: active=1 to take the part out of standby and enable sampling |
ThunderSoft | 0:369a1b265ddb | 81 | data[0] = MMA8652_CTRL_REG1; |
ThunderSoft | 0:369a1b265ddb | 82 | data[1] = 0x21; |
ThunderSoft | 0:369a1b265ddb | 83 | _i2c.write( MMA8652_SLAVE_ADDR, data, 2); |
ThunderSoft | 0:369a1b265ddb | 84 | } |
ThunderSoft | 0:369a1b265ddb | 85 | |
ThunderSoft | 0:369a1b265ddb | 86 | char MMA8652::getWhoAmI(void) |
ThunderSoft | 0:369a1b265ddb | 87 | { |
ThunderSoft | 0:369a1b265ddb | 88 | char Id = 0x00; |
ThunderSoft | 0:369a1b265ddb | 89 | RegRead(MMA8652_WHOAMI, &Id, 1); |
ThunderSoft | 0:369a1b265ddb | 90 | return Id; |
ThunderSoft | 0:369a1b265ddb | 91 | } |
ThunderSoft | 0:369a1b265ddb | 92 | |
ThunderSoft | 0:369a1b265ddb | 93 | void MMA8652::ReadXYZ(float * a) |
ThunderSoft | 0:369a1b265ddb | 94 | { |
ThunderSoft | 0:369a1b265ddb | 95 | char d[7]; |
ThunderSoft | 0:369a1b265ddb | 96 | int16_t t[6]; |
ThunderSoft | 0:369a1b265ddb | 97 | |
ThunderSoft | 0:369a1b265ddb | 98 | RegRead( MMA8652_STATUS, d, 7); |
ThunderSoft | 0:369a1b265ddb | 99 | t[0] = ((d[1] * 256) + ((unsigned short) d[2])); |
ThunderSoft | 0:369a1b265ddb | 100 | t[1] = ((d[3] * 256) + ((unsigned short) d[4])); |
ThunderSoft | 0:369a1b265ddb | 101 | t[2] = ((d[5] * 256) + ((unsigned short) d[6])); |
ThunderSoft | 0:369a1b265ddb | 102 | |
ThunderSoft | 0:369a1b265ddb | 103 | a[0] = (float) t[0] / 16384.0; |
ThunderSoft | 0:369a1b265ddb | 104 | a[1] = (float) t[1] / 16384.0; |
ThunderSoft | 0:369a1b265ddb | 105 | a[2] = (float) t[2] / 16384.0; |
ThunderSoft | 0:369a1b265ddb | 106 | |
ThunderSoft | 0:369a1b265ddb | 107 | } |
ThunderSoft | 0:369a1b265ddb | 108 | |
ThunderSoft | 0:369a1b265ddb | 109 | |
ThunderSoft | 0:369a1b265ddb | 110 | void MMA8652::ReadXYZraw(int16_t * d) |
ThunderSoft | 0:369a1b265ddb | 111 | { |
ThunderSoft | 0:369a1b265ddb | 112 | char res[6]; |
ThunderSoft | 0:369a1b265ddb | 113 | int16_t acc; |
ThunderSoft | 0:369a1b265ddb | 114 | RegRead( MMA8652_OUT_X_MSB, res, 6); |
ThunderSoft | 0:369a1b265ddb | 115 | |
ThunderSoft | 0:369a1b265ddb | 116 | acc = (res[0] << 6) | (res[1] >> 2); |
ThunderSoft | 0:369a1b265ddb | 117 | if (acc > UINT14_MAX/2) |
ThunderSoft | 0:369a1b265ddb | 118 | acc -= UINT14_MAX; |
ThunderSoft | 0:369a1b265ddb | 119 | d[0] = acc; |
ThunderSoft | 0:369a1b265ddb | 120 | acc = (res[2] << 6) | (res[3] >> 2); |
ThunderSoft | 0:369a1b265ddb | 121 | if (acc > UINT14_MAX/2) |
ThunderSoft | 0:369a1b265ddb | 122 | acc -= UINT14_MAX; |
ThunderSoft | 0:369a1b265ddb | 123 | d[1] = acc; |
ThunderSoft | 0:369a1b265ddb | 124 | acc = (res[4] << 6) | (res[5] >> 2); |
ThunderSoft | 0:369a1b265ddb | 125 | if (acc > UINT14_MAX/2) |
ThunderSoft | 0:369a1b265ddb | 126 | acc -= UINT14_MAX; |
ThunderSoft | 0:369a1b265ddb | 127 | d[2] = acc; |
ThunderSoft | 0:369a1b265ddb | 128 | } |
ThunderSoft | 0:369a1b265ddb | 129 | |
ThunderSoft | 0:369a1b265ddb | 130 |