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

📄 ds18b20.c

📁 温度传感器DS18B20的功能实现
💻 C
📖 第 1 页 / 共 2 页
字号:
  unsigned char res_t;    // Resolution
  unsigned int low_t;     // Dept. Dot. use
  
  res_t = * (sct + 4) & 0x60;    // Get resolution from scratchpad data

  if ( Test_Bit (* (sct + 1), 7) )
  {
    * (result + 0) = 0xFF;              // Insignificant, 0x00 is +, 0xFF is -
  }
  else
  {
    * (result + 0) = 0x00;
  }
  
  switch (res_t)
  {  
    case 0x00:    // for 9-bit resolution
      * (result + 1) = * (sct + 0) >> 1;    // Dept. Int.
      * (result + 2) = (* (sct + 0) & 0x01) * 50;  // Dept. Dot.
      break;

    case 0x20:    // for 10-bit resolution
      * (result + 1) = ( (* (sct + 0) >> 2) | (((* (sct + 1)) << 6) & 0x7F) );  // Dept. Int.
      * (result + 2) = (* (sct + 0) & 0x03) * 25;                               // Dept. Dot.
      break;

    case 0x40:    // for 11-bit resolution
      * (result + 1) = ( (* (sct + 0) >> 3) | (((* (sct + 1)) << 5) & 0x7F) );  // Dept. Int.
      low_t = (unsigned int)(* (sct + 0) & 0x07) * 125;                         // Dept. Dot.
      low_t /= 10;
      * (result + 2) = (unsigned char)low_t;
      break;

    case 0x60:    // for 12-bit resolution
      * (result + 1) = ( (* (sct + 0) >> 4) | (((* (sct + 1)) << 4) & 0x7F) );  // Dept. Int.
      low_t = (unsigned int)(* (sct + 0) & 0x0F) * 625;                         // Dept. Dot.
      low_t /= 100;
      * (result + 2) = (unsigned char)low_t;
       break;
   }    // end switch (res_t)   

}
//--------------------------------------------------------------------------------------------------

//////////  Read the value of temperature from DS18B20. Match passed
            //  Input:
                    //  
                        // * Value: will storage temperature.
                            // * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0
                            // * (Value + 1): Int. of temperature value
                            // * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)
            //  Return: 
                    //  0: Opt. false
                    //  1: Opt. OK
unsigned char DS18B20_Single_Read_Temperature (unsigned char * Value)
{
 // unsigned char cpu_sr;
  unsigned char sct[9];   // For temp storage scratchpad data
 
  Enter_Critical ();
 
//  Start convert cycle  
  if (DS18B20_Master_Reset () != 1)     // Master reset
  {
  Value[0]=1;
    Exit_Critical ();
    return 0;
  }
  DS18B20_Write_Byte (0xCC);            // Skip ROM command
  DS18B20_Write_Byte (0x44);            // Convert temperature command
  
  Exit_Critical ();
  Soft_Delay_ms (751);                  // Wait for convert complete
  Enter_Critical ();
//  Cycle end
  
//  Start read SCT cycle  
  if (DS18B20_Master_Reset () != 1)     // Master reset
  {
  Value[0]=2;
    Exit_Critical ();
    return 0;
  }
  DS18B20_Write_Byte (0xCC);            // Skip ROM command
  DS18B20_Write_Byte (0xBE);            // Read SCT
  DS18B20_Multi_Read_Byte (sct, 9);     // Read scratchpad data, 8 Bytes + 1 Byte CRC

//  Cycle end
  
  if (DS18B20_SCT_Check (sct) != 1)     // Check scratchpad with CRC and CONFIG register
  {
  Value[0]=3;
    Exit_Critical ();
    return 0;
  }
 
  DS18B20_Temperature_Format_Transfer (sct, Value);
  
  Exit_Critical ();
  return 1;
}
//--------------------------------------------------------------------------------------------------



//////////  Read the Laser ROM from from DS18B20  //  Notice: Only for single device on 1-Wire Bus 
            //  Input:
                    // * Value: the point for UID data storage. LSB first, MSB last
            //  Return: 
                    //  0: Opt. false
                    //  1: Opt. OK
__monitor unsigned char DS18B20_Single_Read_ROM (unsigned char * Value)
{
  unsigned char i;
  unsigned char rom_temp[8];
  
  if (DS18B20_Master_Reset () != 1)   // Master reset and check, error to return 0
  {
    return 0;
  }
  
  DS18B20_Write_Byte (0x33);    // 0x33 is Read ROM command
  
  DS18B20_Multi_Read_Byte (rom_temp, 8);
  
  if ( DS18B20_UID_Check (rom_temp) != 1)
  {
    return 0;
  }
    
  for (i = 0; i < 8; i++)
  {
    * (Value + i) = rom_temp[i];    // CRC OK, Write back result
  }
  
  return 1;
}
//--------------------------------------------------------------------------------------------------


