#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
sbit duan=P2^6;
sbit wei=P2^7 ;
sbit SDA=P2^0;
sbit SCL=P2^1;
bit flag;
int index=0;
uchar s;
uchar code duanS[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code weiS[]={0xef,0xdf,0xbf};
void delay5us(){
_nop_();
_nop_();
_nop_();
_nop_();
}
void i2cStart(){
SCL=1;
SDA=0; //高变低【有效】
SDA=1;
delay5us();
SDA=0;
delay5us();
SCL=0;
}
void i2cEnd(){
SCL=0;
SDA=0;
SCL=1;
delay5us();
SDA=1;
delay5us();
}
bit i2cAck(){ //我觉得可以用来阻塞或者反复传操作
SCL=1;
delay5us();
if(SDA) {
SCL=0;
return(1);
}
else {
SCL=0;
return(0);
}
}
void i2cSendAck(bit b)
{
SCL=0;
if(b)
SDA=1;
else
SDA=0;
SCL=1;
delay5us();
SCL=0;
SDA=1;
}
void send(uchar DATA){
uchar i;
for(i=0;i<8;i++){
SCL=0;
if (DATA & 0x80)
SDA=1;
else
SDA=0;
SCL=1;
delay5us();
DATA<<=1;
}
SCL=0;
SDA=1;
}
void i2cSend(uchar dataddr ,uchar dat){
i2cStart();
send(0xA0);
if(i2cAck())
flag=1;
else flag=0;
send(dataddr);
if(i2cAck())
flag=1;
else flag=0;
send(dat);
if(i2cAck())
flag=1;
else flag=0;
i2cEnd();
}
uchar accpect(){
uchar i,index1;
for(i=0;i<8;i++){
index1<<=1;
SCL=0;
SCL=1;
delay5us();
if(SDA)
index1|=0X01;
}
return index1;
}
uchar i2cAccp(uchar dataddr){
uchar index1;
i2cStart();
send(0xA0);
if(i2cAck())
flag=1;
else flag=0;
send(dataddr);
i2cAck();
i2cStart();
send(0xA1);
if(i2cAck())
flag=1;
else
flag=0;
// for(i=0;i<8;i++){
// index1<<=1;
// SCL=0;
// SCL=1;
// delay5us();
// if(SDA)
// index1|=0X01;
//}
index1=accpect();
i2cSendAck(1);
i2cEnd();
return index1;
}
void initP3(){
EA=1;
ET1=1;
TR1=1;
TMOD=0x10;
TH1=60927/255;
TL1=60927%255;
}
void display(uchar i){
P0=weiS[index];
switch(index) {
case 0: wei=1;duan=0;wei=0;duan=1;P0=duanS[i/100];
break;
case 1: wei=1;duan=0;wei=0;duan=1;P0=duanS[i/10%10];
break;
case 2: wei=1;duan=0;wei=0;duan=1;P0=duanS[i%10];
break;
}
index++;
if(index>=3)
index=0;
}
void main(){
uchar i=1000;
initP3();
i2cSend(3,236);
while(i--);
s=i2cAccp(3);
while(1);
}
void interP3() interrupt 3{
TH1=60927/255;
TL1=60927%255;
display(s);
}
|