|
SRF08 I2C SONIC RANGER HC11 ācā EXAMPLEAll current Zagros Demo programs (including CoreXXX.s19 files) use the following default connections: /* HC11 MC */ 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) 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: |
|