![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
First
acc.cpp@5:98ad3701ec24, 2016-01-14 (annotated)
- Committer:
- asloop18
- Date:
- Thu Jan 14 22:03:54 2016 +0000
- Revision:
- 5:98ad3701ec24
- Parent:
- 3:824764571657
- Child:
- 6:459d1eb2cc23
accelerometer functions defined, untested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
asloop18 | 3:824764571657 | 1 | /* Program 7.4: Read ADXL345 accelerometer through SPI. |
asloop18 | 3:824764571657 | 2 | Outputs continuously to terminal |
asloop18 | 3:824764571657 | 3 | */ |
asloop18 | 3:824764571657 | 4 | |
asloop18 | 3:824764571657 | 5 | #include "mbed.h" |
asloop18 | 3:824764571657 | 6 | |
asloop18 | 3:824764571657 | 7 | SPI acc(p11,p12,p13); //Set up SPI MOSI, MISO, SCL |
asloop18 | 5:98ad3701ec24 | 8 | DigitalOut cs(p10); //set up chip select |
asloop18 | 3:824764571657 | 9 | Serial pc(USBTX, USBRX); |
asloop18 | 3:824764571657 | 10 | char buffer[6]; //raw filler data |
asloop18 | 3:824764571657 | 11 | int16_t data[3]; //16bit twos-compliment integer data |
asloop18 | 3:824764571657 | 12 | float x,y,z; //set up floats for values to display |
asloop18 | 3:824764571657 | 13 | |
asloop18 | 5:98ad3701ec24 | 14 | void acc_init(void){ |
asloop18 | 3:824764571657 | 15 | cs=1; //initially off |
asloop18 | 3:824764571657 | 16 | acc.format(8,3); //8bit, mode 3 |
asloop18 | 3:824764571657 | 17 | acc.frequency(2000000); //2MHz |
asloop18 | 3:824764571657 | 18 | cs=0; //select |
asloop18 | 3:824764571657 | 19 | acc.write(0x31); //data format register |
asloop18 | 3:824764571657 | 20 | acc.write(0x0B); //+/-16g, 0.004g sensitivity |
asloop18 | 3:824764571657 | 21 | cs=1; //end transmission |
asloop18 | 3:824764571657 | 22 | cs=0; //start new transmission |
asloop18 | 3:824764571657 | 23 | acc.write(0x2D); //power mode = measure |
asloop18 | 3:824764571657 | 24 | acc.write(0x08); |
asloop18 | 3:824764571657 | 25 | cs=1; |
asloop18 | 5:98ad3701ec24 | 26 | } |
asloop18 | 5:98ad3701ec24 | 27 | |
asloop18 | 5:98ad3701ec24 | 28 | float acc_sense(char axis){ |
asloop18 | 5:98ad3701ec24 | 29 | cs=0; |
asloop18 | 5:98ad3701ec24 | 30 | acc.write(0x80|0x40|0x32); //RW bit high, MB bit high, address to ask for a reading |
asloop18 | 5:98ad3701ec24 | 31 | for (int i=0;i<=5;i++){buffer[i]=acc.write(0x00);} //read 6bytes |
asloop18 | 5:98ad3701ec24 | 32 | cs=1; |
asloop18 | 5:98ad3701ec24 | 33 | data[0]=buffer[1]<<8|buffer[0]; //x |
asloop18 | 5:98ad3701ec24 | 34 | data[1]=buffer[3]<<8|buffer[2]; //y combine MSB and LSB |
asloop18 | 5:98ad3701ec24 | 35 | data[2]=buffer[5]<<8|buffer[4]; //z |
asloop18 | 3:824764571657 | 36 | |
asloop18 | 5:98ad3701ec24 | 37 | x=atan(data[0],data[2]); |
asloop18 | 5:98ad3701ec24 | 38 | y=atan(data[1],data[2]); |
asloop18 | 5:98ad3701ec24 | 39 | |
asloop18 | 5:98ad3701ec24 | 40 | if(axis==1) return x; //if asked for x, give x |
asloop18 | 5:98ad3701ec24 | 41 | if(axis==2) return y; //if asked for y, give y |
asloop18 | 3:824764571657 | 42 | } |