超音波センサSRF02のテストプログラム

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Skykon
Date:
Sun Feb 10 19:33:50 2019 +0000
Commit message:
a

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	Sun Feb 10 19:33:50 2019 +0000
@@ -0,0 +1,62 @@
+#include "mbed.h"
+
+// registers
+#define CMD_REG         0x00
+//0x01はunused(reads0x80)
+#define RANGE_H_REG     0x02
+#define RANGE_L_REG     0x03
+//0x04はAutotune Minimum - Hight Byte
+//0x05はAutotune Minimum - Low Byte
+
+// commands
+#define INCH_CMD    0x50//real ranging mode - result in inches
+#define CM_CMD      0x51//real ranging mode - result in centimeters
+#define US_CMD      0x52//real ranging mode - result in micro-seconds
+//0x56 fake ranging mode - result in inches
+//0x57 fake ranging mode - result in centimeters
+//0x58 fake ranging mode - result in micro-seconds
+//0x5C transmit an 8 cycle 40khz burst - no ranging takes place
+//0xA0 1st in sequence to change I2C address
+//0xA5 3rd in sequence to change I2C address
+//0xAA 2nd in sequence to change I2C address
+
+I2C usensor(D14, D15);
+RawSerial pc(SERIAL_TX, SERIAL_RX, 115200);
+//SRF02 usensor(D4, D6, 0xE0);
+
+const int addr = 0xE0;
+
+int main()
+{
+    char cmd[2];
+    while(1){
+        cmd[0] = CMD_REG;
+        cmd[1] = CM_CMD;
+        usensor.write(addr, cmd, 2);
+        
+        wait(0.07);
+        
+        cmd[0] = RANGE_H_REG;
+        usensor.write(addr, cmd, 1);
+        usensor.read(addr | 1, cmd, 2); // read the two-byte echo result
+ 
+        // print the ranging data to the screen
+        float echo = 0.01 * ((cmd[0] << 8) + cmd[1]);
+        pc.printf("Range = %.2f\r\n", echo);
+        wait(0.1);
+        
+    }
+    
+}
+
+
+
+
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Feb 10 19:33:50 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file