#include<reg52.h>
#include<onewire.h>
#define uint unsigned int
unsigned int temp=0;
unsigned char SMG_No_Dot[10]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
};
unsigned char SMG_Dot[10]=
{
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10
};
void delay_SMG(uint time)
{
uint i,j;
for(i=time;i>0;i--)
for(j=114;j>0;j--);
}
void SelectHC573(unsigned char n)
{
switch(n)
{
case 4:
P2=(P2 & 0x1f) | 0x80;
break;
case 5:
P2=(P2 & 0x1f) | 0xa0;
break;
case 6:
P2=(P2 & 0x1f) | 0xc0;
break;
case 7:
P2=(P2 & 0x1f) | 0xe0;
break;
}
}
void Show_SMG_Bit(unsigned char dat,unsigned char pos)
{
SelectHC573(6);
P0=0x01<<pos;
SelectHC573(7);
P0=dat;
}
void Show_SMG()
{
//整数
Show_SMG_Bit(SMG_No_Dot[temp%10],7);
delay_SMG(10);
Show_SMG_Bit(0xff,7);
Show_SMG_Bit(SMG_No_Dot[temp%10],6);
delay_SMG(10);
Show_SMG_Bit(0xff,6);
Show_SMG_Bit(0xff,5);
/*小数处理
Show_SMG_Bit(SMG_No_Dot[temp%10],7);
delay_SMG(10);
Show_SMG_Bit(0xff,7);
Show_SMG_Bit(SMG_Dot[(temp%100)/10],6);
delay_SMG(10);
Show_SMG_Bit(0xff,6);
Show_SMG_Bit(SMG_No_Dot[temp/100],5);
delay_SMG(10);
Show_SMG_Bit(0xff,5);
*/
Show_SMG_Bit(0xff,4);
delay_SMG(10);
Show_SMG_Bit(0xff,3);
delay_SMG(10);
Show_SMG_Bit(0xff,2);
delay_SMG(10);
Show_SMG_Bit(0xff,1);
delay_SMG(10);
Show_SMG_Bit(0xff,0);
delay_SMG(10);
}
void Init()
{
SelectHC573(5);
P0=0x00;
SelectHC573(4);
P0=0xff;
}
void delay(uint time)
{
while(time--)
{
Show_SMG();
}
}
void Read_DS18B20_temp()
{
unsigned char LSB,HSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay(100);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
HSB=Read_DS18B20();
init_ds18b20();
temp=HSB;
temp<<=8;
temp=temp | LSB;
//整数
temp>>=4;
/*小数处理
if(temp & 0xf8==0x00)
{
temp>>=4;
temp=temp*10;
temp=temp+(LSB & 0x0f)*0.625;
}
*/
}
void main()
{
Init();
while(1)
{
Read_DS18B20_temp();
Show_SMG();
}
}
单总线温度传感器DS18B20的基本操作
最新推荐文章于 2024-04-04 17:34:42 发布