这个51单片机数码管显示温度程序

#include<reg51.h>

#define uchar unsigned char
#define uint unsigned int

unsigned char code duan[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f};
sbit ds=P3^7;

uint temp; //定义整形的温度数据

//unsigned code[4];

void delay(uint x) //延时函数
{ uint i;
for(x;x>0;x--)
for(i=110;i>0;i--);

}

void dsinit() //DS18B20复位,初始化函数
{
uint i;
ds=0;
i=103;
while(i--);
ds=1;
i=400;
while(i--);
}

bit tempreadbit(void) //读1位数据函数
{
uint i;
bit dat;
ds=0;i++;
ds=1;i++;i++;
dat=ds;
i=8;
while(i>0)
i--;
return(dat);
}

uchar dsread() //读1个字节数据函数
{
uint n,m;
uchar dat,bi;
for(n=0;n<8;n++)
{
ds=0;
m++;
ds=1;
m++;m++;
bi=ds;
dat=(dat>>1)|(bi<<7);
m=4;
while(m--);
}
return dat;
}

void dswrite(uchar dat) //向DS18B20写一个字节的数据函数
{

uint i;
uchar j;
bit bit1;
for(j=0;j<8;j++)
{
bit1=dat&0x01;
dat=dat>>1;
if(bit1)
{
ds=0;
i++;i++;
ds=1;
i=8;
while(i--);
}
else
{
ds=0;
i=8;while(i--);
ds=1;
i++;i++;
}
}

}

void dskaishi() //DS18B20开始获取温度并转换
{
dsinit();
delay(1);
dswrite(0xcc);
dswrite(0xbe);
}

uint get_temp() //读取寄存器中存储的温度数据
{
uchar a,b;
dsinit();
delay(1);
dswrite(0xcc); //跳过ROM
dswrite(0xbe); //读暂存器
a=dsread(); //读低位 ,有两组数据
b=dsread(); //读高位
temp=b;
temp<<=8;
//temp=temp|a; //两组数据归为一个字节
temp|=a;
temp=temp*0.0625;

return temp;
}

void dsxianshi(uchar temp)
{
uchar bai,shi,ge,xiao;
// uchar tp;
// disdata[0]=tvalue/1000; //百位数
// disdata[1]=tvalue%1000/100; //十位数
// disdata[2]=tvalue%100/10; //个位数
// disdata[3]=tvalue%10; //小数位
//
P2=0xf7;
bai=temp/1000;
P0=duan[bai];
delay(5);
//P0=0x00;

P2=0xfb;
shi=temp%1000/100;
P0=duan[shi]+0x80;
delay(5);
//P0=0x00;

// P2=0xfb;
//
// P0=0x80;
//
// delay(5);
//P0=0x00;

P2=0xfd;
ge=temp%100/10;
P0=duan[ge];
delay(5);
//P0=0x00;

P2=0xfe;
//P0=0X39;
xiao=temp%10;
P0=duan[xiao] ;
delay(5);
//P0=0x00;
}

void main()
{
while(1)
{
dsxianshi(temp);
}
}

这个数码管是用共阴的,这个程序提示有3个警告但是没有错误。下载到单片机的时候都是显示00.00
我弄了好久都不知道是什么原因。
希望能有热心的网友能为我解答,非常感激,谢谢!

你把倒数第三行改为

dsxianshi(get_temp());

试试看?你现在这样不会启动18b20采集温度,全局变量temp得不到温度值。

 

另外,这种用全局变量传值的玩法很不好看,能不用就不要用。

另外2, 全局变量和局部变量或函数参数同名字也不是好玩法。

追问

我按您的指点改了,可是就发现。十位的温度没变就是就是0;最后4个数码管的显示是08.2C;
不管什么给DS18B20加温都是这个值,警告也是出现两个。我想在向您请教下。

温馨提示:答案为网友推荐,仅供参考
相似回答