//IIC??? //addr:???? //reg:????? //len:???? //buf:??? //???:0,?? // ??,???? u8 MPU_Write_Len(u8 addr,u8 reg,u8 len,u8 *buf) { u8 i; MPU_IIC_Start(); MPU_IIC_Send_Byte((addr<<1)|0);//??????+??? if(MPU_IIC_Wait_Ack()) //???? { MPU_IIC_Stop(); return 1; } MPU_IIC_Send_Byte(reg); //?????? MPU_IIC_Wait_Ack(); //???? for(i=0;i<len;i++) { MPU_IIC_Send_Byte(buf[i]); //???? if(MPU_IIC_Wait_Ack()) //??ACK { MPU_IIC_Stop(); return 1; } } MPU_IIC_Stop(); return 0; }
//IIC????? //reg:????? //data:?? //???:0,?? // ??,???? u8 MPU_Write_Byte(u8 reg,u8 data) { MPU_IIC_Start(); MPU_IIC_Send_Byte((MPU_ADDR<<1)|0);//??????+??? if(MPU_IIC_Wait_Ack()) //???? { MPU_IIC_Stop(); return 1; } MPU_IIC_Send_Byte(reg); //?????? MPU_IIC_Wait_Ack(); //???? MPU_IIC_Send_Byte(data);//???? if(MPU_IIC_Wait_Ack()) //??ACK { MPU_IIC_Stop(); return 1; } MPU_IIC_Stop(); return 0; }
//IIC??? //addr:???? //reg:????????? //len:?????? //buf:????????? //???:0,?? // ??,???? u8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf) { MPU_IIC_Start(); MPU_IIC_Send_Byte((addr<<1)|0);//??????+??? if(MPU_IIC_Wait_Ack()) //???? { MPU_IIC_Stop(); return 1; } MPU_IIC_Send_Byte(reg); //?????? MPU_IIC_Wait_Ack(); //???? MPU_IIC_Start(); MPU_IIC_Send_Byte((addr<<1)|1);//??????+??? MPU_IIC_Wait_Ack(); //???? while(len) { if(len==1)*buf=MPU_IIC_Read_Byte(0);//???,??nACK else *buf=MPU_IIC_Read_Byte(1); //???,??ACK len--; buf++; } MPU_IIC_Stop(); //???????? return 0; }
//IIC????? //reg:????? //???:????? u8 MPU_Read_Byte(u8 reg) { u8 res; MPU_IIC_Start(); MPU_IIC_Send_Byte((MPU_ADDR<<1)|0);//??????+??? MPU_IIC_Wait_Ack(); //???? MPU_IIC_Send_Byte(reg); //?????? MPU_IIC_Wait_Ack(); //???? MPU_IIC_Start(); MPU_IIC_Send_Byte((MPU_ADDR<<1)|1);//??????+??? MPU_IIC_Wait_Ack(); //???? res=MPU_IIC_Read_Byte(0);//????,??nACK MPU_IIC_Stop(); //???????? return res; }
//等待应答信号到来 //返回值:1,接收应答失败 // 0,接收应答成功 u8 MPU_IIC_Wait_Ack(void) { u8 ucErrTime=0; MPU_SDA_IN(); //SDA设置为输入 MPU_IIC_SDA=1;MPU_IIC_Delay(); MPU_IIC_SCL=1;MPU_IIC_Delay(); while(MPU_READ_SDA) { ucErrTime++; if(ucErrTime>250) { MPU_IIC_Stop(); return 1; } } MPU_IIC_SCL=0;//时钟输出0 return 0; }