Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
SRF08.cpp
00001 00002 /* 00003 Copyright (c) 2010 Chris Styles ( chris dot styles at mbed dot org ) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 #include "SRF08.h" 00025 00026 00027 SRF08::SRF08(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { 00028 char cmd[2]; 00029 00030 // Set up SRF08 max range and receiver sensitivity over I2C bus 00031 cmd[0] = 0x02; // Range register 00032 cmd[1] = 0x1C; // Set max range about 100cm 00033 m_i2c.write(m_addr, cmd, 2); 00034 cmd[0] = 0x01; // Receiver gain register 00035 cmd[1] = 0x1B; // Set max receiver gain 00036 m_i2c.write(m_addr, cmd, 2); 00037 00038 } 00039 00040 SRF08::~SRF08 () { 00041 00042 } 00043 00044 float SRF08::read() { 00045 00046 char cmd[2]; 00047 char echo[2]; 00048 00049 00050 // Get range data from SRF08 00051 // Send Tx burst command over I2C bus 00052 cmd[0] = 0x00; // Command register 00053 cmd[1] = 0x51; // Ranging results in cm 00054 m_i2c.write(m_addr, cmd, 2); // Send ranging burst 00055 00056 wait(0.07); // Wait for return echo 00057 00058 // Read back range over I2C bus 00059 cmd[0] = 0x02; // Address of first echo 00060 m_i2c.write(m_addr, cmd, 1, 1); // Send address of first echo 00061 m_i2c.read(m_addr, echo, 2); // Read two-byte echo result 00062 00063 // Generate PWM mark/space ratio from range data 00064 float range = (echo[0]<<8)+echo[1]; 00065 00066 return range; 00067 }
Generated on Sun Jul 17 2022 08:25:31 by 1.7.2