#include
#include "cry12864.h"
#include "cry12864.c"
#include
#include
#define HMC5883L_DIR P1DIR
#define HMC5883L_IN P1IN
#define HMC5883L_OUT P1OUT
#define SCL BIT2 //IIC时钟引脚定义
#define SDA BIT3 //IIC数据引脚定义
//DRDY 不接
#define SCL_H (HMC5883L_DIR|=SCL,HMC5883L_OUT|=SCL)
#define SCL_L (HMC5883L_DIR|=SCL,HMC5883L_OUT&=~SCL)
#define SDA_H (HMC5883L_DIR|=SDA,HMC5883L_OUT|=SDA)
#define SDA_L (HMC5883L_DIR|=SDA,HMC5883L_OUT&=~SDA)
#define SlaveAddress 0x3c //定义器件在IIC总线中的从地址
//uchar ge,shi,bai,qian,wan;
unsigned char Rec_Data[6],b[5];
unsigned char x,y,z;
double Angle;
uchar Acr;
/*延时函数*/
void Delay(uint t)
{
while(t--);
}
/*起始信号*/
void IIC_Start(void)
{
SDA_H;
SCL_H;
Delay(50);
SDA_L;
Delay(50);
SCL_L;
}
/*停止信号*/
void IIC_Stop(void)
{
SDA_L;
SCL_H;
Delay(50);
SDA_H;
Delay(50);
}
/*发送应答信号*/
void IIC_SendAck(char Ack)
{
if(Ack) //ack (0:ACK 1:NACK)
SDA_H;
else SDA_L;
SCL_H;
Delay(50);
SCL_L;
Delay(50);
}
/*接收应答信号*/
char IIC_RecAck(void)
{
char CY;
HMC5883L_DIR&=~SDA;
SCL_H;
Delay(50);
if(HMC5883L_DIR&SDA)
CY=1;
else CY=0;
SCL_L;
Delay(50);
return CY;
}
/*向IIC总线发送一个字节数据*/
void HMC5883_Send_Byte(unsigned char Dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(Dat&BIT7)
SDA_H;
else SDA_L;
SCL_H;
Delay(50);
SCL_L;
Delay(50);
Dat<<=1;
}
IIC_RecAck();
}
/*从IIC总线接收一个字节数据*/
unsigned char HMC5883_Rec_Byte(void)
{
unsigned char i,Dat=0;
SDA_H;
HMC5883L_DIR&=~SDA;
for(i=0;i<8;i++)
{
Dat<<=1;
SCL_H;
Delay(50);
if((HMC5883L_IN&SDA)==SDA)
Dat|=BIT0;
SCL_L;
Delay(50);
}
return Dat;
}
/*单字节写HMC5883*/
void Single_Write_HMC5883(unsigned char Address,unsigned char Dat)
{
IIC_Start();
HMC5883_Send_Byte(SlaveAddress);
HMC5883_Send_Byte(Address);
HMC5883_Send_Byte(Dat);
IIC_Stop();
}
/*单字节读HMC5883*/
unsigned char Single_Read_HMC5883(unsigned char Addr)
{
unsigned char Value;
IIC_Start();
HMC5883_Send_Byte(SlaveAddress);
HMC5883_Send_Byte(Addr);
IIC_Start();
HMC5883_Send_Byte(SlaveAddress+1);
Value=HMC5883_Rec_Byte();
IIC_SendAck(1);
IIC_Stop();
return Value;
}
/*多字节读HMC5883*/
void Multiple_Read_HMC5883(void)
{
unsigned char i; //连续读出HMC5883内部角度数据,地址范围0x3~0x5
IIC_Start();
HMC5883_Send_Byte(SlaveAddress);
HMC5883_Send_Byte(0x03);//发送存储单元地址,从0x03开始
IIC_Start();
HMC5883_Send_Byte(SlaveAddress+1);
for(i=0;i<6;i++) //连续读取6个地址数据,存储在Rec_Data
{
Rec_Data=HMC5883_Rec_Byte();
if(i==5)
IIC_SendAck(1); //最后一个数据需要回NOACK
else
IIC_SendAck(0); //回应ACK
}
IIC_Stop();
Delay(100);
}
//初始化HMC5883,根据需要请参考pdf进行修改****
void HMC5883_Init(void)
{
Single_Write_HMC5883(0x00,0x60);
Single_Write_HMC5883(0x02,0x00);
//HMC5883_Send_Byte(0x00);
}
void conversion(uint temp_data)
{ //uchar k;
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;
}
/*********************测试程序**********************/
unsigned char temp[16];
void main(void)
{
//unsigned char m,n,p,q;
char s[10];
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P6DIR=0XFF;P6OUT=0XFF;
P3DIR=0XFF;P3OUT=0XFF;
P4DIR=0XFF;P4OUT=0XFF;
Ini_Lcd();
//display_word();
HMC5883_Init();
do
{
Multiple_Read_HMC5883();
x=Rec_Data[0]<<8 | Rec_Data[1];//Combine MSB and LSB of X Data output register
Draw_Line(0,0,"X轴:");
sprintf(s,"%d",(int)x);
Draw_Line(0,4,s);
y=Rec_Data[4]<<8 | Rec_Data[5];//Combine MSB and LSB of Y Data output register
Draw_Line(1,0,"Y轴:");
sprintf(s,"%d",(int)y);
Draw_Line(1,4,s);
z=Rec_Data[2]<<8 | Rec_Data[3];//Combine MSB and LSB of Z Data output register
Draw_Line(2,0,"Z轴:");
sprintf(s,"%d",(int)z);
Draw_Line(2,4,s);
Angle= atan2((double)y,(double)x)*(180/3.14159265)+180;
//conversion(Angle);
Draw_Line(3,0,"角度:");
sprintf(s,"%f",Angle);
Draw_Line(3,4,s);
Delay(50000);
}
while(1);
}