MB7040 Range finder experiment.
Dependencies: mbed
Fork of SonarRangeFinderV1 by
main.cpp@0:28fb80eb93ca, 2014-12-26 (annotated)
- Committer:
- Belesiu
- Date:
- Fri Dec 26 21:20:04 2014 +0000
- Revision:
- 0:28fb80eb93ca
- Child:
- 1:0cf1f2254f10
First test program for MB7040 ultasonic range finder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Belesiu | 0:28fb80eb93ca | 1 | // Ultra Sonice Range Finder |
Belesiu | 0:28fb80eb93ca | 2 | // Experiement #1 |
Belesiu | 0:28fb80eb93ca | 3 | // Belesiu |
Belesiu | 0:28fb80eb93ca | 4 | // 2014.12.26 |
Belesiu | 0:28fb80eb93ca | 5 | |
Belesiu | 0:28fb80eb93ca | 6 | #include "mbed.h" |
Belesiu | 0:28fb80eb93ca | 7 | |
Belesiu | 0:28fb80eb93ca | 8 | DigitalOut myled(LED1); |
Belesiu | 0:28fb80eb93ca | 9 | |
Belesiu | 0:28fb80eb93ca | 10 | I2C rangefinder(p9, p10); // sda, sc1 |
Belesiu | 0:28fb80eb93ca | 11 | Serial pc(USBTX, USBRX); // tx, rx |
Belesiu | 0:28fb80eb93ca | 12 | |
Belesiu | 0:28fb80eb93ca | 13 | const int addr = 0xE0; |
Belesiu | 0:28fb80eb93ca | 14 | char config_r[2]; |
Belesiu | 0:28fb80eb93ca | 15 | char range_read[2]; |
Belesiu | 0:28fb80eb93ca | 16 | float range; |
Belesiu | 0:28fb80eb93ca | 17 | |
Belesiu | 0:28fb80eb93ca | 18 | int main() |
Belesiu | 0:28fb80eb93ca | 19 | { |
Belesiu | 0:28fb80eb93ca | 20 | int x = 0; |
Belesiu | 0:28fb80eb93ca | 21 | |
Belesiu | 0:28fb80eb93ca | 22 | while(1) { |
Belesiu | 0:28fb80eb93ca | 23 | |
Belesiu | 0:28fb80eb93ca | 24 | // Test serial port |
Belesiu | 0:28fb80eb93ca | 25 | myled = 1; |
Belesiu | 0:28fb80eb93ca | 26 | wait(0.2); |
Belesiu | 0:28fb80eb93ca | 27 | myled = 0; |
Belesiu | 0:28fb80eb93ca | 28 | wait(0.2); |
Belesiu | 0:28fb80eb93ca | 29 | pc.printf("Hello Jim! - %d\n", x++); |
Belesiu | 0:28fb80eb93ca | 30 | |
Belesiu | 0:28fb80eb93ca | 31 | // For MB7040 |
Belesiu | 0:28fb80eb93ca | 32 | |
Belesiu | 0:28fb80eb93ca | 33 | config_r[0] = 0x00; // set pointer reg to ‘cmd register' |
Belesiu | 0:28fb80eb93ca | 34 | config_r[1] = 0x51; // config data byte1 |
Belesiu | 0:28fb80eb93ca | 35 | rangefinder.write(addr, config_r, 2); |
Belesiu | 0:28fb80eb93ca | 36 | wait(0.07); |
Belesiu | 0:28fb80eb93ca | 37 | |
Belesiu | 0:28fb80eb93ca | 38 | config_r[0] = 0x02; //set pointer reg to 'data register' |
Belesiu | 0:28fb80eb93ca | 39 | rangefinder.write(addr, config_r, 1); //send to pointer 'read range' |
Belesiu | 0:28fb80eb93ca | 40 | rangefinder.read(addr, range_read, 2); //read the two-byte range data |
Belesiu | 0:28fb80eb93ca | 41 | range = ((range_read[0] << 8) + range_read[1]); |
Belesiu | 0:28fb80eb93ca | 42 | |
Belesiu | 0:28fb80eb93ca | 43 | pc.printf("Range = %.2f cm\n\r", range); //print range on screen |
Belesiu | 0:28fb80eb93ca | 44 | wait(0.05); |
Belesiu | 0:28fb80eb93ca | 45 | |
Belesiu | 0:28fb80eb93ca | 46 | } |
Belesiu | 0:28fb80eb93ca | 47 | } |