forked from tanchou/Verilog
recodage de lundi
This commit is contained in:
@@ -6,41 +6,40 @@ module dht11_interface #(
|
||||
input wire i_start,
|
||||
output reg o_dht11_data_ready,
|
||||
output reg o_busy,
|
||||
output reg [7:0] o_temp_data,
|
||||
output reg [7:0] o_hum_data,
|
||||
output reg o_dht11_error
|
||||
output reg [15:0] o_temp_data,
|
||||
output reg [15:0] o_hum_data,
|
||||
output reg [7:0] o_checksum,
|
||||
output reg [3:0] o_state
|
||||
);
|
||||
|
||||
// === DHT11 INTERFACE ===
|
||||
|
||||
// === PARAMÈTRES ===
|
||||
localparam T_18MS = CLK_FREQ * 18 / 1_000; // cycles pour 18ms a partir
|
||||
localparam T_80US = CLK_FREQ * 81 / 1_000_000;
|
||||
localparam T_79US = CLK_FREQ * 79 / 1_000_000;
|
||||
localparam T_71US = CLK_FREQ * 71 / 1_000_000;
|
||||
localparam T_51US = CLK_FREQ * 51 / 1_000_000;
|
||||
localparam T_80US = CLK_FREQ * 90 / 1_000_000;
|
||||
localparam T_79US = CLK_FREQ * 70 / 1_000_000;
|
||||
localparam T_71US = CLK_FREQ * 81 / 1_000_000;
|
||||
localparam T_51US = CLK_FREQ * 58 / 1_000_000;
|
||||
localparam T_50US = CLK_FREQ * 50 / 1_000_000;
|
||||
localparam T_49US = CLK_FREQ * 49 / 1_000_000;
|
||||
localparam T_41US = CLK_FREQ * 41 / 1_000_000;
|
||||
localparam T_28US = CLK_FREQ * 28 / 1_000_000;
|
||||
localparam T_26US = CLK_FREQ * 26 / 1_000_000;
|
||||
localparam T_20US = CLK_FREQ * 20 / 1_000_000;
|
||||
localparam T_41US = CLK_FREQ * 50 / 1_000_000;
|
||||
localparam T_28US = CLK_FREQ * 32 / 1_000_000;
|
||||
localparam T_26US = CLK_FREQ * 25 / 1_000_000;
|
||||
localparam T_20US = CLK_FREQ * 18 / 1_000_000;
|
||||
|
||||
|
||||
// === Signal bidirectionnel ===
|
||||
reg sig_dir;
|
||||
reg sig_out;
|
||||
wire sig_in;
|
||||
reg sig_in;
|
||||
|
||||
assign io_dht11_sig = sig_dir ? sig_out : 1'bz; // Si sig_dir = 1, on force la valeur de sig_out sur la ligne, sinon on laisse la ligne libre (1'bz)
|
||||
assign sig_in = io_dht11_sig;
|
||||
|
||||
|
||||
// === REGISTRES ===
|
||||
reg [3:0] state;
|
||||
reg [31:0] timer;
|
||||
|
||||
reg [7:0] temp_data, hum_data;
|
||||
reg [7:0] temp_dec, hum_dec, checksum;
|
||||
reg [2:0] bit_count;
|
||||
reg [5:0] bit_index;
|
||||
reg [39:0] raw_data;
|
||||
@@ -70,6 +69,7 @@ module dht11_interface #(
|
||||
|
||||
// === FSM principale ===
|
||||
always @(posedge i_clk) begin
|
||||
sig_in <= io_dht11_sig;
|
||||
case (state)
|
||||
|
||||
IDLE: begin
|
||||
@@ -102,111 +102,88 @@ module dht11_interface #(
|
||||
end
|
||||
|
||||
WAIT_RESPONSE: begin
|
||||
o_state <= state;
|
||||
|
||||
timer <= timer + 1;
|
||||
|
||||
if (sig_in == 0) begin
|
||||
if (timer > T_20US && timer < T_41US) begin
|
||||
|
||||
|
||||
state <= RESPONSE_LOW;
|
||||
timer <= 0;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_41US) begin
|
||||
state <= ERROR;
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
RESPONSE_LOW: begin
|
||||
o_state <= state;
|
||||
timer <= timer + 1;
|
||||
|
||||
if (sig_in == 1) begin
|
||||
if (timer > T_79US && timer < T_80US) begin
|
||||
|
||||
timer <= 0;
|
||||
state <= RESPONSE_HIGH;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_80US) begin
|
||||
state <= ERROR;
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
RESPONSE_HIGH: begin
|
||||
timer <= timer + 1;
|
||||
|
||||
o_state <= state;
|
||||
if (sig_in == 0) begin
|
||||
if (timer > T_79US && timer < T_80US) begin
|
||||
|
||||
timer <= 0;
|
||||
state <= READ_BITS_LOW;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_80US) begin
|
||||
state <= ERROR;
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
READ_BITS_LOW: begin
|
||||
o_state <= state;
|
||||
timer <= timer + 1;
|
||||
if (sig_in == 1) begin
|
||||
if (timer > T_49US && timer < T_51US) begin
|
||||
|
||||
timer <= 0;
|
||||
state <= READ_BITS_HIGH;
|
||||
end else begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end else if (timer > T_51US) begin
|
||||
state <= ERROR;
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
READ_BITS_HIGH: begin // entre 26 et 28us = 0 et ~70us = 1
|
||||
o_state <= state;
|
||||
timer <= timer + 1;
|
||||
if (sig_in == 0) begin
|
||||
if (timer <= T_26US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
|
||||
|
||||
raw_data <= {raw_data[38:0], (timer > T_28US)}; // 1 si high > ~28us
|
||||
timer <= 0;
|
||||
bit_index <= bit_index + 1;
|
||||
|
||||
if (bit_index == 39) begin // Code a testé ici pour etre sur de capter le dernier bit
|
||||
if (bit_index == 39) begin
|
||||
state <= DONE;
|
||||
end else begin
|
||||
state <= READ_BITS_LOW;
|
||||
end
|
||||
|
||||
end else if (timer > T_71US) begin
|
||||
state <= ERROR;
|
||||
end
|
||||
end
|
||||
|
||||
DONE: begin
|
||||
hum_data <= raw_data[39:32];
|
||||
hum_dec <= raw_data[31:24];
|
||||
temp_data <= raw_data[23:16];
|
||||
temp_dec <= raw_data[15:8];
|
||||
checksum <= raw_data[7:0];
|
||||
|
||||
o_state <= state;
|
||||
if (raw_data[7:0] == (raw_data[39:32] + raw_data[31:24] + raw_data[23:16] + raw_data[15:8])) begin
|
||||
o_hum_data <= raw_data[39:32];
|
||||
o_temp_data <= raw_data[23:16];
|
||||
o_hum_data <= raw_data[39:24];
|
||||
o_temp_data <= raw_data[23:8];
|
||||
o_dht11_data_ready <= 1;
|
||||
end else begin
|
||||
o_dht11_error <= 1;
|
||||
end
|
||||
end
|
||||
|
||||
o_hum_data <= raw_data[39:24];
|
||||
o_temp_data <= raw_data[23:8];
|
||||
o_checksum <= raw_data[7:0];
|
||||
o_dht11_data_ready <= 1;
|
||||
|
||||
o_busy <= 0;
|
||||
state <= IDLE;
|
||||
end
|
||||
|
||||
ERROR: begin
|
||||
o_dht11_error <= 1;
|
||||
state <= IDLE;
|
||||
end
|
||||
|
||||
endcase
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user