// Send one byte via I2C(check ACK). BOOL I2C_WriteByte(BYTE Dat, BYTE I2cDevice) { BYTE i = 8; static BYTE j = 1; //DBG(("(%-.2BX)", Dat)); SetOutputSDA(I2cDevice); ClrSCL(I2cDevice); while(i--) { if(Dat & 0x80) //MSB output first { SetSDA(I2cDevice); } else { ClrSDA(I2cDevice); } Dat <<= 1; SetSCL(I2cDevice); KtIICDelay(); ClrSCL(I2cDevice); } return I2C_ChkAck(I2cDevice); }
/** * @brief This function generates I2C start timing. * @param None * @return None */ void I2cStart(void* I2cMasterHandle) { SetSDA(I2cMasterHandle); SetSCL(I2cMasterHandle); SetOutputSCL(I2cMasterHandle); SetOutputSDA(I2cMasterHandle); ClrSDA(I2cMasterHandle); ClrSCL(I2cMasterHandle); }
/** * @brief This function generates I2C stop timing. * @param None * @return None */ void I2cStop(void* I2cMasterHandle) { SetOutputSCL(I2cMasterHandle); SetOutputSDA(I2cMasterHandle); ClrSDA(I2cMasterHandle); SetSCL(I2cMasterHandle); SetSDA(I2cMasterHandle); SetInputSDA(I2cMasterHandle); }
// send STOP signal. VOID I2C_Stop(BYTE I2cDevice) { // DBG(("I2C_Stop()\n")); SetOutputSCL(I2cDevice); SetOutputSDA(I2cDevice); ClrSDA(I2cDevice); KtIICDelay(); SetSCL(I2cDevice); SetSDA(I2cDevice); SetInputSDA(I2cDevice); }
// send START signal. VOID I2C_Start(BYTE I2cDevice) { SetSDA(I2cDevice); SetSCL(I2cDevice); SetOutputSCL(I2cDevice); SetOutputSDA(I2cDevice); SetSDA(I2cDevice); SetSCL(I2cDevice); KtIICDelay(); ClrSDA(I2cDevice); ClrSCL(I2cDevice); }
// Receive one byte via I2C. BYTE I2C_ReadByte(BYTE I2cDevice) { BYTE i = 8; BYTE Dat = 0; SetInputSDA(I2cDevice); while(i--) { Dat <<= 1; if(GetSDA(I2cDevice)) { Dat |= 0x01; } SetSCL(I2cDevice); KtIICDelay(); ClrSCL(I2cDevice); } SetOutputSDA(I2cDevice); return Dat; }
/** * @brief This function recieve one byte from I2C slave. * @param none * @return Data received from I2C slave */ uint8_t I2cReadByte(void* I2cMasterHandle) { uint8_t i = 8; uint8_t Dat = 0; SetInputSDA(I2cMasterHandle); while(i--) { SetSCL(I2cMasterHandle); Dat <<= 1; if(GetSDA(I2cMasterHandle)) { Dat |= 0x01; } ClrSCL(I2cMasterHandle); } SetOutputSDA(I2cMasterHandle); return Dat; }
/** * @brief This function send one byte to I2C slave. * @param Val: data to be sent * @return * @arg True: Receive ACK from I2C slave * @arg False: Not receive ACK from I2C slave */ bool I2cWriteByte(void* I2cMasterHandle, uint8_t Val) { uint8_t i = 8; SetOutputSDA(I2cMasterHandle); ClrSCL(I2cMasterHandle); while(i--) { if(Val & 0x80) /* MSB output first */ { SetSDA(I2cMasterHandle); } else { ClrSDA(I2cMasterHandle); } Val <<= 1; SetSCL(I2cMasterHandle); ClrSCL(I2cMasterHandle); } return I2cChkAck(I2cMasterHandle); }
BOOL I2C_WriteDWord(DWORD Dat, BYTE I2cDevice) { BYTE i = sizeof(DWORD); DBG(("I2C_WriteByte(%X)\n", Dat)); SetOutputSDA(I2cDevice); ClrSCL(I2cDevice); while(i--) { if(Dat & 0x80000000) //MSB output first { SetSDA(I2cDevice); } else { ClrSDA(I2cDevice); } Dat <<= 1; SetSCL(I2cDevice); KtIICDelay(); ClrSCL(I2cDevice); } return I2C_ChkAck(I2cDevice); }