Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

I2C devantech cmp03 problems.

2005-06-09 by wbounce

I am trying to diagnose a problem I am having with the devantech
compass. I have 3 SRF08 sonars and a cmp03 compass connected to my
MavricII board (M128). Using winavr 3.4.1. I am using a set I2C routines
from Brian Dean's sample programs. I use the same routines to ping the
sonar as I do to read the compass so I am fairly certain they are not
the problem. Because I am pinging all 3 sonars at once, I moved my
reading of the compass to before I do the ping and after I have read the
ranges. I list the compass reading routines bearing16 calls
cmps03_get_word. The ErrorState is consistently being set to -30 which
means a failed start condition. Yet as soon as it returned from
bearing16 it calls the sonar ping which calls the same call but it
succeeds. I have checked the connects to the compass. But after ooking
at the code writing this email up it has to be something other than
that. First 1st thought was bad connection which is usually the problem
when one of the sonars stop working but at least with a sonar it has a
led that flashes when it pings so I can tell it is working. How can I
tell the compass is working? And is there anything in this code that
would cause the I2c to fail?


	tempbearing = bearing16();
	if ( tempbearing > -1) 
		{
		CurrentBearing = tempbearing;
		}
	else
		{
		ErrorState = (int8_t)tempbearing;
		printf_P(PSTR("CError %i"),ErrorState);
		}
	
	printf_P(PSTR("pg "));	
    srf08_ping(ALLDEVICES, RANGE_US);      /* initiate a ping, distance
in cm */


int16_t bearing16(void)
{
  uint16_t d;
  int8_t rc;
printf_P(PSTR("Be "));
  rc = cmps03_get_word(0x60, 2, &d);
  if (rc < 0) {
	ErrorState = rc;
    return (int16_t )rc;
  }

  return d;
}

int8_t cmps03_get_word(uint8_t device, uint8_t addr, uint16_t * value)
{
  uint8_t v1, v2;

  /* start condition */
  if (i2c_start(0x08, 0))
    return -30;

  /* address slave device, write */
  if (i2c_sla_rw(device, 0, TW_MT_SLA_ACK, 0))
    return -31;

  /* write register address */
  if (i2c_data_tx(addr, TW_MT_DATA_ACK, 0))
    return -32;

  /* repeated start condition */
  if (i2c_start(0x10, 0))
    return -33;

   /* address slave device, read */
  if (i2c_sla_rw(device, 1, TW_MR_SLA_ACK, 0))
    return -34;

  /* read data byte 1 */
  if (i2c_data_rx(&v1, I2C_ACK, TW_MR_DATA_ACK, 0))
    return -35;

  /* read data byte 2 */
  if (i2c_data_rx(&v2, I2C_NACK, TW_MR_DATA_NACK, 0))
    return -36;

  if (i2c_stop())
    return -37;

  *value = (v1 << 8) | v2 ;

  return 0;
}


int8_t srf08_ping(uint8_t device, uint8_t cmd)
{
  /* start condition */
  if (i2c_start(0x08, 0))
    return -1;

  /* address slave device, write */
  if (i2c_sla_rw(device, 0, TW_MT_SLA_ACK, 0))
    return -2;

  /* write address */
  if (i2c_data_tx(0x00, TW_MT_DATA_ACK, 0))
    return -3;

  /* write command */
  if (i2c_data_tx(cmd, TW_MT_DATA_ACK, 0))
    return -4;

  if (i2c_stop())
    return -5;

  return 0;
}

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.