刚开始学FPGA,下面是自己写的一个两位数码管译码显示的数字秒表。在这过程中主要有一下几点感悟:1、IF后面最好跟上ELSE。2、把复杂系统分成一个个小模块来实现,模块中的程序越简单越好。3、注意计数的后面要将计数变量清零。感觉FPGA的精髓是在逻辑和时序上,在编写程序的时候要有一个全面的数字系统概念。
module topled(clk,rst,out,sel1);
input clk,rst;
output [7:0]out;
output [1:0]sel1;
wire [7:0]a,b;
reg [7:0] out;
wire [1:0] sel1;
wire [3:0] num1,num2;
count count1(.clk(clk),.rst(rst),.ge(num1),.shi(num2));
sel sel0(.clk(clk),.rst(rst),.sel(sel1));
seg seg0(.num(num1),.SevenSeg(a));
seg seg1(.num(num2),.SevenSeg(b));
always@(sel1)
begin
if(sel1==2) out<=a;
else if(sel1==1) out<=b;
end
endmodule
module sel(clk,rst,sel);
input clk,rst;
output [1:0] sel;
reg [1:0] sel;
reg [27:0] cnt1;
always@(posedge clk)
begin
if(!rst) begin cnt1<=0;sel<=1;end
if(cnt1==1000)
begin
cnt1<=0;
sel<=sel<<1;
sel[0]<=sel[1];
end
else
cnt1<=cnt1+1;
end
endmodule
module count(clk,rst,ge,shi);
input clk;
input rst;
output [3:0] ge,shi;
reg [3:0] ge,shi;
reg [27:0]cnt;
always@(posedge clk)
begin
if(!rst) begin cnt<=0;ge<=0;shi<=0;end
else if(cnt==10000000)
begin
if(ge==9)
begin
ge<=0;
shi<=shi+1;
if(shi==9)
shi<=0;
end
else
ge<=ge+1;
cnt<=0;
end
else
cnt<=cnt+1;
end
endmodule
module seg(num,SevenSeg);
input [3:0] num;
output [7:0] SevenSeg;
reg [7:0] SevenSeg;
always@ (num)
case(num)
4'h0: SevenSeg<=8'b11111100;
4'h1: SevenSeg<=8'b01100000;
4'h2: SevenSeg<=8'b11011010;
4'h3: SevenSeg<=8'b11110010;
4'h4: SevenSeg<=8'b01100110;
4'h5: SevenSeg<=8'b10110110;
4'h6: SevenSeg<=8'b10111110;
4'h7: SevenSeg<=8'b11100000;
4'h8: SevenSeg<=8'b11111110;
4'h9: SevenSeg<=8'b11110110;
default: SevenSeg<=8'b00000000;
endcase
endmodule
文章评论(0条评论)
登录后参与讨论