//////////  Only convert temperature without delay. Single
            //  Input:
                    //  None
            //  Return: 
                    //  0: Opt. false
                    //  1: Opt. OK
unsigned char DS18B20_Single_Only_Convert_Temperature (void)
{
  //unsigned char cpu_sr;
 
  Enter_Critical ();
 
//  Start convert cycle  
  if (DS18B20_Master_Reset () != 1)     // Master reset
  {
    Exit_Critical ();
    return 0;
  }
  DS18B20_Write_Byte (0xCC);            // Skip ROM command
  DS18B20_Write_Byte (0x44);            // Convert temperature command
  
  Exit_Critical ();
  return 1;
}
//--------------------------------------------------------------------------------------------------


//////////  Only read SCT data from DS18B20. Single
            //  Input:
                    //  
                        // * Value: will storage temperature.
                            // * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0
                            // * (Value + 1): Int. of temperature value
                            // * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)
            //  Return: 
                    //  0: Opt. false
                    //  1: Opt. OK
unsigned char DS18B20_Single_Only_Read_Temperature (unsigned char * Value)
{
 // unsigned char cpu_sr;
  unsigned char sct[9];   // For temp storage scratchpad data
 
  Enter_Critical ();
 
//  Start read SCT cycle  
  if (DS18B20_Master_Reset () != 1)     // Master reset
  {
  Value[0]=1;
    Exit_Critical ();
    return 0;
  }
  DS18B20_Write_Byte (0xCC);            // Skip ROM command
  DS18B20_Write_Byte (0xBE);            // Read SCT
  DS18B20_Multi_Read_Byte (sct, 9);     // Read scratchpad data, 8 Bytes + 1 Byte CRC

//  Cycle end
  
  if (DS18B20_SCT_Check (sct) != 1)     // Check scratchpad with CRC and CONFIG register
  {
  Value[0]=2;
    Exit_Critical ();
    return 0;
  }
 
  DS18B20_Temperature_Format_Transfer (sct, Value);
  
  Exit_Critical ();
  return 1;
}
//--------------------------------------------------------------------------------------------------



//////////  以指定的ROM去读温度
            //  Input:
                    //  
                        // * UID: ROM (8 bytes)
                        // * Value: will storage temperature.
                            // * (Value + 0): = 0x00 is above 0, = 0xFF is lower 0
                            // * (Value + 1): Int. of temperature value
                            // * (Value + 2): dot. of temperature value, 0x00 to 0x63 (0.00 to 0.99)
            //  Return: 
                    //  0: Opt. false
                    //  1: Opt. OK
unsigned char DS18B20_UID_Read_Temperature (unsigned char * Value, unsigned char * UID)
{
  //unsigned char cpu_sr;
  unsigned char sct[9];   // For temp storage scratchpad data
 
  Enter_Critical ();
  
  if (DS18B20_UID_Check (UID) != 1)
  {
    Exit_Critical ();
    return 0;
  }
  
//  Start convert cycle  
  if (DS18B20_Master_Reset () != 1)     // Master reset
  {
    Exit_Critical ();
    return 0;
  }
  DS18B20_Write_Byte (0x55);            // Match ROM command
  DS18B20_Multi_Write_Byte (UID, 8);
  DS18B20_Write_Byte (0x44);            // Convert temperature command
  
  Exit_Critical ();
  Soft_Delay_ms (751);                  // Wait for convert complete
  Enter_Critical ();
//  Cycle end
  
//  Start read SCT cycle  
  if (DS18B20_Master_Reset () != 1)     // Master reset
  {
    Exit_Critical ();
    return 0;
  }
  DS18B20_Write_Byte (0x55);            // Skip ROM command
  DS18B20_Multi_Write_Byte (UID, 8);

  DS18B20_Write_Byte (0xBE);            // Read SCT
  DS18B20_Multi_Read_Byte (sct, 9);     // Read scratchpad data, 8 Bytes + 1 Byte CRC

//  Cycle end
  
  if (DS18B20_SCT_Check (sct) != 1)     // Check scratchpad with CRC and CONFIG register
  {
    Exit_Critical ();
    return 0;
  }
 
  DS18B20_Temperature_Format_Transfer (sct, Value);
  
  Exit_Critical ();
  return 1;
}
//--------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

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