Battery hex

Dependencies:   mbed PowerControl SDFileSystem

Fork of HeptaBattery_hex by 智也 大野

Committer:
MEXT1
Date:
Tue Dec 13 10:50:09 2016 +0000
Revision:
2:7c5919465fc8
Parent:
0:30e193b92735
Battery hex

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tomoya123 0:30e193b92735 1 #include "HeptaAccel.h"
tomoya123 0:30e193b92735 2 #include "mbed.h"
tomoya123 0:30e193b92735 3
tomoya123 0:30e193b92735 4 HeptaAccel::HeptaAccel(PinName sda, PinName scl, int aaddr ) : accel(sda,scl),addr(aaddr)
tomoya123 0:30e193b92735 5 {
tomoya123 0:30e193b92735 6 _cmd[0] = 0x2A;//CTL_REG
tomoya123 0:30e193b92735 7 _cmd[1] = 0x01;//ACTIVE
tomoya123 0:30e193b92735 8 accel.write(addr, _cmd, 2);
tomoya123 0:30e193b92735 9 accel.start();
tomoya123 0:30e193b92735 10 accel.write(addr);
tomoya123 0:30e193b92735 11 accel.stop();
tomoya123 0:30e193b92735 12 }
tomoya123 0:30e193b92735 13
tomoya123 0:30e193b92735 14 void HeptaAccel::setup()
tomoya123 0:30e193b92735 15 {
tomoya123 0:30e193b92735 16 _cmd[0] = 0x2A;
tomoya123 0:30e193b92735 17 _cmd[1] = 0x01;
tomoya123 0:30e193b92735 18 accel.frequency(100000);
tomoya123 0:30e193b92735 19 accel.write(addr, _cmd, 2);
tomoya123 0:30e193b92735 20 accel.start();
tomoya123 0:30e193b92735 21 accel.write(addr);
tomoya123 0:30e193b92735 22 accel.stop();
tomoya123 0:30e193b92735 23 }
tomoya123 0:30e193b92735 24
tomoya123 0:30e193b92735 25 void HeptaAccel::sensing(float *ax,float *ay,float *az)
tomoya123 0:30e193b92735 26 {
tomoya123 0:30e193b92735 27 int16_t acc;
tomoya123 0:30e193b92735 28 accel.start();//start
tomoya123 0:30e193b92735 29 accel.write(addr);//write address
tomoya123 0:30e193b92735 30 accel.write(0x01);//X-axis_MSB_REG
tomoya123 0:30e193b92735 31 accel.start();//restart, switch from write to read
tomoya123 0:30e193b92735 32 accel.write(addr|0x01);//read address
tomoya123 0:30e193b92735 33 _xmsb = accel.read(0);//read from selected register with NACK
tomoya123 0:30e193b92735 34 accel.stop();
tomoya123 0:30e193b92735 35 accel.start();
tomoya123 0:30e193b92735 36 accel.write(addr);
tomoya123 0:30e193b92735 37 accel.write(0x02);//X-axis_LSB_REG
tomoya123 0:30e193b92735 38 accel.start();
tomoya123 0:30e193b92735 39 accel.write(addr|0x01);
tomoya123 0:30e193b92735 40 _xlsb = accel.read(0);
tomoya123 0:30e193b92735 41 accel.stop();
tomoya123 0:30e193b92735 42 acc = (_xmsb << 6) | (_xlsb >> 2);
tomoya123 0:30e193b92735 43 if (acc > UINT14_MAX/2)
tomoya123 0:30e193b92735 44 {
tomoya123 0:30e193b92735 45 acc -= UINT14_MAX;
tomoya123 0:30e193b92735 46 }
tomoya123 0:30e193b92735 47 *ax = acc/4096.0*9.81;
tomoya123 0:30e193b92735 48
tomoya123 0:30e193b92735 49 accel.start();
tomoya123 0:30e193b92735 50 accel.write(addr);
tomoya123 0:30e193b92735 51 accel.write(0x03);
tomoya123 0:30e193b92735 52 accel.start();
tomoya123 0:30e193b92735 53 accel.write(addr|0x01);
tomoya123 0:30e193b92735 54 _ymsb = accel.read(0);
tomoya123 0:30e193b92735 55 accel.stop();
tomoya123 0:30e193b92735 56 accel.start();
tomoya123 0:30e193b92735 57 accel.write(addr);
tomoya123 0:30e193b92735 58 accel.write(0x04);
tomoya123 0:30e193b92735 59 accel.start();
tomoya123 0:30e193b92735 60 accel.write(addr|0x01);
tomoya123 0:30e193b92735 61 _ylsb = accel.read(0);
tomoya123 0:30e193b92735 62 accel.stop();
tomoya123 0:30e193b92735 63 acc = (_ymsb << 6) | (_ylsb >> 2);
tomoya123 0:30e193b92735 64 if (acc > UINT14_MAX/2)
tomoya123 0:30e193b92735 65 {
tomoya123 0:30e193b92735 66 acc -= UINT14_MAX;
tomoya123 0:30e193b92735 67 }
tomoya123 0:30e193b92735 68 *ay = acc/4096.0*9.81;
tomoya123 0:30e193b92735 69
tomoya123 0:30e193b92735 70 accel.start();
tomoya123 0:30e193b92735 71 accel.write(addr);
tomoya123 0:30e193b92735 72 accel.write(0x05);
tomoya123 0:30e193b92735 73 accel.start();
tomoya123 0:30e193b92735 74 accel.write(addr|0x01);
tomoya123 0:30e193b92735 75 _zmsb = accel.read(0);
tomoya123 0:30e193b92735 76 accel.stop();
tomoya123 0:30e193b92735 77 accel.start();
tomoya123 0:30e193b92735 78 accel.write(addr);
tomoya123 0:30e193b92735 79 accel.write(0x06);
tomoya123 0:30e193b92735 80 accel.start();
tomoya123 0:30e193b92735 81 accel.write(addr|0x01);
tomoya123 0:30e193b92735 82 _zlsb = accel.read(0);
tomoya123 0:30e193b92735 83 accel.stop();
tomoya123 0:30e193b92735 84 acc = (_zmsb << 6) | (_zlsb >> 2);
tomoya123 0:30e193b92735 85 if (acc > UINT14_MAX/2)
tomoya123 0:30e193b92735 86 {
tomoya123 0:30e193b92735 87 acc -= UINT14_MAX;
tomoya123 0:30e193b92735 88 }
tomoya123 0:30e193b92735 89 *az = acc/4096.0*9.81;
tomoya123 0:30e193b92735 90 }
tomoya123 0:30e193b92735 91
tomoya123 0:30e193b92735 92 float HeptaAccel::x()
tomoya123 0:30e193b92735 93 {
tomoya123 0:30e193b92735 94 int16_t acc;
tomoya123 0:30e193b92735 95 float ax;
tomoya123 0:30e193b92735 96 accel.start();
tomoya123 0:30e193b92735 97 accel.write(addr);
tomoya123 0:30e193b92735 98 accel.write(0x01);
tomoya123 0:30e193b92735 99 accel.start();
tomoya123 0:30e193b92735 100 accel.write(addr|0x01);
tomoya123 0:30e193b92735 101 _xmsb = accel.read(0);
tomoya123 0:30e193b92735 102 accel.stop();
tomoya123 0:30e193b92735 103 accel.start();
tomoya123 0:30e193b92735 104 accel.write(addr);
tomoya123 0:30e193b92735 105 accel.write(0x02);
tomoya123 0:30e193b92735 106 accel.start();
tomoya123 0:30e193b92735 107 accel.write(addr|0x01);
tomoya123 0:30e193b92735 108 _xlsb = accel.read(0);
tomoya123 0:30e193b92735 109 accel.stop();
tomoya123 0:30e193b92735 110 acc = (_xmsb << 6) | (_xlsb >> 2);
tomoya123 0:30e193b92735 111 if (acc > UINT14_MAX/2)
tomoya123 0:30e193b92735 112 {
tomoya123 0:30e193b92735 113 acc -= UINT14_MAX;
tomoya123 0:30e193b92735 114 }
tomoya123 0:30e193b92735 115 ax = acc/4096.0*9.81;
tomoya123 0:30e193b92735 116 return(ax);
tomoya123 0:30e193b92735 117 }
tomoya123 0:30e193b92735 118
tomoya123 0:30e193b92735 119 float HeptaAccel::y()
tomoya123 0:30e193b92735 120 {
tomoya123 0:30e193b92735 121 int16_t acc;
tomoya123 0:30e193b92735 122 float ay;
tomoya123 0:30e193b92735 123 accel.start();
tomoya123 0:30e193b92735 124 accel.write(addr);
tomoya123 0:30e193b92735 125 accel.write(0x03);
tomoya123 0:30e193b92735 126 accel.start();
tomoya123 0:30e193b92735 127 accel.write(addr|0x01);
tomoya123 0:30e193b92735 128 _ymsb = accel.read(0);
tomoya123 0:30e193b92735 129 accel.stop();
tomoya123 0:30e193b92735 130 accel.start();
tomoya123 0:30e193b92735 131 accel.write(addr);
tomoya123 0:30e193b92735 132 accel.write(0x04);
tomoya123 0:30e193b92735 133 accel.start();
tomoya123 0:30e193b92735 134 accel.write(addr|0x01);
tomoya123 0:30e193b92735 135 _ylsb = accel.read(0);
tomoya123 0:30e193b92735 136 accel.stop();
tomoya123 0:30e193b92735 137 acc = (_ymsb << 6) | (_ylsb >> 2);
tomoya123 0:30e193b92735 138 if (acc > UINT14_MAX/2)
tomoya123 0:30e193b92735 139 {
tomoya123 0:30e193b92735 140 acc -= UINT14_MAX;
tomoya123 0:30e193b92735 141 }
tomoya123 0:30e193b92735 142 ay = acc/4096.0*9.81;
tomoya123 0:30e193b92735 143 return(ay);
tomoya123 0:30e193b92735 144 }
tomoya123 0:30e193b92735 145
tomoya123 0:30e193b92735 146 float HeptaAccel::z()
tomoya123 0:30e193b92735 147 {
tomoya123 0:30e193b92735 148 int16_t acc;
tomoya123 0:30e193b92735 149 float az;
tomoya123 0:30e193b92735 150 accel.start();
tomoya123 0:30e193b92735 151 accel.write(addr);
tomoya123 0:30e193b92735 152 accel.write(0x2C);
tomoya123 0:30e193b92735 153 accel.start();
tomoya123 0:30e193b92735 154 accel.write(addr|0x01);
tomoya123 0:30e193b92735 155 _zmsb = accel.read(0);
tomoya123 0:30e193b92735 156 accel.stop();
tomoya123 0:30e193b92735 157 accel.start();
tomoya123 0:30e193b92735 158 accel.write(addr);
tomoya123 0:30e193b92735 159 accel.write(0x2D);
tomoya123 0:30e193b92735 160 accel.start();
tomoya123 0:30e193b92735 161 accel.write(addr|0x01);
tomoya123 0:30e193b92735 162 _zlsb = accel.read(0);
tomoya123 0:30e193b92735 163 accel.stop();
tomoya123 0:30e193b92735 164 acc = (_zmsb << 6) | (_zlsb >> 2);
tomoya123 0:30e193b92735 165 if (acc > UINT14_MAX/2)
tomoya123 0:30e193b92735 166 {
tomoya123 0:30e193b92735 167 acc -= UINT14_MAX;
tomoya123 0:30e193b92735 168 }
tomoya123 0:30e193b92735 169 az = acc/4096.0*9.81;
tomoya123 0:30e193b92735 170 return(az);
tomoya123 0:30e193b92735 171 }
tomoya123 0:30e193b92735 172
tomoya123 0:30e193b92735 173 void HeptaAccel::sensing_u16(char* ax_u16,char* ay_u16,char* az_u16, int *dsize)
tomoya123 0:30e193b92735 174 {
tomoya123 0:30e193b92735 175 char a1[8]={0x00},a2[8]={0x00};
tomoya123 0:30e193b92735 176 accel.start();
tomoya123 0:30e193b92735 177 accel.write(addr);
tomoya123 0:30e193b92735 178 accel.write(0x01);
tomoya123 0:30e193b92735 179 accel.start();
tomoya123 0:30e193b92735 180 accel.write(addr|0x01);
tomoya123 0:30e193b92735 181 _xmsb = accel.read(0);
tomoya123 0:30e193b92735 182 accel.stop();
tomoya123 0:30e193b92735 183 accel.start();
tomoya123 0:30e193b92735 184 accel.write(addr);
tomoya123 0:30e193b92735 185 accel.write(0x02);
tomoya123 0:30e193b92735 186 accel.start();
tomoya123 0:30e193b92735 187 accel.write(addr|0x01);
tomoya123 0:30e193b92735 188 _xlsb = accel.read(0);
tomoya123 0:30e193b92735 189 accel.stop();
tomoya123 0:30e193b92735 190 sprintf( a1, "%02X", ((_xmsb)) & 0xFF);
tomoya123 0:30e193b92735 191 sprintf( a2, "%02X", ((_xlsb)) & 0xFF);
tomoya123 0:30e193b92735 192 ax_u16[0]=a1[0];
tomoya123 0:30e193b92735 193 ax_u16[1]=a1[1];
tomoya123 0:30e193b92735 194 ax_u16[2]=a2[0];
tomoya123 0:30e193b92735 195 ax_u16[3]=a2[1];
tomoya123 0:30e193b92735 196
tomoya123 0:30e193b92735 197 accel.start();
tomoya123 0:30e193b92735 198 accel.write(addr);
tomoya123 0:30e193b92735 199 accel.write(0x03);
tomoya123 0:30e193b92735 200 accel.start();
tomoya123 0:30e193b92735 201 accel.write(addr|0x01);
tomoya123 0:30e193b92735 202 _ymsb = accel.read(0);
tomoya123 0:30e193b92735 203 accel.stop();
tomoya123 0:30e193b92735 204 accel.start();
tomoya123 0:30e193b92735 205 accel.write(addr);
tomoya123 0:30e193b92735 206 accel.write(0x04);
tomoya123 0:30e193b92735 207 accel.start();
tomoya123 0:30e193b92735 208 accel.write(addr|0x01);
tomoya123 0:30e193b92735 209 _ylsb = accel.read(0);
tomoya123 0:30e193b92735 210 accel.stop();
tomoya123 0:30e193b92735 211 sprintf( a1, "%02X", ((_ymsb)) & 0xFF);
tomoya123 0:30e193b92735 212 sprintf( a2, "%02X", ((_ylsb)) & 0xFF);
tomoya123 0:30e193b92735 213 ay_u16[0]=a1[0];
tomoya123 0:30e193b92735 214 ay_u16[1]=a1[1];
tomoya123 0:30e193b92735 215 ay_u16[2]=a2[0];
tomoya123 0:30e193b92735 216 ay_u16[3]=a2[1];
tomoya123 0:30e193b92735 217
tomoya123 0:30e193b92735 218 accel.start();
tomoya123 0:30e193b92735 219 accel.write(addr);
tomoya123 0:30e193b92735 220 accel.write(0x05);
tomoya123 0:30e193b92735 221 accel.start();
tomoya123 0:30e193b92735 222 accel.write(addr|0x01);
tomoya123 0:30e193b92735 223 _zmsb = accel.read(0);
tomoya123 0:30e193b92735 224 accel.stop();
tomoya123 0:30e193b92735 225 accel.start();
tomoya123 0:30e193b92735 226 accel.write(addr);
tomoya123 0:30e193b92735 227 accel.write(0x06);
tomoya123 0:30e193b92735 228 accel.start();
tomoya123 0:30e193b92735 229 accel.write(addr|0x01);
tomoya123 0:30e193b92735 230 _zlsb = accel.read(0);
tomoya123 0:30e193b92735 231 accel.stop();
tomoya123 0:30e193b92735 232 sprintf( a1, "%02X", ((_zmsb)) & 0xFF);
tomoya123 0:30e193b92735 233 sprintf( a2, "%02X", ((_zlsb)) & 0xFF);
tomoya123 0:30e193b92735 234 az_u16[0]=a1[0];
tomoya123 0:30e193b92735 235 az_u16[1]=a1[1];
tomoya123 0:30e193b92735 236 az_u16[2]=a2[0];
tomoya123 0:30e193b92735 237 az_u16[3]=a2[1];
tomoya123 0:30e193b92735 238 *dsize = 4;
tomoya123 0:30e193b92735 239 }
tomoya123 0:30e193b92735 240
tomoya123 0:30e193b92735 241 void HeptaAccel::x_u16(char* a_u16, int *dsize)
tomoya123 0:30e193b92735 242 {
tomoya123 0:30e193b92735 243 char a1[8]={0x00},a2[8]={0x00};
tomoya123 0:30e193b92735 244 accel.start();
tomoya123 0:30e193b92735 245 accel.write(addr);
tomoya123 0:30e193b92735 246 accel.write(0x01);
tomoya123 0:30e193b92735 247 accel.start();
tomoya123 0:30e193b92735 248 accel.write(addr|0x01);
tomoya123 0:30e193b92735 249 _xmsb = accel.read(0);
tomoya123 0:30e193b92735 250 accel.stop();
tomoya123 0:30e193b92735 251 accel.start();
tomoya123 0:30e193b92735 252 accel.write(addr);
tomoya123 0:30e193b92735 253 accel.write(0x02);
tomoya123 0:30e193b92735 254 accel.start();
tomoya123 0:30e193b92735 255 accel.write(addr|0x01);
tomoya123 0:30e193b92735 256 _xlsb = accel.read(0);
tomoya123 0:30e193b92735 257 accel.stop();
tomoya123 0:30e193b92735 258 sprintf( a1, "%02X", ((_xmsb)) & 0xFF);
tomoya123 0:30e193b92735 259 sprintf( a2, "%02X", ((_xlsb)) & 0xFF);
tomoya123 0:30e193b92735 260 a_u16[0]=a1[0];
tomoya123 0:30e193b92735 261 a_u16[1]=a1[1];
tomoya123 0:30e193b92735 262 a_u16[2]=a2[0];
tomoya123 0:30e193b92735 263 a_u16[3]=a2[1];
tomoya123 0:30e193b92735 264 *dsize = 4;
tomoya123 0:30e193b92735 265 }
tomoya123 0:30e193b92735 266
tomoya123 0:30e193b92735 267 void HeptaAccel::y_u16(char* a_u16, int *dsize)
tomoya123 0:30e193b92735 268 {
tomoya123 0:30e193b92735 269 char a1[8]={0x00},a2[8]={0x00};
tomoya123 0:30e193b92735 270 accel.start();
tomoya123 0:30e193b92735 271 accel.write(addr);
tomoya123 0:30e193b92735 272 accel.write(0x03);
tomoya123 0:30e193b92735 273 accel.start();
tomoya123 0:30e193b92735 274 accel.write(addr|0x01);
tomoya123 0:30e193b92735 275 _ymsb = accel.read(0);
tomoya123 0:30e193b92735 276 accel.stop();
tomoya123 0:30e193b92735 277 accel.start();
tomoya123 0:30e193b92735 278 accel.write(addr);
tomoya123 0:30e193b92735 279 accel.write(0x04);
tomoya123 0:30e193b92735 280 accel.start();
tomoya123 0:30e193b92735 281 accel.write(addr|0x01);
tomoya123 0:30e193b92735 282 _ylsb = accel.read(0);
tomoya123 0:30e193b92735 283 accel.stop();
tomoya123 0:30e193b92735 284 sprintf( a1, "%02X", ((_ymsb)) & 0xFF);
tomoya123 0:30e193b92735 285 sprintf( a2, "%02X", ((_ylsb)) & 0xFF);
tomoya123 0:30e193b92735 286 a_u16[0]=a1[0];
tomoya123 0:30e193b92735 287 a_u16[1]=a1[1];
tomoya123 0:30e193b92735 288 a_u16[2]=a2[0];
tomoya123 0:30e193b92735 289 a_u16[3]=a2[1];
tomoya123 0:30e193b92735 290 *dsize = 4;
tomoya123 0:30e193b92735 291 }
tomoya123 0:30e193b92735 292
tomoya123 0:30e193b92735 293 void HeptaAccel::z_u16(char* a_u16, int *dsize)
tomoya123 0:30e193b92735 294 {
tomoya123 0:30e193b92735 295 char a1[8]={0x00},a2[8]={0x00};
tomoya123 0:30e193b92735 296 accel.start();
tomoya123 0:30e193b92735 297 accel.write(addr);
tomoya123 0:30e193b92735 298 accel.write(0x05);
tomoya123 0:30e193b92735 299 accel.start();
tomoya123 0:30e193b92735 300 accel.write(addr|0x01);
tomoya123 0:30e193b92735 301 _zmsb = accel.read(0);
tomoya123 0:30e193b92735 302 accel.stop();
tomoya123 0:30e193b92735 303 accel.start();
tomoya123 0:30e193b92735 304 accel.write(addr);
tomoya123 0:30e193b92735 305 accel.write(0x06);
tomoya123 0:30e193b92735 306 accel.start();
tomoya123 0:30e193b92735 307 accel.write(addr|0x01);
tomoya123 0:30e193b92735 308 _zlsb = accel.read(0);
tomoya123 0:30e193b92735 309 accel.stop();
tomoya123 0:30e193b92735 310 sprintf( a1, "%02X", ((_zmsb)) & 0xFF);
tomoya123 0:30e193b92735 311 sprintf( a2, "%02X", ((_zlsb)) & 0xFF);
tomoya123 0:30e193b92735 312 a_u16[0]=a1[0];
tomoya123 0:30e193b92735 313 a_u16[1]=a1[1];
tomoya123 0:30e193b92735 314 a_u16[2]=a2[0];
tomoya123 0:30e193b92735 315 a_u16[3]=a2[1];
tomoya123 0:30e193b92735 316 *dsize = 4;
tomoya123 0:30e193b92735 317 }