i2c.c
来自「调频收音机芯片5763的驱动代码,C语言编写,直接移植即可,代码已经验证OK」· C语言 代码 · 共 92 行
C
92 行
/*-------------------------------------------------------
Property of Synergy Digital Technology Ltd.
Question/comment : chifai.cheung@synergytech.cn
Complier : Keil uvision C-51
-------------------------------------------------------*/
#include "i2c.h"
char i2c_start()
{
SDA = 1;
SCL = 1;
if (!SCL) return (1); // if SCL is busy, return error
if (!SDA) return (1); // if SDA is busy, return error
SDA = 0;
SCL = 0;
return (0); // return OK
}
void i2c_stop()
{
SDA = 0;
SCL = 1;
SDA = 1;
}
unsigned char i2c_out(unsigned char DATA)
{
unsigned i;
for (i=0; i<8; i++)
{
if (DATA & 0x80) SDA = 1;
else SDA = 0;
SCL = 1;
DATA <<= 1;
SCL = 0;
}
SCL = 1;
i = SDA;
SCL = 0;
return (i); // 0 - ack, 1 - nack
}
unsigned char i2c_in(void)
{
unsigned char i, j;
SDA = 1;
for (i=0; i<8; i++)
{
SCL = 1;
j <<= 1;
if (SDA) j |= 0x01;
SCL = 0;
}
return j; // return received DATA
}
void i2c_ack(void)
{
SDA = 0; // acknowledgement
SCL = 1;
SCL = 0;
}
void i2c_nak(void)
{
SDA = 1; // nagative acknowledgement
SCL = 1;
SCL = 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?