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

📄 ds18b20.c

📁 温度传感器DS18B20的功能实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
****************************************************************************************************
* SSAC_A1
* Main Controller Board
* File name : DS18B20.c
* Use for   : Detect temperature
* Summary   : The C code for DS18B20

****************************************************************************************************
*/

#include  "DS18B20.h"
#include  "DS18B20_ports.h"

void Soft_Delay_us(int n)
{
  TACTL = TASSEL_2 + ID_2 + TAIE + TACLR + MC_2 ;           // SMCLK/8, contmode, interrupt enabled
  while(TAR < n) ;
   return;  
}

void    Soft_Delay_ms(int x){
  int i;
  for(i=0;i<x;i++)
    Soft_Delay_us(1000);
}
void    Soft_Delay_s(int x){
  int i;
  for(i=0;i<x;i++)
    Soft_Delay_ms(1000);
}
////////// Bit operation macro
#define Set_Bit(Address,Bit)    ((Address) |= (1 << Bit))
#define Clear_Bit(Address,Bit)  ((Address) &= ~(1 << Bit))
#define Test_Bit(Address,Bit)   ((Address) & (1 << Bit))
//--------------------------------------------------------------------------------------------------


//////////  Make a Master reset to DS18B20, and check the result 
            //  Input:
                    //  None
            //  Return: 
                    //  0: There is no DS18B20 or master reset operation false
                    //  1: There is and has be 1-Wire reset 
static unsigned char DS18B20_Master_Reset (void)
{
  unsigned char Value;
  
  Value = 0;
  
  DS18B20_Tx_Obtain ();   // Set to low for 720us to reset slave device
  Soft_Delay_us (480);    // 720us

  DS18B20_Tx_Release ();  // Release the 1-Wire Bus
    
  Soft_Delay_us (70);     // 68us. Check the reset result, slave will set the 1-Wire Bus to low
  P6DIR &= ~BIT3;
  Value = DS18B20_Rx ();
    
  Soft_Delay_us (410);    // 480us. Wait for cycle end
  
  if (Value) return 0;
  
  return 1;
}
//--------------------------------------------------------------------------------------------------

//////////  Write bit to DS18B20 
            //  Input:
                    //  Value: bit value will be writed
            //  Return: 
                    //  None
static void DS18B20_Write_Bit (unsigned char Value)
{
  
  if (Value == 0)  // Master Write "0" SLOT
  {
    DS18B20_Tx_Obtain ();
    Soft_Delay_us (65);   //  70us

    DS18B20_Tx_Release ();
    
    Soft_Delay_us (10);   //  No this line
  }  
  else            // Master Write "1" SLOT
  { 
    DS18B20_Tx_Obtain ();
    Soft_Delay_us (10);    //  7us
    
    DS18B20_Tx_Release ();
    
    Soft_Delay_us (65);   //  63us
  }
  
  //Soft_Delay_us (10);     // Wait for 1-wire restore
}
//--------------------------------------------------------------------------------------------------

//////////  Read bit from DS18B20 
            //  Input:
                    //  None
            //  Return: 
                    //  The bit value that has be read out
static unsigned char DS18B20_Read_Bit (void)
{
  unsigned char Value;
  
  DS18B20_Tx_Obtain ();   // Start a read time slot
  Soft_Delay_us (5);
  
  DS18B20_Tx_Release ();  // Release and sample 1-Wire Bus
  Soft_Delay_us (5);   // 13us
   P6DIR &= ~BIT3;
  Value = P6IN & BIT3;  // Wait for read cycle end
  Soft_Delay_us (55);   // 52us
  
  //Soft_Delay_us (10);     // Wait for 1-wire restore
  
  if ( !Value ) return 0;
  
  return 1;
}
//--------------------------------------------------------------------------------------------------

//////////  Write byte to DS18B20 
            //  Input:
                    //  The byte will be writed
            //  Return: 
                    //  None
static void DS18B20_Write_Byte (unsigned char Value)
{
  unsigned char i;
  for (i = 0; i < 8; i++)
  {
    DS18B20_Write_Bit ( Test_Bit (Value, i) );
  }
}
//--------------------------------------------------------------------------------------------------

