module dht11_model ( inout wire data, // Ligne de données bidirectionnelle input wire clk, // Horloge système (27 MHz) input wire rst_n // Reset actif bas ); // Paramètres pour les timings (basés sur une horloge de 27 MHz, période ~37 ns) localparam CLK_FREQ = 27_000_000; // 27 MHz localparam CLK_PERIOD_NS = 37; // 37 ns localparam T_START_LOW = (18_000_000 / CLK_PERIOD_NS); // 18 ms localparam T_START_HIGH = (39_500 / CLK_PERIOD_NS); // 40 µs localparam T_RESP_LOW = (80_000 / CLK_PERIOD_NS); // 80 µs localparam T_RESP_HIGH = (80_000 / CLK_PERIOD_NS); // 80 µs localparam T_BIT0_LOW = (50_000 / CLK_PERIOD_NS); // 50 µs pour bit '0' localparam T_BIT1_LOW = (70_000 / CLK_PERIOD_NS); // 70 µs pour bit '1' localparam T_BIT_GAP = (50_000 / CLK_PERIOD_NS); // 50 µs entre bits localparam DATA_BITS = 40; // 40 bits de données // États de la machine à états de Moore localparam IDLE = 4'd0, WAIT_START_LOW = 4'd1, WAIT_START_HIGH= 4'd2, RESPONSE_LOW = 4'd3, RESPONSE_HIGH = 4'd4, SEND_BIT_LOW = 4'd5, SEND_BIT_HIGH = 4'd6, END_TRANS = 4'd7; // Signaux internes reg [3:0] state; // État actuel reg [19:0] counter; // Compteur pour les timings (supporte jusqu'à 20 ms) reg [5:0] bit_index; // Index du bit à envoyer reg data_out; // Valeur de sortie sur la ligne data reg data_oe; // Output enable (1 = sortie, 0 = haute impédance) wire data_in; // Valeur lue sur la ligne data // Données simulées (exemple : humidité = 45.0%, température = 23.0°C) reg [7:0] humidity_int = 8'h2D; // 45 en décimal reg [7:0] humidity_dec = 8'h00; // 0 reg [7:0] temp_int = 8'h17; // 23 en décimal reg [7:0] temp_dec = 8'h00; // 0 reg [7:0] checksum; // Checksum reg [39:0] data_shift; // Registre pour les 40 bits de données // Gestion de la ligne bidirectionnelle assign data = data_oe ? data_out : 1'bz; assign data_in = data; // Calcul du checksum et préparation des données always @(posedge clk or negedge rst_n) begin if (!rst_n) begin checksum <= 8'h00; data_shift <= 40'b0; end else begin checksum <= humidity_int + humidity_dec + temp_int + temp_dec; data_shift <= {humidity_int, humidity_dec, temp_int, temp_dec, checksum}; end end // Logique séquentielle (machine à états de Moore) always @(posedge clk or negedge rst_n) begin if (!rst_n) begin state <= IDLE; counter <= 20'b0; bit_index <= 6'b0; data_out <= 1'b1; data_oe <= 1'b0; end else begin counter <= counter + 1; // Incrément du compteur par défaut case (state) IDLE: begin counter <= 20'b0; bit_index <= 6'b0; data_out <= 1'b1; data_oe <= 1'b0; if (data_in === 1'b0) // Vérification explicite pour éviter X state <= WAIT_START_LOW; end WAIT_START_LOW: begin data_out <= 1'b1; data_oe <= 1'b0; if (data_in === 1'b1 && counter >= T_START_LOW) state <= WAIT_START_HIGH; else if (data_in === 1'b1) state <= IDLE; // Signal de démarrage trop court end WAIT_START_HIGH: begin data_out <= 1'b1; data_oe <= 1'b0; if (counter >= T_START_HIGH) state <= RESPONSE_LOW; end RESPONSE_LOW: begin data_out <= 1'b0; data_oe <= 1'b1; if (counter >= T_RESP_LOW) state <= RESPONSE_HIGH; end RESPONSE_HIGH: begin data_out <= 1'b1; data_oe <= 1'b1; if (counter >= T_RESP_HIGH) state <= SEND_BIT_LOW; end SEND_BIT_LOW: begin data_out <= 1'b0; data_oe <= 1'b1; if (counter >= (data_shift[39-bit_index] ? T_BIT1_LOW : T_BIT0_LOW)) state <= SEND_BIT_HIGH; end SEND_BIT_HIGH: begin data_out <= 1'b1; data_oe <= 1'b1; if (counter >= T_BIT_GAP) begin counter <= 20'b0; bit_index <= bit_index + 1; if (bit_index + 1 < DATA_BITS) state <= SEND_BIT_LOW; else state <= END_TRANS; end end END_TRANS: begin data_out <= 1'b1; data_oe <= 1'b0; counter <= 20'b0; state <= IDLE; end default: begin state <= IDLE; counter <= 20'b0; data_out <= 1'b1; data_oe <= 1'b0; end endcase end end endmodule