-
Notifications
You must be signed in to change notification settings - Fork 0
/
tarfficlight.v
62 lines (54 loc) · 1.55 KB
/
tarfficlight.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
// create module
module blink (
input wire clk, // Clock signal
input wire reset, // Reset signal
input wire adc_data_valid, // ADC data valid signal
input wire [11:0] adc_data, // ADC data (12-bit)
output wire led // LED output
);
// Define parameters
parameter THRESHOLD = 8'b011111111111; // Set your threshold value here
parameter BLINK_RATE = 24'b000000000000000001111111; // Set your blink rate here
// Registers
reg [11:0] sensor_value;
reg led_state;
reg [23:0] counter;
// FSM states
parameter IDLE = 2'b00;
parameter BLINK_ON = 2'b01;
parameter BLINK_OFF = 2'b10;
// State register
reg [1:0] state;
always @(posedge clk or posedge reset) begin
if (reset) begin
state <= IDLE;
counter <= 0;
led_state <= 0;
end else begin
case (state)
IDLE:
if (adc_data_valid && (adc_data > THRESHOLD)) begin
state <= BLINK_ON;
counter <= 0;
end
BLINK_ON:
if (counter >= BLINK_RATE) begin
state <= BLINK_OFF;
counter <= 0;
end
BLINK_OFF:
if (counter >= BLINK_RATE) begin
state <= BLINK_ON;
counter <= 0;
end
endcase
end
end
always @(posedge clk) begin
// Read the sensor value when ADC data is valid
if (adc_data_valid) begin
sensor_value <= adc_data;
end
end
assign led = (state == BLINK_ON);
endmodule