请大家帮我看一下dis18b20的时序哪里写发错了?在数码管上只显示两位0,下面是程序:
#include<stc15f2k60s2.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define rst573 P2&=0x1f //¹Ø±ÕÑ¡ÖÐ
#define smgY6C P2|=0xC0;//λѡͨ
#define smgY7C P2|=0xe0;//¶Îѡͨ
#define fengming P2|=0xa0;//Ñ¡ÖзäÃùÆ÷+¼ÌµçÆ÷
sbit dq=P1^4;
sbit Wr=P4^2;
code uchar duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};
// È«Ãð+ 1~8wei/*1011111
code uchar wei[]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //λÂë
uchar dspcom=6;
uchar k=0;
uint real_temp;
uchar flag;
void Delay60us(uint i) //@11.0592MHz
{
unsigned char j;
j = 162;
do
{
while (--j);
} while (--i);
}
void Delay30us() //@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
i = 80;
while (--i);
}
void delay(uint z)//msÑÓʱº¯Êý
{
uint x,y;
for(x=z;x>0;x--)
for(y=1100;y>0;y--);
}
void CHS_BEEP_close()
{ Wr=0;
fengming;//
P0=0x0f;
Wr=1;
}
void ds18b20_init()
{
dq=1;
_nop_();
dq=0;
Delay60us(10);
dq=1;
Delay60us(1);
}
void temperaturewrite_Byte(uchar dat)//д×Ö½Úº¯Êý
{
uchar i,j;
dq=1;
for(i=1;i<=8;i++)
{
j=dat&0x01;
dat>>=1;
if(j)
{
dq=0;
i++;i++;
dq=1;
Delay30us();
}
else
{
dq=0;
Delay30us();
dq=1;
_nop_();
}
}
}
bit temperatureread_bit()//¶Áһλ
{
uint i;
bit dat;
dq=0;i++;i++; //i++ ÆðÑÓʱ×÷ÓÃ
Delay30us();
dq=1;i++;i++;
dat=dq;
i=80;while(i>0)i--;
return (dat);
}
uchar temperatureread_Byte()//¶Á×Ö½Úº¯Êý
{
uchar dat,i,j;
dat=0;
dq=0;
i++;i++;
for(i=1;i<=8;i++)
{
j=temperatureread_bit();
dat=(j<<7)|(dat>>1);
}
return(dat);
}
void display(uint x)
{
uchar a,b;
a=x/10;
b=x%10;
smgY7C;//¶Îѡͨ
P0=0xff;//ÏûÒþ
rst573;
smgY6C;//λѡͨ
P0=wei[7];
rst573;
smgY7C;
P0=duan[a];
rst573;
delay(1);
smgY6C;//λѡͨ
P0=wei[8];
rst573;
smgY7C;
P0=duan[b];
rst573;
delay(1);
}
uint get_temperature()//»ñȡζÈÖµ
{
uchar h,l;
uint temp;
ds18b20_init();//Æô¶¯Î¶Èת»»
temperaturewrite_Byte(0xcc);
temperaturewrite_Byte(0x44);
ds18b20_init();
temperaturewrite_Byte(0xcc);
temperaturewrite_Byte(0xbe);
l=temperatureread_Byte();//¶ÁȡζȵÍ×Ö½Ú
h=temperatureread_Byte();//¶Áȡζȸß×Ö½Ú
temp=h;
temp<<=8;
temp=temp|l;
real_temp=temp*0.0625;
return(real_temp);
}
void timer_init()
{
TMOD=0x01;
TH0=(65536-2000)/256;
TL0=(65536-2000)/256;
EA=1;//´ò¿ª×ÜÖжÏ
ET0=1;
}
int main(void)
{
timer_init();
while(1)
{ CHS_BEEP_close(); //¹Ø±Õ·äÃùÆ÷¼ÌµçÆ÷
display(real_temp);
if(flag)
{
flag=0;
real_temp=get_temperature();//¶ÁȡζÈ
}
}
}
void timer_0(void) interrupt 1 using 0
{
TH0=(65536-2000)/256;//ÒѾ×Ô¶¯ÖØ×°³õÖµ
TL0=(65536-2000)/256;
if(++k==150)
{ k=0;//300msÖØлñȡζÈ
flag=1;
}
} |