三路抢答器FPGA设计
按鍵消抖:(延時20ms后輸出有效值)
?30S倒計時:(復位結束或開始按鍵按下后計時器從30開始倒計時,有選手搶答時,倒計時停止,并且輸出O_cnt_flag(此信號拉高其他選手搶答無效))
工程仿真:如下圖所示:
I_key代表三位選手(001(1),010(2),100(4))
I_begin高電平代表主持人按下開始按鍵
O_cnt_flag高電平代表有選手搶答或30S內無人搶答
I_plus,I_sub代表主持人加分和減分
I_people1,2,3代表三位選手編號輸出(數碼管顯示)
O_score1,2,3代表三位選手分數輸出(數碼管顯示)初始每人為10分
第一輪搶答:全局復位(I_rst)結束后,主持人按下開始按鍵,此時倒計時開始;選手1在倒計時為8秒時按下搶答鍵,此時倒計時停止,倒計時顯示8直至開始按鍵按下,顯示選手1編號(I_people1高電平)直至開始按鍵按下,O_cnt_flag信號拉高直至開始按鍵按下;選手2慢于選手1按下搶答鍵,搶答無效,不顯示選手2編號,選手1答對問題后主持人加一分(11分),顯示分數(O_score1)。
第二輪搶答:主持人按下開始鍵,此時倒計時從30開始倒計時,選手1在倒計時15時按下搶答鍵,
............選手1又答對了,再加一分(12)分。
第三輪搶答:主持人按下開始鍵,此時倒計時從30開始倒計時,選手2在倒計時12時按下搶答鍵,..............選手2打錯,扣一分(9)分
第四輪搶答:主持人按下開始鍵,無人搶答,倒計時結束后O_cnt_flag拉高,選手4搶答,搶答無效。
復位鍵按下,分數全部歸10,比賽結束。
按鍵消抖:
`timescale 1ns / 1ps // // Company: // Engineer: // // Create Date: 2022/12/08 14:41:34 // Design Name: // Module Name: delete_dou // Project Name: // Target Devices: // Tool Versions: // Description: // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // // module delete_dou(//自鎖式按鍵input I_clk, //外部 50M 時鐘input I_rst, //外部復位信號,低有效input I_key, //外部按鍵輸入 按鍵按下為低電平output reg O_key_value //按鍵消抖后的數據 );reg [31:0] S_delay_cnt; reg S_key_reg; //***************************************************** //** main code //***************************************************** always @(posedge I_clk) begin if (I_rst) begin S_key_reg <= 1'b0; end else begin S_key_reg <= I_key; end endalways @(posedge I_clk) begin if (I_rst) begin S_delay_cnt <= 32'd0; // end else if(S_key_reg ^ I_key)begin //一旦檢測到按鍵狀態發生變化(有按鍵被按下或釋放) end else if((S_key_reg == 1)&&(S_key_reg ^ I_key))begin //檢測上升沿S_delay_cnt <= 32'd100; //給延時計數器重新裝載初始值(計數時間為 20ms(上板時1000_1000),板子仿真時20us(1000)) end else if((S_key_reg == 1)&&(S_delay_cnt > 32'd0))begin S_delay_cnt <= S_delay_cnt - 1'b1;end else beginS_delay_cnt <= S_delay_cnt;end end always @(posedge I_clk) beginif (I_rst) beginO_key_value <= 3'b0; end else if(S_delay_cnt == 32'd1) begin //當計數器遞減到 1 時,說明按鍵穩定狀態維持了 20msO_key_value <= 1; //并寄存此時按鍵的值end else beginO_key_value <= 1'd0;end end endmodule計時模塊:
`timescale 1ns / 1ps // // Company: // Engineer: // // Create Date: 2022/12/08 15:10:48 // Design Name: // Module Name: cnt_x // Project Name: // Target Devices: // Tool Versions: // Description: // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // //module cnt_x(input I_clk ,input I_rst ,input I_begin ,input [2:0] I_key ,output reg O_cnt_flag ,//表明30S倒計時結束或有人搶答output reg [4:0] O_cnt // output [3:0] O_data_ge , // output [3:0] O_data_shi);reg [25:0] S_cnt;wire I_rst_n;assign I_rst_n = !I_rst; //S_cnt always @(posedge I_clk) begin if (!I_rst_n||I_begin || (S_cnt == 26'd34_999)) begin S_cnt <= 26'd0; end else if((I_key == 0)&&(O_cnt_flag == 0)) begin//有人按下按鍵S_cnt <= S_cnt+1;end else begin S_cnt <= S_cnt; end end //O_cnt always @(posedge I_clk) begin if (!I_rst_n||I_begin) begin O_cnt <= 5'd30; end else if(S_cnt == 26'd34_999) begin//有人按下按鍵O_cnt <= O_cnt-1;end else begin O_cnt <= O_cnt; end endalways @(posedge I_clk) begin if (!I_rst_n||I_begin) begin O_cnt_flag <= 1'b0; end else if(O_cnt == 5'd0 || (I_key != 0)) begin O_cnt_flag <= 1; end else beginO_cnt_flag <= O_cnt_flag; end end// assign O_data_ge = S_cnt % 4'd10; // 個位數 // assign O_data_shi = S_cnt / 4'd10 % 4'd10 ; // 十位數endmodule搶答模塊:
`timescale 1ns / 1ps // // Company: // Engineer: // // Create Date: 2022/12/08 15:26:41 // Design Name: // Module Name: ctrl_people // Project Name: // Target Devices: // Tool Versions: // Description: // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // //module ctrl_people(input I_clk ,//1Minput I_rst , input I_cnt_flag ,input [2:0] I_key , input I_begin ,output reg O_people1 ,output reg O_people2 ,output reg O_people3 ); always @(posedge I_clk) begin if (I_rst||I_begin) begin O_people1 <= 1'd0; end else if((I_cnt_flag == 0)&&I_key[0])begin O_people1 <= 1'd1; end else beginO_people1 <= O_people1; end end always @(posedge I_clk) begin if (I_rst||I_begin) begin O_people2 <= 1'd0; end else if((I_cnt_flag == 0)&&I_key[1])begin O_people2 <= 1'd1; end else beginO_people2 <= O_people2; end end always @(posedge I_clk) begin if (I_rst||I_begin) begin O_people3 <= 1'd0; end else if((I_cnt_flag == 0)&&I_key[2])begin O_people3 <= 1'd1; end else beginO_people3 <= O_people3; end end endmodule得分模塊:
`timescale 1ns / 1ps // // Company: // Engineer: // // Create Date: 2022/12/08 15:55:50 // Design Name: // Module Name: score // Project Name: // Target Devices: // Tool Versions: // Description: // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // //module score(input I_clk ,//1Minput I_rst , input I_begin ,input I_people1 ,input I_people2 ,input I_people3 ,input I_plus ,input I_sub ,output reg [3:0] O_score1 , output reg [3:0] O_score2 , output reg [3:0] O_score3 );// reg S_plus; // reg S_sub; //always @(posedge I_clk) begin // if (I_rst||I_begin) begin // S_plus <= 1'd0; // S_sub <= 1'd0; // end else begin // S_plus <= I_plus; // S_sub <= I_sub; // end //end// reg plus_up; // reg sub_up; //always @(posedge I_clk) begin // if (I_rst||I_begin) begin // plus_up <= 1'd0; // end else if(S_plus^I_plus)begin // plus_up <= plus_up + 1; // end else begin // plus_up <= plus_up; // end //end //always @(posedge I_clk) begin // if (I_rst||I_begin) begin // sub_up <= 1'd0; // end else if(S_sub^I_sub)begin // sub_up <= sub_up + 1; // end else begin // sub_up <= sub_up; // end //endalways @(posedge I_clk) begin if (I_rst) begin O_score1 <= 4'd10; end else if(I_people1&&(I_plus == 1))begin O_score1 <= O_score1 + 1; end else if(I_people1&&(I_sub == 1))begin O_score1 <= O_score1 - 1; end else beginO_score1 <= O_score1;end end always @(posedge I_clk) begin if (I_rst) begin O_score2 <= 4'd10; end else if(I_people2&&(I_plus == 1))begin O_score2 <= O_score2 + 1; end else if(I_people2&&(I_sub == 1))begin O_score2 <= O_score2 - 1; end else beginO_score2 <= O_score2;end end always @(posedge I_clk) begin if (I_rst) begin O_score3 <= 4'd10; end else if(I_people3&&(I_plus == 1))begin O_score3 <= O_score3 + 1; end else if(I_people3&&(I_sub == 1))begin O_score3 <= O_score3 - 1; end else beginO_score3 <= O_score3;end end endmodule總結
以上是生活随笔為你收集整理的三路抢答器FPGA设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【浅谈】四种无线定位原理及算法
- 下一篇: flutter 字符串zip压缩与解压