u8 MPU_Write_Len(u8 addr,u8 reg,u8 len,u8 *buf)
{
u8 i;
IIC_Start();
IIC_Send_Byte((addr<<1)|0);//·????÷?????·+???ü??
if(IIC_Wait_Ack()) //????????
{
IIC_Stop();
return 1;
}
IIC_Send_Byte(reg); //???????÷???·
IIC_Wait_Ack(); //????????
for(i=0;i<len;i++)
{
IIC_Send_Byte(buf); //·???????
if(IIC_Wait_Ack()) //????ACK
{
IIC_Stop();
return 1;
}
}
IIC_Stop();
return 0;
}
//IIC??????
//addr:?÷?????·
//reg:?????????????÷???·
//len:?????????¤??
//buf:??????????????????
//·?????:0,????
// ????,?í?ó?ú??
u8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf)
{
IIC_Start();
IIC_Send_Byte((addr<<1)|0);//·????÷?????·+???ü??
if(IIC_Wait_Ack()) //????????
{
IIC_Stop();
return 1;
}
IIC_Send_Byte(reg); //???????÷???·
IIC_Wait_Ack(); //????????
IIC_Start();
IIC_Send_Byte((addr<<1)|1);//·????÷?????·+???ü??
IIC_Wait_Ack(); //????????
while(len)
{
if(len==1)*buf=IIC_Read_Byte(0);//??????,·???nACK
else *buf=IIC_Read_Byte(1); //??????,·???ACK
len--;
buf++;
}
IIC_Stop(); //?ú?ú????????????
return 0;
}
//IIC??????×???
//reg:?????÷???·
//data:????
//·?????:0,????
// ????,?í?ó?ú??
u8 MPU_Write_Byte(u8 reg,u8 data)
{
IIC_Start();
IIC_Send_Byte((AK8975_DEFAULT_ADDRESS<<1)|0);//·????÷?????·+???ü??
if(IIC_Wait_Ack()) //????????
{
IIC_Stop();
return 1;
}
IIC_Send_Byte(reg); //???????÷???·
IIC_Wait_Ack(); //????????
IIC_Send_Byte(data);//·???????
if(IIC_Wait_Ack()) //????ACK
{
IIC_Stop();
return 1;
}
IIC_Stop();
return 0;
}
//IIC??????×???
//reg:?????÷???·
//·?????:??????????
u8 MPU_Read_Byte(u8 reg) //u8 MPU_Write_Byte(u8 reg,u8 data)
{
u8 res;
IIC_Start();
IIC_Send_Byte((AK8975_DEFAULT_ADDRESS<<1)|0);//·????÷?????·+???ü??
IIC_Wait_Ack(); //????????
IIC_Send_Byte(reg); //???????÷???·
IIC_Wait_Ack(); //????????
IIC_Start();
//IIC_Send_Byte((MPU_ADDR<<1)|1);//·????÷?????·+???ü??
IIC_Wait_Ack(); //????????
res=IIC_Read_Byte(0);//????????,·???nACK
IIC_Stop(); //?ú?ú????????????
return res;
}
a7e7446ac67620d4b4553bf1fb2776a9_157.png (0 Bytes, 下载次数: 0)
下载附件
2010-12-14 22:54 上传 |