Simple library for reading distance from SRF02 Ultrasonic Sensor in I2C mode.
Dependents: Project_A el13jarDistanceSensorProject UltrasonicDistanceSensor-el13jb Distance_Sensor_SRF02 ... more
Revision 2:cb489f486ece, committed 2016-06-22
- Comitter:
- eencae
- Date:
- Wed Jun 22 15:49:12 2016 +0000
- Parent:
- 1:8e6587d88773
- Commit message:
- Added method to change the address of the sensor on the bus. Address is specified using the constructor method.
Changed in this revision
SRF02.cpp | Show annotated file Show diff for this revision Revisions of this file |
SRF02.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 8e6587d88773 -r cb489f486ece SRF02.cpp --- a/SRF02.cpp Sun Mar 08 14:21:40 2015 +0000 +++ b/SRF02.cpp Wed Jun 22 15:49:12 2016 +0000 @@ -7,12 +7,15 @@ #include "mbed.h" #include "SRF02.h" -SRF02::SRF02(PinName sdaPin, PinName sclPin) +SRF02::SRF02(PinName sdaPin, PinName sclPin, char addr) { i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise i2c->frequency(400000); // I2C Fast Mode - 400kHz leds = new BusOut(LED4,LED3,LED2,LED1); + w_addr_ = addr; // set write address of sensor + r_addr_ = w_addr_ + 1; // read address has the lowest bit set (i.e. + 1) + } int SRF02::getDistanceCm() @@ -22,7 +25,7 @@ // need to send CM command to command register data[0] = CMD_REG; data[1] = CM_CMD; - int ack = i2c->write(SRF02_W_ADD,data,2); + int ack = i2c->write(w_addr_,data,2); if (ack) error(); // if we don't receive acknowledgement, flash error message @@ -31,12 +34,12 @@ // we can now read the result - tell the sensor we want the high byte char reg = RANGE_H_REG; - ack = i2c->write(SRF02_W_ADD,®,1); + ack = i2c->write(w_addr_,®,1); if (ack) error(); // if we don't receive acknowledgement, flash error message // if we read two bytes, the register is automatically incremented (H and L) - ack = i2c->read(SRF02_R_ADD,data,2); + ack = i2c->read(r_addr_,data,2); if (ack) error(); // if we don't receive acknowledgement, flash error message @@ -44,6 +47,47 @@ return (data[0] << 8) | data[1]; } +void SRF02::changeAddress(char addr) { + + char data[2]; + + // sequence to change I2C (https://www.robot-electronics.co.uk/htm/srf02techI2C.htm) + // need to write this sequence to the command register, followed by the new address + + data[0] = CMD_REG; + data[1] = 0xA0; // 1st byte in sequence + int ack = i2c->write(w_addr_,data,2); + if (ack) + error(); // if we don't receive acknowledgement, flash error message + + wait_ms(50); // small delay in between + + data[0] = CMD_REG; + data[1] = 0xAA; // 2nd byte in sequence + ack = i2c->write(w_addr_,data,2); + if (ack) + error(); // if we don't receive acknowledgement, flash error message + + wait_ms(50); // small delay in between + + data[0] = CMD_REG; + data[1] = 0xA5; // 3rd byte in sequence + ack = i2c->write(w_addr_,data,2); + if (ack) + error(); // if we don't receive acknowledgement, flash error message + + wait_ms(50); // small delay in between + + data[0] = CMD_REG; + data[1] = addr; // new address + ack = i2c->write(w_addr_,data,2); + if (ack) + error(); // if we don't receive acknowledgement, flash error message + + wait_ms(50); + +} + void SRF02::error() { while(1) {
diff -r 8e6587d88773 -r cb489f486ece SRF02.h --- a/SRF02.h Sun Mar 08 14:21:40 2015 +0000 +++ b/SRF02.h Wed Jun 22 15:49:12 2016 +0000 @@ -8,10 +8,6 @@ #ifndef SRF02_H #define SRF02_H -// addresses -#define SRF02_R_ADD 0xE1 -#define SRF02_W_ADD 0xE0 - // registers #define CMD_REG 0x00 #define RANGE_H_REG 0x02 @@ -28,16 +24,18 @@ @brief Library for interfacing with SRF02 Ultrasonic Sensor in I2C @see http://www.robot-electronics.co.uk/htm/srf02tech.htm -@brief Revision 1.0 +@brief Revision 1.1 @author Craig A. Evans -@date March 2014 +@date June 2016 * * Example: * @code #include "mbed.h" #include "SRF02.h" + + SRF02 sensor(p28,p27,0xE0); // SDA, SCL, address int main() { @@ -62,15 +60,22 @@ * * @param sdaPin - mbed SDA pin * @param sclPin - mbed SCL pin + * @param addr - write address of the SRF02 sensor * */ - SRF02(PinName sdaPin, PinName sclPin); + SRF02(PinName sdaPin, PinName sclPin, char addr); /** Read distance in centimetres * * @returns distance in centimetres (int) * */ int getDistanceCm(); + /** Change I2C address of SRF02 sensor + * + * @param address - @see https://www.robot-electronics.co.uk/htm/srf02techI2C.htm for valid addresses + * + */ + void changeAddress(char addr); private: /** Hangs in infinite loop flashing 'blue lights of death' @@ -82,6 +87,9 @@ private: // private variables I2C* i2c; BusOut* leds; + + char w_addr_; // write address of sensor + char r_addr_; // read address (= write address + 1) }; #endif \ No newline at end of file