/********单字节读取*****************************************/ uint8_t I2C_ReadByte(uint8_t SlaveAddress, uint8_t REG_Address) { char REG_data; IIC_Start(); //起始信号 IIC_SendByte(SlaveAddress); //发送设备地址+写信号 IIC_SendByte(REG_Address); //发送存储单元地址,//从0开始 IIC_Start(); //起始信号 IIC_SendByte(SlaveAddress+1); //发送设备地址+读信号 REG_data = IIC_RecvByte(); //读出寄存器数据 IIC_SendNACK(); IIC_Stop(); //停止信号 return REG_data; }
u8 Single_Read_IIC(u8 SlaveAddress, u8 REG_Address) { u8 REG_data; IIC_Start(); IIC_SendByte(SlaveAddress); IIC_SendByte(REG_Address); IIC_Start(); IIC_SendByte(SlaveAddress+1); REG_data = IIC_RecvByte(); IIC_SendACK(); IIC_Stop(); return REG_data; }
/* * Function: burst read bytes */ void I2C_ReadBytes(uint8_t SlaveAddress, uint8_t REG_Address, uint8_t *buf, uint8_t num) { int i = num; IIC_Start(); //起始信号 IIC_SendByte(SlaveAddress); //发送设备地址+写信号 IIC_SendByte(REG_Address); //发送存储单元地址,//从0开始 IIC_Start(); //起始信号 IIC_SendByte(SlaveAddress+1); //发送设备地址+读信号 for(i = 0; i < num; i++) { buf[i] = IIC_RecvByte(); // if it is the last byte to be read, then it should end up with an IIC_SendNACK() if (i == num - 1) { IIC_SendNACK(); break; // else it needs an acknowledgement } else IIC_SendACK(); } IIC_Stop(); return; }