STM32学习笔记之 DS18B20 SEARCH ROM

发布者:beta12最新更新时间:2024-10-25 来源: cnblogs关键字:STM32  DS18B20  ROM 手机看文章 扫描二维码
随时随地手机看文章

                // serial number search direction write bit
                OWWriteBit(search_direction);

                // increment the byte counter id_bit_number
                // and shift the mask rom_byte_mask
                id_bit_number++;
                rom_byte_mask <<= 1;

                // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
                if (rom_byte_mask == 0)
                {
                    docrc8(ROM_NO[rom_byte_number]);  // accumulate the CRC
                    rom_byte_number++;
                    rom_byte_mask = 1;
                }
            }
        }
        while(rom_byte_number < 8);  // loop until through all ROM bytes 0-7

        // if the search was successful then
        if (!((id_bit_number < 65) || (crc8 != 0)))
        {
            // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
            LastDiscrepancy = last_zero;

            // check for last device
            if (LastDiscrepancy == 0)
                LastDeviceFlag = TRUE;

            search_result = TRUE;
        }       
    }

    // if no device found then reset counters so next 'search' will be like a first
    if (!search_result || !ROM_NO[0])
    {
        LastDiscrepancy = 0;
        LastDeviceFlag = FALSE;
        LastFamilyDiscrepancy = 0;
        search_result = FALSE;
    }

    return search_result;
}

//--------------------------------------------------------------------------
// Verify the device with the ROM number in ROM_NO buffer is present.
// Return TRUE  : device verified present
//        FALSE : device not present
//
int OWVerify()
{
    unsigned char rom_backup[8];
    int i,rslt,ld_backup,ldf_backup,lfd_backup;

    // keep a backup copy of the current state
    for (i = 0; i < 8; i++)
        rom_backup[i] = ROM_NO[i];
    ld_backup = LastDiscrepancy;
    ldf_backup = LastDeviceFlag;
    lfd_backup = LastFamilyDiscrepancy;

    // set search to find the same device
    LastDiscrepancy = 64;
    LastDeviceFlag = FALSE;

    if (OWSearch())
    {
        // check if same device found
        rslt = TRUE;
        for (i = 0; i < 8; i++)
        {
            if (rom_backup[i] != ROM_NO[i])
            {
                rslt = FALSE;
                break;
            }
        }
    }
    else
        rslt = FALSE;

    // restore the search state
    for (i = 0; i < 8; i++)
        ROM_NO[i] = rom_backup[i];
    LastDiscrepancy = ld_backup;
    LastDeviceFlag = ldf_backup;
    LastFamilyDiscrepancy = lfd_backup;

    // return the result of the verify
    return rslt;
}

//--------------------------------------------------------------------------
// Setup the search to find the device type 'family_code' on the next call
// to OWNext() if it is present.
//
void OWTargetSetup(unsigned char family_code)
{
    int i;

    // set the search state to find SearchFamily type devices
    ROM_NO[0] = family_code;
    for (i = 1; i < 8; i++)
        ROM_NO[i] = 0;
    LastDiscrepancy = 64;
    LastFamilyDiscrepancy = 0;
    LastDeviceFlag = FALSE;
}

//--------------------------------------------------------------------------
// Setup the search to skip the current device type on the next call
// to OWNext().
//
void OWFamilySkipSetup()
{
    // set the Last discrepancy to last family discrepancy
    LastDiscrepancy = LastFamilyDiscrepancy;
    LastFamilyDiscrepancy = 0;

    // check for end of list
    if (LastDiscrepancy == 0)
        LastDeviceFlag = TRUE;
}

//--------------------------------------------------------------------------
// 1-Wire Functions to be implemented for a particular platform
//--------------------------------------------------------------------------

//--------------------------------------------------------------------------
// Reset the 1-Wire bus and return the presence of any device
// Return TRUE  : device present
//        FALSE : no device present
//
int OWReset()
{
    u8 temp = 0;
    DQ(1);
    DQ(0);
    DqDelay(784);                    //784
    DQ(1);
    DqDelay(110);                    //110
    temp = READ_DQ;
    DqDelay(675);                      //675

    return !temp;    
}

