#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^7;//DS18B20óëμ¥Ƭ»úᬽó¿ú
sbit RS=P3^0;//LCD1602¿ØÖƶËóëμ¥Ƭ»úμÄᬽó
sbit RW=P3^1;
sbit EN=P3^7;
sbit ST=P0^0;//°′¼ü½ó¿ú
sbit SH=P0^1;
sbit Sjia=P3^2;
sbit Sjian=P3^3;
sbit WBJ=P2^6;//ζ豨¾ˉ½ó¿ú
sbit HBJ=P2^5;//êa¶è±¨¾ˉ½ó¿ú
uchar code str1[]={"Wend:"};
uchar code str2[]={"Shid:"};
uchar data disdata1[6];
uchar data disdata2[4];
uint tvalue;//ζèÖμ
uchar tflag;//ζèÕy¸o±êÖ¾
uint tem0,tem1;
uint temp0,temp1;
uint f=0;
int hhhh=0;
int tsheding=20;//éèÖÃζèÖμ
int hsheding=40;//éèÖÃêa¶èÖμ
void zhongd0() interrupt 0 //ía2¿ÖD¶Ï0 £o¼óéèÖÃ
{
if(ST==1)
{
tsheding++; //ζèÖμ¼ó1
}
if(SH==1)
{
hsheding++; //êa¶èÖμ¼ó1
}
}
void zhongd1() interrupt 2 //ía2¿ÖD¶Ï1 £o¼õéèÖÃ
{
if(ST==1)
{
tsheding--; //ζèÖμ¼õ1
}
if(SH==1)
{
hsheding--; //êa¶èÖμ¼õ1
}
}
void timer0() interrupt 1 //T0¶¨ê±ÖD¶Ï
{
TR0=0;
TR1=0;
TL0=0xB0;//ÖØ×°Öμ,¶¨ê±50000us
TH0=0x3C;
tem0=TL1;//¶áêy
tem1=TH1;
TL1=0x00;//¼ÆêyÆ÷1Çåáã
TH1=0x00;
TR0=1;
TR1=1;
}
void timer1() interrupt 3 //T1¼ÆêyÖD¶Ï
{
TR0=0;
TR1=0;
TL0=0xB0;//ÖØ×°Öμ,¶¨ê±50000us
TH0=0x3C;
TL1=0x00;//¼ÆêyÆ÷1Çåáã
TH1=0x00;
TR0=1;
TR1=1;
}
void init_timer()
{
TMOD=0x51;//0101 0001 ¶¨ê±Æ÷0ÔúÄ£ê½1ÏÂ1¤×÷16붨ê±Æ÷,¶¨ê±·½ê½ ¶¨ê±Æ÷1ÔúÄ£ê½1ÏÂ1¤×÷16λ¼ÆêyÆ÷£¬T1¸oìø±ä¼ó1
TL0=0xB0;//¶¨ê±Æ÷03õÖμ ¶¨ê±50000us
TH0=0x3C;
TL1=0x00;//¶¨ê±Æ÷1Çåáã
TH1=0x00;
TR0=1;//T0¿aê¼¼Æê±
TR1=1;//T1¿aê¼¼Æêy
}
void TPanDuan()//ζè3¬·¶Î§ÅD¶Ï
{
uint k= tvalue/(10);
if(tflag==0)
{
if((k>(tsheding*1.2))||(k<(tsheding*0.8)))
{
WBJ=1;
}
else
{
WBJ=0;
}
}
else
{
if((k>tsheding*(-1.2))||(k<tsheding*(-0.8)))
{
WBJ=1;
}
else
{
WBJ=0;
}
}
}
void delay1ms(uint ms)//Ñóê±1oáÃë
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
void wr_com(uchar com)//1602D′Ö¸áî
{
delay1ms(1);
RS=0;
RW=0;
EN=0;
P1=com;
delay1ms(1);
EN=1;
delay1ms(1);
EN=0;
}
void wr_dat(uchar dat)//1602D′êy¾Y
{
delay1ms(1);
RS=1;
RW=0;
EN=0;
P1=dat;
delay1ms(1);
EN=1;
delay1ms(1);
EN=0;
}
void lcd_init()//16023õê¼»ˉéèÖÃ
{
wr_com(0x38);delay1ms(1); //éèÖÃ16*2ÏÔê¾£¬5*7μãÕó£¬8λêy¾Y½ó¿ú£»2¢Ñóê±
wr_com(0x08);delay1ms(1); //éèÖÃ1ØÏÔê¾£¬1a±ê2»ÏÔê¾£¬1a±ê2»éá˸£»2¢Ñóê±
wr_com(0x01);delay1ms(1); //éèÖÃÏÔê¾ÇåÆ᣻2¢Ñóê±
wr_com(0x06);delay1ms(1); //éèÖÃÏÔê¾1a±êòƶˉλÖã»2¢Ñóê±
wr_com(0x0c);delay1ms(1); //éèÖÃÏÔ꾿a¼°1a±êéèÖã»2¢Ñóê±
}
void display(uchar *p)//ÏÔê¾×ó3ìDò
{
while(*p!=' ')
{
wr_dat(*p);
p++;
delay1ms(1);
}
}
init_play()//16023õê¼»ˉÏÔê¾
{
lcd_init();
wr_com(0x80);
display(str1);
wr_com(0xc0);
display(str2);
}
/**************************ds18203ìDò*************************/
void delay_18B20(uint i)//Ñóê±1΢Ãë
{
while(i--);
}
void ds1820rst()//DS18B20¸′λ
{
uchar x=0;
DQ= 1; //DQÖÃλ
delay_18B20(4); //Ñóê±
DQ = 0; //DQà-μí
delay_18B20(100); //¾«è·Ñóê±′óóú480us
DQ = 1; //à-¸ß
delay_18B20(40);
}
uchar ds1820rd()//DS18B20¶áêy¾Y
{
uchar i=0;
uchar dat=0;
for(i=8;i>0;i--)
{
DQ=0; //¸øÂö3åDÅoÅ
dat>>=1;
DQ = 1; //¸øÂö3åDÅoÅ
if(DQ)
dat|=0x80;
delay_18B20(10);
}
return(dat);
}
void ds1820wr(uchar wdata)//DS18B20D′êy¾Y
{
uchar i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=wdata&0x01;
delay_18B20(10);
DQ=1;
wdata>>=1;
}
}
void read_temp()//¶áè¡Î¶èÖμ2¢×a»»
{
uchar a,b;
ds1820rst();
ds1820wr(0xcc);//ìø1y¶áDòáDoÅ
ds1820wr(0x44);//Æô¶ˉζè×a»»
ds1820rst();
ds1820wr(0xcc);//ìø1y¶áDòáDoÅ
ds1820wr(0xbe);//¶áè¡Î¶è
a=ds1820rd();
b=ds1820rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
if(tvalue<0x0fff)
{
tflag=0;
}
else
{
tvalue=~tvalue+1;
tflag=1;
}
tvalue=tvalue*(0.625);//ζèÖμà©′ó10±¶£¬¾«è·μ½1λD¡êy
}
/*******************************************************************/
void ds1820disp1()//ζèÖμÏÔê¾
{
uchar flagdat;
uchar flagtsheding;
disdata1[0]=tvalue/1000+0x30;//°ùλêy
disdata1[1]=tvalue%1000/100+0x30;//ê®Î»êy
disdata1[2]=tvalue%100/10+0x30;//¸öλêy
disdata1[3]=tvalue%10+0x30;//D¡êyλ
if(tsheding<hhhh)//ÅD¶Ïζèé趨ÖμêÇÕyêǸo
{
flagtsheding='-';
disdata1[4]=(~tsheding+1)/10+0x30;
disdata1[5]=(~tsheding+1)%10+0x30;
}
else
{
flagtsheding=' ';
disdata1[4]=tsheding/10+0x30;
disdata1[5]=tsheding%10+0x30;
}
if(tflag==0)
flagdat=0x20;//Õyζè2»ÏÔê¾·ûoÅ
else
flagdat=0x2d;//¸oζèÏÔ꾸ooÅ:-
if(disdata1[0]==0x30)
{
disdata1[0]=0x20;//èç1û°ùλÎa0£¬2»ÏÔê¾
if(disdata1[1]==0x30)
{
disdata1[1]=0x20;//èç1û°ùλÎa0£¬ê®Î»Îa0ò22»ÏÔê¾
}
}
wr_com(0x85);
wr_dat(flagdat);//ÏÔê¾·ûoÅλ
wr_com(0x86);
wr_dat(disdata1[0]);//ÏÔê¾°ùλ
wr_com(0x87);
wr_dat(disdata1[1]);//ÏÔê¾ê®Î»
wr_com(0x88);
wr_dat(disdata1[2]);//ÏÔ꾸öλ
wr_com(0x89);
wr_dat(0x2e);//ÏÔê¾D¡êyμã
wr_com(0x8a);
wr_dat(disdata1[3]);//ÏÔê¾D¡êyλ
wr_com(0x8b);//ÏÔê¾é趨ζèÖμ
wr_dat('(');
wr_com(0x8c);
wr_dat(flagtsheding);
wr_com(0x8d);
wr_dat(disdata1[4]);
wr_com(0x8e);
wr_dat(disdata1[5]);
wr_com(0x8f);
wr_dat(')');
}
/*******************************************************************/
void ds1820disp2()//êa¶èÖμÏÔê¾
{
f=tem1*256+tem0;
f=f*20;
if((6033<=f)&&(f<=7351))
{
if((7224<f)&&(f<=7351))
{ temp0=0;temp1=(7351-f)*10/127; }
if((7100<f)&&(f<=7224))
{ temp0=1;temp1=(7224-f)*10/124; }
if((6976<f)&&(f<=7100))
{ temp0=2;temp1=(7100-f)*10/124; }
if((6853<f)&&(f<=6976))
{ temp0=3;temp1=(6976-f)*10/123; }
if((6728<f)&&(f<=6853))
{ temp0=4;temp1=(6853-f)*10/125; }
if((6600<f)&&(f<=6728))
{ temp0=5;temp1=(6728-f)*10/128; }
if((6486<f)&&(f<=6600))
{ temp0=6;temp1=(6600-f)*10/132; }
if((6330<f)&&(f<=6486))
{ temp0=7;temp1=(6468-f)*10/138; }
if((6186<f)&&(f<=6300))
{ temp0=8;temp1=(6330-f)*10/144; }
if((6033<f)&&(f<=6186))
{ temp0=9;temp1=(6186-f)*10/153; }
}
else
{
temp0=0;temp1=0;
}
disdata2[0]=temp0+0x30;
disdata2[1]=temp1+0x30;
disdata2[2]=hsheding/10+0x30;
disdata2[3]=hsheding%10+0x30;
wr_com(0xc8);//ÏÔê¾Ëù2aêa¶èÖμ
wr_dat(disdata2[0]);
wr_com(0xc9);
wr_dat(disdata2[1]);
wr_com(0xca);
wr_dat('%');
wr_com(0xcb);//ÏÔê¾é趨êa¶èÖμ
wr_dat('(');
wr_com(0xcc);
wr_dat(disdata2[2]);
wr_com(0xcd);
wr_dat(disdata2[3]);
wr_com(0xce);
wr_dat('%');
wr_com(0xcf);
wr_dat(')');
}
/********************Ö÷3ìDò***********************************/
void main()
{
Sjia=0;
Sjian=0;
WBJ=0;
HBJ=0;
init_play();//16023õê¼»ˉÏÔê¾
delay1ms(50);
EA=1; //¿aÖD¶Ï
EX0=1;//ÔêDíía2¿ÖD¶Ï0ÖD¶Ï
EX1=1;//ÔêDíía2¿ÖD¶Ï1ÖD¶Ï
ET0=1;//ÔêDí¶¨ê±Æ÷0ÖD¶Ï
ET1=1;//ÔêDí¼ÆêyÆ÷1ÖD¶Ï
IT0=1;
IT1=1;
init_timer();//¶¨ê±/¼ÆêyÆ÷3õê¼»ˉéèÖÃ
while(1)
{
read_temp();//¶áè¡Î¶è
TPanDuan(); //ζèêÇ·ñ3¬3ö·¶Î§μÄÅD¶Ï
ds1820disp1();//ÏÔê¾Î¶è
delay1ms(50);
ds1820disp2();//ÏÔê¾êa¶è
delay1ms(50);
}
}
此帖出自
小平头技术问答
一周热门 更多>