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;
}
int main(void)
{
//short aacx,aacy,aacz; //???????????÷????????
//short gyrox,gyroy,gyroz; //??????????????
//short temp; //????
delay_init(); //?????????±????
uart_init(115200); //???????®???¨??????500000
IIC_Init();
while(1)
{
delay_ms(5000);
//count= MPU_Read_Byte(AK8975_RA_WIA);
//printf("%c",count);
//MPU_Read_Byte(AK8975_RA_INFO)
// MPU_Write_Byte(AK8975_RA_CNTL,AK8975_MODE_SINGLE);
delay_ms(10);
MPU_Read_Len(AK8975_DEFAULT_ADDRESS, AK8975_RA_HXL,6,buffer);
x=((u16)buffer[0]<<8)|buffer[1];
y=((u16)buffer[2]<<8)|buffer[3];
z=((u16)buffer[4]<<8)|buffer[5];
//x=(buffer[1]<<8)|buffer[0];
//y=(buffer[3]<<8)|buffer[2];
//z=(buffer[5]<<8)|buffer[4];
printf("%c%c%c",x,y,z);
}
}
为什么数据读不出来
一周热门 更多>