-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathus_sensor.v
71 lines (67 loc) · 1.5 KB
/
us_sensor.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
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 18:35:04 10/15/2018
// Design Name:
// Module Name: kpus
// Project Name: Distance_measurement_UltrasonicSensor
// Target Devices: WAXWING SPARTAN 6 BOARD
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module us_sensor(echo,trigger,ss,en,clk);
input clk,echo;
output reg trigger;
output [7:0] ss;
output [2:0] en;
reg [9:0] distance;
reg nclk;
reg [31:0] t_2c, t_3c, on_count, off_count, t_c, count;
initial begin
trigger=1;
count=0;
t_c=0;
t_2c=0;
t_3c=0;
on_count=1002;
off_count=10000000;
end
sev_seg m1(clk,distance,en,ss);
always @(posedge clk) begin
if(trigger==1) begin
count=count+1;
if(count==on_count) begin
count=0;
trigger=0;
end
end
else begin
count=count+1;
if(count==off_count) begin
count=0;
trigger=1;
end
end
end
always
distance=(t_3c*34)/200000;
always @(posedge clk) begin
if(echo==0) begin
t_3c=t_2c+1; //total pulses for echo=1
t_c=0; //make this counting variable 0 for next time counting purpose
end
else if(echo==1) begin
t_c=t_c+1; //count pulses for echo =1 (time during echo = 1, is propotional to distance)
t_2c=t_c; //save this count to one variable
end
end
endmodule