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

📄 ds18b20.c

📁 倒车雷达的原理图与源码
💻 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 + -