//--------------------------------------------------------------------------
// Send 8 bits of data to the 1-Wire bus
//
void OWWriteByte(unsigned char dat)
{
    u8 i = 0;
    for ( i = 0 ; i < 8 ; i++ )
    {
        DQ(0);
        DqDelay(1);
        if ( dat & 0x01 )
        {
            DQ(1);   
        }
        else
        {
            DQ(0);
        }
        dat = dat >> 1;
        DqDelay(100);
        DQ(1);
        DqDelay(1);           
    }

}

//--------------------------------------------------------------------------
// Send 1 bit of data to teh 1-Wire bus
//
void OWWriteBit(unsigned char bit_value)
{
    DQ(0);
    DqDelay(1);
    if ( bit_value & 0x01 )
    {
        DQ(1);   
    }
    else
    {
        DQ(0);
    }
    DqDelay(100);
    DQ(1);
    DqDelay(1);       
}

//--------------------------------------------------------------------------
// Read 1 bit of data from the 1-Wire bus
// Return 1 : bit read is 1
//        0 : bit read is 0
//
unsigned char OWReadBit()
{
    u8 dat = 0;
    DQ(0);
    DqDelay(1);
    DQ(1);
    DqDelay(2);
    if ( READ_DQ )
    {
        dat = 1;
    }
    DqDelay(100);
    return dat;
}

// TEST BUILD
static unsigned char dscrc_table[] = {
    0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
    157,195, 33,127,252,162, 64, 30, 95,  1,227,189, 62, 96,130,220,
    35,125,159,193, 66, 28,254,160,225,191, 93,  3,128,222, 60, 98,
    190,224,  2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
    70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89,  7,
    219,133,103, 57,186,228,  6, 88, 25, 71,165,251,120, 38,196,154,
    101, 59,217,135,  4, 90,184,230,167,249, 27, 69,198,152,122, 36,
    248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91,  5,231,185,
    140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
    17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
    175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
    50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
    202,148,118, 40,171,245, 23, 73,  8, 86,180,234,105, 55,213,139,
    87,  9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
    233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
    116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};

[1] [2] [3]
关键字:STM32  DS18B20  ROM 引用地址:STM32学习笔记之 DS18B20 SEARCH ROM

上一篇:STM32中断使用笔记
下一篇:STM32中ucos的编写程序

推荐阅读最新更新时间:2026-02-17 20:02

STM32学习笔记之 DS18B20 SEARCH ROM
使用说明,根据MCU不同 用户只需修改4函数 //单总线复位函数 int OWReset(); 单线总线的复位函数,注意这个要做相应修改,如果期间存在要返回1,期间不存在返回0, 直接从总线上读取的是期间存在返回0,不存在返回1 //向总线发送一个字节 void OWWriteByte(unsigned char dat); //向总线发送一位 void OWWriteBit(unsigned char bit_value); //读取总线一位 unsigned char OWReadBit(); 使用时用 int OWFirst();发现第一个单线器件 如果期间存在返回1,并且把ID存在 unsigned char
[单片机]
单片机 STM32 HAL 温湿度 DS18B20
/*************笔记**************** 1、CubeMX 定义任意一个引脚,作为数据脚,并对引脚作出如下配置: GPlO output level --LOW GPIO mode --Output open drai GPIO Pull-up/Pull-down --No pull-up and no pull-down Maximum output speed --LOW User label --DS18S20 --------------------------------------------------------- *********
[单片机]
完美实现STM32单总线挂多个DS18B20
一般常见的STM32的关于DS18B20的例程都是检测一个传感器,代码一般都是跳过ROM检测,直接获取温度值。这种写法并不适用于单总线上挂载多个DS18B20的情况,Sandeepin的这个代码就是针对这种情况完善的单总线挂多个DS18B20检测,实现获取每个DS18B20的ID和温度。 主要的DS18B20时序代码没变,增加了搜索ROM函数,获取温度时先匹配ID。 核心代码如下: DS18B20.c文件代码: #include DS18B20.h #include Delay.h #include stdio.h // printf用 #define DS18B20_GPIO_NUM
[单片机]
完美实现<font color='red'>STM32</font>单总线挂多个<font color='red'>DS18B20</font>
STM32下单只DS18B20的驱动
折腾了一晚上,才把DS18B20的驱动移植到STM32上来。以前在51上使用过单个和多个连接的DS18B20,有现成的程序了,以为很快就能弄好,结果还是被卡住了,下面说下几个关键点吧: 首先是延时的问题,STM32上若用软件延时的话不太好算时间,所以要么用定时器要么用SysTick这个定时器来完成延时的计算。相比之下用SysTick来的简单方便点。 接着是STM32 IO脚的配置问题,因为51是双向的IO,所以作为输入输出都比较方便。STM32的IO是准双向的IO,网上查了下资料,说将STM32的IO配置成开漏输出,然后外接上拉即可实现双向IO。于是我也按规定做了,但调了老半天都不成功,是因为DS18B20没有响应的信号
[单片机]
stm32 驱动DS18B20温度传感器
#include temp.h #define u8 uint8_t #define u16 uint16_t #define u32 uint32_t #define DS18B20_PORT GPIOA #define DS18B20_PIN GPIO_Pin_1 #define DS18B20_CLK RCC_APB2Periph_GPIOA #define RW1820_DQ_HIGH GPIO_SetBits(DS18B20_PORT, DS18B20_PIN); #define RW1820_DQ_LOW GPIO_ResetBits(DS18B20_PORT
[单片机]
STM32单片机学习(11) DS18B20温度传感器实验
本程序主要实现 DS18B20温度传感器数据获取,并利用串口通信把温度数据传至计算机 注:使用普中科技开发板测试时,需要拔掉Boot1插口,因为用到的是PA15管脚, 由开发板电路图可知,需要改变PA15 管脚的映射,将其设置成普通IO口 参考资料 DS18B20中文手册.pdf http://download.csdn.net/detail/leytton/7742193 STM32-外设篇 视频教程(Cortex-M3)-主讲人:刘洋 http://yun.baidu.com/pcloud/album/info?uk=2853967793&album_id=5492137931588632574 main.c
[单片机]
stm32上移植了ucos,现在需要使用到DS18B20采集温度
DS18B20的操作时序要使用到延时,我是用ucos的延时函数还是自己循环写一个延时? 现在有个问题:假如用ucos的延时函数,如果出现更高优先级的任务,那么会将读DS18B20这个任务挂起,会破坏DS18B20的时序操作,读不出来温度。这个延时我应该怎么处理? 分享到: 2012-08-14 08:40提问者采纳 第一 读取18B20的时候关闭任务切换,不进行任务调度 第二 通过任务通讯 互斥量 或者邮箱 让其他任务挂起 第三 关闭中断 第四 将操作18b20的任务优先级设置高点或者操作时提升其任务优先级 延时的话可以用ucos的延时函数 也可以自己写,不过还是用ucos的好 追问 谢
[单片机]
STM32单片机的内存分布详解(1)
STM32具有和PC机类似的结构组成。那么我们平时听到的RAM和ROM就相当于PC的内存条和硬盘,当然了PC的硬盘和单片机的ROM也并不是所谓的只读,只不过以前的技术原因很难做到多次读写,因为最初的存储器是纸带、熔丝或者其他一次性存储器,因此只能读取,也就是ROM,当然那时候也不需要持续的修改。随着技术的革新,这些最初的ROM慢慢的增大内存、增加写功能、提高读写速度。而ROM的叫法也一直延续下来。 扩展一下,为什么ROM也可以写,不用ROM当RAM呢?因为ROM还是太慢了,读取可还行(比如F7、H7就支持QSPI运行程序),写就是肉眼可见级别的延迟了。所以RAM这种需要高速读写的还是让SRAM、SDRAM、DDR等
[单片机]
小广播
最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

厂商技术中心

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2026 EEWORLD.com.cn, Inc. All rights reserved