您当前的位置:五五电子网电子知识单片机-工控设备51单片机数字温度计程序 正文
数字温度计程序

数字温度计程序

点击数:7633 次   录入时间:03-04 11:42:55   整理:http://www.55dianzi.com   51单片机
{
 unsigned  char i,j,k;
 for(i=0;i<8;i++)
   for(j=0;j<25;j++)
     for(k=0;k<250;k++);
}

void Delay15()                          //延时15us
{
 unsigned  char i;
 for(i=0;i<8;i++);
}

void Delay60()                          //延时60us
{
 unsigned  char i;
 for(i=0;i<30;i++);
}

void Write0TS()                         //写bit 0
{
     TSOR=1;
     TSOR=0;
     Delay15();
     Delay15();
     Delay15();
     Delay15();
     TSOR=1;
     _nop_();
     _nop_();
}

void Write1TS()                          //写bit 1
{
     TSOR=1;
     TSOR=0;
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     TSOR=1;
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     Delay15();
     Delay15();
     Delay15();
}

bit ReadTS()
{
 bit b;
     TSOR=1;
     TSOR=0;
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     TSOR=1;
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     _nop_();
     b=TSOR;            
     Delay15();
     Delay15();
     Delay15();
     _nop_();
     _nop_();
     return b;
}

void ResetTS()                            //复位
{unsigned char i;

   TSOR=1;
   TSOR=0;
   for(i=0;i<8;i++)
     Delay60();
   TSOR=1;              
   while(TSOR);         
   for(i=0;i<8;i++)
     Delay60();
}

void WriteByteTS(unsigned char byte)      //写一个字节(byte)
{unsigned char i;
 for(i=0;i<8;i++)
 {
   if(byte&0x01)
     Write1TS();
   else
     Write0TS();
   byte=byte>>1;
 }
}

unsigned char ReadByteTS()                //读一个字节(byte)
{
 unsigned char 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();
     ReadByteTS();
     ReadByteTS();
     ReadByteTS();
     ReadByteTS();
     ReadByteTS();
     ReadByteTS();
     ReadByteTS();
     temp1=temp1<<4;
     temp1+=(temp2&0xF0)>>4;
      temp2=(temp2&0x0F)?5:0;
}



上一页  [1] [2] [3] [4] 


本文关键字:程序  数字温度计  51单片机单片机-工控设备 - 51单片机