Question: Hello, im trying to make a stopwatch program that resets / starts + stops with two buttons, has 4 modes based on s witches (

Hello, im trying to make a stopwatch program that resets/starts+stops with two buttons, has 4 modes based on s witches (mode 1 counts up from 00:00, mode 2 counts up from value, mode 3 counts down from 99:99, mode 4 counts down from value), and inputs the value from switches. its almost working, i think it is a small problem i have, any help is appreciated thank you!
module stopwatch_timer(
input clk,
input reset,
input start_stop,
input [1:0] mode_select, // mode selection
input [7:0] preset_value, // modes 2 and 4(8 switches)
output reg [6:0] seg, // segments
output reg [3:0] an // anode activation
);
reg [5:0] seconds =0;
reg [7:0] milliseconds =0;
reg running =0; // flag
reg [19:0] count =0; // clock divider counter
reg [1:0] display_counter =0; // counter to cycle
// Mode selection and Timer Logic
always @(posedge clk or posedge reset) begin
if (reset) begin
// Reset logic, set initial values based on the mode
case (mode_select)
2'b00: begin // Mode 1: Count up from 00:00
seconds <=0;
milliseconds <=0;
end
2'b01: begin // Mode 2: Count up from preset value
seconds <= preset_value[7:4];
milliseconds <=0;
end
2'b10: begin // Mode 3: Count down from 99:99
seconds <=99;
milliseconds <=99;
end
2'b11: begin // Mode 4: Count down from preset value
seconds <= preset_value[7:4];
milliseconds <= preset_value[3:0];
end
default: begin
seconds <=0;
milliseconds <=0;
end
endcase
running <=0;
count <=0;
end else begin
// Handle the start/stop functionality
if (start_stop) running <= ~running;
// Increment or decrement logic based on the mode and running state
if (running && count >=50000) begin
count <=0;
case (mode_select)
2'b00,2'b01: begin // Modes 1 and 2: Counting up
if (milliseconds ==99) begin
milliseconds <=0;
if (seconds ==99)
seconds <=0;
else
seconds <= seconds +1;
end else begin
milliseconds <= milliseconds +1;
end
end
2'b10,2'b11: begin // Modes 3 and 4: Counting down
if (milliseconds ==0) begin
if (seconds !=0) begin
milliseconds <=99;
seconds <= seconds -1;
end else if (seconds ==0 && milliseconds ==0) begin
// Optionally stop timer when it reaches 00:00
running <=0;
end
end else begin
milliseconds <= milliseconds -1;
end
end
endcase
end else if (running) begin
count <= count +1;
end
end
end
// Display Logic
always @(posedge clk) begin
if (count ==0) begin // update display
case (display_counter)
2'b00: begin
seg <= decode_seven_segment(seconds /10);
an <=4'b1110;
end
2'b01: begin
seg <= decode_seven_segment(seconds %10);
an <=4'b1101;
end
2'b10: begin
seg <= decode_seven_segment(milliseconds /10);
an <=4'b1011;
end
2'b11: begin
seg <= decode_seven_segment(milliseconds %10);
an <=4'b0111;
end
endcase
display_counter <=(display_counter +1)%4;
end
end
function [6:0] decode_seven_segment;
input [3:0] digit;
begin
case (digit)
4'h0: decode_seven_segment =7'b1000000; //0
4'h1: decode_seven_segment =7'b1111001; //1
4'h2: decode_seven_segment =7'b0100100; //2
4'h3: decode_seven_segment =7'b0110000; //3
4'h4: decode_seven_segment =7'b0011001; //4
4'h5: decode_seven_segment =7'b0010010; //5
4'h6: decode_seven_segment =7'b0000010; //6
4'h7: decode_seven_segment =7'b1111000; //7
4'h8: decode_seven_segment =7'b0000000; //8
4'h9: decode_seven_segment =7'b0010000; //9
default: decode_seven_segment =7'b1111111; // blank
endcase
end
endfunction
endmodule

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!