EmbeddedRelated.com
Forums

Anyone Help me in interfacing LPC2129 with HDPM01(Digital Compass) using I2c

Started by kris...@gmail.com September 7, 2010
Refer data sheet : www.hoperf.com/upfile/hdpm01.pdf

Here is my sample program for interfacing HDPM01 with LPC2129 with I2C..
Pls Help me further to get data from compass how should i calulate the direction from that data.........

************************************************************************************/
#include
#include

#define C_SET 1
#define C_RESET 0

unsigned char set_position;
/***********************************************************************************************/
void delay (unsigned int t)//if t is 1ms
{
unsigned int i;
while(t--)
{
for(i=0;i<1000;i++);
}
}
/******************************settings for UART1 and I2C**********************************************/
void Initalize()
{
VPBDIV = 0X00;//initilizing VPB DIVIDER
PINSEL0 =0X00050050;//FOR UART1 AND i2c SDA and SCL
U1LCR = 0X83;
U1DLL = 0X61;
U1DLM = 0X00;
U1LCR = 0X03;
I2SCLL = 0x25;
I2SCLH = 0x25;//setting the i2c bit rate as 400kbps
}
/***********************************************************************************************/
unsigned char I2C_Start_Condition()
{
/*enable start condition in master tx mode*/
I2CONSET = 0x60;

/*checking for SI to go high*/
while(!((I2CONSET & 0x08)==0x08));
while(!(I2STAT == 0x08));

/*clearing SI and START */
I2CONCLR = 0X28;
return 0;
}
/**************************************************************************************************/
unsigned char I2C_Stop_Condition(void)
{
/*sending stop bit by making STO high*/
I2CONSET = 0X50;

/*checking for SI to go high*/

while(!(I2CONSET==0x58));

I2CONCLR = 0X08;
return 0;
}
/**************************************************************************************************/
unsigned char I2C_Send_Byte(unsigned char data,unsigned char status)
{
/*slave address and W bit loaded*/
I2DAT = data;

/*waiting for slave addr to be send */
while(!(I2STAT == status));

/*clearing SI*/
I2CONCLR = 0X08;
return 0;
}
/**************************************************************************************************/
unsigned char I2C_Send_Byte0(unsigned char data)
{
/*slave address and W bit loaded*/
I2DAT = data;
delay(50);
printf("%0x\n",I2STAT);
/*waiting for slave addr to be send */
//while(!(I2STAT == status));
/*clearing SI*/
I2CONCLR = 0X08;
return 0;
}
/**********************************************************************************************/
void Compass_SET()
{
I2C_Start_Condition();
I2C_Send_Byte(0x60,0x18); //calling to slave address [0110xx0] to WRITE
I2C_Send_Byte(0x00,0x28);
I2C_Send_Byte(0x02,0x28);
delay(50);
I2C_Stop_Condition();
printf("Compass Set\n");
}
/*********************************************************************************************/
void Compass_RESET()
{
I2C_Start_Condition();
I2C_Send_Byte(0x60,0x18); //calling to slave address [0110xx0] to WRITE
I2C_Send_Byte(0x00,0x28);
I2C_Send_Byte(0x04,0x28);
delay(50);
I2C_Stop_Condition();
printf("Compass Reset\n");
}
/************************************************************************************************/
void Compass_Set_Reset(void)
{
if(set_position == C_SET)
{
Compass_SET();
set_position = C_RESET;
}
else
{
Compass_RESET();
set_position = C_SET;
}
delay(10);
}
/**************************************************************************************************/
void Compass_Calibration()
{
// unsigned int i;
Compass_Set_Reset();
delay(50);
Compass_Set_Reset();
delay(50);
Compass_Set_Reset();
}
/**************************************************************************************************/
void Get_Compass_Data(void)
{
//unsigned int i;//unsigned char data[8];
//Compass_Set_Reset();
I2C_Start_Condition();
I2C_Send_Byte(0x61,0x40);
delay(500);
//for(i=0;i<1;i++)
//{
// delay(500);
//data[i]=I2DAT;
printf("%0x",I2DAT);
printf("%0x",I2ADR);
//data<<=2;
//}
// printf("%c",data);
// I2C_Send_Byte(0x00,0x28);
// I2C_Send_Byte(0x01,0x28);
//I2C_Stop_Condition();
// printf("OK\n") ;
// delay(50);
// I2C_Start_Condition();
// I2C_Send_Byte(0x60,0x18);
// I2C_Send_Byte(0x01,0x28);
// I2C_Stop_Condition();
// I2C_Start_Condition();
// I2C_Send_Byte0(0x61);

// IIC_WriteByte(0x61); */
}

/**************************************************************************************************/
void main()
{
//s unsigned int i;
Initalize();
delay(50);
set_position=C_SET;
Compass_Calibration();
delay(50);
//for(i=0;i<4;i++)
//{
Get_Compass_Data();
//}
}
/**************************************************************************************************/

An Engineer's Guide to the LPC2100 Series

>Refer data sheet : www.hoperf.com/upfile/hdpm01.pdf
>
>Here is my sample program for interfacing HDPM01 with LPC2129 with I2C..
>Pls Help me further to get data from compass how should i calulate the >direction from that data.........

"Read the datasheet for me, debug my code because I can't seem to get it working, and then write all my application code for me too because I haven't got time...."