篮球30s倒计时
**
一、目的:
實現30s倒計時,并且在數碼管實時顯示計數時間,每一秒led綠色燈閃爍一次,計時結束時數碼管停止,同時紅色led燈常亮。二、代碼部分:
代碼主要有三個部分,時鐘分頻模塊,數碼管顯示模塊以及頂層模塊(這里我沒有去調用內部pLL) 時鐘分頻模塊如下: module div_25mhz( input i_clk,input i_rst,output reg clk_1hz); parameter clk_cnt = 24_999_999; reg [31:0] cnt; always @(posedge i_clk or negedge i_rst) beginif(!i_rst) begincnt <= 32'd0;clk_1hz <= 0;endelse if(cnt == clk_cnt) begincnt <= 32'd0;clk_1hz <= ~clk_1hz;endelse begincnt <= cnt +1'b1;clk_1hz <= clk_1hz;end end endmodule數碼管顯示模塊代碼如下:
module basketball_timing(input i_clk,input i_rst,input cnt_en,output reg led_zt,output reg led_0,output reg [6:0] smg_shi,output reg [6:0] smg_ge ); parameter num_0 = 7'b100_0000,num_1 = 7'b111_1001,num_2 = 7'b010_0100,num_3 = 7'b011_0000,num_4 = 7'b001_1001,num_5 = 7'b001_0010,num_6 = 7'b000_0010,num_7 = 7'b111_1000,num_8 = 7'b000_0000,num_9 = 7'b001_0000;reg [3:0] cnt_shi; reg [3:0] cnt_ge; always @(posedge i_clk or negedge i_rst) beginif(!i_rst) begincnt_shi <= 4'd3;cnt_ge <= 4'd0;led_0 <= 0;led_zt <= 0;endelse if((cnt_shi == 4'd0)&&(cnt_ge == 4'd0)) begincnt_shi <= cnt_shi;cnt_ge <= cnt_ge;led_0 <= 1;endelse if(!cnt_en) begincnt_shi <= cnt_shi;cnt_ge <= cnt_ge;led_zt <= 0;endelse if(cnt_ge == 4'd0) begincnt_shi <= cnt_shi - 1'b1;cnt_ge <= 4'd9;endelse begincnt_shi <= cnt_shi;cnt_ge <= cnt_ge - 1'b1;led_zt <= 1;end end always @( * ) begincase(cnt_shi)4'd0: smg_shi <= num_0;4'd1: smg_shi <= num_1;4'd2: smg_shi <= num_2;4'd3: smg_shi <= num_3;default: ;endcase end always @( * ) begincase(cnt_ge)4'd0: smg_ge <= num_0;4'd1: smg_ge <= num_1;4'd2: smg_ge <= num_2;4'd3: smg_ge <= num_3;4'd4: smg_ge <= num_4;4'd5: smg_ge <= num_5;4'd6: smg_ge <= num_6;4'd7: smg_ge <= num_7;4'd8: smg_ge <= num_8;4'd9: smg_ge <= num_9;default: ;endcase endendmodule頂層模塊代碼如下:
module basketball_timing_top(input i_clk,input i_rst,input cnt_en,output led_zt,output led_0,output [6:0] smg_shi,output [6:0] smg_ge ); wire clk_1hz; div_25mhz u1(.i_clk(i_clk),.i_rst(i_rst),.clk_1hz(clk_1hz) ); basketball_timing u2(.i_clk(clk_1hz),.i_rst(i_rst),.cnt_en(cnt_en),.led_zt(led_zt),.led_0(led_0),.smg_shi(smg_shi),.smg_ge(smg_ge) ); endmodule三、總結
總的來說這些代碼是最基本的,入門就需要掌握,小白可以看看試著謝謝,加油,趕路人!
總結
- 上一篇: 测试真相 | 软件测试真的只是“点,点,
- 下一篇: protocol buffer 使用之