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

📄 18b20.c

📁 含读出内部光绘ROM的DS18b20的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <msp430x14x.h>
#include "18b20.h"
#define DQ  BIT5

float        Temper[7]={0.0};                   //测量到的实际温度值
unsigned char temper_c[8]={0xAA,0,0,0,0,0,0,0x01};   //将测量到的实际温度值*1倍,转整型
unsigned int temperature=0;         
unsigned char Error = 0;            //DS18B20复位成功标志,0:失败;1:成功
unsigned int value0[9]={2,2,2,2,2,2,2,2,2};  //存放读出18B20的64位光绘序列号

// 现在所使用的6只18B20的64位序列号
//1:00 00 00 01 AC D3 E1 28
//2:A1 00 00 01 BB 34 FB 28
//3:D9 00 00 01 BB 3B D3 28
//4:0F 00 00 01 BB 30 6B 28
//5:19 00 00 01 BB 49 83 28
//6:CB 00 00 01 BB 4C 83 28


/*****************************************************************************
在时钟选择为MCLK的时钟源为TX2CLK,分频因子为1,SMCLK的时钟源为TX2CLK,分频因子为1
外接晶体振荡器为8M时的延时程序,延时时间=(6*nUs+6)*0.125us(指令周期)
******************************************************************************/
void delay(unsigned int nUs)    //延时时间=(6*nUs+6)*0.125us(指令周期)
{
 while(nUs--);
}


/*****************************************************************************
在时钟选择为MCLK的时钟源为TX2CLK,分频因子为1,SMCLK的时钟源为TX2CLK,分频因子为1
外接晶体振荡器为8M时的延时800毫秒程序
******************************************************************************/
void delay_800ms()    //延时时间=(6*nUs+6)*0.125us(指令周期)
{
  unsigned char i;
 for(i=18;i>0;i--)
 {
  delay(60000);
 }
}

/*****************************************************************************
DS18B20复位程序
******************************************************************************/
char DS1820_Reset(void)
{
 char presence;
 _DINT();                      //关闭中断
 P5DIR |= DQ;                  // 设定管脚为输出方向
 P5OUT &= ~(DQ);               // 将DQ管脚拉低   
 delay(639);                   // 延时480微妙
 P5OUT |= DQ;                  // 将DQ管脚拉高 	
 delay(72);                    // 延时55微妙
 P5DIR &= ~(DQ);               // 设定管脚为输入方向
 presence = (char)(P5IN & DQ); // 读取数据
 if(presence==BIT5)            //0001 1111b=1f
  {
   Error =1;                   //失败1             
  }
 else
  {
   Error = 0;                  //初始化成功
  }
 _EINT();                      //打开中断
 delay(532);                   // 延时400微妙
 return(presence);
}

/*****************************************************************************
读DS18B20数据(一次读8个BIT)程序
******************************************************************************/
char DS1820_ReadByte(void)
{
 char i;
 char value = 0;
 char presence;
  _DINT();                        //关闭中断
 for (i = 8;i > 0;i--)
  {
   value >>= 1;   
   P5DIR |= DQ;                    // 设定管脚为输出方向 
   P5OUT &= ~(DQ);                 // 将DQ管脚拉低
   delay(7);                       //延时6us
   P5OUT |= DQ;
   delay(11);                      //延时9us
   P5DIR &= ~(DQ);                 // 设定管脚为输入方向

   presence = (char)(P5IN & DQ);   // 读取数据
   if(presence) value |= 0x80;
   delay(59);                      // 延时45微妙    
   P5DIR |= DQ;                    // 将DQ管脚拉高
   P5OUT |= DQ;
   delay(12);                      // 延时10微妙 
   }
  _EINT();                         //打开中断
 return value;
}


/*****************************************************************************
向DS18B20写数据程序
******************************************************************************/
void DS1820_WriteByte(char val)
{
 char i;
 char nBit;
 _DINT();                      //关闭中断
 for (i=8; i>0; i--)
   {
    P5DIR |= DQ;               // 设定管脚为输出方向
    P5OUT &= ~(DQ);            // 将DQ管脚拉低
    delay(7);                  //延时6us

    nBit = val & 0x01;         // 输出数据 
    if (nBit)
     {
      P5OUT |= DQ;
     }
    else
     {
      P5OUT &= ~(DQ);
     }
  _EINT();                      //打开中断
 delay(66);                    // 延时50微妙
 P5OUT |= DQ;                  // 将DQ管脚拉高
 val >>= 1;
 delay(12);                    // 延时10微妙
    }
}


