📄 i2c.c
字号:
// ---------------------- Software I2C bus implementation ----------------------
/* FileName: i2c.c
Version : 0.1
Last Updated: 2007-3-2
Description:
This library contains the necessary functions for Simulating SPI bus.
Program Coder: Seeseawe
*/
// Start an I2C communication
void I2C_Start(void)
{
I2C_SDA = 1;
I2C_SCL = 1;
//delay4us();
I2C_SDA = 0;
//delay4us();
I2C_SCL = 0;
}
// Stop an I2C Communication
void I2C_Stop(void)
{
I2C_SDA = 0;
I2C_SCL = 1;
//delay4us();
I2C_SDA = 1;
//delay4us();
}
// Send a byte to I2C Bus
void I2C_SendByte(uint8 dByte)
{
uint8 i;
for (i = 0; i < 8; i++)
{
I2C_SDA = (dByte<<i) & 0x80;
I2C_SCL = 1;
//delay4us();
I2C_SCL = 0;
}
// Release the bus and clock out ACK signal from Slave
I2C_SDA = 1;
I2C_SCL = 1;
//delay4us();
/*
if (I2C_SDA == 1)
ACK = 0;
else
ACK = 1;
*/
I2C_SCL = 0;
}
// Read a byte from the slave
uint8 I2C_ReadByte(void)
{
uint8 dByte;
uint8 i;
for ( i = 0; i < 8; i++ )
{
I2C_SDA = 1; // Release I2C_SDA
I2C_SCL = 1; // Release I2C_SCL
while(I2C_SCL == 0); // Synchronize clock
delay4us();
dByte <<= 1; // Shift left the result
if (I2C_SDA)
dByte |= 0x01; // Set actual I2C_SDA state to LSB
I2C_SCL = 0; // Force a clock cycle
delay4us();
}
return dByte;
}
// Send an ACK or NACK signal to the slave
void I2C_SendAck(bit ACK)
{
I2C_SDA = ACK;
I2C_SCL = 1;
while(I2C_SCL == 0); // Synchronize clock
delay4us();
I2C_SCL = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -