//***************************************************voidSingle_Write_HMC5883(ucharREG_Address,ucharREG_data){HMC5883_Start();//起始信号HMC5883_SendByte(SlaveAddress);//发送设备地址+写信号HMC5883_SendByte(REG_Address);//内部寄存器地址,请参考中文pdfHMC5883_SendByte(REG_data);//内部寄存器数据,请参考中文pdfHMC5883_Stop();//发送停止信号}//********单字节读取内部寄存器*************************ucharSingle_Read_HMC5883(ucharREG_Address){ucharREG_data;HMC5883_Start();//起始信号HMC5883_SendByte(SlaveAddress);//发送设备地址+写信号HMC5883_SendByte(REG_Address);//发送存储单元地址,从0开始HMC5883_Start();//起始信号HMC5883_SendByte(SlaveAddress+1);//发送设备地址+读信号REG_data=HMC5883_RecvByte();//读出寄存器数据HMC5883_SendACK(1);HMC5883_Stop();//停止信号returnREG_data;}//******************************************************////连续读出HMC5883内部角度数据,地址范围0x3~0x5////******************************************************voidMultiple_read_HMC5883(void){uchari;HMC5883_Start();//起始信号HMC5883_SendByte(SlaveAddress);//发送设备地址+写信号HMC5883_SendByte(0x03);//发送存储单元地址,从0x3开始HMC5883_Start();//起始信号HMC5883_SendByte(SlaveAddress+1);//发送设备地址+读信号for(i=0;i<6;i++)//连续读取6个地址数据,存储中BUF{BUF[i]=HMC5883_RecvByte();//BUF[0]存储数据if(i==5){HMC5883_SendACK(1);//最后一个数据需要回NOACK}else{HMC5883_SendACK(0);//回应ACK}}HMC5883_Stop();//停止信号Delay5ms();}//初始化HMC5883,根据需要请参考pdf进行修改****voidInit_HMC5883(){Single_Write_HMC5883(0x02,0x00);//}
1