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

📄 ds1820.h

📁 MSP430F149驱动DS18B20程序(IAR开发环境)
💻 H
字号:
/************************************************
* 文件名称:ds1820.h
* 摘	要:温度传感器的操作函数
*
* 当前版本:1.0
* 作	者:李连骏
* 完成日期:2007年8月21日
*************************************************/
#define DS1820_DIR  P1DIR
#define DS1820_IN  P1IN
#define DS1820_OUT  P1OUT

#define DS1820_DQ  BIT2  //定义DS1820的接口

// DS1820 ROM命令宏定义
#define ReadRom   0x33
#define MatchRom  0x55
#define SearchRom  0xf0
#define AlertSearch  0xec
#define CopyScratchpad  0x48
#define SkipRom   0xcc
#define ReadPower  0xb4

// DS1820 功能命令宏定义
#define ConvertTemperature 0x44
#define ReadScratchpad  0xbe
#define WriteScratchpad  0x4e
#define RecallE   0xb8

#define SMCLK   4000  //(KHz)用于系统延时


// 变量定义
unsigned char GetScratchpad[9];
unsigned char ResultTemperatureH;  //温度值的高八位(为0则温度值为正,否则为负)
unsigned char ResultTemperatureL;  //温度值的低八位(表示温度值)
unsigned char COUNT_REMAIN;  
unsigned char COUNT_PER;  
 
void DS1820_WriteBit(unsigned char oww_dat);
void DS1820_WriteByte(unsigned char oww_dat);
void DS1820_ReadTemp(void);         //读取当前温度值
unsigned char DS1820_Init(void);
unsigned char DS1820_ReadBit(void);
unsigned char DS1820_ReadByte(void);
void Delay10us(void);
void DelayX10us(unsigned char x10us);

// 功能函数定义

unsigned char DS1820_Init(void){
  unsigned char result;
  DS1820_DIR |= DS1820_DQ;       // ow output
  DS1820_OUT &= ~DS1820_DQ;      // DS1820_DQ=0;
  DelayX10us(48);       // Bus master pulling low 480us minimum;
  DS1820_OUT |= DS1820_DQ;       // DS1820_DQ=1;
  DelayX10us(6);        // Resister pull up 15-60us;
  DS1820_DIR &= ~DS1820_DQ;      // ow input
  result = DS1820_IN & DS1820_DQ;
  DS1820_DIR |= DS1820_DQ;       // ow output
  DelayX10us(48);       // End of timeslot total 480us;
  return(result);       // any 1 wire device ?result:=1 no devide; ?result:=0 have device;
}//Intialization the 1-wire devices;

unsigned char DS1820_ReadBit(void){
  unsigned char result;
  DS1820_DIR |= DS1820_DQ;       // ow output
  DS1820_OUT &= ~DS1820_DQ;      // DS1820_DQ=0;
  _NOP();               // Start of timeslot;
  DS1820_OUT |= DS1820_DQ;       // DS1820_DQ=1;
  _NOP();_NOP();_NOP();_NOP();
          // Wait from the start;
  DS1820_DIR &= ~DS1820_DQ;      // ow input
  result = DS1820_IN & DS1820_DQ;
  DS1820_DIR |= DS1820_DQ;       // ow output
  return(result);       // return the result of the 1-wire devide;
}//Read a bit on the 1-wire bus;

void DS1820_WriteBit(unsigned char oww_dat){
  DS1820_DIR |= DS1820_DQ;       // ow output
  DS1820_OUT &= ~DS1820_DQ;      // DS1820_DQ=0;
  if (1 == oww_dat) 
    DS1820_OUT |= DS1820_DQ;     // DS1820_DQ=1;
  DelayX10us(10);       // Remain the state for 100us;
  DS1820_OUT |= DS1820_DQ;       // DS1820_DQ=1;
}//Write a bit to the 1-wire bus;

unsigned char DS1820_ReadByte(void){
  unsigned char i;
  unsigned char result=0;
  for(i = 0; i < 8; i++){
    if(DS1820_ReadBit())
      result |= 0x01 << i;
    DelayX10us(12);     // ??
  }
  return(result);       // return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;

void DS1820_WriteByte(unsigned char oww_dat){
  unsigned char i,temp;
  for(i = 0; i < 8; i++){
    temp = oww_dat >> i;
    temp &= 0x01;
    DS1820_WriteBit(temp);
  }
  DelayX10us(7);        // ??
}//Write a byte to the 1-wire bus;


void DS1820_ReadTemp(void)
{
 //float TEMPERATURE;
 DS1820_Init();
 //if(result1==1)result1=2;
 DS1820_WriteByte(SkipRom);
 _NOP();
  //There is just one DS1820 on the bus;
 DS1820_WriteByte(ConvertTemperature);
 DelayX10us(10);
  //Start to convert temperature;
 DS1820_Init();
 DS1820_WriteByte(SkipRom);
 _NOP();
 DS1820_WriteByte(ReadScratchpad);
 
 GetScratchpad[0]=DS1820_ReadByte();//TEMPERATURE LSB; 
 GetScratchpad[1]=DS1820_ReadByte();//TEMPERATURE MSB; 
 GetScratchpad[2]=DS1820_ReadByte();//TH/USER BYTE 1;  
 GetScratchpad[3]=DS1820_ReadByte();//TL/USER BYTE 2; 
 GetScratchpad[4]=DS1820_ReadByte();//RESERVED; 
 GetScratchpad[5]=DS1820_ReadByte();//RESERVED;  
 GetScratchpad[6]=DS1820_ReadByte();//COUNT REMAIN; 
 GetScratchpad[7]=DS1820_ReadByte();//COUNT PER °C;  
 GetScratchpad[8]=DS1820_ReadByte();//CRC;
 
 ResultTemperatureL=GetScratchpad[0];
 ResultTemperatureH=GetScratchpad[1];
 COUNT_REMAIN=GetScratchpad[6];
 COUNT_PER=GetScratchpad[7];
 
  
 g_WeatherStationPara.Temperature=GetScratchpad[0]/2-0.25+1-COUNT_REMAIN/COUNT_PER;  //Result of temperature;
 //return TEMPERATURE; 
}

 

void Delay10us(void){
  unsigned char i;
  for (i=0; i<(SMCLK/500-3); i++);
}

//  Time is accurately !!
void DelayX10us(unsigned char x10us){
  unsigned int i;
  for (i=0; i<x10us; i++)
    Delay10us();
}

⌨️ 快捷键说明

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