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