⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds18b20.c

📁 caijiwendu,dallas ds18b20
💻 C
📖 第 1 页 / 共 2 页
字号:
	  { 
            //25.0625   0x0191  bytetc[1]=0x01,bytetc[0]=0x91
           //msb is sign'+',lsb is decimal digits
           //example  (0x01<<8)+0x91=0x0191;
           //(0x01a1>>4) = 0x0019;(25)
           //the least Significant bit 0x0001 is decimal digits(0.0625)
            T_count =  (bytetc[1] << 8) + bytetc[0];
	    T_count = T_count >> 4;
	   }
      T_counth=T_count/256;
    T_countl=T_count%256;
    put0_char(0x36);
    put0_char(T_counth);
    put0_char(T_countl);
    put0_char(0x34);
    put0_char(T_count);
    */
}
//14 00 00 0 78 0f bc 28
//99(crc) 00 00(serialnumh) 01 23(serialnumm) d2 c9(serialnuml) 28(family code)
void Sendromnum_DsB(unsigned char dsbcrc,unsigned int serialnumh,unsigned int serialnumm,unsigned int serialnuml) 
{
  //from bit 0 to bit 63
DSBWriteByte(0x28); //ds18b20 family code 
//serial number
DSBWriteByte(serialnuml%256);
DSBWriteByte(serialnuml/256);
DSBWriteByte(serialnumm%256);
DSBWriteByte(serialnumm/256);
DSBWriteByte(serialnumh%256);
DSBWriteByte(serialnumh/256);
//crc
DSBWriteByte(dsbcrc);
}
//SINGLE READ ROM 33H
//MATCH ROM 55H
void Romcomand_DsB(unsigned char romcommand) 
{
 int i=0,j=7;   //,k=7
   bitdsb[i]=0;
   bytedsb[j]=0;
   DS18B20_InitC();
   DS18B20_Init();          //1 Initialization 
   DSBWriteByte(romcommand); 
        
}
void Check_DsB(void) 

