srf08 i2c sonic ranger hc11 c example
all current zagros demo programs (including corexxx.s19 files) use the following default connections:
/* hc11 mc */
/* pa0 sonar echo return */
/* pa1 encoder input (main) */
/* pa2 encoder input */
/* pa3 enable a */
/* pa4 enable b */
/* pa5 dir a */
/* pa6 dir b */
/* pd2 - sonar ping 1 */
/* pd3 - sonar ping 2 */
/* pd4 - i2c sclbit */
/* pd5 - i2c sdabit */
do not forget the pull up resistor (1.8k) on the scl and sda lines.
these are easily adapted to communicate with any i2c device.
these are the basic functions required to use the devantech srf08 i2c sonic ranger (sonar).
int sonic_range(int unit)
this function reads the devantech i2c sonic range module in inches (lower byte)
int ranger_version(void)
this function reads the devantech i2c sonic range module version number
void set_ranger_address(void)
this function sets the devantech i2c sonic range module address
int light_sensor(int unit)
this function reads the devantech i2c sonic range module light sensor
all of the functions use the i2c routines by grant beattie. (hc11i2c.zip) these functions implement an i2c bit-bang master on two port d pins.
hc11 i2c communication routines
int sonic_range(int unit)
this function reads the range of the first two units 0xe0 and 0xe2
int sonic_range(int unit)
{
int dummy;
char address=0xe0;
/* select unit address */
switch(unit)
{
case 1:
address=0xe0;
break;
case 2:
address=0xe2;
break;
}/* end switch */
/* write ranger module */
gi2c.mode = i2cmode_write|i2cmode_subaddr1;
gi2c.address = address; //0xe0; // compass standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 80; // wa '1' sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
dummy=0;
while(dummy<32000)
{
dummy++;
} /* outer delay loop */
gi2c.mode = i2cmode_write;
gi2c.address = address; //0xe0; // compass standard address.
gi2c.subaddr1 = 2; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 3; // sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
gi2c.mode = i2cmode_read;
gi2c.address = address; //0xe0; // compass standard address.
gi2c.subaddr1 = 3; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = ' '; // sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
return(gi2c.buffer[0]);
} /* end ranger */
int ranger_verion(void)
this function will read the version of the unit a address 0xe0 to 0xe2. change this to other address as needed
int ranger_version(void)
{
/* read compass module */
gi2c.mode = i2cmode_write;
gi2c.address = 0xe0; // ranger standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 0; // sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
gi2c.mode = i2cmode_read;
gi2c.address = 0xc0; // compass standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = ' '; // sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
return(gi2c.buffer[0]);
} /* end ranger version */
void set_ranger_address(void)
this function will reset the address of the unit at address 0xe0 to 0xe2
void set_ranger_address(void)
{
gi2c.mode = i2cmode_write|i2cmode_subaddr1;
gi2c.address = 0xe0; // compass standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 160; // sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
gi2c.mode = i2cmode_write|i2cmode_subaddr1;
gi2c.address = 0xe0; // compass standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 170; // data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
gi2c.mode = i2cmode_write|i2cmode_subaddr1;
gi2c.address = 0xe0; // compass standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 165; // data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
gi2c.mode = i2cmode_write|i2cmode_subaddr1;
gi2c.address = 0xe0; // compass standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 0xe2; // new address
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
}/* end ranger address change */
int light_sensor(int unit)
this function will read the light sensor reading at address 0xe0
int light_sensor(int unit)
{
int dummy;
char address=0xe0;
/* select unit address */
switch(unit)
{
case 1:
address=0xe0;
break;
case 2:
address=0xe2;
break;
}/* end switch */
/* write ranger module */
gi2c.mode = i2cmode_write|i2cmode_subaddr1;
gi2c.address = address; //0xe0; // compass standard address.
gi2c.subaddr1 = 0; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 80; // wa '1' sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
dummy=0;
while(dummy<32000)
{
dummy++;
} /* outer delay loop */
gi2c.mode = i2cmode_write;
gi2c.address = address; //0xe0;
gi2c.subaddr1 = 2; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = 1; // sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
gi2c.mode = i2cmode_read;
gi2c.address = address; //0xe0;
gi2c.subaddr1 = 1; // internal address msb = 00.
gi2c.subaddr2 = 0; // internal address lsb = 00.
gi2c.buffer[0] = ' '; // sample data to write.
gi2c.buffer[1] = ' ';
gi2c.datalength = 1;
if( i2ctransfer() )
printf("\r\ni2c read error: 0x%02x\n", i2cgetlasterror());
return(gi2c.buffer[0]);
} /* end ranger */
other i2c devices available from zagros robotics:
devantech sp03 speech to text module
copyright 2008 - zagros robotics, all rights reserved - please send webpage comments or corrections to webmaster@zagrosrobotics.com - zagros robotics, po box 460342, st. louis, mo 63146-7342, (314)341-1836 info@zagrosrobotics.com for answers to any questions.
home | what's new | lab notes | handy files | cool links | contact us | webmaster