/*! * @brief SCCB起始信号 * @since v5.0 */ static uint8 SCCB_Start(void) { SDA_H(); SCL_H(); SCCB_DELAY(); SDA_DDR_IN(); if(!SDA_IN()) { SDA_DDR_OUT(); return 0; /* SDA线为低电平则总线忙,退出 */ } SDA_DDR_OUT(); SDA_L(); SCCB_DELAY(); SCL_L(); if(SDA_IN()) { SDA_DDR_OUT(); return 0; /* SDA线为高电平则总线出错,退出 */ } //SDA_DDR_OUT(); //SDA_L(); //SCCB_delay(); return 1; }
/*! * @brief 接收SCCB总线的数据 * @return 接收到的数据 * @since v5.0 */ static int SCCB_ReceiveByte(void) { uint8 i = 8; uint8 ReceiveByte = 0; //SDA_H(); //SCCB_DELAY(); SDA_DDR_IN(); while(i--) { ReceiveByte <<= 1; SCL_L(); SCCB_DELAY(); SCL_H(); SCCB_DELAY(); if(SDA_IN()) { ReceiveByte |= 0x01; } } SDA_DDR_OUT(); SCL_L(); return ReceiveByte; }
/** * \brief I2C Start,Internal function * \retval true */ static bool I2C_Start(void) { SDA_DDR_OUT(); SDA_H(); SCL_H(); I2C_DELAY(); SDA_L(); I2C_DELAY(); SCL_L(); return true; }
/*! * @brief SCCB 等待应答 * @return 应答结果(0表示无应答,1表示有应答) * @since v5.0 */ static int SCCB_WaitAck(void) { SCL_L(); //SDA_H(); SDA_DDR_IN(); SCCB_DELAY(); SCL_H(); SCCB_DELAY(); if(SDA_IN()) //应答为高电平,异常,通信失败 { SDA_DDR_OUT(); SCL_L(); return 0; } SDA_DDR_OUT(); SCL_L(); return 1; }
/******************************************************************** * 函数名:SCCB_WaitAck * 描述 :SCCB 等待应答 * 输入 :无 * 输出 :返回为:=1有ACK,=0无ACK * 注意 :内部调用 ********************************************************************/ static int SCCB_WaitAck(void) { SCL_L(); SCCB_DELAY(); SDA_H(); SCCB_DELAY(); SCL_H(); SDA_DDR_IN(); SCCB_DELAY(); if(SDA_IN()) { SDA_DDR_OUT(); SCL_L(); return 0; } SDA_DDR_OUT(); SCL_L(); return 1; }
/** * \brief I2C Wait Ack,Internal function * \return 应答信号 */ static bool I2C_WaitAck(void) { uint8_t ack; SDA_DDR_IN(); SCL_L(); I2C_DELAY(); SCL_H(); I2C_DELAY(); ack = SDA_IN(); SCL_L(); SDA_DDR_OUT(); return ack; }
/** * \brief I2C 接收一个字节数据,Internal function * \return 待接收的数据(字节) */ static uint8_t I2C_GetByte(void) { uint8_t i,byte; i = 8; byte = 0; SDA_DDR_IN(); while(i--) { SCL_L(); I2C_DELAY(); SCL_H(); I2C_DELAY(); byte = (byte<<1)|(SDA_IN() & 1); } SCL_L(); SDA_DDR_OUT(); return byte; }