{ 
   int i=0,j=7,k=7;   
   bitdsb[i]=0;
   bytedsb[j]=0;
   DS18B20_InitC();
   DS18B20_Init();          //1 Initialization    
   
   DSBWriteByte(0x33);      //2 ROM Function Command
   Read_DsB();							//3 Read Data
   for(j=7;j>=0;j--)
   { for(k=7;k>=0;k--)
        {  bytedsb[j] <<=1;
        	  if(bitdsb[j*8+k]==1)
             bytedsb[j] |=0x01;
        }
      put0_char( bytedsb[j]);
    
      //return (bytedsb[j]);
    }
    
} 
void Swirereadt(unsigned char dsbcrc,unsigned int serialnumh,unsigned int serialnumm,unsigned int serialnuml)
{
 DS18B20_Init();  
   // Check_DsB(); 
    DSBWriteByte(0x55);
    Sendromnum_DsB(dsbcrc,serialnumh,serialnumm,serialnuml);
    DSBWriteByte(0xbe);
    Read_Dstc();                      //3 Read Data
    T_counth=T_count/256;
    T_countl=T_count%256;
    //put0_char(0x36);
    put0_char(T_counth);
    put0_char(T_countl);
    put0_char(0x33);
    put0_char(T_count);
    //put0_char(0x37);
}
/*
Convert T [44h]The temperature conversion will be performed and then the DS18B20 will remain idle.
If the bus master issues read time slots following this command, the DS18B20 will output 0 on the bus
as long as it is busy making a temperature conversion; it will return a 1 when the temperature conversion 
is complete.Temperature conversion takes up to 750 ms.Following the issuance of the Convert T [44h] 
command, a temperature conversion is performed and the thermal data is stored in the scratchpad memory 
in a 16-bit, sign-extended two’s complement format. The temperature information can be retrieved over 
the 1-Wire interface by issuing a Read Scratchpad [BEh] command once the conversion has been performed. 
The data is transferred over the 1-Wire bus, LSB first. The MSB of the temperature register contains 
the “sign” (S) bit, denoting whether the temperature is positive or negative.
*/ 
void Tc_DsB( void )  
{ 
  //int i=0,j=8,k=7;
   DS18B20_Init();          //1 Initialization     
   DSBWriteByte(0xcc);      //skip rom [cch]
   DSBWriteByte(0x44);     //2 ROM Function Command convert T[44h]
      HIGH_DSB();
      delay(1000);    //Temperature conversion takes up to 750 ms. 
      delay(1000); 
      delay(1000); 
      delay(1000);
      delay(1000);
    DS18B20_Init();  
   // Check_DsB(); 
    DSBWriteByte(0x55);
    Sendromnum_DsB(0x99,0x0000,0x0123,0xd2c9);
    DSBWriteByte(0xbe);
    Read_Dstc();                      //3 Read Data
    /*
      for(j=8;j>=0;j--)
          { 
            for(k=7;k>=0;k--)
                {  bytetc[j] <<=1;
                if(bittc[j*8+k]==1)
                bytetc[j] |=0x01;
                }
          }

     if(bytetc[1]&0x10)
   	 {bytetc[1] = ~bytetc[1];
   	  bytetc[0] = 0x10-bytetc[0];
   	  T_count = (bytetc[1] << 8) + bytetc[0];
			T_count = T_count>>4;
			T_count = -T_count;
   	  }
	 else
	  { T_count =  (bytetc[1] << 8) + bytetc[0];
	    T_count = T_count >> 4;
	   }
      */
 T_counth=T_count/256;
    T_countl=T_count%256;
    put0_char(0x36);
    put0_char(T_counth);
    put0_char(T_countl);
    put0_char(0x34);
    put0_char(T_count);
    put0_char(0x37);
    
     DS18B20_Init();  
    //Check_DsB(); 
    DSBWriteByte(0x55);
    Sendromnum_DsB(0x14,0x0000,0x0178,0x0fbc);
    DSBWriteByte(0xbe);
    Read_Dstc();                      //3 Read Data
    /*
      for(j=8;j>=0;j--)
          { 
            for(k=7;k>=0;k--)
                {  bytetc[j] <<=1;
                if(bittc[j*8+k]==1)
                bytetc[j] |=0x01;
                }
          }

     if(bytetc[1]&0x10)
   	 {bytetc[1] = ~bytetc[1];
   	  bytetc[0] = 0x10-bytetc[0];
   	  T_count = (bytetc[1] << 8) + bytetc[0];
			T_count = T_count>>4;
			T_count = -T_count;
   	  }
	 else
	  { T_count =  (bytetc[1] << 8) + bytetc[0];
	    T_count = T_count >> 4;
	   }
    */
T_counth=T_count/256;
T_countl=T_count%256;
put0_char(0x35);
put0_char(T_counth);
put0_char(T_countl);
put0_char(0x33);
put0_char(T_count);
put0_char(0x38);
/*
for(j=8;j>=0;j--)
{ 
for(k=7;k>=0;k--)
  {  
  bytetc[j] <<=1;
  if(bittc[j*8+k]==1)
  bytetc[j] |=0x01;
  }
//putchar( bytetc[j]);
}
*/
   
/*
     if(bytetc[1]&0x10)
   	 {
           //-25.0625   0xfe6f bytetc[1]=0xfe,bytetc[0]=0x6f
           //msb is sign'-',lsb is decimal digits
           //example ~0xfe=0x01,0x10-0x6f=0xa1;(0x01<<8)+0xa1=0x01a1;
           //(0x01a1>>4)-1 = 0x001a-1=0x0019;(25)
           //the least Significant bit 0x0001 is decimal digits(0.0625)
           bytetc[1] = ~bytetc[1];
   	  bytetc[0] = 0x10-bytetc[0];
   	  T_count = (bytetc[1] << 8) + bytetc[0];
			T_count = (T_count>>4)-1;
			T_count = -T_count;
   	  }
	 else
	  { 
            //25.0625   0x0191  bytetc[1]=0x01,bytetc[0]=0x91
           //msb is sign'+',lsb is decimal digits
           //example  (0x01<<8)+0x91=0x0191;
           //(0x01a1>>4) = 0x0019;(25)
           //the least Significant bit 0x0001 is decimal digits(0.0625)
            T_count =  (bytetc[1] << 8) + bytetc[0];
	    T_count = T_count >> 4;
	   }
	   //putchar(T_count);
	   //return(T_count);
     */

}
void send_tc(void)
{ 
DS18B20_Init();          //1 Initialization     
DSBWriteByte(0xcc);      //skip rom [cch]
DSBWriteByte(0x44);     //2 ROM Function Command convert T[44h]
HIGH_DSB();
delay(1000);    //Temperature conversion takes up to 750 ms. 
delay(1000); 
delay(1000); 
delay(1000);
delay(1000);

put0_char(0x60);
Swirereadt(0x99,0x0000,0x0123,0xd2c9);
put0_char(0x61);
Swirereadt(0x14,0x0000,0x0178,0x0fbc);
put0_char(0x62);
Swirereadt(0x01,0x0000,0x0177,0xfe45);
put0_char(0x63);
Swirereadt(0x65,0x0000,0x0178,0x2264);
put0_char(0x64);
Swirereadt(0x89,0x0000,0x0178,0x4f79);
put0_char(0x65);
Swirereadt(0xa6,0x0000,0x0178,0x4bc8);
put0_char(0x66);
Swirereadt(0xf0,0x0000,0x0178,0x3272);
put0_char(0x67);
}

/*
//spi write high to low bit
void DSWriteByte( unsigned char dscommand )
{
	unsigned char i,j,k;
        for(i=0; i<8; i++)
	{
            if ( (dscommand & 0x80 ))
            {     HIGH_DS();
                  NOP();
                  LOW_DS();
                  for (k=5;k>0;k--)
                  {NOP();}
                 HIGH_DS();
                 for (j=0;j<60;j++)    
                 { 
                    NOP();
                 }
                  NOP();      //Recovery Time
                  NOP();
                  NOP();
                  NOP();
                  NOP();
                  NOP();
               }   
              else
              {   HIGH_DS();
                  NOP();
                  LOW_DS(); 
                  for (j=0;j<60;j++)    //60us-120us  
                  { 
                      NOP();
                  }
                  HIGH_DS();  //Recovery Time                                         
                  for (k=10;k>0;k--)
                  {NOP();}
                  }
            dscommand <<= 1;
      }	
}
*/










⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -