📄 24c02 ic总线.c
字号:
#include"reg51.h"
#include"intrins.h"
#define uint unsigned int
#define uchar unsigned char
#define writedeviceaddress 0xa0
sbit scl=P3^7;
sbit sda=P3^6;
sbit P10=P1^0;
bit nackflag;
void delay(uint t)
{
uchar i;
for(;t>0;t--)
for(i=0;i<124;i++)
{;}
}
void start()
{
sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();
sda=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
}
void stop()
{
sda=0;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();
sda=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
}
void testack()
{
sda=1;
scl=1;
nackflag=0;
if(sda==1) nackflag=1;
scl=0;
}
void send8bit(uchar input)
{
uchar temp,i=8;
temp=input;
for(i=8;i>0;i--)
{
sda=temp&0x80;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
temp<<=1;
}
}
void sendbyte(uchar address,uchar ch)
{
start();
send8bit(0xa0);
testack();
send8bit(address);
testack();
send8bit(ch);
testack();
stop();
delay(10);
}
uchar Read(void) //读取字节程序
{
uint Bitcount=8,temp=0;
sda=1;
do {
scl=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();
if(sda)
temp=temp|0x01;
else
temp=temp&0xfe;
if(Bitcount-1)
temp=temp<<1;
Bitcount--;
}while(Bitcount);
return temp;
}
void ReadFromRom(uchar Add,uchar ch) //读以Add为起始地址的Num个数读取到数组Data[]中
{
uchar i;
for(i=0;i<Num;i++)
{
Start();
Send(0xa0);
testack();
Send(Add+i);
testack();
Start();
Send(0xa1);
testack();
*(Data+i)=Read();
scl=0;
testack();
Stop();
}
}
void main()
{
uchar i;
while(1)
{
for(i=0;i<0x20;i++)
{
sendbyte(i,i+1);
delay(5);
}
delay(1000);
P10=~P10;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -