i2c_rxd_txd.c
来自「这是适用I/O口模拟的 I2C 程序」· C语言 代码 · 共 108 行
C
108 行
/*************************** Function Message ****************************
** Funtion Name : I2C_Txd
** Arguments: BYTE bComOrder: the communication order
** Return Value: return the communication status
** Restrictions: none
** created by: ZhangBin
** Last modified: 12/2/04 (MM/DD/YY)
** Description: the main program of the EOS System
***************************************************************************/
void I2C_Txd(BYTE data)
{
BYTE i = 8;
do
{
if(data & 0x80)
SDA_DIR_IN(); //SDA = 1;
else
SDA_DIR_OUT(); //SDA = 0;
I2C_DELAY();
SCL_DIR_IN(); //SCL = 1;
I2C_DELAY();
SCL_DIR_OUT(); //SCL = 0;
I2C_DELAY();
data <<= 1;
}while(--i);
SDA_DIR = 1;
SCL_DIR_IN(); //SCL = 1;
if(!SDA)
I2C_DELAY(); //this delay must be added
SCL_DIR_OUT(); //SCL = 0;
SDA_DIR = 0;
I2C_DELAY();
};
/*************************** Function Message ****************************
** Funtion Name : I2C_Rxd
** Arguments: BYTE bComOrder: the communication order
** Return Value: return the communication status
** Restrictions: none
** created by: ZhangBin
** Last modified: 12/2/04 (MM/DD/YY)
** Description: the main program of the EOS System
***************************************************************************/
BYTE I2C_Rxd(void)
{
BYTE i = 8;
BYTE data = 0;
SDA_DIR = 1; //bus free;
do{
data <<= 1;
SCL_DIR_IN(); //SCL = 1;
I2C_DELAY();
data |= SDA;
SCL_DIR_OUT(); //SCL = 0;
I2C_DELAY();
}while(--i);
SDA_DIR = 0;
return data;
};
/*************************** Function Message ****************************
** Funtion Name : I2C_ACK
** Arguments: BYTE bComOrder: the communication order
** Return Value: return the communication status
** Restrictions: none
** created by: ZhangBin
** Last modified: 12/2/04 (MM/DD/YY)
** Description: the main program of the EOS System
***************************************************************************/
void I2C_ACK(void)
{
SDA_DIR_OUT(); //SDA = 0;
SCL_DIR_IN(); //SCL = 1;
I2C_DELAY();
SCL_DIR_OUT(); //SCL = 0;
};
/*************************** Function Message ****************************
** Funtion Name : I2C_ACK
** Arguments: BYTE bComOrder: the communication order
** Return Value: return the communication status
** Restrictions: none
** created by: ZhangBin
** Last modified: 12/2/04 (MM/DD/YY)
** Description: the main program of the EOS System
***************************************************************************/
void I2C_NACK(void)
{
SDA_DIR_IN(); //SDA = 1;
SCL_DIR_IN(); //SCL = 1;
I2C_DELAY();
SCL_DIR_OUT(); //SCL = 0;
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?