//////////  Read byte from DS18B20 
            //  Input:
                    //  
            //  Return: 
                    //  The byte that has be read out
static unsigned char DS18B20_Read_Byte (void)
{
  unsigned char i;
  unsigned char Value;
  Value = 0;
  for (i = 0; i < 8; i++)
  {
    if ( DS18B20_Read_Bit () ) Set_Bit (Value, i);
  }
  
  return Value;
}
//--------------------------------------------------------------------------------------------------

//////////  Multi Write byte to DS18B20 
            //  Input:
                    //  * Value: The source data
                    //  Number: number of the data
            //  Return: 
                    //  None
static void DS18B20_Multi_Write_Byte (unsigned char * Value, unsigned char Number)
{
  unsigned char i;
  
  if (Number == 0) return;
  
  for (i = 0; i < Number; i++)
  {
    DS18B20_Write_Byte ( * (Value + i) );
  }
}
//--------------------------------------------------------------------------------------------------

//////////  Multi Read byte from DS18B20 
            //  Input:
                    //  * Value: The source data
                    //  Number: number of the data
            //  Return: 
                    //  None
static void DS18B20_Multi_Read_Byte (unsigned char * Value, unsigned char Number)
{
  unsigned char i;
  
  if (Number == 0) return;
  
  for (i = 0; i < Number; i++)
  {
    * (Value + i) = DS18B20_Read_Byte ();
  }
  
}
//--------------------------------------------------------------------------------------------------




//////////  8-bit CRC check, X8+X5+X4+1, to check the data get from DS18B20 
            //  Input:
                    // Crc_Org: Oringal crc value
                    // * Value: the data need do a crc check
                    // Length: The length of data 
            //  Return: 
                    //  Return = crc result
static unsigned char DS18B20_Crc_Check (unsigned char Crc_Org, unsigned char * Value, unsigned char Length)
{
  unsigned char i;
  
  while (Length--)
  {
    for (i = 0x01; i != 0; i <<= 1)
    {
      if ( (Crc_Org & 0x01) != 0)  // LSB of crcorg
      {
        Crc_Org >>= 1;
        Crc_Org ^= 0x8C;
      }
      else
      {
        Crc_Org >>= 1;
      }
      
      if ( ((* Value) & i) != 0)
      {
        Crc_Org ^= 0x8C;
      }      
    }
  Value ++;  
  }
  return Crc_Org;
}
//--------------------------------------------------------------------------------------------------

//////////  Laser ROM check 
            //  Input:
                    // * UID: Laser rom data to check
            //  Return: 
                    //  0: Opt. false
                    //  1: Opt. OK
static unsigned char DS18B20_UID_Check (unsigned char * UID)
{
   
  if ( * (UID + 0) != 0x28 ) return 0;    // DS18B20's family code is 0x28. To avoid always 0 error
  
  if ( DS18B20_Crc_Check (0x00, UID, 7) != * (UID + 7) ) return 0;  // CRC check error;
  
  return 1;
}
//--------------------------------------------------------------------------------------------------

//////////  Scratchpad check 
            //  Input:
                    // * SCT: scratchpad
            //  Return: 
                    //  0: Opt. false
                    //  1: Opt. OK
static unsigned char DS18B20_SCT_Check (unsigned char * SCT)
{
     
  if ( ((* (SCT + 4)) & 0x9F) != 0x1F) return 0;    // To avoid always 0 error
  
  if ( (DS18B20_Crc_Check (0x00, SCT, 8)) != (* (SCT + 8)) ) return 0;   // Crc check error
  
  return 1;
}
//--------------------------------------------------------------------------------------------------

//////////  Temperature format transfer 
            //  Input:
                    //  
                        // * sct: The data need to transfer. 8 bytes
                        // * result: 3 bytes, is insignificant byte, integer byte, low bytes
            //  Return: 
                    //  None
static void DS18B20_Temperature_Format_Transfer (unsigned char * sct, unsigned char * result)
{

⌨️ 快捷键说明

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