![](/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@8:bbe3e332fe94, 2017-01-17 (annotated)
- Committer:
- ezimmerman17
- Date:
- Tue Jan 17 19:30:09 2017 +0000
- Revision:
- 8:bbe3e332fe94
- Parent:
- 7:ee7f2c33d45d
- Child:
- 9:0f4c86e7218a
more accel sensor messing about ;
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); |
swescott17 | 0:1ebe73e062a7 | 3 | |
ezimmerman17 | 6:b196b0a8f337 | 4 | void configure_acceleration(void){ |
swescott17 | 0:1ebe73e062a7 | 5 | |
swescott17 | 0:1ebe73e062a7 | 6 | const int address = 0x53 << 1; // set 'address' to |
ezimmerman17 | 8:bbe3e332fe94 | 7 | /*i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 8 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 9 | i2c_port.write(0x1D); //to set the Tap Threshold |
swescott17 | 0:1ebe73e062a7 | 10 | i2c_port.write(0x50 << 2); |
swescott17 | 0:1ebe73e062a7 | 11 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 12 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 13 | |
swescott17 | 0:1ebe73e062a7 | 14 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 15 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 16 | i2c_port.write(0x21);//set Tap Duration |
swescott17 | 0:1ebe73e062a7 | 17 | i2c_port.write(0x0A); |
swescott17 | 0:1ebe73e062a7 | 18 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 19 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 20 | |
swescott17 | 0:1ebe73e062a7 | 21 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 22 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 23 | int temp[2] = {0x22, 0x05}; |
swescott17 | 0:1ebe73e062a7 | 24 | i2c_port.write(temp[0]);//set Tap Gap |
swescott17 | 0:1ebe73e062a7 | 25 | i2c_port.write(temp[1]); |
swescott17 | 0:1ebe73e062a7 | 26 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 27 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 28 | |
swescott17 | 0:1ebe73e062a7 | 29 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 30 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 31 | i2c_port.write(0x23);//set Tap Window |
swescott17 | 0:1ebe73e062a7 | 32 | i2c_port.write(0xFF); |
swescott17 | 0:1ebe73e062a7 | 33 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 34 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 35 | |
swescott17 | 0:1ebe73e062a7 | 36 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 37 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 38 | i2c_port.write(0x2A);//set Tap Axis |
swescott17 | 0:1ebe73e062a7 | 39 | i2c_port.write(0x07); |
swescott17 | 0:1ebe73e062a7 | 40 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 41 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 42 | |
swescott17 | 0:1ebe73e062a7 | 43 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 44 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 45 | i2c_port.write(0x2E);//set interupt Enable |
swescott17 | 0:1ebe73e062a7 | 46 | i2c_port.write(0x20|0x40); |
swescott17 | 0:1ebe73e062a7 | 47 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 48 | wait(0.01); |
swescott17 | 0:1ebe73e062a7 | 49 | |
swescott17 | 0:1ebe73e062a7 | 50 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 51 | i2c_port.write(address); |
swescott17 | 0:1ebe73e062a7 | 52 | i2c_port.write(0x2F);//set interupt map |
swescott17 | 0:1ebe73e062a7 | 53 | i2c_port.write(0x20|0x40); |
swescott17 | 0:1ebe73e062a7 | 54 | i2c_port.stop(); |
ezimmerman17 | 8:bbe3e332fe94 | 55 | wait(0.01); all the above should confige tap functions for accel sensor, so it's unneccesary*/ |
swescott17 | 0:1ebe73e062a7 | 56 | |
swescott17 | 0:1ebe73e062a7 | 57 | i2c_port.start(); |
swescott17 | 0:1ebe73e062a7 | 58 | i2c_port.write(address); |
ezimmerman17 | 8:bbe3e332fe94 | 59 | i2c_port.frequency(2000000); |
swescott17 | 0:1ebe73e062a7 | 60 | i2c_port.write(0x2D);//power control register |
swescott17 | 0:1ebe73e062a7 | 61 | i2c_port.write(0x08);//measure mode |
swescott17 | 0:1ebe73e062a7 | 62 | i2c_port.stop(); |
swescott17 | 0:1ebe73e062a7 | 63 | wait(0.01); |
ezimmerman17 | 8:bbe3e332fe94 | 64 | } |
ezimmerman17 | 8:bbe3e332fe94 | 65 | |
ezimmerman17 | 8:bbe3e332fe94 | 66 | void getacceleration (void){ |
ezimmerman17 | 8:bbe3e332fe94 | 67 | const int address = 0x53 << 1; |
ezimmerman17 | 8:bbe3e332fe94 | 68 | char buffer[6]; |
ezimmerman17 | 8:bbe3e332fe94 | 69 | int16_t rawdata[3]; |
ezimmerman17 | 8:bbe3e332fe94 | 70 | i2c_port.write(address); |
ezimmerman17 | 8:bbe3e332fe94 | 71 | i2c_port.write(0x80|0x40|0x32); |
ezimmerman17 | 8:bbe3e332fe94 | 72 | for(int i=0; i<= 5; i++) |
ezimmerman17 | 8:bbe3e332fe94 | 73 | { |
ezimmerman17 | 8:bbe3e332fe94 | 74 | buffer[i]=12c.write(0x00); |
ezimmerman17 | 8:bbe3e332fe94 | 75 | //pc.printf("x = %+1.2fg\t", buffer[i]); |
ezimmerman17 | 8:bbe3e332fe94 | 76 | } |
ezimmerman17 | 8:bbe3e332fe94 | 77 | cs=1; |
ezimmerman17 | 8:bbe3e332fe94 | 78 | rawdata[0] = buffer[1]<<8 | buffer[0]; |
ezimmerman17 | 8:bbe3e332fe94 | 79 | rawdata[1] = buffer[3]<<8 | buffer[2]; |
ezimmerman17 | 8:bbe3e332fe94 | 80 | rawdata[2] = buffer[5]<<8 | buffer[4]; |
ezimmerman17 | 8:bbe3e332fe94 | 81 | data[0] = rawdata[0]; |
ezimmerman17 | 8:bbe3e332fe94 | 82 | data[1] = rawdata[1]; |
ezimmerman17 | 8:bbe3e332fe94 | 83 | float x = data[0]; |
ezimmerman17 | 8:bbe3e332fe94 | 84 | float y = data[1]; |
ezimmerman17 | 8:bbe3e332fe94 | 85 | pc.printf("x = %+1.2fg\t y = %+1.2fg\n\r", rawdata[0],rawdata[1]); //Just grabbed from SPI code. Not formatted right |
ezimmerman17 | 8:bbe3e332fe94 | 86 | i2c_port.stop(); |
ezimmerman17 | 8:bbe3e332fe94 | 87 | |
ezimmerman17 | 8:bbe3e332fe94 | 88 | } |