![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
First
acc.cpp
- Committer:
- asloop18
- Date:
- 2016-01-14
- Revision:
- 3:824764571657
- Child:
- 5:98ad3701ec24
File content as of revision 3:824764571657:
/* Program 7.4: Read ADXL345 accelerometer through SPI. Outputs continuously to terminal */ #include "mbed.h" SPI acc(p11,p12,p13); //Set up SPI MOSI, MISO, SCL DigitalOut cs(p14); //set up chip select Serial pc(USBTX, USBRX); char buffer[6]; //raw filler data int16_t data[3]; //16bit twos-compliment integer data float x,y,z; //set up floats for values to display int main(){ cs=1; //initially off acc.format(8,3); //8bit, mode 3 acc.frequency(2000000); //2MHz cs=0; //select acc.write(0x31); //data format register acc.write(0x0B); //+/-16g, 0.004g sensitivity cs=1; //end transmission cs=0; //start new transmission acc.write(0x2D); //power mode = measure acc.write(0x08); cs=1; while(1){ wait(0.2); cs=0; acc.write(0x80|0x40|0x32); //RW bit high, MB bit high, address for (int i=0;i<=5;i++){buffer[i]=acc.write(0x00);} //read 6bytes cs=1; data[0]=buffer[1]<<8|buffer[0]; data[1]=buffer[3]<<8|buffer[2]; //combine MSB and LSB data[2]=buffer[5]<<8|buffer[4]; x=0.004*data[0]; y=0.004*data[1]; z=0.004*data[2]; //convert to float, actual g value pc.printf("x = %+1.2fg\t y = %+1.2fg\t z = %+1.2fg\n\r", x,y,z); } }