给你一个现成的程序
#include
#define uchar unsigned char
#define uint unsigned int
//=====================================================
sbit DQ=P1^6;
//======================================================
sbit LED1=P1^3;
sbit LED2=P1^2;
sbit FMQ=P1^1;
//======================================================
unsigned int temp, temp1,temp2, xs;
//======================================================
void delay1(uint m)
{
uint i,j;
for(i=m;i>0;i--)
for(j=110;j>0;j--);
}
//======================================================
void delay(uint m)
{
while(m--);
}
//======================================================
void Init_DS18B20() //初始化18b20
{
unsigned char x=0;
DQ = 1;
delay(8);
DQ = 0;
delay(80); //>480us
DQ = 1;
delay(4);
x=DQ; //延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}
//=========================================================
uchar readchar() //读出一字节
{
uchar i=0;
uchar dat = 0;
for (i=0;i<8;i++)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
delay(4);
}
return(dat);
}
//===================================================
writechar(unsigned char dat) //写入一字节
{
unsigned char i=0;
for (i=0; i<8; i++)
{
DQ = 0;
DQ = dat&0x01;
delay(5); //60us~120us
DQ = 1;
dat>>=1; //从最低位到最高位传入
}
}
//===========================================================
readtemperature() //读取温度
{
uchar a=0;
unsigned b=0;
unsigned t=0;
Init_DS18B20();
writechar(0xCC); // 跳过读序号列号的操作/
writechar(0x44); // 启动温度转换
delay(5); // 重要
Init_DS18B20();
writechar(0xCC); //跳过读序号列号的操作
writechar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度/
delay(3);
a=readchar(); //读取温度值低位 /
b=readchar(); //读取温度值高位 /
temp1=b<<4; //高8位中后三位数的值
temp1+=(a&0xf0)>>4; //低8位中的高4位值加上高8位中后三位数的值 temp1室温整数值
temp2=a&0x0f; //小数的值
temp=((b*256+a)>>4); //当前采集温度值除16得 实际温度值 zhenshu
xs=temp2*0.0625*10; //小数位,若为0.5则算为5来显示 xs小数 xiaoshu
//===============================================================================
if(temp>=30.0) //报警模块
FMQ=0;
else
FMQ=1;
if(temp>=25.0 && temp<30.0)
LED1=0,
LED2=1;
else
LED1=1;
}
//================================================================
void wenduxianshi() //数码管显示
{
int a,b,c;
a=(temp/10);
switch(a)
{
case 0 :P2=0x00 |0x04;
P3=0x28;
break;
case 1 :P2=0x18 |0x04;
P3=0xe8;
break;
case 2 :P2=0x10 |0x04;
P3=0x30;
break;
case 3 :P2=0x10 |0x04;
P3=0xa0;
break;
case 4 :P2=0x08 |0x04;
P3=0xe0;
break;
case 5 :P2=0x80 |0x04;
P3=0xa0;
break;
case 6 :P2=0x80 |0x04;
P3=0x20;
break;
case 7 :P2=0x10 |0x04;
P3=0xe8;
break;
case 8 :P2=0x00 |0x04;
P3=0x20;
break;
case 9 :P2=0x00 |0x04;
P3=0xa0;
break;
}
delay(800);
b=(temp%10);
switch(b)
{
case 0 :P2=0x00 |0x20;
P3=0x28 &0xdf;
break;
case 1 :P2=0x18 |0x20;
P3=0xe8 &0xdf;
break;
case 2 :P2=0x10 |0x20;
P3=0x30 &0xdf;
break;
case 3 :P2=0x10 |0x20;
P3=0xa0 &0xdf;
break;
case 4 :P2=0x08 |0x20;
P3=0xe0 &0xdf;
break;
case 5 :P2=0x80 |0x20;
P3=0xa0 &0xdf;
break;
case 6 :P2=0x80 |0x20;
P3=0x20 &0xdf;
break;
case 7 :P2=0x10 |0x20;
P3=0xe8 &0xdf;
break;
case 8 :P2=0x00 |0x20;
P3=0x20 &0xdf;
break;
case 9 :P2=0x00 |0x20;
P3=0xa0 &0xdf;
break;
}
delay(800);
c=(xs%10);
switch(c)
{
case 0 :P2=0x00 |0x40;
P3=0x28;
break;
case 1 :P2=0x18 |0x40;
P3=0xe8;
break;
case 2 :P2=0x10 |0x40;
P3=0x30;
break;
case 3 :P2=0x10 |0x40;
P3=0xa0;
break;
case 4 :P2=0x08 |0x40;
P3=0xe0;
break;
case 5 :P2=0x80 |0x40;
P3=0xa0;
break;
case 6 :P2=0x80 |0x40;
P3=0x20;
break;
case 7 :P2=0x10 |0x40;
P3=0xe8;
break;
case 8 :P2=0x00 |0x40;
P3=0x20;
break;
case 9 :P2=0x00 |0x40;
P3=0xa0;
break;
}
delay(800);
P2=0x80;
P3=0x38 |0x04;
}
//======================================================
main()
{
while(1)
{
readtemperature();
wenduxianshi();
}
}
//=======================================================
现成代码我也有 但是我在意的是我的代码到底哪里出问题了