|
module example1(clk,rst_n,led);
//
input clk;
input rst_n;
//输出信号
output[7:0] led;
reg[7:0] led;
//计数器
reg[24:0] count1;
reg[2:0] count2;
//倍频
reg clk_temp;
//Creat a new clk 1Hz
always@(posedge clk or posedge rst_n)
begin
if(rst_n)
count1 <= 'd0;
else
if(count1 >= 'd50000000)
count1 <= 'd0;
else
count1 <= count1 + 'd1;
end
always@(posedge clk or posedge rst_n)
begin
if(rst_n)
clk_temp <= 'b0;
else
clk_temp <= (count1 >= 'd25000000)?'b1:'b0;
end
//LED
/*always@(posedge clk_temp or posedge rst_n)
begin
if(rst_n)
led[7:0] <= 'd0;
else
led[7:0] <= led[7:0] + 'd1;
end*/
/*always@(posedge clk_temp or posedge rst_n)
begin
if(rst_n)
count2 <= 'd0;
else
count2 <= count2 + 'd1;
end
always@(count2 or rst_n)
begin
if(rst_n)
led <= 8'b00000000;
else
case(count2)
3'b000 : led <= 8'b00000001;
3'b001 : led <= 8'b00000010;
3'b010 : led <= 8'b00000100;
3'b011 : led <= 8'b00001000;
3'b100 : led <= 8'b00010000;
3'b101 : led <= 8'b00100000;
3'b110 : led <= 8'b01000000;
3'b111 : led <= 8'b10000000;
endcase
end*/
always@(posedge clk_temp or posedge rst_n)
begin
if(rst_n)
led <= 8'b00000000;
else
if(led == 8'b00000000)
led <= 8'b00000001;
else
begin
led[7:0] <= {led[6:0],led[7]};
end
end
endmodule
一个是case方式, 一个是移位方式。
这个简单的代码花了好长时间,也不知道怎么突然就OK了,这个教训告诉我,以前错误的代码有时候是需要保存的,可以前后对比分析,找出自己错误的原因。