Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Hmc5883L Compass sensor interfacing with lpc2148

Status
Not open for further replies.

sundar11

Member level 1
Joined
Apr 16, 2014
Messages
41
Helped
2
Reputation
4
Reaction score
2
Trophy points
8
Activity points
272
Hi everyone

I am going to interface a the compass sensor Hmc5883l to the lpc2148. i Performed self test on the sensor with the controller. it shows some readings. The datasheet tells that the value in the registers are in 16bit 2's complement form, can anyone give idea to convert to these values to decimal in order to check the limits.

Regards

Sundar
 

In 2's complement format, we have signed integers..
in order to convert, take the MSB and check if it is 1 or 0. If it is 0, the output is the remaining 15 bits read in decimal. If 1, negate the remaining 15 bits and the output will be -1* the decimal.

eg.
let us assume we ha a variable num, which has a binary like
num = 1010101010b;
int decimal = 0;
//mask the msb
if(num & 32768) // (32768 is dec of 1000 0000 0000 0000)
{
decimal = -1*(!(num & 32767)); //32767 is dec equivalent to 01111 1111 1111 1111. Here ! denotes not operation
}
else
{
decimal = (num & 32767);
}

Hope this helps..

PS: you will have to rewrite this program according to ur processor.
 

hi u7karsh

Thanks for your Reply

Actually my values are in 16 bit hex two's complement form and i have converted them to decimal

Regards
Sundar

- - - Updated - - -

hi everyone

How to do calibration for hmc5883l, i have referred the data sheet and performed the self test but i think my results are appropriate. i have posted my code , any ideas would be helpful. my i2c works good i tested with logic analyser.
Code:
Unsigned char HMC5883l()
{
  UHWORD x,y,z,x1,y1,z1,Msb,Lsb;
	float x2;
	I2C_Master_Write_0(HMC5883L_WriteAddress);
	TransmitBuffer[0] = 0x00; // config register a
	TransmitBuffer[1]=0x70,
	TransmitBuffer[2]=0x18;
	Data(TransmitBuffer,3);// sends the datas
  I2C_Stop();
	
	I2C_Master_Write_0(HMC5883L_WriteAddress);
	Single_Byte(0x01); //  config register b
	Single_Byte(0x40); // gain value
  I2C_Stop();
	
	I2C_Master_Write_0(HMC5883L_WriteAddress);
	Single_Byte(HMC5883L_ModeRegisterAddress); 
	Single_Byte(HMC5883L_ContinuousModeCommand);// setting continous mode
  I2C_Stop();

	I2C_Master_Read_0(HMC5883L_ReadAddress,6); // read all six registers
  Msb = ReceiveBuffer[0];
	Lsb = ReceiveBuffer[1];
  x = (Msb<<8)|Lsb; // storing 16 bit value in a single variable
	x1 = ~x;
	x1 =(x1+1);// performs two's complement
	
	
	
	LCD_Command(0x80);
	Display_Integer(x1,16);
	
	Msb = I2C_0_ReceiveBuffer[2];
	Lsb = I2C_0_ReceiveBuffer[3];

  z = (Msb<<8)|Lsb;
	z1 = ~z;
	z1 =(z1+1);
	LCD_Command(0x85);
	Display_Integer(z1,16);
	Msb = I2C_0_ReceiveBuffer[4];
	Lsb = I2C_0_ReceiveBuffer[5];
  	
  y = (Msb<<8)|Lsb;
	y1 = ~y;
	y1 =(y1+1);

	LCD_Command(0x89);

	Display_Integer(y1,16);
	
	I2C_Master_Write_0(HMC5883L_WriteAddress);
        Single_Byte(HMC5883L_DataOutputXMSBAddress);

	x2=atan2(y1,x1)* (180 /3.14159265) + 180; // calculating angle
	
	return x2;

}
 

You might want to consider this: **broken link removed**
for this, this guy wrote a library for arduino and the functions can be found at **broken link removed**

You can check this and rework on this to create your own function as per this library..
 
hi u7karsh

Thanks for link, i visited the link it is very useful, i am reworking my code

Regards
Sundar
 

If you still face any problem, post here or pm me anytime :)
 

hi u7karsh

Thanks for your information, i will do the needful thing.

Regards
Sundar
 

hi u7karsh

I tryied to understand the code from the link, but i struck in the functions mills ,max and min. is there any manual regarding the calibration of this sensor. without calibration i am getting values around 13,180,225,242,252,269. any ideas would be helpful

Regards
Sundar
 

millis is use to get the time that has past since your hardware is turned on.. it basically returns the milliseconds that have passed.. max calculates the maximum of inputs while min calculates minimum of all inputs..
Yes the problem is that we have 2 north poles.. one is magnetic north and other is true north.. in order to caliberate ur readings with true north, u need to do the caliberation..

moreover if possible, show us ur code so that we might further help u out..
 
Hi all
I have calibrated the HMC5883l and got output!!. Thanks for your Support.. For Reference verify **broken link removed**
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top