📄 adt75_2.c
字号:
#include<reg51.h>
#include<instrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SDA=P1^0;
sbit SCL=P1^1;
bit ack;
float t,t0;
uchar i,j;
uchar code p[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar code wei[]={0xC0,0xf9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98,0xFF};
void start()
{ SDA=1;
_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=0;
_nop_();_nop_();
}
void stop()
{ SDA=0;
_nop_();_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SDA=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
}
void send(uchar c)
{ uchar bitcnt;
for(bitcnt=0;bitcnt<8;bitcnt++)
{ if((c<<bitcnt)&0x80) SDA=1;
else SDA=0;
_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=0;
}
_nop_();_nop_();
SDA=1;
_nop_();_nop_();
SCL=1;
_nop_();_nop_();_nop_();
if(SDA==1) ack=0;
else ack=1;
SCL=0;
_nop_();_nop_();
}
uchar receive()
{ uchar retc;
uchar bitcnt;
retc=0;
SDA=1;
for(bitcnt=0;bitcnt<8;bitcnt++)
{ _nop_();
SCL=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=1;
_nop_();_nop_();
retc=retc<<1;
if(SDA==1) retc=retc+1;
_nop_();_nop_();
}
SCL=0;
_nop_();_nop_();
return(retc);
}
void ack(bit a)
{ if(a==0) SDA=0;
else SDA=1;
_nop_();_nop_();_nop_();
SCL=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCL=0;
_nop_();_nop_();
}
void display(uchar temp)
{ uchar dis[4];
temp=t0;
dis[0]=temp/1000;
dis[1]=(temp-dis[0]*1000)/100;
dis[2]=(temp-dis[0]*1000-dis[1]*100)/10;
dis[3]=temp%10;
for(i=0;i<3;i++)
{ P0=p[i];
j=3-i;
if(j==2) P1=wei[dis[j]]&0x7F;
else P1=wei[dis[j]];
delay();
}
delay();
}
void delay(void)
{
unsigned char m,s;
for(m=20;m>0;m--)
for(s=150;s>0;s--);
}
void main()
{ uchar t1,t2;
start();
send(0x91);
t1=receive();
ack(0);
t2=receive();
ack(1);
stop();
t=t1+t2;
t=t>>4;
t0=t*10;
display(t0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -