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;
}