www.pudn.com > 18B20.zip > main.c, change:2015-05-06,size:864b


#include<reg51.h> 
#include"temp.h" 
sbit ff=P2^0; 
unsigned char code DIG_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; 
void delay500us(void)   //误差 0us 
{ 
    unsigned char a,b; 
    for(b=71;b>0;b--) 
        for(a=2;a>0;a--); 
} 
void Display(int temp) 	  
{ 
   	float tp;   
	if(temp< 0)				//当温度值为负数 
  	{	 ff=0x40;    //为负显示负号
		temp=temp-1; 
		temp=~temp; 
		tp=temp; 
		temp=tp*0.0625*100+0.5; 
   	} 
 	else 
  	{	ff=0x00;	 //为正则不显示 
		tp=temp; 
		temp=tp*0.0625*100; 
		} 
    P2=0xfd; 
	P0 = DIG_CODE[temp % 10000 / 1000]; 
    delay500us();	 
	P0=0x00; 
	P2=0xfb; 
	P0 = DIG_CODE[temp % 1000 / 100]| 0x80;	  //小数点位 
    delay500us();	 
	P0=0x00; 
	P2=0xf7; 
    P0 = DIG_CODE[temp % 100 / 10]; 
    delay500us();	 
	P0=0x00; 
  } 
  void main()		  
{ 
	while(1) 
	{ 
		Display(Ds18b20ReadTemp()); 
	} 
}