使用说明,根据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;
上一篇:STM32中断使用笔记
下一篇:STM32中ucos的编写程序
- 热门资源推荐
- 热门放大器推荐
- 基于 Blackfin 数字信号处理器 (DSP) 的 ADZS-BF518F-EZLITE、ADSP-BF518F EZ-Kit Lite 评估系统
- 使用 ON Semiconductor 的 CAT3200Z 的参考设计
- LTC2162 演示板,16 位 65Msps ADC,LVDS 输出,5-140MHz
- 使用 Analog Devices 的 LTC3526BEDC-2 的参考设计
- LT3091HT7 在极低输出电压下低压降操作的典型应用
- 蓝牙协议分析工具nRF52840 MDK USB Dongle
- NCV2902DR2G 维恩桥振荡器运算放大器的典型应用
- LTC4100EGN 演示板,智能电池充电器 DCIN = 15V-20V / 3.5V
- TC78H620FNG 双桥直流有刷电机驱动器评估板
- 带有 DRP w/Try.SRC 和 Type-C 插座的 PTN5110 USB PD 的典型应用



非常经典的关于LLC的杨波博士论文
MCP6C02T-050H/Q8B

1CIS223-04TG3M






京公网安备 11010802033920号