Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:6e12cd8b71b4, committed 2018-11-22
- Comitter:
- aldomarez
- Date:
- Thu Nov 22 20:11:48 2018 +0000
- Commit message:
- SPI
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Nov 22 20:11:48 2018 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h"
+
+SPI acc(D11,D12,D13); // setup SPI interface on pins D11--MOSI>>SDA,D12--MISO>>SDO,D13--SCL
+DigitalOut cs(D10); // use pin 14 as chip select
+
+Serial pc(USBTX, USBRX); // setup USB interface to host terminal
+
+char buffer[6]; // raw data array type char
+signed short data[3]; // acc data is signed 16 bit from -32,768 to +32,767
+float x, y, z; // floating point data
+int main() {
+ cs=1;
+ acc.format(8,3); // 8 bit data, Mode 3
+ acc.frequency(2000000); // 2MHz clock rate
+ cs=0;
+ acc.write(0x31); // data format register
+ acc.write(0x0B); // format +/-16g, 0.004g/LSB
+ cs=1;
+ cs=0;
+ acc.write(0x2D); // power ctrl register
+ acc.write(0x08); // measure mode
+ cs=1;
+ while (1) {
+ wait(0.2);
+ cs=0;
+ acc.write(0x80|0x40|0x32); // RW bit high, MB bit high, plus address
+ for (int i = 0;i<=5;i++) {
+ buffer[i]=acc.write(0x00); // read back 6 data bytes
+ }
+ cs=1;
+ data[0] = buffer[1]<<8 | buffer[0]; // combine MSB and LSB
+ data[1] = buffer[3]<<8 | buffer[2];
+ data[2] = buffer[5]<<8 | buffer[4];
+ x=0.004*data[0]; y=0.004*data[1]; z=0.004*data[2]; // convert to floating point
+ pc.printf("x = %+1.2fg\t y = %+1.2fg\t z = %+1.2fg\n\r", x, y,z); //print to screen
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Nov 22 20:11:48 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/a7c7b631e539 \ No newline at end of file