![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Mini Project 10: Displaying stuff from day 7
Dependencies: DmTouch_UniGraphic UniGraphic mbed
acceler_sensor.cpp@13:b3637adeaa2a, 2017-01-17 (annotated)
- Committer:
- ezimmerman17
- Date:
- Tue Jan 17 23:34:00 2017 +0000
- Revision:
- 13:b3637adeaa2a
- Parent:
- 9:0f4c86e7218a
- Child:
- 14:682bdfcff46a
accelerometer is working kinda ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
swescott17 | 0:1ebe73e062a7 | 1 | #include "acceler_sensor.h" |
swescott17 | 0:1ebe73e062a7 | 2 | DigitalOut led3(LED3); |
ezimmerman17 | 13:b3637adeaa2a | 3 | const int address = 0xA6; |
swescott17 | 0:1ebe73e062a7 | 4 | |
ezimmerman17 | 6:b196b0a8f337 | 5 | void configure_acceleration(void){ |
swescott17 | 0:1ebe73e062a7 | 6 | |
ezimmerman17 | 13:b3637adeaa2a | 7 | // set 'address' to |
ezimmerman17 | 13:b3637adeaa2a | 8 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 9 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 10 | i2c_port.write(0x1D); //to set the Tap Threshold |
swescott17 | 0:1ebe73e062a7 | 11 | i2c_port.write(0x50 << 2); |
swescott17 | 0:1ebe73e062a7 | 12 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 13 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 14 | |
swescott17 | 0:1ebe73e062a7 | 15 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 16 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 17 | i2c_port.write(0x21);//set Tap Duration |
swescott17 | 0:1ebe73e062a7 | 18 | i2c_port.write(0x0A); |
swescott17 | 0:1ebe73e062a7 | 19 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 20 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 21 | |
swescott17 | 0:1ebe73e062a7 | 22 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 23 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 24 | int temp[2] = {0x22, 0x05}; |
swescott17 | 0:1ebe73e062a7 | 25 | i2c_port.write(temp[0]);//set Tap Gap |
swescott17 | 0:1ebe73e062a7 | 26 | i2c_port.write(temp[1]); |
swescott17 | 0:1ebe73e062a7 | 27 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 28 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 29 | |
swescott17 | 0:1ebe73e062a7 | 30 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 31 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 32 | i2c_port.write(0x23);//set Tap Window |
swescott17 | 0:1ebe73e062a7 | 33 | i2c_port.write(0xFF); |
swescott17 | 0:1ebe73e062a7 | 34 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 35 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 36 | |
swescott17 | 0:1ebe73e062a7 | 37 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 38 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 39 | i2c_port.write(0x2A);//set Tap Axis |
swescott17 | 0:1ebe73e062a7 | 40 | i2c_port.write(0x07); |
swescott17 | 0:1ebe73e062a7 | 41 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 42 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 43 | |
swescott17 | 0:1ebe73e062a7 | 44 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 45 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 46 | i2c_port.write(0x2E);//set interupt Enable |
swescott17 | 0:1ebe73e062a7 | 47 | i2c_port.write(0x20|0x40); |
swescott17 | 0:1ebe73e062a7 | 48 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 49 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 50 | |
swescott17 | 0:1ebe73e062a7 | 51 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 52 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 53 | i2c_port.write(0x2F);//set interupt map |
swescott17 | 0:1ebe73e062a7 | 54 | i2c_port.write(0x20|0x40); |
swescott17 | 0:1ebe73e062a7 | 55 | i2c_port.stop(); |
ezimmerman17 | 13:b3637adeaa2a | 56 | wait(0.01); //all the above should confige tap functions for accel sensor, so it's unneccesary |
swescott17 | 0:1ebe73e062a7 | 57 | |
swescott17 | 0:1ebe73e062a7 | 58 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 59 | i2c_port.write(address); |
ezimmerman17 | 8:bbe3e332fe94 | 60 | i2c_port.frequency(2000000); |
swescott17 | 0:1ebe73e062a7 | 61 | i2c_port.write(0x2D);//power control register |
swescott17 | 0:1ebe73e062a7 | 62 | i2c_port.write(0x08);//measure mode |
swescott17 | 0:1ebe73e062a7 | 63 | i2c_port.stop(); |
ezimmerman17 | 8:bbe3e332fe94 | 64 | } |
ezimmerman17 | 8:bbe3e332fe94 | 65 | |
ezimmerman17 | 13:b3637adeaa2a | 66 | void getacceleration (float* data){ |
ezimmerman17 | 8:bbe3e332fe94 | 67 | char buffer[6]; |
ezimmerman17 | 13:b3637adeaa2a | 68 | char idontknow[1]; |
ezimmerman17 | 13:b3637adeaa2a | 69 | idontknow[0] = 0x32; |
ezimmerman17 | 8:bbe3e332fe94 | 70 | int16_t rawdata[3]; |
ezimmerman17 | 13:b3637adeaa2a | 71 | i2c_port.write(address,idontknow,1); |
ezimmerman17 | 13:b3637adeaa2a | 72 | i2c_port.read(address,buffer,6); |
ezimmerman17 | 13:b3637adeaa2a | 73 | /*i2c_port.write(address); |
ezimmerman17 | 8:bbe3e332fe94 | 74 | i2c_port.write(0x80|0x40|0x32); |
ezimmerman17 | 8:bbe3e332fe94 | 75 | for(int i=0; i<= 5; i++) |
ezimmerman17 | 8:bbe3e332fe94 | 76 | { |
ezimmerman17 | 13:b3637adeaa2a | 77 | buffer[i]=i2c_port.write(0x00); |
ezimmerman17 | 8:bbe3e332fe94 | 78 | //pc.printf("x = %+1.2fg\t", buffer[i]); |
ezimmerman17 | 13:b3637adeaa2a | 79 | }*/ |
ezimmerman17 | 8:bbe3e332fe94 | 80 | rawdata[0] = buffer[1]<<8 | buffer[0]; |
ezimmerman17 | 8:bbe3e332fe94 | 81 | rawdata[1] = buffer[3]<<8 | buffer[2]; |
ezimmerman17 | 8:bbe3e332fe94 | 82 | rawdata[2] = buffer[5]<<8 | buffer[4]; |
ezimmerman17 | 13:b3637adeaa2a | 83 | data[0] = rawdata[0]*0.004; |
ezimmerman17 | 13:b3637adeaa2a | 84 | data[1] = rawdata[1]*0.004; |
ezimmerman17 | 13:b3637adeaa2a | 85 | //float x = 0.004*data[0]; |
ezimmerman17 | 13:b3637adeaa2a | 86 | //float y = 0.004*data[1]; |
ezimmerman17 | 13:b3637adeaa2a | 87 | |
ezimmerman17 | 13:b3637adeaa2a | 88 | pc.printf("x = %+1.2fg\t y = %+1.2fg\n\r", data[0],data[1]); //Just grabbed from SPI code. Not formatted right |
ezimmerman17 | 13:b3637adeaa2a | 89 | |
ezimmerman17 | 13:b3637adeaa2a | 90 | |
ezimmerman17 | 8:bbe3e332fe94 | 91 | } |