Xbee uplink

Dependencies:   mbed PowerControl SDFileSystem

Fork of HeptaXbee_Uplink by 智也 大野

Committer:
tomoya123
Date:
Tue Dec 13 07:58:31 2016 +0000
Revision:
1:c0aa8983398d
Parent:
0:9a52d95a79f7
Xbee Uplink

Who changed what in which revision?

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