#include<reg52.h>
#include <stdio.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit R=P2^3;
sbit lcdrs=P2^0;
sbit lcdrw=P2^1;
sbit lcden=P2^2;
sbit di=P2^4;
unsigned char
time ;
uchar th,tl,a,b,c,d,f,j,e;
uint temp;
unsigned char code table[]={"0123456789.:'C"};
unsigned char code table1[]={"temp:"};
unsigned char code error[]={" error!"};
void delay1ms()
{ unsigned int x,y; for(x=0;x<10;x++) for(y=33;y>0;y--); }
void delay(unsigned int z)
{ unsigned char x; for(x=z;x>0;x--) delay1ms();}
void write_com(unsigned char com)
{
lcdrs=0;
SBUF=com;
while(TI==0);TI=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(unsigned char dat)
{
lcdrs=1;
SBUF=dat;
while(TI==0);TI=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init(void)
{
lcden=0;
lcdrw=0;
write_com(0x01);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
}
/*DS18b20操作program*/
bit datayn(void) //初始化设置
{
bit flag;
di=1;
for(time=0;time<2;time++);
di=0;
for(time=0;time<200;time++);
di=1;
for(time=0;time<10;time++);
flag=di;
for(time=0;time<100;time++);
di=1;
for(time=0;time<10;time++);
return(flag) ;
}
uchar read_onechar(void)
{
uchar i,dat;
for(i=0;i<8;i++);
{
di=1;_nop_();
di=0;
dat>>=1; _nop_(); _nop_(); _nop_();_nop_(); _nop_();
di=1;
for(time=0;time<2;time++);
if(di==1) dat|=0x80;
else dat|=0x00;
for(time=0;time<4;time++);
}
return dat;
}
void write_onechar(uchar dat)
{
uchar i,time;
for(i=0;i<8;i++);
{
di=1; _nop_();
di=0; for(time=0;time<5;time++);
di=dat&0x01; for(time=0;time<15;time++);
di=1;
_nop_();_nop_();
dat>>=1;
}
for(time=0;time<3;time++);
}
void display(void)
{
unsigned int i=0;
write_com(0x80);
if(datayn()==1)
{
while(error[i]!=' ')
{
write_data(error[i]);
i++;
}
}
else
{
while(table1[i]!=' ')
{
write_data(table1[i]);
i++;
}
}
}
void readtemp()
{
di=1;_nop_();_nop_();
datayn();
write_onechar(0XCC);
write_onechar(0x44);
for(time=0;time<100;time++);
datayn();
write_onechar(0XCC);
write_onechar(0XBE);
tl=read_onechar(); th=read_onechar();
di=1; _nop_(); _nop_(); _nop_();
// tl=0x6f; th=0xfe; // 用这个代替上面的th,tl后,下面都正常,说明是上面的问题 。
if (th>>7==0)
{
e=1;
a=tl>>4|th<<4;
b=a/100;c=(a%100)/10;d=a%10;
j=tl&0x0f;f=(j*10/16)%10;
}
else
{
e=0;
temp=(~(tl|th<<8)+1);temp*=0.0625;b=temp/100;c=temp/10%10;d=temp%10;f=(temp*10)%10;
write_com(0x85); write_data('-');
}
write_com(0x86); write_data(table[b]);write_data(table[c]);
write_data(table[d]);write_data(0x2e);write_data(table[f]);
write_data(table[12]);write_data('c');
}
void main()
{
SCON=0x00;
R=0; _nop_(); _nop_(); R=1;
init();
display();
while(1)
{
readtemp();
}
} 感觉问题 在 那两张截图图片上 哪位大神有空帮忙看看 先谢谢了
-
-
-
一周热门 更多>