Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SRF02 ultrasonic ranger

Status
Not open for further replies.

haseeb123

Newbie level 6
Joined
Jun 7, 2011
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,416
hello

can some one please tell me how i can measure beyond 255cm i2c mode with the SRF02 ultrasonic ranger?
I can read the LOW byte but reading the high byte seems confusing....or can i only read upto 255cm? it does not say anywhere on the data sheet

i have observed that when lowbyte goes beyond 255, then the highbyte goes to '1' otherwise it stays low.

im using pic18f2420 with ccs compiler. below is my code. please can you tell me what is wrong with it.


int16 temp_var;
int
empty_bim_level,
rangehigh,
rangelow,
lightsensor;
int level_detect(void)
{
i2c_start();
i2c_write(0xE0); // address of the ranger
i2c_write(0x00); // command register
i2c_write(0x51); // measurement in cm
i2c_stop();

delay_ms(100);

i2c_start(); // start I2C
i2c_write(0xE0); // SRF08 I2C address with R/W bit clear
i2c_write(0x01); // SRF08 light sensor register address
i2c_start(); // send a restart sequence
i2c_write(0xE1); // SRF02 I2C address with R/W bit set
lightsensor = i2c_read(1); // get light sensor and send acknowledge.
rangehigh = i2c_read(1); // get the high byte of the range and send acknowledge.
rangelow = i2c_read(0); // get low byte of the range - note we don't acknowledge the last byte.
i2c_stop(); // send stop sequence
return rangelow;
}
void(main)
{
while(true)
{
level = level_detect(); //detect level and save in variable

if(rangehigh) //range is > 255
{
temp_var = 256 + level; //build 16bit variable
//break 16bit variable so i can print on screen
rangelow = temp_var;
rangehigh = temp_var / 256;

fprintf(User,"Range is %u%u cm\n\r",rangehigh, rangelow); //print to screen
}
else //range < 255

fprintf(User,"Range is %u cm\n\r",level); //print to screen direct
}
}
 

The SRF02 documentation tells clearly about a 16 bit result. You simply have to assemble both bytes. With CCS you have the built-in make16() function for this purpose: result = make16(rangehigh, rangelow);
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top