thermometer_digital/rtl/digital_thermometer.v

79 lines
2.1 KiB
Verilog
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Engineer: Yangshenbo
// version:V1.0
// Create Date: 2026/03/22
//////////////////////////////////////////////////////////////////////////////////
module digital_thermometer(
input clk,
input rst_n,
input vin,
input mode,
output reg [19:0]freq_x100hz,
output reg [15:0]temp_out,
output reg temp_valid
);
wire [19:0] freq;
wire freq_valid;
//映射表:
reg [15:0] temp_lut [0:800];
integer i;
initial begin
for (i = 0; i <= 800; i = i + 1) begin
// 50.0kHz->-40°C, 130.0kHz->85°C
// temp = -400 + (1250 * i) / 800
temp_lut[i] = -400 + (1250 * i + 400) / 800;
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
freq_x100hz <= 0;
temp_out <= 0;
temp_valid <= 0;
end
else begin
temp_valid <= freq_valid;
if (freq_valid) begin
freq_x100hz <= freq;
if (mode == 0) begin
// 模式0线性输出
// 频率范围500~130050.0kHz~130.0kHz
if (freq < 500)
temp_out <= -400;
else if (freq > 1300)
temp_out <= 850;
else
temp_out <= -400 + ((freq - 500) * 1250) / 800;
end else begin
// 模式1查表输出
if (freq < 500)
temp_out <= temp_lut[0];
else if (freq > 1300)
temp_out <= temp_lut[800];
else
temp_out <= temp_lut[freq - 500];
end
end
end
end
pulse_freq_10ms u_freq_measure (
.clk (clk),
.rst_n (rst_n),
.vin (vin),
.freq (freq), //1302 ->130.2KHz
.valid (freq_valid)
);
endmodule