//***************************************
// HMC5883 IIC测验程序// 运用单片机STC89C51 // 晶振:11.0592M// 显现:LCD1602// 编译环境 Keil uVision2// 参阅宏晶网站24c04通讯程序// 时刻:2011年3月1日//****************************************#include#include //Keil library #include //Keil library #include #define uchar unsigned char#define uint unsigned int //运用的端口,请依照以下接线#define DataPort P0 //LCD1602数据端口sbit SCL=P1^0; //IIC时钟引脚界说sbit SDA=P1^1; //IIC数据引脚界说sbit LCM_RS=P2^0; //LCD1602指令端口 sbit LCM_RW=P2^1; //LCD1602指令端口 sbit LCM_EN=P2^2; //LCD1602指令端口 #define SlaveAddress 0x3C //界说器材在IIC总线中的从地址typedef unsigned char BYTE;typedef unsigned short WORD;BYTE BUF[8]; //接纳数据缓存区 uchar ge,shi,bai,qian,wan; //显现变量int dis_data; //变量void delay(unsigned int k);void InitLcd();void Init_HMC5883(void); //初始化5883void WriteDataLCM(uchar dataW);void WriteCommandLCM(uchar CMD,uchar Attribc);void DisplayOneChar(uchar X,uchar Y,uchar DData);void conversion(uint temp_data);void Single_Write_HMC5883(uchar REG_Address,uchar REG_data); //单个写入数据uchar Single_Read_HMC5883(uchar REG_Address); //单个读取内部寄存器数据void Multiple_Read_HMC5883(); //接连的读取内部寄存器数据//以下是模仿iic运用函数-------------void Delay5us();void Delay5ms();void HMC5883_Start();void HMC5883_Stop();void HMC5883_SendACK(bit ack);bit HMC5883_RecvACK();void HMC5883_SendByte(BYTE dat);BYTE HMC5883_RecvByte();void HMC5883_ReadPage();void HMC5883_WritePage();//-----------------------------------//*********************************************************void conversion(uint 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 delay(unsigned int k) { unsigned int i,j; for(i=0;i