这是我以前的代码,你看看,另外看看你的跳线帽插上了没有。。。
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop() _nop_()
bit flag;
sbit SDA=P3^7;
sbit DA=P2^2;sbit DB=P2^3;sbit DC=P2^4;
void delay(ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x80,0x40};
uchar initDS13B20()
{
uchar i;
SDA=0;i=70;
while(i--);
SDA=1;i=0;
while(SDA)
{
i++;
delay(1);
if(i>50)
return 0;
}
return 1;
}
void DS13B20write(uchar com)
{
uchar i,j;
for(i=0;i<8;i++)
{
SDA=0;j++;
SDA=(com&0x01);
j=6;while(j--);
SDA=1;
com=(com>>1);
}
}
uchar DS13B20read()
{
uchar i,j,di=0,dat=0;
for(i=0;i<8;i++)
{
SDA=0;j++;
SDA=1;j++;j++;
di=SDA;
dat=((dat>>1)|(di<<7));
j=4;
while(j--);
}
return dat;
}
void change()
{
initDS13B20();
delay(1);
DS13B20write(0xcc);
DS13B20write(0x44);
}
void getvalue()
{
initDS13B20();
delay(1);
DS13B20write(0xcc);
DS13B20write(0xbe);
}
int read()
{
uchar dal=0,dah=0;
int dat=0;
change();
getvalue();
dal=DS13B20read();
dah=DS13B20read();
dat=((dah<<8)|dal);
return dat;
}
float changeC()
{
int a;
float value;
a=read();
if(a>=0)
{
flag=1;
value=a*0.0625;
}
else
{
flag=0;
value=(~(a-1))*0.625;
}
return value;
}
void disp1(float num)
{
uchar a,i,j;
for(i=0;i<6;i++)
{
switch(i)
{
case 0:DA=0;DB=0;DC=0;a=(int)num/10; break;
case 1:DA=1;DB=0;DC=0;a=(int)num%10; break;
case 2:DA=0;DB=1;DC=0;a=16; break;
case 3:DA=1;DB=1;DC=0;a=(int)(num*10)%10; break;
case 4:DA=0;DB=0;DC=1;a=(int)(num*100)%10; break;
case 5:DA=1;DB=0;DC=1;a=(int)(num*1000)%10; break;
case 6:DA=0;DB=1;DC=1;a=(int)(num*10000)%10; break;
}
P0=table[a];
j=50;
while(j--);
P0=0x00;
}
}
void disp0(float num1)
{
uchar a,i,j;
float num;
num=0-num1;
for(i=0;i<7;i++)
{
switch(i)
{
case 0:DA=0;DB=0;DC=0;a=17; break;
case 1:DA=1;DB=0;DC=0;a=(int)num/10; break;
case 2:DA=0;DB=1;DC=0;a=(int)num%10;break;
case 3:DA=1;DB=1;DC=0;a=16; break;
case 4:DA=0;DB=0;DC=1;a=(int)(num*10)%10; break;
case 5:DA=1;DB=0;DC=1;a=(int)(num*100)%10; break;
case 6:DA=0;DB=1;DC=1;a=(int)(num*1000)%10; break;
case 7:DA=1;DB=1;DC=1;a=(int)(num*10000)%10; break;
}
P0=table[a];
j=50;
while(j--);
P0=0x00;
}
}
void main()
{
float value;
while(1)
{
value=changeC();
if(flag==1)
disp1(value);
else disp0(value);
}
} |