/*****************************************************************************
读DS18B20的64位光绘序列号程序
******************************************************************************/
void Read_rom(void)
{
 unsigned char i;
 DS1820_Reset();                // 复位
 DS1820_WriteByte(0x33);        // read ROM
 
 
 for(i=0;i<8;i++)
 {
  value0[i]=DS1820_ReadByte();    //读低位
 }
 /*
 value0[1]=DS1820_ReadByte();    //读低位
 value0[2]=DS1820_ReadByte();    //读低位
 value0[3]=DS1820_ReadByte();    //读低位
 value0[4]=DS1820_ReadByte();    //读低位
 value0[5]=DS1820_ReadByte();    //读低位
 value0[6]=DS1820_ReadByte();    //读低位
 value0[7]=DS1820_ReadByte();    //读低位
 value0[8]=DS1820_ReadByte();    //读低位
 */
}


/*****************************************************************************
读第一只18B20的温度的程序
******************************************************************************/
void readtemp_no0()
{
 unsigned char temp_low,temp_high;   //温度值
 DS1820_Reset();                     // 复位
 DS1820_WriteByte(0x55);             //match ROM,保证有相同序列号的芯片会响应
 
 DS1820_WriteByte(0x28);
 DS1820_WriteByte(0xE1);
 DS1820_WriteByte(0xD3);
 DS1820_WriteByte(0xAC);
 DS1820_WriteByte(0x01);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0x00);
 
 DS1820_WriteByte(0x44);            // 开始转换
 delay_800ms();                     //延时800ms
 
 DS1820_Reset();                    // 复位
 DS1820_WriteByte(0x55);            //match ROM,保证有相同序列号的芯片会响应
 
 DS1820_WriteByte(0x28);
 DS1820_WriteByte(0xE1);
 DS1820_WriteByte(0xD3);
 DS1820_WriteByte(0xAC);
 DS1820_WriteByte(0x01);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0x00);

 DS1820_WriteByte(0xBE);            // Read Scratch
 temp_low=DS1820_ReadByte();        //读低位
 temp_high=DS1820_ReadByte();       //读高位
 temperature=(temp_high&0x0f);
 temperature<<=8;
 temperature|=temp_low;
 Temper[1]=temperature*0.0625;
 temper_c[1]=(char)Temper[1]*1;
}

/*****************************************************************************
读第二只18B20的温度的程序
******************************************************************************/
void readtemp_no1()
{
 unsigned char temp_low,temp_high;       //温度值
 DS1820_Reset();                // 复位
 DS1820_WriteByte(0x55);        //match ROM,保证有相同序列号的芯片会响应
 
 DS1820_WriteByte(0x28);
 DS1820_WriteByte(0xFB);
 DS1820_WriteByte(0x34);
 DS1820_WriteByte(0xBB);
 DS1820_WriteByte(0x01);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0xA1);
 
 DS1820_WriteByte(0x44);        // 开始转换
 delay_800ms();                 //延时800ms
 
 DS1820_Reset();                // 复位
 DS1820_WriteByte(0x55);        //match ROM,保证有相同序列号的芯片会响应
 
 DS1820_WriteByte(0x28);
 DS1820_WriteByte(0xFB);
 DS1820_WriteByte(0x34);
 DS1820_WriteByte(0xBB);
 DS1820_WriteByte(0x01);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0x00);
 DS1820_WriteByte(0xA1);

 DS1820_WriteByte(0xBE);       // Read Scratch
 temp_low=DS1820_ReadByte();    //读低位
 temp_high=DS1820_ReadByte(); //读高位
 temperature=(temp_high&0x0f);
 temperature<<=8;
 temperature|=temp_low;
 Temper[2]=temperature*0.0625;
 temper_c[2]=(char)Temper[2]*1;
}

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

⌨️ 快捷键说明

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