#define SlaveAddress 0x3C //定义器件在i2c中的地址
#include "hmc5883.h"
#include "uart.h"
#include "stc.h"
sbit SCL=P1^2; //IICê±ÖóÏß
sbit SDA=P1^3; //IICêy¾YÏß
unsigned char ge,shi,bai,qian,wan;
unsigned char BUF[8];
void Conversion(unsigned int temp_data)
{
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000;
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000;
bai=temp_data/100+0x30 ;
temp_data=temp_data%100;
shi=temp_data/10+0x30 ;
temp_data=temp_data%10;
ge=temp_data+0x30;
}
void delay5us()
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 24;
while (--i);
}
/*****************Æeê¼DÅoÅ**************/
void hmc5883_start()
{
SDA = 1;
SCL = 1;
delay5us();
SDA = 0;
delay5us();
SCL = 0;
}
/*******************í£Ö1DÅoÅ**************/
void hmc5883_stop()
{
SDA = 0;
SCL = 1;
delay5us();
SDA = 1;
delay5us();
}
/********************·¢Ëíó|′eDÅoÅ***********/
void hmc5883_sendack(bit ack)
{
SDA = ack;
SCL = 1;
delay5us();
SCL = 0;
delay5us();
}
/*********************½óêüó|′eDÅoÅ**********/
bit hmc5883_recvack()
{
SCL = 1;
delay5us();
CY = SDA;
SCL = 0;
delay5us();
return CY;
}
/**********************·¢Ëíò»¸ö×Ö½úêy¾Y*******/
void hmc5883_sendbyte(unsigned char dat)
{
unsigned char i;
for(i=0; i<8; i++)
{
dat <<= 1;
SDA = CY;
SCL = 1;
delay5us();
SCL = 0;
delay5us();
}
hmc5883_recvack();
}
/*************************½óêüò»¸ö×Ö½úêy¾Y*********/
unsigned char hmc5883_recvbyte()
{
unsigned char i;
unsigned char dat = 0;
SDA = 1;
for(i=0; i<8; i++)
{
dat <<= 1;
SCL = 1;
delay5us();
dat |= SDA;
SCL = 0;
delay5us();
}
return dat;
}
/*************************D′èëμ¥×Ö½úêy¾Y***********/
void single_write_hmc5883(unsigned char reg_address, unsigned char reg_data)
{
hmc5883_start();
hmc5883_sendbyte(SlaveAddress);
hmc5883_sendbyte(reg_address);
hmc5883_sendbyte(reg_data);
hmc5883_stop();
}
/**************************á¬Dø¶áè¡Äú2¿¼Ä′æÆ÷******/
void multiple_read_hmc5883(void)
{
unsigned char i;
hmc5883_start();
hmc5883_sendbyte(SlaveAddress);
hmc5883_sendbyte(0x03);
hmc5883_start();
hmc5883_sendbyte(SlaveAddress+1);
for (i=0; i<6; i++)
{
BUF[i] = hmc5883_recvbyte();
if (i == 5)
{
hmc5883_sendack(1);
}
else
{
hmc5883_sendack(0);
}
}
hmc5883_stop();
delay5us();
SendString1(BUF);
}
void init_hmc5883()
{
single_write_hmc5883(0x02, 0x01);
}
/********************êy¾Y′|àí**********************/
void send_hmc5883()
{
static int X,Y,Z;
static double Angle_XY=0,Angle_XZ=0,Angle_YZ=0;
static unsigned char str[20];
multiple_read_hmc5883();
X=BUF[0] << 8 | BUF[1]; //Combine MSB and LSB of X Data output register ?????
Z=BUF[2] << 8 | BUF[3]; //Combine MSB and LSB of Z Data output register
Y=BUF[4] << 8 | BUF[5]; //Combine MSB and LSB of Y Data output register
if(X>0x7fff)X-=0xffff;
if(Y>0x7fff)Y-=0xffff;
if(Z>0x7fff)Z-=0xffff;
Angle_XY= atan2((double)Y,(double)X) * (180 / 3.14159265) + 180;
Angle_XY*=10;
Conversion(Angle_XY);
sprintf(str, "XY:%c%c%c.%c\r\n", qian, bai, shi, ge);
SendString1(str);
Angle_XZ= atan2((double)Z,(double)X) * (180 / 3.14159265) + 180;
Angle_XZ*=10;
Conversion(Angle_XZ);
sprintf(str, "XZ: %c%c%c.%c\r\n", qian, bai, shi, ge);
SendString1(str);
Angle_YZ= atan2((double)Z,(double)Y) * (180 / 3.14159265) + 180;
Angle_YZ*=10;
Conversion(Angle_YZ);
sprintf(str, "YZ: %c%c%c.%c\r\n", qian, bai, shi, ge);
SendString1(str);
}