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.
Dependents: Project_A el13jarDistanceSensorProject UltrasonicDistanceSensor-el13jb Distance_Sensor_SRF02 ... more
SRF02.cpp
00001 /** 00002 @file SRF02.cpp 00003 00004 @brief Member functions implementations 00005 00006 */ 00007 #include "mbed.h" 00008 #include "SRF02.h" 00009 00010 SRF02::SRF02(PinName sdaPin, PinName sclPin, char addr) 00011 { 00012 i2c = new I2C(sdaPin,sclPin); // create new I2C instance and initialise 00013 i2c->frequency(400000); // I2C Fast Mode - 400kHz 00014 leds = new BusOut(LED4,LED3,LED2,LED1); 00015 00016 w_addr_ = addr; // set write address of sensor 00017 r_addr_ = w_addr_ + 1; // read address has the lowest bit set (i.e. + 1) 00018 00019 } 00020 00021 int SRF02::getDistanceCm() 00022 { 00023 char data[2]; 00024 00025 // need to send CM command to command register 00026 data[0] = CMD_REG; 00027 data[1] = CM_CMD; 00028 int ack = i2c->write(w_addr_,data,2); 00029 if (ack) 00030 error(); // if we don't receive acknowledgement, flash error message 00031 00032 // this will start the sensor ranging, the datasheet suggests a delay of at least 65 ms before reading the result 00033 wait_ms(70); 00034 00035 // we can now read the result - tell the sensor we want the high byte 00036 char reg = RANGE_H_REG; 00037 ack = i2c->write(w_addr_,®,1); 00038 if (ack) 00039 error(); // if we don't receive acknowledgement, flash error message 00040 00041 // if we read two bytes, the register is automatically incremented (H and L) 00042 ack = i2c->read(r_addr_,data,2); 00043 if (ack) 00044 error(); // if we don't receive acknowledgement, flash error message 00045 00046 // high byte is first, then low byte, so combine into 16-bit value 00047 return (data[0] << 8) | data[1]; 00048 } 00049 00050 void SRF02::changeAddress(char addr) { 00051 00052 char data[2]; 00053 00054 // sequence to change I2C (https://www.robot-electronics.co.uk/htm/srf02techI2C.htm) 00055 // need to write this sequence to the command register, followed by the new address 00056 00057 data[0] = CMD_REG; 00058 data[1] = 0xA0; // 1st byte in sequence 00059 int ack = i2c->write(w_addr_,data,2); 00060 if (ack) 00061 error(); // if we don't receive acknowledgement, flash error message 00062 00063 wait_ms(50); // small delay in between 00064 00065 data[0] = CMD_REG; 00066 data[1] = 0xAA; // 2nd byte in sequence 00067 ack = i2c->write(w_addr_,data,2); 00068 if (ack) 00069 error(); // if we don't receive acknowledgement, flash error message 00070 00071 wait_ms(50); // small delay in between 00072 00073 data[0] = CMD_REG; 00074 data[1] = 0xA5; // 3rd byte in sequence 00075 ack = i2c->write(w_addr_,data,2); 00076 if (ack) 00077 error(); // if we don't receive acknowledgement, flash error message 00078 00079 wait_ms(50); // small delay in between 00080 00081 data[0] = CMD_REG; 00082 data[1] = addr; // new address 00083 ack = i2c->write(w_addr_,data,2); 00084 if (ack) 00085 error(); // if we don't receive acknowledgement, flash error message 00086 00087 wait_ms(50); 00088 00089 } 00090 00091 void SRF02::error() 00092 { 00093 while(1) { 00094 leds->write(15); 00095 wait(0.1); 00096 leds->write(0); 00097 wait(0.1); 00098 } 00099 }
Generated on Wed Jul 13 2022 17:49:25 by
1.7.2