?? ds18b20.c
字號:
/*
***************************************************************************************************
* Copyright (C),2007
* Author : Zhongsan Yan
* Email : yanzhongsan@gmail.com
* Date : 2007-10-17
* File name : DS18B20.c
* Description : DS1302 driver file
* Version : V 1.0
* Others : This file is the driver of DS1302
***************************************************************************************************
*/
//Header files
#include "includes.h"
#include "DS18B20.h"
#include "HT1621B.h"
/*
***************************************************************************************************
* Function name :
* Description :
* Input :
* Output :
* Others :
***************************************************************************************************
*/
UCHAR_8 DS18B20_reset(void)
{
UCHAR_8 bit;
volatile UCHAR_8 time_over;
SET_TEMP_LINE_OUT;
SET_TEMP_LINE_LOW;
DELAY_1us(500);
SET_TEMP_LINE_HIGH;
SET_TEMP_LINE_IN;
while (TEMP_LINE_IS_HIGH&&time_over)
{
WDR();
time_over++;
}
if (0==time_over)
{
return(0xff);
}
time_over=1;
bit=TEMP_LINE_STATUS;
while ((TEMP_LINE_IS_LOW)&&time_over)
{
WDR();
time_over++;
}
if (0==time_over)
{
return(0xff);
}
return(bit);
}
UINT_16 DS18B20_read_byte(void)
{
UCHAR_8 i;
UINT_16 value=0;
DELAY_1us(10);
for (i=0;i<16;i++)
{
SET_TEMP_LINE_OUT;
value>>=1;
SET_TEMP_LINE_LOW;
DELAY_1us(3);
SET_TEMP_LINE_HIGH;
SET_TEMP_LINE_IN;
DELAY_1us(7);
if (TEMP_LINE_IS_HIGH)
{
value |= 0x8000;
}
DELAY_1us(55);
}
return(value);
}
void DS18B20_write_byte(UCHAR_8 val)
{
UCHAR_8 i;
DELAY_1us(10);
SET_TEMP_LINE_OUT;
for (i=0;i<8;i++)
{
SET_TEMP_LINE_LOW;
DELAY_1us(2);
if (val&0x01)
{
SET_TEMP_LINE_HIGH;
}
val >>= 1;
DELAY_1us(35);
SET_TEMP_LINE_HIGH;
DELAY_1us(3);
}
}
UCHAR_8 DS18B20Begin(void)
{
if (0xff==DS18B20_reset())
{
return(0xff);
}
DS18B20_write_byte(0xCC);//skip rom 命令
DS18B20_write_byte(0x44);//Convert T 命令
return (1);
}
UINT_16 DS18B20_get_tem(void)
{
if (0xff==DS18B20_reset())
{
return(0xff);
}
DS18B20_write_byte(0xCC);//skip rom 命令
DS18B20_write_byte(0xBE);//Read Scratchpad 命令
return(DS18B20_read_byte());
}
void TempCodeCover(UINT_16 temp)
{
temp >>= 4;
temp &= 0x00FF;
if (0x00==TESTBIT(temp,7))
{
CLEARBIT(SysFlag,Nag_bit);//清除負溫標志
if (temp>100)
{
//超過99度將顯示為99度
HT1621BWritedata(20,LED_CODE[9]);
HT1621BWritedata(22,LED_CODE[9]);
}
else
{
if (temp>=10)
{
if (TESTBIT(SysFlag,Mute_Bit))
{
HT1621BWritedata(20,LED_CODE[(temp/10)%10]&0x7F);//顯示十位數,不顯示喇叭
}
else
{
HT1621BWritedata(20,LED_CODE[(temp/10)%10]);//顯示十位數,顯示喇叭
}
}
else
{
if (TESTBIT(SysFlag,Mute_Bit))
{
HT1621BWritedata(20,0x00);//不顯示
}
else
{
HT1621BWritedata(20,0x80);//顯示喇叭
}
}
HT1621BWritedata(22,LED_CODE[temp%10]);//顯示個位
}
}
else
{
SETBIT(SysFlag,Nag_bit);//設置負溫標志
temp = -temp;//取反
temp &= 0x07ff;//去符號位
temp = temp>>4;//去小數部分
if (temp>=10)
{
HT1621BWritedata(20,LED_CODE[(temp/10)%10]);//顯示十位數
}
else
{
HT1621BWritedata(20,0x00);//不顯示
}
HT1621BWritedata(22,0x00);//顯示個位
}
}
void ReadTempAndDisplay(void)
{
static UCHAR_8 sw=0;
if (0x00==sw)
{
sw = 0x01;
DS18B20Begin();
}
else
{
sw = 0x00;
TempCodeCover(DS18B20_get_tem());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -