谁能帮我看看哪里出错了······关于DS18B20在1602上显示的!!!!下面是程序
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P2^0;
sbit lcden=P2^2;
sbit key=P3^4;
sbit DQ=P1^1;
uchar time;
uchar j,k,l;
uint i;
uchar TL;
uchar TH;
uchar TD;
uchar TN;
uchar table[16]="abcdefghijklmnyz";
uchar table1[20]="0123456789abcdef :/C";
uchar tab2[6]="Temp:";
uchar m,l,miao=0,fen,nian=11,yue=5,ri=17,wendu;
void write_com(uchar com);
void write_data(uchar ata) ;
void delay(uint z) ;
bit init_ds(void) //send reset and initialization command 发送复位和初始化命令
{
// uint i;
bit flag;
DQ=1;
for(time=0;time<2;time++) ;
DQ=0;
for(time=0;time<200;time++) ;
DQ=1;
for(time=0;time<10;time++);
flag=DQ;
for(time=0;time<200;time++);
return(flag) ;
}
WriteOne(uchar dat)
{
i=0;
for (i=0; i<8; i++)
{
DQ=0;
_nop_();
DQ=1;
DQ=dat&0x01;
for(time=0;time<10;time++) ;
DQ=0;
for(time=0;time<1;time++) ;
dat>>=1;
for(time=0;time<4;time++) ;
}
}
void Ready(void)
{
init_ds();
WriteOne(0xcc);
WriteOne(0x44);
for(time=0;time<100;time++) ;
init_ds();
WriteOne(0xCC);
WriteOne(0x44);
WriteOne(0xBE);
}
uchar ReadOne(void)
{
// uint i=0;
uchar dat;
for(i=0;i<8;i++)
{
DQ=0;
_nop_();
DQ=1;
dat>>=1;
_nop_();
DQ=0;
for(time=0;time<2;time++) ;
if(DQ==1) dat|=0x80;
else dat|=0x00;
for(time=0;time<8;time++) ;
return(dat);
}
}
void display_temp1(uchar x)
{
j=x/100;
k=(x%100)/10;
l=x%10;
write_com(0x80+0x46);
write_data(table1);
write_data(table1[k]) ;
write_data(table1[l]) ;
delay(50);
}
void display_temp2(uchar x)
{
write_com(0x80+0x4a);
write_data(table1[x]);
delay(50);
}
/*uchar code tab[]={ 0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
};*/
void delay(uint z)
{
uint i;
for(;z>0;z--)
for(i=110;i>0;i--);
}
void init()
{
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
/*void key_san() //按键扫描
{
if(key==0)
{
delay(2);
if(key==0) //按键成立进入
{
while(!key); //按键退出消抖。
m++; //改变运行模式
if(m>4) //如果M+到5或以上M就复位。清0
{
m=0;
}
}
}
} */
void write_com(uchar com)
{
//wela=0;
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar ata)
{ //wela=0;
lcdrs=1;
P0=ata;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void display()
{
//uchar h;
write_com(0x80+0x00);
write_com(0x80+0x00) ;
write_data(table1[2]);
write_data(table1[0]);
write_data(table1[nian%100/10]); //年
write_data(table1[nian%10]);
write_data(table1[18]);
write_data(table1[yue%100/10]); //月
write_data(table1[yue%10]);
write_data(table1[18]);
write_data(table1[ri%100/10]); //日
write_data(table1[ri%10]);
/*for(k=0;k<=6;k++)
{
write_data(tab2[k]);
}
write_com(0x80+0x40);
write_data(table1[fen%100/10]); //时位 或 分位
write_data(table1[fen%10]);
write_data(table1[17]);
write_data(table1[miao%100/10]); //分位 或 秒位
write_data(table1[miao%10]);
write_data(table1[17]);
write_data(table1[i%100/10]); //秒位 或 毫秒位
write_data(table1[i%10]);
for(k=0;k<=3;k++)
{
write_data(tab2[0]);
}
write_data(table1[wendu%100/10]);
write_data(table1[wendu%10]);
write_data(0xdf);
write_data(table1[19]); */
}
void main()
{
init();
//uchar TL;
//uchar TH;
//uchar TD;
//uchar TN;
/* TMOD=0X10;
TH1=(65535-2000)/256; //2MS
TL1=(65535-2000)%256;
EA=1;
ET1=1;
TR1=1; */
while(1)
{Ready();
TL=ReadOne();
TH=ReadOne(); display();
delay(5);
if(init_ds()==1)
{
write_com(0x80+0x40);
write_data(table[2]);
}
TN=TH*16+TL/16;
TD=(TL%16)*10/16;
display_temp1(TN);
display_temp2(TD);
/* key_san(); //按键扫描
if(m==4) //判断M是否等于4,如果成立就把I清0。
{
i=0;
miao=0;
fen=0;
} */
}
}
/*void int_time1() interrupt 3
{
TH1=(65535-2000)/256; //2MS
TL1=(65535-2000)%256;
j++;
if(j>=4) //J为秒变量。这个没仔细算。大概一秒。可能会有误差,这里还判断按键值,只有M=1和3才进行正常计时,
{
if(m==1||m==3)
{
i++;
if(i>=100)
{
i=0;
miao++;
if(miao>=60)
{
miao=0;
fen++;
fen=fen==10?0:fen;
}
}
}
j=0;
}
}
*/
[img]file:///C:/Users/lenovo/AppData/Roaming/Tencent/Users/583852668/QQ/WinTemp/RichOle/PO%7BJG99QS(XWPNX_%7DU%7D)Q[D.jpg[/img]
电路 |