//***************************************// HMC5883 IIC測(cè)試程序// 使用單片機(jī)STC89C51 // 晶振:11.0592M// 顯示:LCD1602// 編譯環(huán)境 Keil uVision2// 參考宏晶網(wǎng)站24c04通信程序// 時(shí)間:2011年3月1日//****************************************#include #include //Keil library #include //Keil library #include #define uchar unsigned char#define uint unsigned int //使用的端口,請(qǐng)按照以下接線#define DataPort P0 //LCD1602數(shù)據(jù)端口sbit SCL=P1^0; //IIC時(shí)鐘引腳定義sbit SDA=P1^1; //IIC數(shù)據(jù)引腳定義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]; //接收數(shù)據(jù)緩存區(qū) 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); //單個(gè)寫入數(shù)據(jù)uchar Single_Read_HMC5883(uchar REG_Address); //單個(gè)讀取內(nèi)部寄存器數(shù)據(jù)void Multiple_Read_HMC5883(); //連續(xù)的讀取內(nèi)部寄存器數(shù)據(jù)//以下是模擬iic使用函數(shù)-------------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; //取余運(yùn)算qian=temp_data/1000+0x30 ;temp_data=temp_data%1000; //取余運(yùn)算bai=temp_data/100+0x30 ;temp_data=temp_data%100; //取余運(yùn)算shi=temp_data/10+0x30 ;temp_data=temp_data%10; //取余運(yùn)算ge=temp_data+0x30; }/*******************************/void delay(unsigned int k) { unsigned int i,j; for(i=0;i
評(píng)論