/************************************************************************************************************/
#include "stm32f7xx_hal_conf.h"
#define MMA_ADR 0x3c
uint8_t HMC_I2C_Write(uint8_t address,uint8_t data)
{
I2C1_Start();
I2C1_SendByte(MMA_ADR);//
I2C1_Ack();
I2C1_SendByte(address); //
I2C1_Ack();
I2C1_SendByte(data);
I2C1_Ack();
I2C1_Stop();
return 1;
}
uint8_t HMC_I2C_Read(uint8_t address)//
{
uint8_t temp=0;
I2C1_Start();
I2C1_SendByte(MMA_ADR);
I2C1_Ack();
I2C1_SendByte(address); //
I2C1_Ack();
I2C1_Start();
I2C1_SendByte(MMA_ADR|0x01);//
I2C1_Ack();
temp=I2C1_ReceiveByte();
I2C1_Ack();
I2C1_Stop();
return temp;
}
void HMC_MultipleRead(uint8_t* pBuffer)
{
uint8_t i=0;
I2C1_Start();
I2C1_SendByte(0x3c);
I2C1_WaitAck();
I2C1_SendByte(0x03);
I2C1_WaitAck();
I2C1_Start();
I2C1_SendByte(0x3c+1);
I2C1_WaitAck();
for(i=0;i<6;i++)
{
pBuffer[i]=I2C1_ReceiveByte();
if(i == 5)
I2C1_NAck();
else
I2C1_Ack();
}
I2C1_Stop();
}
void HMC_GetValue(int16_t* pBuffer)
{
uint8_t tmp[6];
double angle;
HMC_MultipleRead(tmp);
pBuffer[0]=(int16_t) ( (((uint16_t)tmp[0])<<8) + tmp[1] ); //Xор
pBuffer[1]=(int16_t) ( (((uint16_t)tmp[2])<<8) + tmp[3] ); //Zор
pBuffer[2]=(int16_t) ( (((uint16_t)tmp[4])<<8) + tmp[5] ); //Yор
if(pBuffer[0] > 0x7fff)pBuffer[0] -= 0xffff;
if(pBuffer[1] > 0x7fff)pBuffer[1] -= 0xffff;
if(pBuffer[2] > 0x7fff)pBuffer[2] -= 0xffff;
angle = atan2(pBuffer[2],pBuffer[0])*(180/3.1415926)+180;
printf("x = %d ",pBuffer[0]);
printf("y = %d ",pBuffer[2]);
printf("anlge = %f \r\n",angle);
}
uint16_t HMC5883_Init(void)
{
HMC_I2C_Write(0x02,0x00);//
HMC_I2C_Write(0x01,0xe0);
return HMC_I2C_Read(10);
}
|