#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define duanma P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit ds=P3^7;
bit flag;
uint temp;
uint shu=0;
uchar displaydata[4];
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//
void digdisplay();
void digdisplay1();
void delayus(uint t)
{
while(t--);
}
void ds_reset() //18b20初始化
{
ds=1;
delayus(5);
ds=0;
delayus(80);//480-960us
ds=1;
delayus(14);
if(ds==0)
flag=1;
else
flag=0;
delayus(20);
}
bit ds_read_bit()//读一位
{
bit dat;
ds=0;
_nop_();
_nop_();
ds=1;
_nop_();
dat=ds;
delayus(8);
return dat;
}
uchar ds_read_byte() //读一个字节
{
uchar i,j,k;
for(i=0;i<8;i++)
{
j=ds_read_bit();
k=(j<<7)|(k>>1);
}
return k;
}
void ds_write_byte(uchar dat)//
{
uchar i;
for(i=0;i<8;i++)
{
ds=0;
_nop_();
ds=dat&0x01;//
delayus(1);
ds=1;
dat=dat>>1;
}
//delayus(3);
}
uint read_temperature()//
{
uchar a,b;
ds_reset();
ds_write_byte(0xcc);
ds_write_byte(0xbe);
a=ds_read_byte();
b=ds_read_byte();
temp=b;
temp=temp<<8;
temp=temp|a;
temp=temp*0.0625*10+0.5;
return temp;
}
void digdisplay1()
{
displaydata[2]=table[temp/100];
displaydata[1]=table[temp%100/10]|0x80;
displaydata[0]=table[temp%10];
}
void main()
{
while(1)
{
ds_reset();
ds_write_byte(0xcc);
ds_write_byte(0x44);
read_temperature();
digdisplay1();
digdisplay( );
} //while(1);
}
void digdisplay()
{
/
switch(shu) //
{
case(0):
LSA=0;LSB=0;LSC=0; break;//
case(1):
LSA=1;LSB=0;LSC=0; break;//
case(2):
LSA=0;LSB=1;LSC=0; break;//
case(3):
LSA=1;LSB=1;LSC=0; break;
case(4):
LSA=0;LSB=0;LSC=1; break;
case(5):
LSA=1;LSB=0;LSC=1; break;
case(6):
LSA=0;LSB=1;LSC=1; break;
case(7):
LSA=1;LSB=1;LSC=1; break;
}
duanma=displaydata[shu];
shu++;
if(shu>3)
shu=0;
}
|