Max Hc11 I2C communcation

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 SRF08 Range Finder

Devantech SP03 Speech to Text Module

Devantech MD03 Motor Driver

Devantech Magnetic Compass

Parts for all of your Electronics and Robotic projects

Copyright 2022 - Zagros Robotics, All Rights Reserved - Please send webpage comments or corrections to webmaster@zagrosrobotics.com - Zagros Robotics,PO Box 460342, St. Louis, MO 63146, info@zagrosrobotics.com for answers to any questions.