EmbeddedRelated.com
Forums

Interfacing HDPM01(Digital Compass) with LPC2129 by I2C....Pls Findout what is the wrong in my code....

Started by kris...@gmail.com September 2, 2010
First cycle: START followed by a calling to slave address [0110xx0] to WRITE (8th SCL, SDA keep low). [xx] is determined by factory programming, total 4 different addresses are available.
Second cycle: After a acknowledge signal is received by master device (HOPERF device pulls SDA line low during 9th SCL pulse), master device sends “[00000000]” as the target address to be written into. HOPERF device should acknowledge at the end (9th SCL pulse). Note: since HOPERF device has only one internal register that can be written into, so user should always indicate “[00000000]” as the write address.
Third cycle: Master device writes to internal HOPERF device memory the code “[00000001]” as a wake-up call to initiate a data acquisition. HOPERF device should send acknowledge.
A STOP command indicates the end of write operation.
A minimal 5ms wait should be given to HOPERF device to finish a data acquisition and return a valid output. The TM bit (Take Measurement bit in control register) will be automatically reset to “0” after data from A/D converter is ready. The transition from “1” to “0” of TM bit also indicates “data ready”. The device will go into sleep mode afterwards. Analog circuit will be powered off, but I2C portion will continue be active and data will not be lost.
Fourth cycle: Master device sends a START command followed by calling HOPERF device address with a WRITE (8th SCL, SDA keep low). A ‘Acknowledge’ should be send by HOPERF device at the end.
Fifth cycle: Master device writes to HOPERF device a “[00000000]” as the starting address to read from internal memory. Since “[00000000]” is the address of internal control register, reading from this address can serve as a verification operation to confirm the write command has been successful. Note: the starting address in principle can be any of the 5 addresses. For example, user can start read from address [0000001], which is X channel MSB.
HDPM01
Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: s...@hoperf.com http://www.hoperf.com 17
Sixth cycle: Master device calls HOPERF device address with a READ (8th SCL cycle SDA line high). HOPERF device should acknowledge at the end. Seventh cycle: Master device cycles SCL line, first addressed memory data appears on SDA line. If in step 7, “[00000000]” was sent, internal control register data should appear (in the following steps, this case is assumed). Master device should send ‘Acknowledge’ at the end.
Eighth cycle: Master device continues cycle SCL line, next byte of internal memory should appear on SDA line (MSB of X channel). The internal memory address pointer automatically moves to the next byte. Master acknowledges.
Ninth cycle: LSB of X channel.
Tenth cycle: MSB of Y channel.
Eleventh cycle: LSB of Y channel.
Master ends communications by NOT sending ‘Acknowledge’ and also followed by a ‘STOP’ command.

/***********************************************************************************************/
#include
#include
/***********************************************************************************************/
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;
// delay(50);
/*checking for SI to go high*/
// printf("%0x",I2CONSET);
while(!(I2CONSET==0x58));
// printf("%0x",I2CONSET);
//printf("Check Inside Stop Condition");

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;
printf("%0x",I2STAT);
/*waiting for slave addr to be send */
//while(!(I2STAT == status));
/*clearing SI*/
I2CONCLR = 0X08;
return 0;
}
/**************************************************************************************************/
void main()
{
unsigned char a;
initalize();
printf("Program Begins Here\n");
I2C_Start_Condition();
I2C_Send_Byte(0x60,0x18); //calling to slave address [0110xx0] to WRITE
printf("0x60 Sent\n");
I2C_Send_Byte(0x00,0x30);
printf("0x00 Sent\n");
I2C_Send_Byte(0x01,0x28);
printf("0x01 Sent\n");
I2C_Stop_Condition();
I2C_Start_Condition();
I2C_Send_Byte(0x60,0x18);
I2C_Send_Byte(0x01,0x28);
I2C_Send_Byte(0x61,0xF8);
//printf("OK2");
delay(50);
delay(50);
delay(50);
a=I2DAT;
printf("%0x",a);
}
/**************************************************************************************************/

An Engineer's Guide to the LPC2100 Series

Hi,

