i2c总线地址问题
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#include<intrins.h>
sbit SCL=P2^1;
sbit SDA=P2^0;
bit write=0;
sbit shi=P1^1;
sbit ge=P1^0;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char m1;
unsigned int m;
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++);
}
void delaynms(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
void start()
{
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}
void stop()
{
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0;
SCL=0;
}
bit Ask()
{
bit ack_bit;
SDA=1;
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit=SDA;
SCL=0;
return ack_bit;
}
unsigned char ReadData()
{
unsigned char i;
unsigned char x;
for(i=0;i<8;i++)
{
SCL=1;
x<<=1;
x|=(unsigned char)SDA;
SCL=0;
}
return(x);
}
void WriteCurrent(unsigned char y)
{
unsigned char i;
for(i=0;i<8;i++)
{
SDA=(bit)(y&0x80);
_nop_();
SCL=1;
_nop_();
_nop_();
SCL=0;
y<<=1;
}
}
void WriteSet(unsigned char add,unsigned char dat)
{
start();
WriteCurrent(0xa0);
Ask();
WriteCurrent(add);
Ask();
WriteCurrent(dat);
Ask();
stop();
delaynms(4);
}
unsigned char ReadCurrent()
{
unsigned char x;
start();
WriteCurrent(0xa1);
Ask();
x=ReadData();
stop();
return x;
}
unsigned char ReadSet(unsigned char setadd)
{
start();
WriteCurrent(0xa0);
Ask();
WriteCurrent(setadd);
Ask();
return(ReadCurrent());
}
void display()
{
P0=table[m/10];
shi=0;
delaynms(2);
P0=table[m%10];
ge=0;
delaynms(2);
}
main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TH1=(65536-50000)%256;
ET0=1;
EA=1;
SDA=1;
SCL=1;
m=ReadSet(2);
TR0=1;
while(1);
{
display();
if(write==1)
{
write=0;
WriteSet(2,m);
}
}
}
void t0() interrupt 1
{
TH0=(65536-50000)/256;
TH1=(65536-50000)%256;
m1++;
if(m1==20)
{
m1=0;
m++;
write=1;
if(m==100) m=0;
}
}
我想问的是为什么 WriteSet(2,m);入口地址是2,这个数字是不是可以随便给的?
#include<intrins.h>
sbit SCL=P2^1;
sbit SDA=P2^0;
bit write=0;
sbit shi=P1^1;
sbit ge=P1^0;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char m1;
unsigned int m;
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++);
}
void delaynms(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
void start()
{
SDA=1;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL=0;
}
void stop()
{
SDA=0;
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA=0;
SCL=0;
}
bit Ask()
{
bit ack_bit;
SDA=1;
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ack_bit=SDA;
SCL=0;
return ack_bit;
}
unsigned char ReadData()
{
unsigned char i;
unsigned char x;
for(i=0;i<8;i++)
{
SCL=1;
x<<=1;
x|=(unsigned char)SDA;
SCL=0;
}
return(x);
}
void WriteCurrent(unsigned char y)
{
unsigned char i;
for(i=0;i<8;i++)
{
SDA=(bit)(y&0x80);
_nop_();
SCL=1;
_nop_();
_nop_();
SCL=0;
y<<=1;
}
}
void WriteSet(unsigned char add,unsigned char dat)
{
start();
WriteCurrent(0xa0);
Ask();
WriteCurrent(add);
Ask();
WriteCurrent(dat);
Ask();
stop();
delaynms(4);
}
unsigned char ReadCurrent()
{
unsigned char x;
start();
WriteCurrent(0xa1);
Ask();
x=ReadData();
stop();
return x;
}
unsigned char ReadSet(unsigned char setadd)
{
start();
WriteCurrent(0xa0);
Ask();
WriteCurrent(setadd);
Ask();
return(ReadCurrent());
}
void display()
{
P0=table[m/10];
shi=0;
delaynms(2);
P0=table[m%10];
ge=0;
delaynms(2);
}
main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TH1=(65536-50000)%256;
ET0=1;
EA=1;
SDA=1;
SCL=1;
m=ReadSet(2);
TR0=1;
while(1);
{
display();
if(write==1)
{
write=0;
WriteSet(2,m);
}
}
}
void t0() interrupt 1
{
TH0=(65536-50000)/256;
TH1=(65536-50000)%256;
m1++;
if(m1==20)
{
m1=0;
m++;
write=1;
if(m==100) m=0;
}
}
我想问的是为什么 WriteSet(2,m);入口地址是2,这个数字是不是可以随便给的?