18b20 仿真

  • 格式:doc
  • 大小:90.50 KB
  • 文档页数:6

/////////////////////////////////////////////////////////////////////////

#include

#include

#define uchar unsigned char

/////////////////////////////////////////////////////////////////////////

sbit TSOR = P3^7;

static uchar temp1,temp2;

uchar code

tab0[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

uchar code tab2[4]={0xfe,0xfd,0xfb,0xf7};

uchar data tab3[4]=0;

////////////////////////////////////////////////////////////////////////

void delay(uchar a);

void Delay15();

void Delay60();

void Delay100ms();

void Write0TS();

void Write1TS();

bit ReadTS();

void ResetTS();

void WriteByteTS(uchar byte);

uchar ReadByteTS();

void InitTS();

void GetTempTS();

void display(void);

////////////////////////////////////////////////////////////////////////

void main(void)

{

while(1)

{

InitTS();

GetTempTS();

display();

}

}

///////////////////////////////////////////////////////////////////////

void delay(uchar a)

{

unsigned i,j;

for(i=0;i

for(j=0;j<200;j++);

}

////////////////////////////////////////////////////////////////////////

void Delay100ms()

{

uchar i,j,k;

for(i=0;i<8;i++)

for(j=0;j<25;j++)

for(k=0;k<250;k++);

}

////////////////////////////////////////////////////////////////////////

void Delay15()

{

uchar i;

for(i=0;i<8;i++);

}

////////////////////////////////////////////////////////////////////////

void Delay60()

{

uchar i;

for(i=0;i<30;i++);

}

////////////////////////////////////////////////////////////////////////

void Write0TS()

{

TSOR=1;

TSOR=0;

Delay15();

Delay15();

Delay15();

Delay15();

TSOR=1;

_nop_();

_nop_();

}

////////////////////////////////////////////////////////////////////////

void Write1TS()

{

TSOR=1;

TSOR=0;

_nop_();

_nop_();

_nop_();

TSOR=1;

Delay15();

Delay15();

Delay15();

Delay15();

}

////////////////////////////////////////////////////////////////////////

bit ReadTS()

{

bit b;

TSOR=1;

TSOR=0;

_nop_();

_nop_();

_nop_();

_nop_();

TSOR=1;

Delay15();

b=TSOR;

Delay15();

Delay15();

Delay15();

_nop_();

_nop_();

return b;

}

/////////////////////////////////////////////////////////////////////////

void ResetTS()

{

uchar i;

TSOR=1;

TSOR=0;

for(i=0;i<8;i++)

Delay60();

TSOR=1;

while(TSOR);

for(i=0;i<5;i++)

Delay60();

}

/////////////////////////////////////////////////////////////////////////

void WriteByteTS(uchar byte)

{

uchar i;

for(i=0;i<8;i++)

{

if(byte&0x01)

Write1TS();

else

Write0TS();

byte=byte>>1;

}

}

/////////////////////////////////////////////////////////////////////////

uchar ReadByteTS()

{

uchar i,j;

bit b;

j=0;

for(i=0;i<8;i++)

{

b=ReadTS();

if(b)

j+=1;

j=_cror_(j,1);

}

return j;

}

////////////////////////////////////////////////////////////////////////

void InitTS()

{

ResetTS();

WriteByteTS(0xCC);

WriteByteTS(0x4E);

WriteByteTS(0x64);

WriteByteTS(0x8A);

WriteByteTS(0x1F);

}

////////////////////////////////////////////////////////////////////////

void GetTempTS()

{

ResetTS();

WriteByteTS(0xCC);

WriteByteTS(0x44);

Delay100ms();

ResetTS();

WriteByteTS(0xCC);

WriteByteTS(0xBE);

temp2=ReadByteTS();

temp1=ReadByteTS();

temp1=temp1<<4;

temp1+=(temp2&0xF0)>>4;

temp2=(temp2&0x0F)?5:0;

}

////////////////////////////////////////////////////////////////////////

void display(void)

{

uchar i;

tab3[1]=temp1/100;

tab3[2]=temp1%100/10;

tab3[3]=temp1%10;

tab3[0]=0;

for(i=0;i<4;i++)

{

P1=tab0[tab3[i]];

P2=tab2[i];

delay(20);

}

}

///////////////////////////////////////////////////////////////////////

分享到搜狐微博