晶振11.0592M/********************************************************************/
/*******************I2C模仿通讯及AT24C02底层驱动程序****************/
/********************************************************************/
//AT24C02
//delayus: 延时约5us
void delayus()
{
;;
}
//i2c:init I2C总线初始化
void i2c_init()
{
SCL = 1;
delayus();
SDA = 1;
delayus();
}
//lcd:start 起始信号
void start()
{
SCL = 1;
SDA = 1;
delayus();
SDA = 0;
delayus();
}
//i2c:stop 中止信号
void stop()
{
SCL = 1;
SDA = 0;
delayus();
SDA = 1;
delayus();
}
//i2c:ack 从机应对信号
void ack()
{
uchar i = 0;
//在SCL = 1时,读取SDA的数据判忙
SCL = 1;
delayus();
while((SDA == 1)&& i < 250)
i++;
//判忙之后,等候发送数据/指令
SCL = 0;
delayus();
}
//nack: 主机非应对信号
void nack()
{
//主机发送非应对信号1
SCL = 1;
delayus();
SDA = 1;
delayus();
//等候发送数据/指令
SCL = 0;
delayus();
}
//write:byte 写一个字节数据
void write_byte(uchar date)
{ //从最高位发送
uchar i, temp;
temp = date;
for(i = 0; i < 8; i++)
{
temp <<= 1;
SCL = 0;
delayus();
SDA = CY;
delayus();
SCL = 1;
delayus();
SCL = 0;
delayus();
}
//等候从机应对
SCL = 0;
delayus();
SDA = 1;//置1,等候从机应对0
delayus();
}
//read:byte 读一个字节数据
uchar read_byte()
{
uchar i, j, k;
SCL = 0;
delayus();
for(i =0; i < 8; i++)
{
SCL = 1;
delayus();
j = SDA;
k = (k << 1) | j;
delayus();
SCL = 0;
delayus();
}
delayus();
return (k);
}
//write:AT24C02 写AT24C02
void write_c02(uchar address, uchar date)
{
start();
write_byte(write_add);
ack();
write_byte(address);
ack();
write_byte(date);
ack();
stop();
delay(12);
}
//read:AT24C02 读AT24C02
uchar read_c02(uchar add)
{
uchar date = 0;
start();
write_byte(write_add);
ack();
write_byte(add);
ack();
start();
write_byte(read_add);
ack();
date = read_byte();
nack();
return (date);
}
单片机I2C驱动子程序
晶振110592M***************************************************************************************I2C模
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/guandian/254008.html