by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:371be129802f, 2012-10-15 (annotated)
- Committer:
- robt
- Date:
- Mon Oct 15 21:25:27 2012 +0000
- Revision:
- 0:371be129802f
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:371be129802f | 1 | /*Program Example 7.4: Reads values from accelerometer through SPI, and outputs |
robt | 0:371be129802f | 2 | continuously to terminal screen. |
robt | 0:371be129802f | 3 | */ |
robt | 0:371be129802f | 4 | |
robt | 0:371be129802f | 5 | #include "mbed.h" |
robt | 0:371be129802f | 6 | SPI acc(p11,p12,p13); // set up SPI interface on pins 11,12,13 |
robt | 0:371be129802f | 7 | DigitalOut cs(p14); // use pin 14 as chip select |
robt | 0:371be129802f | 8 | Serial pc(USBTX, USBRX); // set up USB interface to host terminal |
robt | 0:371be129802f | 9 | char buffer[6]; //raw data array type char |
robt | 0:371be129802f | 10 | int16_t data[3]; // 16-bit twos-complement integer data |
robt | 0:371be129802f | 11 | float x, y, z; // floating point data, to be displayed on-screen |
robt | 0:371be129802f | 12 | |
robt | 0:371be129802f | 13 | int main() { |
robt | 0:371be129802f | 14 | cs=1; //initially ADXL345 is not activated |
robt | 0:371be129802f | 15 | acc.format(8,3); // 8 bit data, Mode 3 |
robt | 0:371be129802f | 16 | acc.frequency(2000000); // 2MHz clock rate |
robt | 0:371be129802f | 17 | cs=0; //select the device |
robt | 0:371be129802f | 18 | acc.write(0x31); // data format register |
robt | 0:371be129802f | 19 | acc.write(0x0B); // format +/-16g, 0.004g/LSB |
robt | 0:371be129802f | 20 | cs=1; //end of transmission |
robt | 0:371be129802f | 21 | cs=0; //start a new transmission |
robt | 0:371be129802f | 22 | acc.write(0x2D); // power ctrl register |
robt | 0:371be129802f | 23 | acc.write(0x08); // measure mode |
robt | 0:371be129802f | 24 | cs=1; //end of transmission |
robt | 0:371be129802f | 25 | while (1) { // infinite loop |
robt | 0:371be129802f | 26 | wait(0.2); |
robt | 0:371be129802f | 27 | cs=0; //start a transmission |
robt | 0:371be129802f | 28 | acc.write(0x80|0x40|0x32); // RW bit high, MB bit high, plus address |
robt | 0:371be129802f | 29 | for (int i = 0;i<=5;i++) { |
robt | 0:371be129802f | 30 | buffer[i]=acc.write(0x00); // read back 6 data bytes |
robt | 0:371be129802f | 31 | } |
robt | 0:371be129802f | 32 | cs=1; //end of transmission |
robt | 0:371be129802f | 33 | data[0] = buffer[1]<<8 | buffer[0]; // combine MSB and LSB |
robt | 0:371be129802f | 34 | data[1] = buffer[3]<<8 | buffer[2]; |
robt | 0:371be129802f | 35 | data[2] = buffer[5]<<8 | buffer[4]; |
robt | 0:371be129802f | 36 | x=0.004*data[0]; y=0.004*data[1]; z=0.004*data[2]; // convert to float, |
robt | 0:371be129802f | 37 | //actual g value |
robt | 0:371be129802f | 38 | pc.printf("x = %+1.2fg\t y = %+1.2fg\t z = %+1.2fg\n\r", x, y,z); //print |
robt | 0:371be129802f | 39 | } |
robt | 0:371be129802f | 40 | } |
robt | 0:371be129802f | 41 |