按照梅总的思路,将案件消抖+7段数码管结合起来做一个随机数产生的小玩具,输入两个按钮,一个start,按下开始以50mhz频率计数,一个pause按钮,按下暂停计数,得到的数马马虎虎就算作随机数。
上代码,这个是顶层模块
- module key_hex8 (
- clk,
- rst_n,
- key_start,
- key_pause,
- sel,
- seg
- );
- input clk;
- input rst_n;
- input key_start;
- input key_pause;
- output [7:0] sel;
- output [6:0] seg;
- wire start_key_flag;
- wire pause_key_flag;
- wire[31:0] disp_data;
- key_filter start_key(
- .Clk(clk),
- .Rst_n(rst_n),
- .key_in(key_start),
- .key_flag(start_key_flag),
- .key_state()
- );
- key_filter pause_key(
- .Clk(clk),
- .Rst_n(rst_n),
- .key_in(key_pause),
- .key_flag(pause_key_flag),
- .key_state()
- );
- ctrl my_ctrl(
- .clk(clk),
- .rst_n(rst_n),
- .key_start_flag(start_key_flag),
- .key_pause_flag(pause_key_flag),
- .disp_data(disp_data)
- );
- HXE8 my_Hxe8(
- .Clk(clk),
- .Rst_n(rst_n),
- .En(1),
- .disp_data(disp_data),
- .sel(sel),
- .seg(seg)
- );
- endmodule
复制代码
这个是梅总的按键消抖模块,中文注释乱码。。。请梅总自己解决
- module key_filter(Clk,Rst_n,key_in,key_flag,key_state);
- input Clk;
- input Rst_n;
- input key_in;
-
- output reg key_flag;
- output reg key_state;
-
- localparam
- IDEL = 4'b0001,
- FILTER0 = 4'b0010,
- DOWN = 4'b0100,
- FILTER1 = 4'b1000;
-
- reg [3:0]state;
- reg [19:0]cnt;
- reg en_cnt; //浣胯兘璁℃暟瀵勫瓨鍣?
-
- //瀵瑰閮ㄨ緭鍏ョ殑寮傛淇″彿杩涜鍚屾澶勭悊
- reg key_in_sa,key_in_***;
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)begin
- key_in_sa <= 1'b0;
- key_in_*** <= 1'b0;
- end
- else begin
- key_in_sa <= key_in;
- key_in_*** <= key_in_sa;
- end
-
- reg key_tmpa,key_tmpb;
- wire pedge,nedge;
- reg cnt_full;//璁℃暟婊℃爣蹇椾俊鍙?
-
- //浣跨敤D瑙﹀彂鍣ㄥ瓨鍌ㄤ袱涓浉閭绘椂閽熶笂鍗囨部鏃跺閮ㄨ緭鍏ヤ俊鍙凤紙宸茬粡鍚屾鍒扮郴缁熸椂閽熷煙涓級鐨勭數骞崇姸鎬?
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)begin
- key_tmpa <= 1'b0;
- key_tmpb <= 1'b0;
- end
- else begin
- key_tmpa <= key_in_***;
- key_tmpb <= key_tmpa;
- end
- //浜***敓璺冲彉娌夸俊鍙?
- assign nedge = !key_tmpa & key_tmpb;
- assign pedge = key_tmpa & (!key_tmpb);
-
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)begin
- en_cnt <= 1'b0;
- state <= IDEL;
- key_flag <= 1'b0;
- key_state <= 1'b1;
- end
- else begin
- case(state)
- IDEL :
- begin
- key_flag <= 1'b0;
- if(nedge)begin
- state <= FILTER0;
- en_cnt <= 1'b1;
- end
- else
- state <= IDEL;
- end
-
- FILTER0:
- if(cnt_full)begin
- key_flag <= 1'b1;
- key_state <= 1'b0;
- en_cnt <= 1'b0;
- state <= DOWN;
- end
- else if(pedge)begin
- state <= IDEL;
- en_cnt <= 1'b0;
- end
- else
- state <= FILTER0;
-
- DOWN:
- begin
- key_flag <= 1'b0;
- if(pedge)begin
- state <= FILTER1;
- en_cnt <= 1'b1;
- end
- else
- state <= DOWN;
- end
-
- FILTER1:
- if(cnt_full)begin
- key_flag <= 1'b1;
- key_state <= 1'b1;
- state <= IDEL;
- end
- else if(nedge)begin
- en_cnt <= 1'b0;
- state <= DOWN;
- end
- else
- state <= FILTER1;
-
- default:
- begin
- state <= IDEL;
- en_cnt <= 1'b0;
- key_flag <= 1'b0;
- key_state <= 1'b1;
- end
-
- endcase
- end
-
-
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- cnt <= 20'd0;
- else if(en_cnt)
- cnt <= cnt + 1'b1;
- else
- cnt <= 20'd0;
-
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- cnt_full <= 1'b0;
- else if(cnt == 999_999)
- cnt_full <= 1'b1;
- else
- cnt_full <= 1'b0;
- endmodule
复制代码
这个是梅总的7段显示模块
- module HXE8(Clk,Rst_n,En,disp_data,sel,seg);
- input Clk; //50M
- input Rst_n;
- input En; //鏁扮爜绠℃樉绀轰娇鑳斤紝1浣胯兘锛?鍏抽棴
-
- input [31:0]disp_data;
-
- output [7:0] sel;//鏁扮爜绠′綅閫夛紙閫夋嫨褰撳墠瑕佹樉绀虹殑鏁扮爜绠★級
- output reg [6:0] seg;//鏁扮爜绠℃閫夛紙褰撳墠瑕佹樉绀虹殑鍐呭锛?
-
- reg [14:0]divider_cnt;//25000-1
-
- reg clk_1K;
- reg [7:0]sel_r;
-
- reg [3:0]data_tmp;//鏁版嵁缂撳瓨
- // 鍒嗛璁℃暟鍣ㄨ鏁版ā鍧?
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- divider_cnt <= 15'd0;
- else if(!En)
- divider_cnt <= 15'd0;
- else if(divider_cnt == 24999)
- divider_cnt <= 15'd0;
- else
- divider_cnt <= divider_cnt + 1'b1;
- //1K鎵弿鏃堕挓鐢熸垚妯″潡
- always@(posedge Clk or negedge Rst_n)
- if(!Rst_n)
- clk_1K <= 1'b0;
- else if(divider_cnt == 24999)
- clk_1K <= ~clk_1K;
- else
- clk_1K <= clk_1K;
-
- //8浣嶅惊鐜Щ浣嶅瘎瀛樺櫒
- always@(posedge clk_1K or negedge Rst_n)
- if(!Rst_n)
- sel_r <= 8'b0000_0001;
- else if(sel_r == 8'b1000_0000)
- sel_r <= 8'b0000_0001;
- else
- sel_r <= sel_r << 1;
-
- always@(*)
- case(sel_r)
- 8'b0000_0001:data_tmp = disp_data[3:0];
- 8'b0000_0010:data_tmp = disp_data[7:4];
- 8'b0000_0100:data_tmp = disp_data[11:8];
- 8'b0000_1000:data_tmp = disp_data[15:12];
- 8'b0001_0000:data_tmp = disp_data[19:16];
- 8'b0010_0000:data_tmp = disp_data[23:20];
- 8'b0100_0000:data_tmp = disp_data[27:24];
- 8'b1000_0000:data_tmp = disp_data[31:28];
- default:data_tmp = 4'b0000;
- endcase
-
- always@(*)
- case(data_tmp)
- 4'h0:seg = 7'b1000000;
- 4'h1:seg = 7'b1111001;
- 4'h2:seg = 7'b0100100;
- 4'h3:seg = 7'b0110000;
- 4'h4:seg = 7'b0011001;
- 4'h5:seg = 7'b0010010;
- 4'h6:seg = 7'b0000010;
- 4'h7:seg = 7'b1111000;
- 4'h8:seg = 7'b0000000;
- 4'h9:seg = 7'b0010000;
- 4'ha:seg = 7'b0001000;
- 4'hb:seg = 7'b0000011;
- 4'hc:seg = 7'b1000110;
- 4'hd:seg = 7'b0100001;
- 4'he:seg = 7'b0000110;
- 4'hf:seg = 7'b0001110;
- endcase
-
- assign sel = (En)?sel_r:8'b0000_0000;
- endmodule
复制代码
这个是我写的控制模块
- module ctrl(
- clk,
- rst_n,
- key_start_flag,
- key_pause_flag,
- disp_data
- );
- input clk;
- input rst_n;
- input key_start_flag;
- input key_pause_flag;
- output[31:0] disp_data;
- reg pause;//0计数,1停止
- always @(posedge clk or negedge rst_n)begin
- if(rst_n==1'b0)begin
- pause <= 1'b1;
- end
- else begin
- if( key_start_flag ) pause <= 1'b0;
- else if( key_pause_flag ) pause <= 1'b1;
- else ;
- end
- end
- reg[31:0] cnt;
- always @(posedge clk or negedge rst_n)begin
- if(rst_n==1'b0)begin
- cnt <= 0;
- end
- else begin
- if( ~pause ) cnt <= cnt + 1'd1;
- else ;
- end
- end
- assign disp_data = cnt;
- endmodule
复制代码
|