Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

SRF02 ultrasonic ranger

Status
Not open for further replies.

haseeb123

New Member
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
}
}
 
Status
Not open for further replies.

Latest threads

Back
Top