程序在下面就是数码管显示时候在15.9度网上升时候变成20.0了!!!是怎么回事啊?我是用51
开发板弄得
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^2;
uchar temp;
sbit duan=P2^6;
sbit wei=P2^7;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
unsigned char code tablea[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0x00};
void delay(uint t);
void delay1ms(uint z);
void init_ds18b20(void);
void write_byte(uchar dat);
uchar read_byte(void);
uchar readtemp(void);
void display();
void delay(uint t)
{
while(t--);
}
void delay1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void Init_ds18b20(void)
{
unsigned char x=0;
DQ = 1;
delay(2);
DQ = 0;
delay(60);
DQ = 1;
delay(2);
x=DQ;
delay(15);
}
unsigned char read_byte(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat |=0x80;
delay(4);
}
return(dat);
}
void write_byte(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay(2);
DQ = 1;
dat>>=1;
}
}
uchar readtemp(void)
{
uchar a,b,t;
delay(40);
Init_ds18b20();
write_byte(0xCC);
write_byte(0x44);
delay(40);
Init_ds18b20();
write_byte(0xCC);
write_byte(0xBE);
delay(40);
a=read_byte();
b=read_byte();
b<<=8;
t=(b|a)*0.0625*10;
return t;
}
void display()
{
uchar ge,shi,shifen;
shi=temp/100;
ge=(temp%100)/10;
shifen=temp%10;
wei=1;
P0=0xfe;
wei=0;
duan=1;
P0=table[shi];
duan=0;
delay1ms(4);
wei=1;
P0=0xfd;
wei=0;
duan=1;
P0=tablea[ge];
duan=0;
delay1ms(4);
wei=1;
P0=0xfb;
wei=0;
duan=1;
P0=table[shifen];
duan=0;
delay1ms(4);
}
void main()
{
while(1)
{
temp=readtemp();
delay(5);
display();
}
}
一周热门 更多>