求高手指点,我的4位数码管全显示,就像复位一样!
时间:10-02
整理:3721RD
点击:
本程序是DS18B20实现温度测量并显示在数码管上的!
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit D0=P3^4; //数据线
uint num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71, //不带小数点的0~9;
0xbf,0x86,0xdb,0xaf,
0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,
0xb9,0xde,0xf9,0xf1}; //带小数点的0~9;
void delayms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=124;j>0;j--);
}
void delayus(uchar ms) //微秒级的延时;延时2*ms+5(μs).
{
while(--ms);
}
void init()
{
uchar pan=1;
while(pan)
{
D0=1;
_nop_();
_nop_();
D0=0;
delayus(185);//延时750μs;
delayus(185);
D0=1;
delayms(5);
if(D0==0) pan=0;
D0=1;
}
}
void write_byte(uchar dat) //写字节
{
uchar i ;
bit w;
for(i=8;i>0;i--)
{
w=dat&0x01;
dat>>=1;
if(w==0) //写0;
{
D0=0;
delayus(25);
D0=1;
}
else //写1;
{
D0=0;
delayus(2);
D0=1;
delayus(20);
D0=1;
}
}
}
uchar read_byte()
{
uchar dat1,j;
for(j=8;j>0;j--)
{
D0=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(D0==1)
dat1=dat1|0x80;
delayus(15);
dat1>>=1;
}
return dat1;
}
uint gettemp() //获得温度数值
{
uchar a,b;
uint temp;
float ftemp ;
init();
delayms(1);
write_byte(0xcc); //跳过读ROM指令
write_byte(0x44); //开始温度转换;
delayms(1000);
write_byte(0xbe); //读取RAM中的数据
a=read_byte();
b=read_byte();
temp=b;
temp<<=8;
temp=temp|a;
ftemp=temp*0.0625;
temp=ftemp*10+0.5;
ftemp=ftemp+0.05;
return temp;
}
void display(num) //数码管显示没有错误。我试过
{
uchar shi,ge,shifen;
shi=num/100;
ge=num%100/10;
shifen=num%100%10;
P1=0x01;
P0=table[shi];
delayms(5);
P1=0x02;
P0=table[ge+10];
delayms(5);
P1=0x04;
P0=table[shifen];
delayms(5);
}
void main()
{
while(1)
{
num=gettemp();
display(num);
}
}
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit D0=P3^4; //数据线
uint num;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71, //不带小数点的0~9;
0xbf,0x86,0xdb,0xaf,
0xe6,0xed,0xfd,0x87,
0xff,0xef,0xf7,0xfc,
0xb9,0xde,0xf9,0xf1}; //带小数点的0~9;
void delayms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=124;j>0;j--);
}
void delayus(uchar ms) //微秒级的延时;延时2*ms+5(μs).
{
while(--ms);
}
void init()
{
uchar pan=1;
while(pan)
{
D0=1;
_nop_();
_nop_();
D0=0;
delayus(185);//延时750μs;
delayus(185);
D0=1;
delayms(5);
if(D0==0) pan=0;
D0=1;
}
}
void write_byte(uchar dat) //写字节
{
uchar i ;
bit w;
for(i=8;i>0;i--)
{
w=dat&0x01;
dat>>=1;
if(w==0) //写0;
{
D0=0;
delayus(25);
D0=1;
}
else //写1;
{
D0=0;
delayus(2);
D0=1;
delayus(20);
D0=1;
}
}
}
uchar read_byte()
{
uchar dat1,j;
for(j=8;j>0;j--)
{
D0=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
if(D0==1)
dat1=dat1|0x80;
delayus(15);
dat1>>=1;
}
return dat1;
}
uint gettemp() //获得温度数值
{
uchar a,b;
uint temp;
float ftemp ;
init();
delayms(1);
write_byte(0xcc); //跳过读ROM指令
write_byte(0x44); //开始温度转换;
delayms(1000);
write_byte(0xbe); //读取RAM中的数据
a=read_byte();
b=read_byte();
temp=b;
temp<<=8;
temp=temp|a;
ftemp=temp*0.0625;
temp=ftemp*10+0.5;
ftemp=ftemp+0.05;
return temp;
}
void display(num) //数码管显示没有错误。我试过
{
uchar shi,ge,shifen;
shi=num/100;
ge=num%100/10;
shifen=num%100%10;
P1=0x01;
P0=table[shi];
delayms(5);
P1=0x02;
P0=table[ge+10];
delayms(5);
P1=0x04;
P0=table[shifen];
delayms(5);
}
void main()
{
while(1)
{
num=gettemp();
display(num);
}
}