求18b20+51单片机的C语言程序

2025年01月07日 06:17
有4个网友回答
网友(1):

#include
unsigned char LedT0;
unsigned char Led1;
unsigned char Led2;
unsigned char Led3;
unsigned char Led4;
sbit P2_4=P2^4;
sbit P2_5=P2^5;
sbit P2_6=P2^6;
sbit P2_7=P2^7;

sbit P3_3=P3^3;
sbit P3_4=P3^4;
sbit P3_5=P3^5;
sbit P3_6=P3^6;
sbit P3_7=P3^7;
sbit BUZZER=P2^3;
sbit P0_5=P0^5;

bit flag=0;

unsigned char Buzz=0;

unsigned int h_temp=399,l_temp=199;

unsigned int time=0;
const unsigned char table[18]=

//{0x28,0xEB,0x32,0xA2,0xE1,0xA4,0x24,0xEA,0x20,0xA0,0x60,0x25,0x3c,0x23,0x34,0x74,0xF7,0xD7,0xFF};
{0xD7,0x14,0xCD,0x5D,0x1E,0x5B,0xDB,0x15,0xDF,0x5F,0x9F,0xDA,0xC3,0xDC,0xCB,0x8B,0x0B,0x00};

sbit DQ =P2^2;

void delay(unsigned int i)
{
while(i--);

}

Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1;
delay(8);
DQ = 0;
delay(80);
DQ = 1;
delay(14);
x=DQ;
delay(20);
}

ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
EA=0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
delay(4);
}
EA=1;
return(dat);
}

WriteOneChar(unsigned char dat)
{
unsigned char i=0;
EA=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(5);
DQ = 1;
dat>>=1;
}
EA=1;
delay(4);
}

ReadTemperature(void)
{
union uni_a{
unsigned int temp;
unsigned char a[2];
} uni;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
uni.a[1]=ReadOneChar();
uni.a[0]=ReadOneChar();
if(uni.temp<0x0fff)
{
flag=0;
}
else
{
flag=1;
uni.temp=~uni.temp+1;
}
uni.temp=uni.temp*0.625;
return(uni.temp);
}

void int1() interrupt 1 using 1
{
TF0 = 0;
TH0 = 0xF6;
TL0 = 0x3D;
time++;
if(Buzz==1)
{
if(time%50==0)
{
BUZZER=!BUZZER;
}
}
else if(Buzz==2)
{
if(time%70==0)
{
BUZZER=!BUZZER;
}
}
P2 |= 0xF0;

switch(LedT0)
{
case 0:
P0 = table[Led1];
P2_4 = 0;
LedT0 = 1;
break;
case 1:
P0 = table[Led2];
P2_5 = 0;
LedT0 = 2;
break;
case 2:
P0 = table[Led3];
P2_6 = 0;
P0_5=1;
LedT0 = 3;
break;
case 3:
P0 = table[Led4];
P2_7 = 0;
LedT0 = 0;
break;
default:
LedT0 = 0;
break;
}
}
//
void delay1(int i)
{
int j,k;
for(j=0;j {
for(k=0;k<125;k++);
}

}
//
void shezhi()
{
unsigned int a;
unsigned char Ledn;
for(;;)
{
if(P3_3==0)
{
for (a=0; a<1000; a++);

if(!P3_3)
{
if(Led1<9)

{
Led1=9;
}
Led1++;
if(Led1>15)
{
Led1=10;
}
if(Led1==10)

{

Led2=h_temp/100;
Led3=h_temp%100/10;
Led4=h_temp%10;
}
else if(Led1==11)
{
Led2=l_temp/100;
Led3=l_temp%100/10;
Led4=l_temp%10;
}
else
{
Led2=0;
Led3=0;
Led4=0;
}
while(!P3_3);
}
}
if(P3_4==0)
{
for (a=0; a<1000; a++);

if(!P3_4)
{
if(Ledn==0)
{
Led2+=1;
if(Led2==10)
Led2=0;
}
else if(Ledn==1)
{
Led3+=1;
if(Led3==10)
Led3=0;
}
else
{
Led4+=1;
if(Led4==10)
Led4=0;
}
while(!P3_4);
}
}
else if(!P3_5)
{
for (a=0; a<1000; a++);

if(!P3_5)
{
if(Ledn==0)
{
Ledn=1;
}
else if(Ledn==1)
{
Ledn=2;
}
else
{
Ledn=0;
}
while(!P3_5);
}
}
else if(!P3_6)
{
for (a=0; a<1000; a++);

if(!P3_6)
{
if(Led1==10)
{
h_temp= Led4+Led3*10+Led2*100;

}
else if(Led1==11)
{
l_temp= Led4+Led3*10+Led2*100;

}
else
{

}
break;
while(!P3_6);
}
}
else if(!P3_7)
{
for (a=0; a<1000; a++);

if(!P3_7)
{
break;
while(!P3_7);
}
}

}

}
void main(void)
{
unsigned int temp,a;
unsigned char Ledn;
TMOD = 0x01;
TH0 = 0xF6;
TL0 = 0x3D;
TR0 = 1;
ET0 = 1;
TF0 = 0;
EA = 1;

LedT0 = 0;

Led1 = 0;
Led2 = 0;
Led3 = 0;
Led4 = 0;
Ledn = 0;
P1=0Xfe;
for(a=0;a<8;a++)
{
P1<<=1;
delay1(200);
}
P1=0xff;
for(;;)
{
if(time>350)
{
time=0;
temp=ReadTemperature();
if(!flag)
{
Led1 = temp/1000;
Led2 = temp%1000/100;
Led3 = temp%100/10;
Led4 = temp%10;
}
else
{
Led1 = 17;
Led2 = temp%1000/100;
Led3 = temp%100/10;
Led4 = temp%10;

}
}

if(P3_3==0)
{
for (a=0; a<1000; a++);

if(!P3_3)
{
shezhi();
// while(!P3_3);
}
}
if(temp>=h_temp)
{
Buzz=1;
P1=0XF0;

}
else if(temp<=l_temp)
{
Buzz=2;
P1=0X0F;

}
else if(Buzz)
{
Buzz=0;
BUZZER=1;
P1=0XFF;
}

}

}

网友(2):

网上可以搜索到得,偶原来找到过

网友(3):

你可以到我博客上面找,只是你要把DS18B20驱动程序和1602驱动程序组合起来。或者要的话给我留言,我给你弄,谢谢!
http://hi.baidu.com/chinasndw

网友(4):

这个我做过,调试通过了