-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdht11.v
209 lines (196 loc) · 4.26 KB
/
dht11.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
`timescale 100ns/100ns
`define REQUEST_LOW_GE 180
`define REQUEST_HIGH_E 400
`define RESPONSE_LOW_E 540
`define RESPONSE_HIGH_E 800
`define DATA_0_LOW_E 540
`define DATA_0_HIGH_E 240
`define DATA_1_LOW_E 540
`define DATA_1_HIGH_E 700
`define STATE_INITIAL 0
`define STATE_REQUEST_LOW 1
`define STATE_REQUEST_HIGH 2
`define STATE_RESPONSE_LOW 3
`define STATE_RESPONSE_HIGH 4
`define STATE_DATA_SEND_LOW 5
`define STATE_DATA_SEND_HIGH 6
`define DUMP_FILE "./dht11.vcd"
//`define DEBUG 1
module dht11(
inout data_io
);
reg clk = 0;
reg clk_state = 0;
reg [2:0] state = `STATE_INITIAL;
reg out_data = 1;
reg [10:0] counter = 0;
reg [10:0] second_counter = 0;
assign data_io = (state == `STATE_INITIAL || state == `STATE_REQUEST_HIGH || state == `STATE_REQUEST_LOW) ? 1'bZ : out_data;
reg data_prev_state;
reg data_cur_state;
reg [39:0] dht_data = 40'b1000000000000000000000000000000000010110;
always #1 clk = ~clk;
always
begin
#1 clk_state = ~clk_state && (state != `STATE_INITIAL);
end
initial
begin
$dumpfile(`DUMP_FILE);
$dumpvars();
end
always @ (data_io)
begin
if (state == `STATE_INITIAL && data_prev_state == 1 && data_io == 0)
begin
`ifdef DEBUG
$display("From init to req_low with cnt %x", counter);
`endif
counter <= 0;
state = `STATE_REQUEST_LOW;
end
end
always @ (posedge clk, negedge clk)
begin
data_prev_state <= data_io;
end
always @ (posedge clk_state, negedge clk_state)
begin
counter <= counter + 1;
if (state == `STATE_REQUEST_LOW)
begin
if (counter < `REQUEST_LOW_GE)
begin
if (data_prev_state == 0 && data_io == 1)
begin
`ifdef DEBUG
$display("From req_low to init with cnt %x", counter);
`endif
state = `STATE_INITIAL;
counter <= 0;
end
end
else
begin
if (data_prev_state == 0 && data_io == 1)
begin
`ifdef DEBUG
$display("From req_low to req_high with cnt %x", counter);
`endif
state = `STATE_REQUEST_HIGH;
counter <= 0;
end
end
end
else if (state == `STATE_REQUEST_HIGH)
begin
if (counter != `REQUEST_HIGH_E)
begin
if (data_prev_state == 1 && data_io == 0)
begin
`ifdef DEBUG
$display("From req_high to req_low with cnt %x", counter);
`endif
state = `STATE_REQUEST_LOW;
counter <= 0;
end
end
else
begin
`ifdef DEBUG
$display("From req_high to resp_low with cnt %x", counter);
`endif
state = `STATE_RESPONSE_LOW;
counter <= 0;
out_data <= 0;
end
end
else if (state == `STATE_RESPONSE_LOW)
begin
if (counter == `RESPONSE_LOW_E)
begin
`ifdef DEBUG
$display("From resp_low to resp_high with cnt %x", counter);
`endif
state = `STATE_RESPONSE_HIGH;
counter <= 0;
out_data <= 1;
end
end
else if (state == `STATE_RESPONSE_HIGH)
begin
if (counter == `RESPONSE_HIGH_E)
begin
`ifdef DEBUG
$display("From resp_high to data with cnt %x", counter);
`endif
counter <= 0;
second_counter <= 0;
if (dht_data[0] == 0)
begin
state = `STATE_DATA_SEND_LOW;
end
else
begin
state = `STATE_DATA_SEND_HIGH;
end
end
end
else if (state == `STATE_DATA_SEND_LOW)
begin
if (second_counter == 40)
begin
`ifdef DEBUG
$display("From send_data_low to init with cnt %x", counter);
`endif
state = `STATE_INITIAL;
end
if (counter < `DATA_0_LOW_E)
begin
out_data <= 0;
end
else if (counter >= `DATA_0_LOW_E && counter < `DATA_0_LOW_E + `DATA_0_HIGH_E)
begin
out_data <= 1;
end
else
begin
if (dht_data[second_counter+1] == 1)
begin
state = `STATE_DATA_SEND_HIGH;
end
second_counter <= second_counter + 1;
counter <= 0;
out_data <= 0;
end
end
else if (state == `STATE_DATA_SEND_HIGH)
begin
if (second_counter == 40)
begin
`ifdef DEBUG
$display("From send_data_high to init with cnt %x", counter);
`endif
state = `STATE_INITIAL;
end
if (counter < `DATA_1_LOW_E)
begin
out_data <= 0;
end
else if (counter >= `DATA_1_LOW_E && counter < `DATA_1_LOW_E + `DATA_1_HIGH_E)
begin
out_data <= 1;
end
else
begin
if (dht_data[second_counter+1] == 0)
begin
state = `STATE_DATA_SEND_LOW;
end
second_counter <= second_counter + 1;
counter <= 0;
out_data <= 0;
end
end
end
endmodule