version for edu_robot TM
Fork of SRF02 by
Embed:
(wiki syntax)
Show/hide line numbers
SRF02.cpp
00001 #include "SRF02.h" 00002 00003 /* Creates an instance of class. Setting the pin used for I2C, the address of device and the measure range type. */ 00004 SRF02::SRF02(PinName sda, PinName scl, int addr, char type) : _i2c(sda, scl), _addr(addr) 00005 { 00006 00007 _typem = type; 00008 } 00009 00010 /* Destroyer of class instance. */ 00011 SRF02::~SRF02() 00012 { 00013 00014 } 00015 00016 /* Start and return the range measure. */ 00017 float SRF02::read() 00018 { 00019 char command[2]; 00020 char result[2]; 00021 00022 command[0] = 0x00; // Set the command register 00023 00024 command[1] = _typem; // Ranging results in type indicated in a costructor 00025 _i2c.write(_addr, command, 2); // Send the command to start a ranging burst 00026 00027 wait_ms(70); // Wait 70mS for complete the measure 00028 00029 command[0] = 0x02; // The start address of measure result 00030 _i2c.write(_addr, command, 1, 1); // Send address to read a measure 00031 _i2c.read(_addr, result, 2); // Read two byte of measure 00032 00033 float range = (result[0]<<8)+result[1]; // Convert the two byte in a float value 00034 00035 return range; 00036 }
Generated on Sat Jul 16 2022 05:32:16 by 1.7.2