版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/louyangyang91/article/details/55511369
最近使用到DS18B20,一个总线挂载多个传感器,为了识别每个传感器的实际位置,想了一个办法:在使用前先对DS18B20编号并记录该编号的实际放置位置。这样在读取数据后即可得到是哪个位置编号的温度值。看了看手册发现内部的高低温报警阈值是存储在EEPROM正好用来存储位置编号。
首先了解下通讯过程,ROM指令,RAM指令以及寄存器内容:
位置编号存储在 高温限值(TH)和低温限值(TL)中
写入编号程序如下:
DS18B20_Start (); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
//rom指令
DS18B20_Write_Byte(0xcc);// skip rom
//ram指令
DS18B20_Write_Byte(0x4e);// 写暂存器
//写报警值
DS18B20_Write_Byte(addrH);// 写入高位
DS18B20_Write_Byte(addrL);// 写入低位
DS18B20_Rst();
DS18B20_Check();
//rom指令
DS18B20_Write_Byte(0xcc);// skip rom
//ram指令
DS18B20_Write_Byte(0x4e);// 写暂存器
//写报警值
DS18B20_Write_Byte(addrH);// 写入高位
DS18B20_Write_Byte(addrL);// 写入低位
写入以后需要存储到EEPROM中,程序如下:
DS18B20_Start (); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Rst();
DS18B20_Check();
//rom指令
DS18B20_Write_Byte(0xcc);// skip rom
//ram指令
DS18B20_Write_Byte(0x48);// 复制暂存器
DS18B20_Write_Byte(0xcc);// skip rom
//ram指令
DS18B20_Write_Byte(0x48);// 复制暂存器
读取编号程序如下:
DS18B20_Start (); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0xbe);// 读暂存器
DS18B20_Write_Byte(0xbe);// 读暂存器
TL = DS18B20_Read_Byte(); // 温度低位0
TH = DS18B20_Read_Byte(); // 温度高位1
ADDRH = DS18B20_Read_Byte(); // 地址高位2
ADDRL = DS18B20_Read_Byte(); // 地址低位 3