-
Notifications
You must be signed in to change notification settings - Fork 0
/
measuring.v
244 lines (229 loc) · 5.07 KB
/
measuring.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//======================================================================
//
// Copyright (C) 2018
// All rights reserved
//
// filename: measuring
//
// created by Tyl at 2019-01-11 16:27:29
// modified by Tyl at 2019-01-13 00:37:00
//
//======================================================================
// Quartus II Verilog Template
// 4-State Moore state machine
// A Moore machine's outputs are dependent only on the current state.
// The output is written only when the state changes. (State
// transitions are synchronous.)
module measuring
(
input clk, reset,
input hscan_switch,
input pause,
input [9: 0]data,
/*output statetest,
output [9: 0]pretest, midtest, retest, maxvoltest, volrelatest, volBitest,
output [11: 0]volBCDtest,
output [12: 0]precounttest,
output [19: 0]freqBitest,
output [23: 0]freqBCDtest,
output triggertest,
output [14: 0]trigger_counttest,
output prechecktest,
output [13: 0]Ncounttest,
output [23: 0]outcounttest,*/
//频率电压均输出BCD码
//频率以Hz为单位,幅值1MHz以内
//电压以mV为单位,赋值4V以内
output reg [23: 0]freqout,
output reg [11: 0]volout
);
// Declare state register
reg state;
// Declare states
parameter S0 = 0, S1 = 1;
parameter trigger_vol = 10'b10_0000_0000;
reg [9: 0]pre;
reg [9: 0]mid;
reg [9: 0]re;
reg trigger;
reg [9: 0]maxvol;
reg [9: 0]volrela;
wire [9: 0]volBi;
wire [11: 0]volBCD;
integer precount;
reg [19: 0]freqBi;
wire [23: 0]freqBCD;
integer outcount;
reg precheck;
integer trigger_count;
//测试模块
/*assign statetest = state;
assign pretest = pre;
assign midtest = mid;
assign retest = re;
assign maxvoltest = maxvol;
assign volrelatest = volrela;
assign volBitest = volBi;
assign volBCDtest = volBCD;
assign precounttest = precount;
assign freqBitest = freqBi;
assign freqBCDtest = freqBCD;
assign triggertest = trigger;
assign prechecktest = precheck;
assign trigger_counttest = trigger_count;
assign Ncounttest = Ncount;
assign outcounttest = outcount;*/
// Output depends only on the state
always @ (state) begin
case (state)
S0:
trigger = 'b0;
S1:
trigger = 'b1;
endcase
end
// Determine the next state and output
always @ (posedge clk or posedge reset)
begin
if (reset)
state <= S0;
else
case (state)
S0:
//判断触发
if(pre > trigger_vol && re < trigger_vol)
state <= S1;
else
state <= S0;
S1:
state <= S0;
endcase
end
//移位寄存模块
always@(posedge clk)
begin
pre <= data;
mid <= pre;
re <= mid;
end
//测压模块
always@(posedge clk)
begin
if(trigger == 'b1)
begin
volrela[9: 1] <= maxvol[8: 0];
volrela[0] <= 'b0;
maxvol <= 'b0;
end
else if(maxvol < re)
maxvol <= re;
end
//相对峰峰值转绝对峰峰值
assign volBi = (volrela + 1) * 400 / 1024;
//参考谭军提供的算法
assign volBCD[11: 8] = volBi / 100 % 10;
assign volBCD[7: 4] = volBi / 10 % 10;
assign volBCD[3: 0] = volBi % 10;
//频率预测量模块
always@(posedge clk)
begin
if(trigger == 'b1)
begin
if(hscan_switch == 'b0)
//高频
begin
if(500000 / precount >= 1000)
precheck <= 'b1;
else
precheck <= 'b0;
end
else
//低频
begin
if(2500 / precount >= 71)
precheck <= 'b1;
else
precheck <= 'b0;
end
precount <= 'b0;
end
else
precount <= precount + 1;
end
//测频模块
always@(posedge clk)
begin
//if(hscan_switch == 'b1 && outcount == 1000 || hscan_switch == 'b0 && outcount == 5)
if(hscan_switch == 'b0)
begin
if(outcount >= 250000)
//输出
begin
if(precheck == 'b1)
freqBi <= trigger_count * 2;
else
begin
freqBi <= 500000 * trigger_count / outcount;
end
trigger_count <= 0;
end
else
//计数
begin
if(trigger == 'b1)
trigger_count <= trigger_count + 1;
end
end
else
begin
if(outcount >= 1250)
//输出
begin
if(precheck == 'b1)
freqBi = trigger_count * 2;
else
freqBi = 2500 * trigger_count / outcount;
trigger_count <= 0;
end
else
//计数
begin
if(trigger == 'b1)
trigger_count <= trigger_count + 1;
end
end
end
//======================================================================
//转换二进制频率为BCD码,此算法由重庆大学20162628谭军提供
assign freqBCD[23:20] = freqBi / 100000 %10;
assign freqBCD[19:16] = freqBi / 10000 %10;
assign freqBCD[15:12] = freqBi / 1000 %10;
assign freqBCD[11:8] = freqBi / 100 %10;
assign freqBCD[7:4] = freqBi / 10 %10;
assign freqBCD[3:0] = freqBi %10;
//======================================================================
//输出模块,0.5s输出一次
always@(posedge clk)
begin
if(pause == 'b0)
begin
outcount <= outcount + 1;
if(hscan_switch == 'b0)
begin
if(outcount >= 250000)
begin
volout <= volBCD;
freqout <= freqBCD;
outcount <= 0;
end
end
else
if(outcount >= 1250)
begin
volout <= volBCD;
freqout <= freqBCD;
outcount <= 0;
end
end;
end
endmodule