📄 iic.c.bak
字号:
#include <reg52.h>
#include "delay.h"
#include "iic.h"
/*启动IIC设备*/
void start(void)
{
DelayMs(5);
SDA = 1 ;
Delay(5);
SCL = 1 ;
Delay(5);
SDA = 0 ;
Delay(5);
SCL = 0 ;
Delay(5);
}
/*停止IIC设备*/
void stop(void)
{
Delay(5);
SCL = 0 ;
Delay(5);
SDA = 0 ;
Delay(5);
SCL = 1 ;
Delay(5);
SDA = 1 ;
Delay(5);
DelayMs(15);
}
/*检测ack*/
bit testack(void)
{
bit AckFlag;
Delay(5);
SDA = 1;
Delay(5);
SCL = 1;
Delay(5);
AckFlag = SDA;
SCL = 0;
Delay(5);
return(AckFlag);
}
/*发送应答信号*/
void sendack(void)
{
Delay(5);
SDA = 0;
Delay(5);
SCL = 1;
Delay(5);
SCL = 0;
Delay(5);
SDA = 1;
Delay(5);
}
/*发送非应答信号*/
void sendNack(void)
{
Delay(5);
SDA = 1 ;
Delay(5);
SCL = 1 ;
Delay(5);
SCL = 0 ;
Delay(5);
}
/*写1个字节的数据*/
void write8bit(unsigned char T_Data)
{
unsigned char i ;
for(i = 8; i != 0; i--)
{
SDA = (bit)(T_Data & 0x80);
Delay(5);
SCL = 1 ;
Delay(5);
SCL = 0 ;
Delay(5);
T_Data = T_Data << 1 ;
Delay(5);
}
}
/*读1个字节的数据*/
unsigned char read8bit(void)
{
unsigned char i = 0 ;
unsigned char R_Data = 0x00 ;
for(i = 8; i != 0; i--)
{
SDA = 1;
Delay(5);
SCL = 0 ;
Delay(5);
Delay(5);
SCL = 1 ;
R_Data = R_Data << 1 ;
R_Data = R_Data |((unsigned char)(SDA)) ;
SCL = 0 ;
Delay(5);
}
return(R_Data);
}
/*写1个字节的数据到指定的地址*/
void write_I2C(unsigned char RomAddress,unsigned char T_Data)
{
start() ;
write8bit(WrDevAddr) ;
testack() ;
write8bit(RomAddress) ;
testack() ;
write8bit(T_Data) ;
testack() ;
stop() ;
}
/*读1个字节的数据*/
unsigned char read_I2C(unsigned char RomAddress)
{
unsigned char R_Data = 0x00 ;
start() ;
write8bit(WrDevAddr) ;
testack() ;
write8bit(RomAddress) ;
testack() ;
start() ;
write8bit(RdDevAddr) ;
testack() ;
R_Data = read8bit() ;
sendNack() ;
stop() ;
return(R_Data) ;
}
/*指定的地址写页-8字节数据*/
void WriteOnePage (unsigned char addr,unsigned char *thedata) //写入一页8个字节到指定开始地址,可以自动翻页
{
unsigned char i; //计数器
start(); //开始总线
write8bit(WrDevAddr); //发送控制数据
testack(); //检查应答信息
write8bit(addr); //写入地址
testack();
for(i=0;i<8;i++) //循环写入第一页的数据
{
write8bit(*thedata); //写入数据
testack();
thedata++; //数据指针加1
}
stop(); //停止总线
}
/*指定的地址写N(N<=8)字节数据*/
void WriteNByte (unsigned char addr,unsigned char *thedata,unsigned char n) //写入N个字到指定开始地址
{
unsigned char i; //计数器
start(); //开始总线
write8bit(WrDevAddr); //发送控制数据
testack(); //检查应答信息
write8bit(addr); //写入地址
testack();
for(i=0;i<n;i++) //循环写入第一页的数据
{
write8bit(*thedata); //写入数据
testack();
thedata++; //数据指针加1
}
stop(); //停止总线
}
/*指定地址,页写n个数据*/
void WritePages(unsigned char addr,unsigned char *thedata,unsigned char n)
{
unsigned char page=n/8;
unsigned char leave=n%8;
unsigned char i;
for(i=0;i<page;i++)
{
WriteOnePage((addr+8*i),thedata);
thedata=thedata+8;
}
WriteNByte((addr+8*page),thedata,leave);
}
/*指定地址,读取8字节,页读*/
void ReadoOnePage(unsigned char addr,unsigned char *thedata) //连续读取8个字节 页
{
unsigned char i;
start();
write8bit(WrDevAddr);
testack();
write8bit(addr);
testack();
start();
write8bit(RdDevAddr) ;
testack();
for(i=0;i<7;i++) //8字节
{
*thedata=read8bit(); //读取数据
sendack(); //发送应答,表示继续读取
thedata++; //数据指针加1
}
*thedata=read8bit(); //读取最后一个数据
sendNack(); //发送非应答,结束读取
stop(); //停止总线
}
/*连续读取N个字节*/
void ReadNByte (unsigned char addr,unsigned char *thedata,unsigned char n)
{
unsigned char i;
start();
write8bit(WrDevAddr);
testack();
write8bit(addr);
testack();
start();
write8bit(RdDevAddr) ;
testack();
for(i=0;i<n-1;i++) //8字节
{
*thedata=read8bit(); //读取数据
sendack(); //发送应答,表示继续读取
thedata++; //数据指针加1
}
*thedata=read8bit(); //读取最后一个数据
sendNack(); //发送非应答,结束读取
stop();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -