STM32学习笔记之 DS18B20 SEARCH ROM

发布者:beta12最新更新时间:2024-10-25 来源: cnblogs关键字:STM32  DS18B20  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 ROM_NO[8];数组里面

然后一直调用 int  OWSearch(); 发现其他期间 如果期间存在返回1,并且把ID存在 unsigned char ROM_NO[8];数组里面

 

以下为具体代码,可以将以下的存为一个.h文件,使用的时候直接包含进去就可以调用…

#ifndef     SEARCH_H_H_H
#define        SEARCH_H_H_H
#include

#include '18B20.h'

/* Private define ------------------------------------------------------------*/
//ROM操作指令
//读ROM
#define     READ_ROM            0x33
//匹配ROM
#define     MATCH_ROM            0x55
//跳过ROM
#define        SKIP_ROM            0xcc
//搜索ROM
#define        SEARCH_ROM            0xf0
//告警搜索
#define        ALARM_SEARCH        0xec

//存储器操作指令
//写暂存存储器
#define        WRITE_SCRATCHPAD    0x4e
//读暂存存储器
#define        READ_SCRATCHPAD        0xbe
//复制暂存存储器
#define        COPY_SCRATCHPAD        0x48
//温度变换
#define        CONVERT_TEMPERATURE    0x44
//重新调出
#define        RECALL_EPROM        0xb8
//读电源
#define        READ_POWER_SUPPLY    0xb4  

// method declarations
int  OWFirst();
int  OWNext();
int  OWVerify();
void OWTargetSetup(unsigned char family_code);
void OWFamilySkipSetup();
int  OWReset();
void OWWriteByte(unsigned char byte_value);
void OWWriteBit(unsigned char bit_value);
unsigned char OWReadBit();
int  OWSearch();
unsigned char docrc8(unsigned char value);

// global search state
unsigned char ROM_NO[8];
int LastDiscrepancy;
int LastFamilyDiscrepancy;
int LastDeviceFlag;
unsigned char crc8;

//--------------------------------------------------------------------------
// Find the 'first' devices on the 1-Wire bus
// Return TRUE  : device found, ROM number in ROM_NO buffer
//        FALSE : no device present
//
int OWFirst()
{
    // reset the search state
    LastDiscrepancy = 0;
    LastDeviceFlag = FALSE;
    LastFamilyDiscrepancy = 0;

    return OWSearch();
}

//--------------------------------------------------------------------------
// Find the 'next' devices on the 1-Wire bus
// Return TRUE  : device found, ROM number in ROM_NO buffer
//        FALSE : device not found, end of search
//
int OWNext()
{
    // leave the search state alone
    return OWSearch();
}

//--------------------------------------------------------------------------
// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
// search state.
// Return TRUE  : device found, ROM number in ROM_NO buffer
//        FALSE : device not found, end of search
//
int OWSearch()
{
    int id_bit_number;
    int last_zero, rom_byte_number, search_result;
    int id_bit, cmp_id_bit;
    unsigned char rom_byte_mask, search_direction;

    // initialize for search
    id_bit_number = 1;
    last_zero = 0;
    rom_byte_number = 0;
    rom_byte_mask = 1;
    search_result = 0;
    crc8 = 0;

    // if the last call was not the last one
    if (!LastDeviceFlag)
    {
        //USART_SendString('Searching !LastDeviceFlag');
        // 1-Wire reset
        if (!OWReset())
        {

            // reset the search
            LastDiscrepancy = 0;
            LastDeviceFlag = FALSE;
            LastFamilyDiscrepancy = 0;
            return FALSE;
        }

        // issue the search command
        OWWriteByte(SEARCH_ROM); 

        // loop to do the search
        do
        {
            // read a bit and its complement
            id_bit = OWReadBit();
            cmp_id_bit = OWReadBit();

            // check for no devices on 1-wire
            if ((id_bit == 1) && (cmp_id_bit == 1))
                {
                    USART_SendString('NODeviceFlagDO');
                    break;
                }
            else
            {
                // all devices coupled have 0 or 1
                if (id_bit != cmp_id_bit)
                    search_direction = id_bit;  // bit write value for search
                else
                {
                    // if this discrepancy if before the Last Discrepancy
                    // on a previous next then pick the same as last time
                    if (id_bit_number < LastDiscrepancy)
                        search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
                    else
                        // if equal to last pick 1, if not then pick 0
                        search_direction = (id_bit_number == LastDiscrepancy);

                    // if 0 was picked then record its position in LastZero
                    if (search_direction == 0)
                    {
                        last_zero = id_bit_number;

                        // check for Last discrepancy in family
                        if (last_zero < 9)
                            LastFamilyDiscrepancy = last_zero;
                    }
                }

                // set or clear the bit in the ROM byte rom_byte_number
                // with mask rom_byte_mask
                if (search_direction == 1)
                    ROM_NO[rom_byte_number] |= rom_byte_mask;
                else
                    ROM_NO[rom_byte_number] &= ~rom_byte_mask;

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

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

小广播
最新单片机文章
何立民专栏 单片机及嵌入式宝典

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

厂商技术中心

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

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