本文主要是介绍基于DS18B20的温度检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
DS18B20是DALLAS半导体公司生产的单总线数字温度传感器,其输出的是数字信号,具有体积小,功耗低,抗干扰能力强,精度高的特点。
温度范围-55摄氏度至+125摄氏度,在-10摄氏度至+85摄氏度可以达到不超过 0.5摄氏度的误差。单端口节省FPGA接口资源。传感器内部含EEPROM内部存储温度阈值,测量分辨率,系统掉电保护数据。
正文
一、基于DS18B20的温度检测
1.项目需求
实验目标:基于ds18b20数据温度传感器,完成基于FPGA的温度计设计。(数码管显示)
2.技术介绍
每个DS18B20的片内ROM都存有一个独一无二的64位编码。在该ROM编码的低8位保存有DS18B20的分类编码:28h;中间的48位是独一无二的序列号;最高8位是前面56位的CRC循环冗余校验码 (CRC=X8+X5+X4+1)。
因为每个DS18B20的序列号都不一样,所以一条总线上可以控制多个DS18B20。
温度测量方法:DS18B20中的温度传感器可完成对温度的测量,他的温度转换精度用户可自定义为9、10、11、12位,精度分别为0.5℃、0.25℃、0.125℃、0.0625℃分辨率。
符号标志位(S)(5位)表示温度的正负极性:若S=0,则为正数;若S=1,则为负数。
温度数据正数源码输出,负数补码输出(计算温度时求补码)输出二进制数*温度精度=当前温度
高速缓冲器中第5个字节即为配置寄存器,通过修改R1,R2的值来配置DS18B20的分辨率。TM(0,工作模式,1测试模式),默认12位宽。
DS18B20实现温度转换
主机控制DS18B20完成温度转换必须经过三个步骤:每一次读写之前都要对DS18B20进行 初始化操作,初始化成功后发送一条ROM命令,最后发送RAM命令,这样才能对DS18B20进行预定的操作。
1.初始化:单总线上的所有操作都必须以初始化为开始。初始化序列由总线上的主设备发出的复位脉冲以及紧跟着从设备回应的存在脉冲构成。该存在脉冲是让总线主设备知道DS18B20在总线上并准备好运行。
2.ROM命令:搜索ROM[F0h](识别从机)、读ROM[33h](数据总线存在一个从机)、匹配ROM[55h](多从机选定从机工作)、跳过ROM[CCh]、警报搜索ROM[ECh]。
3.RAM命令:温度转换[44h](单次温度转换)、写入暂存器[4Eh](向传感器写入温度阈值与分辨率)、读取高速缓存器[BEh]、复制高速缓存器[48H1]、召回EEPROM[B8h]、读取供电模式[B4h]。(0寄存电容,1外部供电)
数据传输过程:与DS18B20所有的通信都是由初始化开始的,初始化由主设备发出的复位脉冲及从设备响应的存在脉冲组成。主设备发出480-960us的低电平脉冲,之后释放总线,紧接着主设备进行480us的总线检测。总线有低电平出现表示总线上的器件应答。从设备进行480-960us的应答检测,检测到后进行数据输出到数据总线。
写时序:主设备通过写时序将命令写入DS18B20中,写时序模式有1,0两种模式。数据总线由高电平拉至低电平时进入写时序。
读时序:发送读取供电模式[B4h]或读高速缓存区[BEh]命令时,及时生成读时序在读时隙从设备才可以向主设备传输数据。
这里使用状态机,简便实验代码。
3.顶层架构
4.端口描述
clk | 时钟(50Mhz) |
rst_n | 复位信号(低电平有效) |
dp | 数据总线 |
[2:0]sel | 位选信号 |
[7:0]seg | 段选信号 |
二、代码验证
数据处理模块
module ds18b20_data(input clk ,input rst_n ,inout dp ,//数据总线output wire[19:0] data_out ,//读取到的数据output reg data_flag //符号位);//0011_0011;跳过ROM命令 CCh
//0010_0010;温度转换命令 44h
//1111_0010;写入暂存器命令 4Eh
//0111_1101;读温度命令 BEhparameter date = 3'd1,//初始化wier = 3'd2,//写命令dend = 3'd3,//等待转换datt = 3'd4,//再次初始化romx = 3'd5,//Rom功能命令duqu = 3'd6;//数据读取
parameter cch = 16'h44cc,beh = 16'hbecc;reg [4:0]cnt_1ns;//1ns计数器
reg clk_1ns;//1ns时钟reg [19:0]cnt_us;//us计数器
reg flag_us;//存在脉冲有效信号reg[2:0]stater;//状态机状态
reg[3:0]bit_cnt;//bit计数器reg[15:0]data_temp;//存储接收的温度数据
reg[19:0]data_temp_t;//温度数据进行正负判断,小数点位统一,((数据*625(精度)/10000)=摄氏度)*1000=数码管数据,反码源码转换reg dp_en;//输出为1,输入为0
reg dp_out;//输入0,输出为操作命令assign dp = (dp_en ==1'b1)? dp_out:1'bz;always@(posedge clk_1ns,negedge rst_n) //符号位
beginif(rst_n == 0)data_flag <= 1'b0;elseif((stater == duqu)&&(bit_cnt == 4'd15)&&(cnt_us == 20'd60)&&(data_temp[15]== 1'b0))data_flag <= 1'b0;else if((stater == duqu)&&(bit_cnt == 4'd15)&&(cnt_us == 20'd60)&&(data_temp[15]== 1'b1))data_flag <= 1'b1;elsedata_flag <= data_flag;
endalways@(posedge clk,negedge rst_n)//1ns计数器使能模块
beginif(rst_n == 0)cnt_1ns <= 5'd0;elseif(cnt_1ns == 5'd24)cnt_1ns <= 5'd0;elsecnt_1ns <= cnt_1ns + 5'd1;
endalways@(posedge clk,negedge rst_n)//1ns时钟
beginif(rst_n == 0)clk_1ns <= 1'b0;elseif(cnt_1ns == 5'd24)clk_1ns <= ~clk_1ns;elseclk_1ns <= clk_1ns;
endalways@(posedge clk_1ns,negedge rst_n)//us计数器使能
beginif(rst_n == 0)cnt_us <= 20'd0;elseif((((stater == date)||(stater == datt))&&(cnt_us == 20'd999))||(((stater == wier)||(stater == romx)||(stater == duqu))&&(cnt_us == 20'd64))||((stater == dend)&&(cnt_us == 20'd750_000)))cnt_us <= 20'd0;elsecnt_us <= cnt_us + 20'd1;
end always@(posedge clk_1ns,negedge rst_n)//存在脉冲有效
beginif(rst_n == 0)flag_us <= 1'b0;elseif((((stater == date)||(stater == datt))&&(cnt_us == 20'd570)&&(dp == 1'b0)))flag_us <= 1'b1;elseif(((stater == date)||(stater == datt))&&(cnt_us == 20'd999))flag_us <= 1'b0;elseflag_us <= flag_us;
endalways@(posedge clk_1ns,negedge rst_n)//bit计数器使能
beginif(rst_n == 0)bit_cnt <= 4'd0;elseif(((stater == wier)||(stater == romx)||(stater == duqu))&&(bit_cnt == 4'd15)&&(cnt_us == 20'd64))bit_cnt <= 4'd0;elseif(((stater == wier)||(stater == romx)||(stater == duqu))&&(cnt_us == 20'd64))bit_cnt <= bit_cnt + 4'd1;elsebit_cnt <= bit_cnt;
endalways@(posedge clk_1ns,negedge rst_n)//存储接收的温度数据
beginif(rst_n == 0)data_temp <= 16'd0;elseif((stater == duqu)&&(cnt_us == 20'd13))data_temp <= {dp,data_temp[15:1]}; elsedata_temp <= data_temp;
endalways@(posedge clk_1ns,negedge rst_n)//温度数据进行正负判断,小数点位统一,((数据*625(精度)/10000)=摄氏度)*1000=数码管数据,反码源码转换
beginif(rst_n == 0)data_temp_t <= 20'd0;elseif((stater == duqu)&&(bit_cnt == 4'd15)&&(cnt_us == 20'd60)&&(data_temp[15]== 1'b0))data_temp_t <= data_temp[11:0];elseif(((stater == duqu)&&(bit_cnt == 4'd15)&&(cnt_us == 20'd60)&&(data_temp[15]== 1'b0)))data_temp_t <= ~data_temp[11:0] + 1'b1;elsedata_temp_t <= data_temp_t;
endassign data_out = (data_temp_t * 625) /10;always@(posedge clk_1ns,negedge rst_n)//状态机状态跳转条件
beginif(rst_n == 0)stater <= date;else case(stater)date : if((cnt_us == 20'd999) && (flag_us == 1'b1))stater <= wier;elsestater <= date;wier : if((bit_cnt == 4'd15) &&(cnt_us == 20'd64))stater <= dend;elsestater <= wier;dend : if(cnt_us == 20'd750_000)stater <= datt;elsestater <= dend;datt : if((cnt_us == 20'd999) && (flag_us == 1'b1))stater <= romx;elsestater <= datt;romx : if((bit_cnt == 4'd15) &&(cnt_us == 20'd64))stater <= duqu;elsestater <= romx;duqu : if((bit_cnt == 4'd15) &&(cnt_us == 20'd64))stater <= date;elsestater <= duqu;default :stater <= date;endcase
endalways@(posedge clk_1ns,negedge rst_n)//输出为1,输入为0
begin //输入0,输出为操作命令if(rst_n == 0)begindp_en <= 1'b0;dp_out <= 1'b0;endelsecase(stater)date : if(cnt_us <499)begindp_en <= 1'b1;dp_out <= 1'b0;endelsebegindp_en <= 1'b0;dp_out <= 1'b0;endwier : if(cnt_us >62)begindp_en <= 1'b0;dp_out <= 1'b0;endelseif(cnt_us <= 1)begindp_en <= 1'b1;dp_out <= 1'b0;endelseif(beh[bit_cnt] == 1'b0)begindp_en <= 1'b1;dp_out <= 1'b0;endelseif(beh[bit_cnt] == 1'b1)begindp_en <= 1'b0;dp_out <= 1'b0;enddend : begindp_en <= 1'b1;dp_out <= 1'b1;end datt : if(cnt_us <499)begindp_en <= 1'b1;dp_out <= 1'b0;endelsebegindp_en <= 1'b0;dp_out <= 1'b0;end romx : if(cnt_us >62)begindp_en <= 1'b0;dp_out <= 1'b0;endelseif(cnt_us <= 1)begindp_en <= 1'b1;dp_out <= 1'b0;endelseif(cch[bit_cnt] == 1'b0)begindp_en <= 1'b1;dp_out <= 1'b0;endelseif(cch[bit_cnt] == 1'b1)begindp_en <= 1'b0;dp_out <= 1'b0;endduqu : if(cnt_us <= 1)begindp_en <= 1'b1;dp_out <= 1'b0;endelsebegindp_en <= 1'b0;dp_out <= 1'b0;end default : begindp_en <= 1'b0;dp_out <= 1'b0;endendcase
end endmodule
数码管显示模块
module shu_ma_g_MAX(input clk ,input rst_n ,input [19:0] data ,//数据10进制input [ 5:0] point ,//小数点位input signe ,//正负号位input seg_en ,//数码管使能output reg[2:0] sel ,output reg[7:0] seg );parameter cnt_1ms_max = 16'd49999;wire[3:0] data1 ;//数码管数据
wire[3:0] data2 ;//数码管数据
wire[3:0] data3 ;//数码管数据
wire[3:0] data4 ;//数码管数据
wire[3:0] data5 ;//数码管数据
wire[3:0] data6 ;//数码管数据reg[23:0]data_reg ;//寄存显示数据reg[15:0]cnt_1ms ;//1ms计数器
reg cnt_1ms_flag ;
reg[2:0] cnt_sel ;//扫描周期计数器
reg[3:0] data_zhan ;//数码管显示暂存数据
reg point_flag ;//小数点使能信号
reg[2:0] sel_reg ;//暂存输出always@(posedge clk,negedge rst_n)//判断符号位
beginif(rst_n == 0)data_reg <= 24'b0;elseif((data6)||(point[5]))data_reg <= {data6,data5,data4,data3,data2,data1};elseif(((data5)||(point[4]))&&(signe == 1'b1))//最高位为符号位011-111data_reg <= {4'd10,data5,data4,data3,data2,data1};elseif(((data5)||(point[4]))&&(signe == 1'b0))data_reg <= {4'd11,data5,data4,data3,data2,data1};elseif(((data4)||(point[3]))&&(signe == 1'b1))//次高位为符号位001-111data_reg <= {4'd11,4'd10,data4,data3,data2,data1};elseif(((data4)||(point[3]))&&(signe == 1'b0))data_reg <= {4'd11,4'd11,data4,data3,data2,data1};elseif(((data3)||(point[2]))&&(signe == 1'b1))//次次高位为符号位000-111data_reg <= {4'd11,4'd11,4'd10,data3,data2,data1};elseif(((data3)||(point[2]))&&(signe == 1'b0))data_reg <= {4'd11,4'd11,4'd11,data3,data2,data1};elseif(((data2)||(point[1]))&&(signe == 1'b1))//次次次高位为符号位000-011data_reg <= {4'd11,4'd11,4'd11,4'd10,data2,data1};elseif(((data2)||(point[1]))&&(signe == 1'b0))data_reg <= {4'd11,4'd11,4'd11,4'd11,data2,data1};elseif(((data1)||(point[0]))&&(signe == 1'b1))//次次次次高位为符号位000-001data_reg <= {4'd11,4'd11,4'd11,4'd11,4'd10,data1};elsedata_reg <= {4'd11,4'd11,4'd11,4'd11,4'd11,data1};endalways@(posedge clk,negedge rst_n) //计数器驱动模块
beginif(rst_n == 0)cnt_1ms <= 16'd0;elseif(cnt_1ms == cnt_1ms_max)cnt_1ms <= 16'd0;elsecnt_1ms <= cnt_1ms + 16'd1;endalways@(posedge clk,negedge rst_n)//1ms标志信号
beginif(rst_n == 0)cnt_1ms_flag <= 1'b0;elseif(cnt_1ms == cnt_1ms_max)cnt_1ms_flag <= 1'b1;elsecnt_1ms_flag <= 1'b0;
endalways@(posedge clk,negedge rst_n)//扫描周期计数器使能
beginif(rst_n == 0)cnt_sel <= 3'd0;elseif((cnt_sel == 3'd5)&&(cnt_1ms_flag <= 1'b1))cnt_sel <= 3'd0;elseif(cnt_1ms_flag <= 1'b1)cnt_sel <= cnt_sel + 3'd1;elsecnt_sel <= cnt_sel;
endalways@(posedge clk,negedge rst_n)//暂存输出
beginif(rst_n == 0)sel_reg <= 3'd0;elseif((cnt_sel == 3'd0)&&(cnt_1ms_flag <= 1'b1))sel_reg <= 3'd0;elseif(cnt_1ms_flag <= 1'b1)sel_reg <= sel_reg + 1'd1;elsesel_reg <= sel_reg;
endalways@(posedge clk,negedge rst_n)//段码数据传递
beginif(rst_n == 0)data_zhan <= 4'd0;elseif((seg_en == 1'b1)&&(cnt_1ms_flag <= 1'b1))case(cnt_sel)3'd0 : data_zhan <= data_reg[3:0];3'd1 : data_zhan <= data_reg[7:4];3'd2 : data_zhan <= data_reg[11:8];3'd3 : data_zhan <= data_reg[15:12];3'd4 : data_zhan <= data_reg[19:16];3'd5 : data_zhan <= data_reg[23:20];default : data_zhan <= 4'd0;endcaseelsedata_zhan <= data_zhan;
endalways@(posedge clk,negedge rst_n)//小数点点亮
beginif(rst_n == 0)point_flag <= 1'b0;elseif(cnt_1ms_flag <= 1'b1)point_flag <= ~point[cnt_sel];elsepoint_flag <= point_flag;
endalways@(posedge clk,negedge rst_n)//段码数值判断并输出
beginif(rst_n == 0) seg <= 8'b1111_1111;//0elsecase(data_zhan)4'd0 : seg <= {point_flag,7'd100_0000};//04'd1 : seg <= {point_flag,7'd100_0000};//14'd2 : seg <= {point_flag,7'd100_0000};//24'd3 : seg <= {point_flag,7'd100_0000};//34'd4 : seg <= {point_flag,7'd100_0000};//44'd5 : seg <= {point_flag,7'd100_0000};//54'd6 : seg <= {point_flag,7'd100_0000};//64'd7 : seg <= {point_flag,7'd100_0000};//74'd8 : seg <= {point_flag,7'd100_0000};//84'd9 : seg <= {point_flag,7'd100_0000};//94'd10: seg <= 8'b1011_1111;//-4'd11: seg <= 8'b1111_1111;//熄灭
/* 4'd10: seg <= {point_flag,7'd100_0000};//A4'd11: seg <= {point_flag,7'd100_0000};//b4'd12 : seg <= {point_flag,7'd100_0000};//C4'd13 : seg <= {point_flag,7'd100_0000};//d4'd14 : seg <= {point_flag,7'd100_0000};//E4'd15 : seg <= {point_flag,7'd100_0000};//F
*/default : seg <= 8'd1100_0000;endcase
endalways@(posedge clk,negedge rst_n)
beginif(rst_n == 0) sel <= 3'd0;//0 elsesel <= sel_reg;
ender_bcd er_bcd_inst(.clk (clk ),.rst_n(rst_n),.data (data ),//数据10进制.data1(data1),.data2(data2),.data3(data3),.data4(data4), .data5(data5),.data6(data6) ); endmodule
数码管辅助模块
module er_bcd(input clk ,input rst_n ,input [19:0] data ,//数据10进制output reg[3:0] data1 ,//bcd码output reg[3:0] data2 ,//bcd码output reg[3:0] data3 ,//bcd码output reg[3:0] data4 ,//bcd码 output reg[3:0] data5 ,//bcd码output reg[3:0] data6 //bcd码 );reg [4 :0]cnt_shift ;
reg [43:0]data_shift ;//暂存转换后的BCD码
reg shift_flag ;always@(posedge clk, negedge rst_n)//记时钟21次后清零,大约20ns*20=400ns进行一次数据转码并输出
beginif(rst_n == 0)cnt_shift <= 5'd0;elseif((cnt_shift == 5'd21)&&(shift_flag == 1'b1))//计数清零cnt_shift <= 5'd0;elseif(shift_flag == 1'b1)cnt_shift <= cnt_shift + 5'd1;elsecnt_shift <= cnt_shift;
endalways@(posedge clk, negedge rst_n)
beginif(rst_n == 0)data_shift <= 43'd0;elseif(cnt_shift == 5'd0)data_shift <= {24'd0,data};elseif((cnt_shift <= 20)&&(shift_flag == 1'b0))begindata_shift[23:20] <= (data_shift[23:20] > 4)?(data_shift[23:20] + 2'd3):(data_shift[23:20]);//大四加三data_shift[27:24] <= (data_shift[27:24] > 4)?(data_shift[27:24] + 2'd3):(data_shift[27:24]);//大四加三data_shift[31:28] <= (data_shift[31:28] > 4)?(data_shift[31:28] + 2'd3):(data_shift[31:28]);//大四加三data_shift[35:32] <= (data_shift[35:32] > 4)?(data_shift[35:32] + 2'd3):(data_shift[35:32]);//大四加三data_shift[39:36] <= (data_shift[39:36] > 4)?(data_shift[39:36] + 2'd3):(data_shift[39:36]);//大四加三data_shift[43:40] <= (data_shift[43:40] > 4)?(data_shift[43:40] + 2'd3):(data_shift[43:40]);//大四加三endelseif((cnt_shift <= 20)&&(shift_flag == 1'b1))data_shift <= data_shift << 1;elsedata_shift <= data_shift;
endalways@(posedge clk, negedge rst_n)//相对于时钟信号,0时数据计算,1时进行数据移位
beginif(rst_n == 0)shift_flag <= 1'b0;elseshift_flag = ~shift_flag;
endalways@(posedge clk, negedge rst_n)
beginif(rst_n == 0)begindata1 <= 4'b0;data2 <= 4'b0;data3 <= 4'b0;data4 <= 4'b0;data5 <= 4'b0;data6 <= 4'b0;endelse if(cnt_shift == 5'd21)begindata1 <= data_shift[23:20];//分段传递data2 <= data_shift[27:24];//分段传递data3 <= data_shift[31:28];//分段传递data4 <= data_shift[35:32];//分段传递data5 <= data_shift[39:36];//分段传递data6 <= data_shift[43:40];//分段传递end
endendmodule
顶层连线
module ds18b20_ctrl(input clk ,input rst_n ,inout dp ,output [2:0] sel ,output [7:0] seg );wire data_flag;
wire [19:0]data;ds18b20_data ds18b20_data(.clk (clk ),.rst_n (rst_n ),.dp (dp ), .data_out (data ),.data_flag (data_flag ));shu_ma_g_MAX shu_ma_g_MAX(.clk (clk ) ,.rst_n (rst_n ) ,.data (data ) ,//数据10进制.point (6'b001000 ) ,//小数点位.signe (data_flag ) ,//正负号位.seg_en (1'b1 ) ,//数码管使能.sel (sel ) ,.seg (seg ) );endmodule
三、仿真验证
这里进行初始化,写命令跳过ROM命令,状态跳转到等待状态
经过480us后存在脉冲有效信号拉高,证明识别到传感器
数据开始从总线上读入
这里可以看到,有数据输出,实验成功
参考资料
DS18B20
这篇关于基于DS18B20的温度检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!