?? ds18b20_driver.c
字號:
#define DS18B20_Driver
#include <DS18B20_Driver.h>
void DS18B20_Init(void)
{
unsigned char Init_Data[3]={20,10,0x3f}; //分辨率為10位
unsigned char i=0;
Reset(); //復位
Write_Byte(SKIP_ROM); //跳過ROM命令
Write_Byte(WRITE_SCRATCHPAD); //寫存儲器命令
for(i=0;i<3;i++)
{
Write_Byte(Init_Data[i]);
}
Reset(); //復位
Write_Byte(SKIP_ROM); //跳過ROM命令
Write_Byte(COPY_SCRATCHPAD); //拷貝存儲器命令
}
//************************************************************
//************************************************************
uint Get_Temp(void)
{
unsigned char i=0;
unsigned char TempH=0,TempL=0;
unsigned int Temp = 0;
// float temprature=0;
Reset(); //復位
Write_Byte(SKIP_ROM); //跳過ROM命令
Write_Byte(CONVERT_T); //啟動轉換命令
// Wire_Delay(50000);
Reset(); //復位
Write_Byte(SKIP_ROM); //跳過ROM命令
Write_Byte(READ_SCRATCHPAD); //讀存儲器命令
TempL=Read_Byte(); //讀取溫度值低8位
TempH=Read_Byte(); //讀取溫度值高8位
//TempH<<=4;
//temprature=TempH+TempL*0.0625;
//return(temprature);
Temp = TempH ;
Temp<<=8;
Temp = Temp|TempL;
return(Temp);
}
void Wire_Delay(unsigned int num)
{
unsigned int i=0;
i=num;
while(--i);
}
unsigned char Reset(void)
{
unsigned char Presence_Flag=0; //主機接收到1,說明從機不在線,0為在線
DQ=0; //拉低DQ線
Wire_Delay(480); //延時至少480微秒
DQ=1; //釋放DQ線
Wire_Delay(70); //等待18B20響應
Wire_Delay(120); //在采樣周期的中間采樣(采樣周期為60~240微秒)
Presence_Flag=DQ; //采樣
Wire_Delay(120); //等待剩余的采樣周期
Wire_Delay(240); //等待時序結束
return(Presence_Flag);
}
unsigned char Read_Bit(void)
{
unsigned char readbit=0;
DQ=0; //拉低DQ
Wire_Delay(1); //延時至少1微秒
P1MDOUT &=0xef;
DQ=1; //釋放DQ
Wire_Delay(10); //最多延時14微秒采樣,此處為10微秒
readbit=DQ;
Wire_Delay(110); //讀取周期為(60~120)微秒
P1MDOUT|=0x10;
return(readbit);
}
void Write_Bit(unsigned char num)
{
DQ=0;
Wire_Delay(10);
if(num & 0x01)
{DQ=1;}
else
{DQ=0;}
Wire_Delay(50);
DQ=1;
Wire_Delay(60);
}
void Write_Byte(unsigned char val)
{
unsigned char i=0;
for(i=0;i<8;i++)
{
Write_Bit(val);
val>>=1;
}
}
unsigned char Read_Byte(void)
{
unsigned char i=0,receive_data=0;
for(i=0;i<8;i++)
{
receive_data>>=1;
if(Read_Bit())
{
receive_data |=0x80;
}
else
{
receive_data &=0x7f;
}
}
return(receive_data);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -