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

📄 ds18b20_4m.c

📁 温度传感器DS18B20功能函数
💻 C
字号:
//===============================================================================
//模块名称:  DS18B20.c
//简介:  温度传感器DS18B20功能函数 
//说明:本程序时序要求严格,因此使用了指令周期延时,只适用于40MHZ时钟;一个指令周期为0。1US
//温度输出为实际温度1000倍的十六进制数,如23.25度,输出为0x915;
//若为负温,最高位置1;其它不变。
//下一步工作:1.修改应答程序,使程序不要工作在死循环里;
//           2.写一线上带多DS18B20的程序。
//===============================================================================
#include <p18F4520.h>
#include <delays.h>
#include"coolbaby.h"
#include "ds18b20.h"
//**********************************************************************
// 
// ds18b20_rst:   DS18B20复位
// 
//功   能  ----DS18B20复位
//入口参数:  ----无
//出口参数:  ----无
//**********************************************************************
//void ds18b20_rst(void)
//{
//  char presence=1;
//  while(presence)
//  { 
  
//    DQ_LOW();/*主机拉至低电平*/
//   Delay100TCYx(60);/*保持至少480us*/
//    //delay(20,90); 
   
//    DQ_HIGH();/*释放总线等电阻拉高总线*/
//   Delay10TCYx(40);/*保持15-60us*/
   
//   //上面的延时大于40US就不正常了*/
   
//   //delay(2,100);//延时70us
    
//    if(DQ==0) 
//    {
//    presence=0; /*接收到应答信号*/
//    }
//    Delay100TCYx(43);/*延时430us*/
//      //delay(2,60); //延时430us
//   }
//}


void ds18b20_rst(void) 
{ 
    DQ_LOW() ;                                //主机拉至低电平
    Delay100TCYx(6);/*保持600us*/
    DQ_HIGH(); //释放总线
    //Delay10TCYx(2);/*2us*/ 
    Nop();
    Nop();                             
    while(DQ==1); 
    while(DQ==0); 
    Delay100TCYx(2);/*保持600us*/
} 



 //**********************************************************************
 //read_ds18b20:  读DS18B20                                                *
 //功     能  ----读DS18B20                                                      *
 //入口参数:  ----无                                                    *
 //出口参数:  ----DS18B20内部表示的温度值的16进制,FC90~0000~07D0     *
 //**********************************************************************
unsigned char ds18b20_read(void)
{
 Uchar i;
 Uchar value=0;                                //读出温度
 for(i=8;i>0;i--)
 {
   value>>=1; 
   DQ_LOW();
   //Delay10TCYx(2);//6us
   Nop();
   Nop();
   DQ_HIGH();                               //拉至高电平
   //Delay10TCYx(12);//5us
   Nop();
   Nop();
   Nop();
   Nop();
   Nop();
   if(DQ) value|=0x80;
   Delay10TCYx(5);
   //delay(2,7);                             //63us
  }
  return(value);
}

//unsigned int read_ds18b20(void)              
//{ 
//     int i=0;
//     unsigned int u=0;
//       for (i=0;i<16;i++)
//     	{   
//        	DQ_LOW();
//        	u>>=1;
//         DQ_HIGH(); 
//         if(DQ) u|=0x8000;
//             delay_ds18b20(3);
//     }
//       return (u);
//}

 //**********************************************************************
 //ds18b20_wr:  向DS18B20写指令
 //功     能  ----向DS18B20写指令
 //入口参数:  ----DS18B20操作指令
 //出口参数:  ----无
 //**********************************************************************
void ds18b20_wr(unsigned char val)
{
Uchar i;
Uchar temp;
 for(i=8;i>0;i--)
 {
   temp=val&0x01;                            //最低位移出
   DQ_LOW(); 
   //Delay10TCYx(5);       //从高拉至低电平,产生写时间隙,5us
   Nop();
   Nop();
   Nop();
   Nop();
   if(temp==1) 
   {
    DQ_HIGH();                   //如果写1,拉高电平
   }
   Delay10TCYx(6);/*延时63us*/ 
  
   
   DQ_HIGH(); 
   //Delay10TCYx(5);       //2us
   Nop();
   Nop();
   val=val>>1;                               //右移一位
  }
}
//void ds18b20_wr(unsigned int val)         
//{  
//     int i=0;
//         for (i=0;i<8;i++)
//     { 
//           DQ_LOW(); 
//             DQ =val&0x01;
//             delay_ds18b20(2);
//             DQ=1;
//             val>>=1;
//     }
//}
 
//**********************************************************************
//get_temp:  读DS18B20温度
//功     能  ----读DS18B20温度
//入口参数:  ----无
//出口参数:  ----实际温度的1000倍,若为负温,高两位置1;其它不变。
//**********************************************************************

//启动温度转换函数
unsigned long get_temp(void)
{ 
unsigned int tp=0;/*采集到的原始温度数据全16位*/
unsigned long temp=0;/*处理后的温度数据*/
char i;
//DQ_HIGH();
ds18b20_rst();                                 //复位等待从机应答 
ds18b20_wr(SkipROM);                        //忽略ROM匹配 
ds18b20_wr(B20Convert);                        //发送温度转化命令  

delay_ms(10);/*延时500ms,确保温度转换完成*/

ds18b20_rst();                                 //再次复位,等待从机应答 
ds18b20_wr(SkipROM);                        //忽略ROM匹配 
ds18b20_wr( B20ReadScr);                        //发送读温度命令

tp=ds18b20_read(); /*读出温度(先读低8位,后读高8位,最后合并为一个16位数*/
tp<<=8;
tp|=ds18b20_read();                         
//delay_ds18b20(4);要加延时的?

DQ_HIGH();                               //释放总线 

if(tp>0x0fff)                        //判断是否为负温,取反加一,乘6.25
           {      
                 tp=~tp;
                 tp++;
                 temp=tp*625;
                 temp|=0x80000000;/*负温,最高位置1;其它不变。*/
           }
     else
           temp=tp*625;//正温
     return(temp);
}



void delay_us(unsigned int us)
{
Delay10TCYx((us * Fosc) / 40000000); // Delay of ms
return;
} 

void delay_ms(unsigned int ms)//单位10MS
{
Uchar i;
for(i=0;i<100;i++)
{
Delay1KTCYx(ms); 
}
return;
} 


//------------------------------------------------
//延时函数
void delay(char x,char y) 
{
  char z;
  do{
      z=y;
      do{;}while(--z);
     }while(--x);
 }
//其指令时间为:7+(3*(Y-1)+7)*(X-1)如果再加上函数调用的call 指令、页面设定、传递参数花掉的7 个指令。
//则是:14+(3*(Y-1)+7)*(X-1)。

void delay_ds18b20(unsigned int us)
{
     unsigned int i;
     for(i=0;i<=us;i++)
     {}
} 

⌨️ 快捷键说明

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