lpcware

Urgent!! LPC1769 compass sensor HMC6352 I2C

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by sgtengxiao on Mon Oct 15 02:56:35 MST 2012
This is related to my current project, and the deadline is near. Hope any one can help me find my code error.  I will wait online~  :(:(:(
Below is my code:

#include "lpc17xx_pinsel.h"
#include "lpc17xx_i2c.h"
#include "lpc17xx_timer.h"
#include "stdio.h"

#define I2CDEV LPC_I2C2
static const int READ_WRITE_OPERATION = 0x42;
static const int COMMAND_GET_DATA = 0x41;

void readFromCompassHMC6352(void);

static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type rxsetup;
rxsetup.sl_addr7bit = addr;
rxsetup.tx_data = NULL; // Get address to read at writing address
rxsetup.tx_length = 0;
rxsetup.rx_data = buf;
rxsetup.rx_length = len;
rxsetup.retransmissions_max = 3;
if (I2C_MasterTransferData(I2CDEV, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){
  return (0);
} else {
  return (-1);
}
}

static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type txsetup;
txsetup.sl_addr7bit = addr;
txsetup.tx_data = buf;
txsetup.tx_length = len;
txsetup.rx_data = NULL;
txsetup.rx_length = 0;
txsetup.retransmissions_max = 3;
if (I2C_MasterTransferData(I2CDEV, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){
  return (0);
} else {
  return (-1);
}
}

void readFromCompassHMC6352(void)
{
int value = -1;
uint8_t data[2]={0,0};
while(1) {
  // Send a "Get Data" command to the compass
  data[0]= COMMAND_GET_DATA;
  I2CWrite(READ_WRITE_OPERATION, data, 2);
  // datasheet suggests to wait at least 6000 microseconds
  Timer0_Wait(6);
  // Read two bytes from the compass (compass heading)
  data[0] = 1;
  I2CRead(READ_WRITE_OPERATION, data, 2);
  value = ((data[0] << 8)+data[1]) / 10;
  // read compass twice to clear last reading
  Timer0_Wait(6);
  data[0] = 0;
  I2CRead(READ_WRITE_OPERATION,data, 2);
  //Display the value on the display
  if(value >= 0 && value <= 359) {
   //Display degree value
   [COLOR=red]printf("The angle is %d\n",value);
[/COLOR]  }
  Timer0_Wait(60);
}
}

static void init_i2c(void)
{
PINSEL_CFG_Type PinCfg;
/* Initialize I2C2 pin connect
  * P0.10 - I2C_SDA(Serial Data)
  * P0.11 - I2C_SCL(Serial Clock line)
  * */
PinCfg.Funcnum = 2;
PinCfg.Pinnum = 10;
PinCfg.Portnum = 0;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 11;
PINSEL_ConfigPin(&PinCfg);
// Initialize I2C peripheral
I2C_Init(LPC_I2C2, 100000);
/* Enable I2C2 operation */
I2C_Cmd(LPC_I2C2, ENABLE);
}

int main(void)
{
  init_i2c();
  Timer0_Wait(1);}
  readFromCompassHMC6352();
  return 1;
}


The result printf "anlge" is always 0. I debugged one by one, and find that in I2CWrite and I2CRead functions, it always returns -1, which means the data transfer fails.

Any one can help me?  pls~~~~~~

Outcomes