让led闪烁
module led(sys_clk,
sys_rstn,
led
);
input sys_clk;
input sys_rstn;
output [2:0] led;
reg [2:0] led;
reg [24:0] delay_cnt;
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
delay_cnt <= 25'd0;
else
begin
if(delay_cnt == 25'd24999999)
delay_cnt <= 25'd0;
else
delay_cnt<=delay_cnt+1'b1;
end
end
always@(posedge sys_clk or negedge sys_rstn)
begin
if(!sys_rstn)
led<=3'b111;
else
begin
if(delay_cnt==25'd24999999)
led<=~led;
else
led<=led;
end
end
endmodule
此帖出自
小平头技术问答
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:Liqingtao
//
// Create Date: 09:51:48 11/22/2016
// Design Name:
// Module Name: div
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module div(
clk,
rst_n,
div_clk
);
//input signal;
input clk;
input rst_n;
//output signal;
output div_clk;
reg [31:0] counter;
reg div_clk;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
counter <= 32'd0;
div_clk <= 1'b0;
end
else begin
if(counter==32'h17d7840)
begin
counter <=32'h0;
div_clk <=~div_clk;
end
else
counter <= counter+1'b1;
end
end
endmodule
PS:分频模块;跨时钟信号自己利用计数器进行分频容易出现亚稳态,建议使用PLL进行分/倍频;
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer: Liqingtao
//
// Create Date: 10:07:29 11/22/2016
// Design Name:
// Module Name: led
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module led(
div_clk,
rst_n,
cn
);
//input signal;
input div_clk;
input rst_n;
//output signal;
output[5:0] cn;
reg [5:0] cn;
reg [2:0] counter;
always@(posedge div_clk or negedge rst_n)
begin
if(!rst_n)
begin
counter <= 3'd0;
end
else begin
if(counter==3'b110)
begin
counter <=3'd0;
end
else
counter <=counter+1'b1;
end
end
always@(posedge div_clk or negedge rst_n)
begin
if(!rst_n)
begin
cn <= 6'd0;
end
else begin
case(counter)
6'b000000:
cn<= 6'b000000;
6'b000001:
cn<= 6'b000001;
6'b000010:
cn<= 6'b000011;
6'b000011:
cn<= 6'b000111;
6'b000100:
cn<= 6'b001111;
6'b000101:
cn<= 6'b011111;
6'b000110:
cn<= 6'b111111;
default:
cn<= 6'b0;
endcase
end
end
//assign cn = counter;
endmodule
PS:这是LED灯闪烁的代码,这是我以前写的,现在想想还有一个更优的想法:利用移位操作符,代码更短;
led<=6'1111110;
led<={led[4:0],led[5]};
一点点小的想法,嘿嘿~~~
一周热门 更多>