SRF08 Libraray

Dependents:   mbedDemoDisplay mbedDemoDisplay Roaming SRF08HelloWorld ... more

Committer:
chris
Date:
Tue Oct 31 17:59:10 2017 +0000
Revision:
2:aeec981d1d2a
Parent:
0:752fc2753b90
Remove mbed.bld

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:752fc2753b90 1
chris 0:752fc2753b90 2 /*
chris 0:752fc2753b90 3 Copyright (c) 2010 Chris Styles ( chris dot styles at mbed dot org )
chris 0:752fc2753b90 4
chris 0:752fc2753b90 5 Permission is hereby granted, free of charge, to any person obtaining a copy
chris 0:752fc2753b90 6 of this software and associated documentation files (the "Software"), to deal
chris 0:752fc2753b90 7 in the Software without restriction, including without limitation the rights
chris 0:752fc2753b90 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
chris 0:752fc2753b90 9 copies of the Software, and to permit persons to whom the Software is
chris 0:752fc2753b90 10 furnished to do so, subject to the following conditions:
chris 0:752fc2753b90 11
chris 0:752fc2753b90 12 The above copyright notice and this permission notice shall be included in
chris 0:752fc2753b90 13 all copies or substantial portions of the Software.
chris 0:752fc2753b90 14
chris 0:752fc2753b90 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
chris 0:752fc2753b90 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
chris 0:752fc2753b90 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
chris 0:752fc2753b90 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
chris 0:752fc2753b90 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
chris 0:752fc2753b90 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
chris 0:752fc2753b90 21 THE SOFTWARE.
chris 0:752fc2753b90 22 */
chris 0:752fc2753b90 23
chris 0:752fc2753b90 24 #include "SRF08.h"
chris 0:752fc2753b90 25
chris 0:752fc2753b90 26
chris 0:752fc2753b90 27 SRF08::SRF08(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
chris 0:752fc2753b90 28 char cmd[2];
chris 0:752fc2753b90 29
chris 0:752fc2753b90 30 // Set up SRF08 max range and receiver sensitivity over I2C bus
chris 0:752fc2753b90 31 cmd[0] = 0x02; // Range register
chris 0:752fc2753b90 32 cmd[1] = 0x1C; // Set max range about 100cm
chris 0:752fc2753b90 33 m_i2c.write(m_addr, cmd, 2);
chris 0:752fc2753b90 34 cmd[0] = 0x01; // Receiver gain register
chris 0:752fc2753b90 35 cmd[1] = 0x1B; // Set max receiver gain
chris 0:752fc2753b90 36 m_i2c.write(m_addr, cmd, 2);
chris 0:752fc2753b90 37
chris 0:752fc2753b90 38 }
chris 0:752fc2753b90 39
chris 0:752fc2753b90 40 SRF08::~SRF08() {
chris 0:752fc2753b90 41
chris 0:752fc2753b90 42 }
chris 0:752fc2753b90 43
chris 0:752fc2753b90 44 float SRF08::read() {
chris 0:752fc2753b90 45
chris 0:752fc2753b90 46 char cmd[2];
chris 0:752fc2753b90 47 char echo[2];
chris 0:752fc2753b90 48
chris 0:752fc2753b90 49
chris 0:752fc2753b90 50 // Get range data from SRF08
chris 0:752fc2753b90 51 // Send Tx burst command over I2C bus
chris 0:752fc2753b90 52 cmd[0] = 0x00; // Command register
chris 0:752fc2753b90 53 cmd[1] = 0x51; // Ranging results in cm
chris 0:752fc2753b90 54 m_i2c.write(m_addr, cmd, 2); // Send ranging burst
chris 0:752fc2753b90 55
chris 0:752fc2753b90 56 wait(0.07); // Wait for return echo
chris 0:752fc2753b90 57
chris 0:752fc2753b90 58 // Read back range over I2C bus
chris 0:752fc2753b90 59 cmd[0] = 0x02; // Address of first echo
chris 0:752fc2753b90 60 m_i2c.write(m_addr, cmd, 1, 1); // Send address of first echo
chris 0:752fc2753b90 61 m_i2c.read(m_addr, echo, 2); // Read two-byte echo result
chris 0:752fc2753b90 62
chris 0:752fc2753b90 63 // Generate PWM mark/space ratio from range data
chris 0:752fc2753b90 64 float range = (echo[0]<<8)+echo[1];
chris 0:752fc2753b90 65
chris 0:752fc2753b90 66 return range;
chris 0:752fc2753b90 67 }