Секундомер
Автор: Ярослав Березовский • Июнь 21, 2020 • Лабораторная работа • 381 Слов (2 Страниц) • 289 Просмотры
Виниченко А. ИВТ-24
Лабораторная номер 5
Секундомер
module stopwatch (
input [1:0]KEY,
input clk,
output reg device_running,
output reg [6:0] HEX0,
output reg [6:0] HEX1,
output reg [6:0] HEX2,
output reg [6:0] HEX3);
//регистр счётчика
reg [16:0] pulse_counter;
reg [4:0]first_counter;
reg [4:0]second_counter;
reg [4:0]third_counter;
reg [4:0]fourth_counter;
//описание компаратора
wire hundredth_of_second_passed = (pulse_counter == 17'd2); //259999 Для проверки малое значение
wire first_pass=(first_counter==5'd9);
wire second_pass=(second_counter==5'd9);
wire third_pass=(third_counter==5'd9);
wire fourth_pass=(fourth_counter==5'd9);
//описание счётчика
always @(negedge KEY[0])
begin
device_running <= ~device_running;
end
always @(posedge clk or posedge KEY[1]) begin
if (KEY[1]) pulse_counter <= 0; //асинхронный сброс
else begin
if (device_running | hundredth_of_second_passed) begin
if (hundredth_of_second_passed) pulse_counter <= 0; //синхронный сброс
else pulse_counter <= pulse_counter + 1;
end else pulse_counter <= pulse_counter + 1;
end
end
//1
always@(posedge clk or posedge KEY[1])begin
if (KEY[1]) first_counter <= 0; //асинхронный сброс
else begin
if (device_running | first_pass) begin
if (first_pass&&hundredth_of_second_passed ) first_counter <= 0; //синхронный сброс
else begin
if (hundredth_of_second_passed) first_counter <= first_counter + 1 ;
end
end else ;
end
case (first_counter)
5'b00000: HEX0 = 7'b1000000;//"0"
5'b00001: HEX0 = 7'b1111001;//"1"
5'b00010: HEX0 = 7'b0100100;//"2"
5'b00011: HEX0 = 7'b0110000;//"3"
5'b00100: HEX0 = 7'b0011001;//"4"
5'b00101: HEX0 = 7'b0010010;//"5"
5'b00110: HEX0 = 7'b0000010;//"6"
5'b00111: HEX0 = 7'b1111000;//"7"
5'b01000: HEX0 = 7'b0000000;//"8"
5'b01001: HEX0 = 7'b0010000;//"9"
endcase
end
//2
always@(posedge clk or posedge KEY[1])begin
if (KEY[1]) second_counter <= 0; //асинхронный сброс
else begin
if (device_running | second_pass) begin
if (second_pass&&first_pass&&hundredth_of_second_passed) second_counter <= 0; //синхронный сброс
else begin
if(first_pass&&hundredth_of_second_passed) second_counter <= second_counter + 1;
...