This text sounds very close to what I've read in a Memsic magnetometer
datasheet. I also had trouble decrypting the Engrish but I did succeed
in the end. Here are my findings:
http://mbed.org/users/igorsk/notebook/memsic-magnetometers/

On Thu, Sep 2, 2010 at 13:53, wrote:
> First cycle: START followed by a calling to slave address [0110xx0] to WRITE (8th SCL, SDA keep low). [xx] is determined by factory programming, total 4 different addresses are available.
> Second cycle: After a acknowledge signal is received by master device (HOPERF device pulls SDA line low during 9th SCL pulse), master device sends [00000000] as the target address to be written into. HOPERF device should acknowledge at the end (9th SCL pulse). Note: since HOPERF device has only one internal register that can be written into, so user should always indicate [00000000] as the write address.
> Third cycle: Master device writes to internal HOPERF device memory the code [00000001] as a wake-up call to initiate a data acquisition. HOPERF device should send acknowledge.
> A STOP command indicates the end of write operation.
> A minimal 5ms wait should be given to HOPERF device to finish a data acquisition and return a valid output. The TM bit (Take Measurement bit in control register) will be automatically reset to 0 after data from A/D converter is ready. The transition from 1 to 0 of TM bit also indicates data ready. The device will go into sleep mode afterwards. Analog circuit will be powered off, but I2C portion will continue be active and data will not be lost.
> Fourth cycle: Master device sends a START command followed by calling HOPERF device address with a WRITE (8th SCL, SDA keep low). A Acknowledge should be send by HOPERF device at the end.
> Fifth cycle: Master device writes to HOPERF device a [00000000] as the starting address to read from internal memory. Since [00000000] is the address of internal control register, reading from this address can serve as a verification operation to confirm the write command has been successful. Note: the starting address in principle can be any of the 5 addresses. For example, user can start read from address [0000001], which is X channel MSB.
> HDPM01
> Tel: +86-755-82973805 Fax: +86-755-82973550 E-mail: s...@hoperf.com http://www.hoperf.com 17
> Sixth cycle: Master device calls HOPERF device address with a READ (8th SCL cycle SDA line high). HOPERF device should acknowledge at the end. Seventh cycle: Master device cycles SCL line, first addressed memory data appears on SDA line. If in step 7, [00000000] was sent, internal control register data should appear (in the following steps, this case is assumed). Master device should send Acknowledge at the end.
> Eighth cycle: Master device continues cycle SCL line, next byte of internal memory should appear on SDA line (MSB of X channel). The internal memory address pointer automatically moves to the next byte. Master acknowledges.
> Ninth cycle: LSB of X channel.
> Tenth cycle: MSB of Y channel.
> Eleventh cycle: LSB of Y channel.
> Master ends communications by NOT sending Acknowledge and also followed by a STOP command.
>
> /***********************************************************************************************/
> #include
> #include
> /***********************************************************************************************/
> void delay (unsigned int t)//if t=10 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;
> // delay(50);
> /*checking for SI to go high*/
> // printf("%0x",I2CONSET);
> while(!(I2CONSET==0x58));
> // printf("%0x",I2CONSET);
> //printf("Check Inside Stop Condition");
>
> 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;
> printf("%0x",I2STAT);
> /*waiting for slave addr to be send */
> //while(!(I2STAT == status));
> /*clearing SI*/
> I2CONCLR = 0X08;
> return 0;
> }
> /**************************************************************************************************/
> void main()
> {
> unsigned char a;
> initalize();
> printf("Program Begins Here\n");
> I2C_Start_Condition();
> I2C_Send_Byte(0x60,0x18); //calling to slave address [0110xx0] to WRITE
> printf("0x60 Sent\n");
> I2C_Send_Byte(0x00,0x30);
> printf("0x00 Sent\n");
> I2C_Send_Byte(0x01,0x28);
> printf("0x01 Sent\n");
> I2C_Stop_Condition();
> I2C_Start_Condition();
> I2C_Send_Byte(0x60,0x18);
> I2C_Send_Byte(0x01,0x28);
> I2C_Send_Byte(0x61,0xF8);
> //printf("OK2");
> delay(50);
> delay(50);
> delay(50);
> a=I2DAT;
> printf("%0x",a);
> }
> /**************************************************************************************************/
>
>