相关资料 可以去小脚丫官网查询想要信息,链接 小脚丫STEP开源社区
相关模块 时钟分频 带复位的时钟分频:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 module divide ( input clk, input rst_n, output clkout ); parameter WIDTH = 3 ; parameter N = 5 ; reg [WIDTH-1 :0 ] cnt_p,cnt_n; reg clk_p,clk_n; always @ (posedge clk or negedge rst_n ) begin if (!rst_n) cnt_p<=0 ; else if (cnt_p==(N-1 )) cnt_p<=0 ; else cnt_p<=cnt_p+1 ; end always @ (posedge clk or negedge rst_n) begin if (!rst_n) clk_p<=0 ; else if (cnt_p<(N>>1 )) clk_p<=0 ; else clk_p<=1 ; end always @ (negedge clk or negedge rst_n) begin if (!rst_n) cnt_n<=0 ; else if (cnt_n==(N-1 )) cnt_n<=0 ; else cnt_n<=cnt_n+1 ; end always @ (negedge clk) begin if (!rst_n) clk_n<=0 ; else if (cnt_n<(N>>1 )) clk_n<=0 ; else clk_n<=1 ; end assign clkout = (N==1 ) ? clk : ( N[0 ]?(clk_p&clk_n):clk_p );endmodule
不带复位的时钟分频:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 module divide ( input clk, output clkout ); parameter WIDTH = 3 ; parameter N = 5 ; reg [WIDTH-1 :0 ] cnt_p,cnt_n; reg clk_p,clk_n; always @ (posedge clk) begin if (cnt_p==(N-1 )) cnt_p<=0 ; else cnt_p<=cnt_p+1 ; end always @ (posedge clk) begin if (cnt_p<(N>>1 )) clk_p<=0 ; else clk_p<=1 ; end always @ (negedge clk) begin if (cnt_n==(N-1 )) cnt_n<=0 ; else cnt_n<=cnt_n+1 ; end always @ (negedge clk) begin if (cnt_n<(N>>1 )) clk_n<=0 ; else clk_n<=1 ; end assign clkout = (N==1 ) ? clk : ( N[0 ]?(clk_p&clk_n):clk_p );endmodule
按键消抖 对一个按键进行消抖(最基础):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 module debounce( input clk_1kHz, input key, output reg key_out ); reg [3 :0 ]cnt; always @(posedge clk_1kHz)begin if (!key) cnt<=0 ; else if (cnt==4'd10 ) cnt<=4'd10 ; else cnt<=cnt+1 ; if (cnt==4'd9 ) key_out<=1 ; else key_out<=0 ; end endmodule
暂未确认正确:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 module debounce ( input clk, input rst, input [N-1 :0 ] key, output [N-1 :0 ] key_pulse ); parameter N = 1 ; reg [N-1 :0 ] key_rst_pre; reg [N-1 :0 ] key_rst; wire [N-1 :0 ] key_edge; always @(posedge clk or negedge rst) begin if (!rst) begin key_rst <= {N{1'b1 }}; key_rst_pre <= {N{1'b1 }}; end else begin key_rst <= key; key_rst_pre <= key_rst; end end assign key_edge = key_rst_pre & (~key_rst); reg [17 :0 ] cnt; always @(posedge clk or negedge rst) begin if (!rst) cnt <= 18'h0 ; else if (key_edge) cnt <= 18'h0 ; else cnt <= cnt + 1'h1 ; end reg [N-1 :0 ] key_sec_pre; reg [N-1 :0 ] key_sec; always @(posedge clk or negedge rst) begin if (!rst) key_sec <= {N{1'b1 }}; else if (cnt==18'h3ffff ) key_sec <= key; end always @(posedge clk or negedge rst) begin if (!rst) key_sec_pre <= {N{1'b1 }}; else key_sec_pre <= key_sec; end assign key_pulse = key_sec_pre & (~key_sec); endmodule
计数器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 reg [2 :0 ] cnt_8; initial cnt_8 <= 3'd0 ;always @(posedge clk_1kHz)begin if (cnt_8==3'd7 ) cnt_8<=3'd0 ; else cnt_8<=cnt_8+1 ; end
数码管显示 非扫描型 (共阴极):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 module Display( input number, output reg [8 :0 ] seg, ); initial begin seg <= 9'h00 ; end always @(number)begin case (number) 0 : seg <= 9'h3f ; 1 : seg <= 9'h06 ; 2 : seg <= 9'h5b ; 3 : seg <= 9'h4f ; 4 : seg <= 9'h66 ; 5 : seg <= 9'h6d ; 6 : seg <= 9'h7d ; 7 : seg <= 9'h07 ; 8 : seg <= 9'h7f ; default : seg <= 9'h6f ; endcase end endmodule
扫描型 (共阴极):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 module Display( input clk_1kHz, input [3 :0 ] tens, input [3 :0 ] ones, …… input [3 :0 ] xxxx, output reg [7 :0 ] seg, output reg [7 :0 ] cat ); initial begin seg <= 8'b0000_0000 ; cat <= 8'b1111_1111 ; end always @(cnt_8) begin case (cnt_8) 0 : begin cat<=8'b1111_1110 ; case (ones) 0 : seg<=8'b0011_1111 ; 1 : seg<=8'b0000_0110 ; 2 : seg<=8'b0101_1011 ; 3 : seg<=8'b0100_1111 ; 4 : seg<=8'b0110_0110 ; 5 : seg<=8'b0110_1101 ; 6 : seg<=8'b0111_1101 ; 7 : seg<=8'b0000_0111 ; 8 : seg<=8'b0111_1111 ; default : seg<=8'b0110_1111 ; endcase end 1 : begin cat<=8'b1111_1101 ; case (ones) 0 : seg<=8'b0011_1111 ; 1 : seg<=8'b0000_0110 ; 2 : seg<=8'b0101_1011 ; 3 : seg<=8'b0100_1111 ; 4 : seg<=8'b0110_0110 ; 5 : seg<=8'b0110_1101 ; 6 : seg<=8'b0111_1101 ; 7 : seg<=8'b0000_0111 ; 8 : seg<=8'b0111_1111 ; default : seg<=8'b0110_1111 ; endcase end …… default : begin cat<=8'b0111_1111 ; case (xxxx) 0 : seg<=8'b0011_1111 ; 1 : seg<=8'b0000_0110 ; 2 : seg<=8'b0101_1011 ; 3 : seg<=8'b0100_1111 ; 4 : seg<=8'b0110_0110 ; 5 : seg<=8'b0110_1101 ; 6 : seg<=8'b0111_1101 ; 7 : seg<=8'b0000_0111 ; 8 : seg<=8'b0111_1111 ; default : seg<=8'b0110_1111 ; endcase end endcase end reg [2 :0 ] cnt_8; initial cnt_8 <= 3'd0 ;always @(posedge clk_1kHz)begin if (cnt_8==3'd7 ) cnt_8<=3'd0 ; else cnt_8<=cnt_8+1 ; end endmodule
点阵显示 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 module Lattice_Display( input clk_1kHz, output reg [7 :0 ] row, output reg [7 :0 ] col_r, output reg [7 :0 ] col_g ); always @(cnt_8)begin case (cnt_8) 3'd7 :begin row=8'b0111_1111 ;col_r<=8'b0000_0011 ;col_g<=8'b0000_0000 ;end 3'd6 :begin row=8'b1011_1111 ;col_r<=8'b0000_0011 ;col_g<=8'b0000_0000 ;end 3'd5 :begin row=8'b1101_1111 ;col_r<=8'b0000_0000 ;col_g<=8'b0000_0000 ;end 3'd4 :begin row=8'b1110_1111 ;col_r<=8'b0000_0000 ;col_g<=8'b0000_0011 ;end 3'd3 :begin row=8'b1111_0111 ;col_r<=8'b0000_0000 ;col_g<=8'b0000_0011 ;end 3'd2 :begin row=8'b1111_1011 ;col_r<=8'b0000_0000 ;col_g<=8'b0000_0000 ;end 3'd1 :begin row=8'b1111_1101 ;col_r<=8'b0000_0011 ;col_g<=8'b0000_0011 ;end 3'd0 :begin row=8'b1111_1110 ;col_r<=8'b0000_0011 ;col_g<=8'b0000_0011 ;end endcase end reg [2 :0 ] cnt_8; initial cnt_8 <= 3'd0 ;always @(posedge clk_1kHz)begin if (cnt_8==3'd7 ) cnt_8<=3'd0 ; else cnt_8<=cnt_8+1 ; end endmodule
蜂鸣器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 module Beeper( input clk_10MHz, input tone_en, input [4 :0 ] tone, output reg piano_out ); reg [15 :0 ] time_end; always @(tone) begin case (tone) 5'd1 : time_end = 16'd19110 ; 5'd2 : time_end = 16'd17026 ; 5'd3 : time_end = 16'd15169 ; 5'd4 : time_end = 16'd14317 ; 5'd5 : time_end = 16'd12755 ; 5'd6 : time_end = 16'd11363 ; 5'd7 : time_end = 16'd10124 ; 5'd8 : time_end = 16'd9556 ; 5'd9 : time_end = 16'd8513 ; 5'd10 : time_end = 16'd7584 ; 5'd11 : time_end = 16'd7159 ; 5'd12 : time_end = 16'd6378 ; 5'd13 : time_end = 16'd5682 ; 5'd14 : time_end = 16'd5062 ; 5'd15 : time_end = 16'd4782 ; 5'd16 : time_end = 16'd4257 ; 5'd17 : time_end = 16'd3792 ; 5'd18 : time_end = 16'd3579 ; 5'd19 : time_end = 16'd3189 ; 5'd20 : time_end = 16'd2841 ; 5'd21 : time_end = 16'd2531 ; default :time_end = 16'd65535 ; endcase end reg [17 :0 ] time_cnt;always @(posedge clk_10MHz) begin if (!tone_en) begin time_cnt <= 1'b0 ; end else if (time_cnt >= time_end) begin time_cnt <= 1'b0 ; end else begin time_cnt <= time_cnt + 1'b1 ; end end always @(posedge clk_10MHz) begin if (time_cnt == time_end) begin piano_out <= ~piano_out; end else begin piano_out <= piano_out; end end endmodule