下面是我的程序
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit dq=P1^0;
uint x,y;
uchar shi,ge,shifen,d;
uchar code num[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delayus(uchar z)
{
while(z--);
}
void delayms(uchar z)
{
for(x=110;x>0;x--)
for(y=z;y>0;y--){};
}
void ds18b20init()
{
uint t;
dq=0;
delayus(110);
dq=1;
delayus(4);
t=dq;
while(t);
}
void ds18b20write(uchar a)
{
uint i;
for(i=0;i<8;i++)
{
dq=0;
delayus(1);
dq=a&0x01;
delayus(11);
dq=1;
a>>=1;
}
}
uchar ds18b20readbit()
{
uchar dat;
dq=0;
_nop_();
dq=1;
dat=dq;
return dat;
}
uchar ds18b20read()
{
uchar i,j,k;
k=0;
for(i=0;i<8;i++)
{
j=ds18b20readbit();
k=(j<<7)|(k>>1);
}
return k;
}
uchar ds18b20tempchange()
{
uchar a,b;
uint c;
ds18b20init();
ds18b20write(0xcc);
ds18b20write(0x44);
//delayms(100);
ds18b20init();
ds18b20write(0xcc);
ds18b20write(0xbe);
//delayms(100);
a=ds18b20read();
b=ds18b20read();
b<<=8;
c=b|a;
d=c*0.0625*10+0.5;
return d;
}
void display()
{
wela=0;
dula=0;
shi=d/100;
ge=d%100/10;
shifen=d%100%10;
while(1)
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=num[shi];
dula=0;
delayms(1);
wela=1;
P0=0xfd;
wela=0;
dula=1;
P0=num[ge];
dula=0;
delayms(1);
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=0x80;
dula=0;
delayms(1);
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=num[shifen];
dula=0;
delayms(1);
}
}
void main()
{
while(1)
{
display();
}
}
此帖出自
小平头技术问答
一周热门 更多>