Part 1 跑马灯例程的实现方法锦集
硬软件配置:
// Engineer: Test202_Led10flash_DEO_xxx
/// Create Date: 2016/3/12
// Module Name: Led10flash_DEO_xxx
// Project Name: Led10flash_DEO_xxx
// Target Devices: CycloneIII EP3C16F484C6N
// Tool versions: Quartus 11.0+Modelsim SE12.0
// Additional Comments: 25_000_000;
1、方法一:链接符移位操作实现跑马灯,源代码:
//方法一:连接符移位操作实现跑马灯,在DEO上跑通//
module Led10flash_DEO_shift(clk,rstn,led);
input clk, rstn;
output[9:0] led;
reg[18:0] count;
reg[7:0] cnt;
//50MHz,20ns,20*500_000=10ms,2^19 count=[18:0]
parameter dely1ms=500_000;
always @(posedge clk or negedge rstn)
begin
if(!rstn) begin count<=18'd0;end
else if(count==dely1ms) begin count<=16'd0; end
else begin count<=count+1'd1;end
end
always @(posedge clk or negedge rstn)
begin
if(!rstn) begin cnt<=0;end
else if(cnt==9) begin cnt<=0;end
else if(count==dely1ms) begin cnt<=cnt+1'd1;end
end
//移位操作实现跑马灯
reg[9:0] shiftR;
always @(posedge clk or negedge rstn)
begin
if(!rstn) begin shiftR<=9'h01;end//初始化为9'h01很重要
else if(cnt==9) begin shiftR<={shiftR[0],shiftR[9:1]};end
else begin shiftR<=shiftR;end
end
assign led=shiftR;
endmodule
2、方法二:移位运算符实现跑马灯,源代码:
module Led10flash_DEO_Operator(clk,rstn,led);
input clk, rstn;
output[9:0] led;
reg[18:0] count;
reg[7:0] cnt;
//50MHz,20ns,20*500_000=10ms,2^19 count=[18:0]
parameter dely1ms=500_000;
always @(posedge clk or negedge rstn)
begin
if(!rstn) begin count<=18'd0;end
else if(count==dely1ms) begin count<=16'd0; end
else begin count<=count+1'd1;end
end
always @(posedge clk or negedge rstn)
begin
if(!rstn) begin cnt<=0;end
else if(cnt==9) begin cnt<=0;end
else if(count==dely1ms) begin cnt<=cnt+1'd1;end
end
//移位实现跑马灯
reg[9:0] shiftL;
always @(posedge clk)
begin
if(!rstn)begin shiftL<=9'h01;end
else if(cnt==9)begin shiftL<={shiftL<<1};end//移位运算符
else if(shiftL==10'h400)begin shiftL<=10'h01;end
//end
else begin
shiftL<=shiftL;
end
end
assign led=shiftL;
endmodule
3、方法三:case语句实现跑马灯,源代码:
module Led10flash_DEO_case(clk,rstn,led);
input clk,rstn;
output[9:0] led;
//reg[9:0] led;
//50MHz,20ns,20*500_000=10ms,2^19 count=[18:0]
parameter dely1ms=500_000;
reg[18:0] count;
reg[3:0] cnt;
always @(posedge clk or negedge rstn)
begin
if(!rstn) begin count<=18'd0;end
else if(count==dely1ms) begin count<=16'd0; end
else begin count<=count+1'd1;end
end
always @(posedge clk or negedge rstn)
begin
if(!rstn) begin cnt<=0;end
else if(cnt==9) begin cnt<=0;end
else if(count==dely1ms) begin cnt<=cnt+1'd1;end
end
reg[9:0]shiftL;
reg[3:0]i;
always@(posedge clk or negedge rstn)
begin
if(!rstn)begin shiftL<=10'h01;i<=4'd0;end
else begin
case(i)//case语句循环
4'd0:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h02;end
else begin shiftL<=10'h01;end
end
4'd1:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h04;end
else begin shiftL<=10'h02;end
end
4'd2:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h08;end
else begin shiftL<=10'h04;end
end
4'd3:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h10;end
else begin shiftL<=10'h08;end
end
4'd4:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h20;end
else begin shiftL<=10'h10;end
end
4'd5:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h40;end
else begin shiftL<=10'h20;end
end
4'd6:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h80;end
else begin shiftL<=10'h40;end
end
4'd7:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h100;end
else begin shiftL<=10'h80;end
end
4'd8:begin
if(cnt==9)begin i<=i+4'd1;shiftL<=10'h200;end
else begin shiftL<=10'h100;end
end
4'd9:begin
if(cnt==9)begin i<=4'd0;shiftL<=10'h01;end
else begin shiftL<=10'h200;end
end
default:begin shiftL<=10'h01;i=4'd0;end
endcase
end
end
assign led=shiftL;
endmodule
4、方法四:case语句+连接符移位操作实现左循环右循环式跑马灯,源代码:
module Led10flash_DEO_caseLR(clk,rstn,led);
input clk,rstn;
output[9:0] led;
parameter dely=500_000;//19位,所以count为[18:0]
reg[18:0]count;
reg[3:0]cnt;
always@(posedge clk or negedge rstn)
begin
if(!rstn) begin count<=18'd0;end
else if(count==dely)begin count<=18'd0;end
else count<=count+1'd1;
end
always@(posedge clk or negedge rstn)
begin
if(!rstn) begin cnt<=3'd0;end
else if(cnt==9) begin cnt<=3'd0;end
else if(count==dely)begin cnt<=cnt+1'd1;end
end
reg[9:0] shiftLR;
reg[2:0] i;
//连接符移位操作实现左右循环跑
always @(posedge clk or negedge rstn)
begin
if(!rstn)begin i<=3'd0;shiftLR<=10'h01;end
else if(cnt==9)begin//循环
case(i)
3'd0:begin
shiftLR<={shiftLR[8:0],shiftLR[9]}; //左移//if(cnt==9)
if(shiftLR==10'h200) begin
i=3'd1;
shiftLR<=10'h100; //右移
end
end
3'd1:begin
shiftLR<={shiftLR[0],shiftLR[9:1]};//右移 //if(cnt==9)
if(shiftLR==10'h01)begin
i=3'd0;
shiftLR<=10'h02;//左移
end
end
default:begin i<=3'd0; shiftLR<=10'h01;end
endcase
end
end
assign led=shiftLR;
endmodule
5、方法五:Moore状态机实现跑马灯,源代码:
///////////////////*******************************////
//这个是在另DEO开发板上跑通的,所要注意引脚数目和引脚分配//
///////////////////******************************////
module led10flash_DEO(clk,rst,led);
input clk,rst;
output[9:0] led;
reg[9:0] led;
reg[3:0] state;
reg[24:0] cnt;
reg[11:0] count;
parameter s0='d0,s1='d1,s2='d2,s3='d3,s4='d4,s5='d5,s6='d6,s7='d7,s8='d8,s9='d9,s10='d10,s11='d11;
parameter led0=8'b1111_1111,led1=8'b0000_0000,led2=8'b1000_0000,led3=8'b0100_0000,led4=8'b0010_0000,
led5=8'b0001_0000,led6=8'b0000_1000,led7=8'b0000_0100,led8=8'b0000_0010,led9=8'b0000_0001,
led10=8'b1010_1010,led11=8'b0101_0101;
parameter dely=250_000_000;//延时0.5s,25位,[24:0]
wire clk2hz;
always @(posedge clk)
begin
if(!rst) cnt<=1'b0;
else if(cnt==dely) cnt<=1'b0;
else cnt<=cnt+1'b1;
end
assign clk2hz=cnt[24];
always @(posedge clk2hz )
begin
if(!rst) state<=s0;
else if(count==11) begin count<=0;end
// else if(cnt==dely) begin //count<=count+1'b1;
else
case(state)
s0:begin if(count==0)begin count<= count+1'd1;state<=s1;end
end
s1:begin if(count==1)begin count<= count+1'd1; state<=s2;end
else state<=s1;
end
s2:begin if(count==2)begin count<= count+1'd1; state<=s3;end
else state<=s2;
end
s3:begin if(count==3)begin count<= count+1'd1; state<=s4;end
else state<=s3;
end
s4:begin if(count==4)begin count<= count+1'd1; state<=s5;end
else state<=s4;
end
s5:begin if(count==5)begin count<= count+1'd1; state<=s6;end
else state<=s5;
end
s6:begin if(count==6)begin count<= count+1'd1; state<=s7;end
else state<=s6;
end
s7:begin if(count==7)begin count<= count+1'd1; state<=s8;end
else state<=s7;
end
s8:begin if(count==8)begin count<= count+1'd1; state<=s9;end
else state<=s8;
end
s9:begin if(count==9)begin count<= count+1'd1; state<=s10;end
else state<=s9;
end
s10:begin if(count==10)begin count<= count+1'd1; state<=s11;end
else state<=s10;
end
s11:begin if(count==11)begin count<= count+1'd1; state<=s0;end
else state<=s0;
end
default:state<=s0;
endcase
end
always @(state)
begin
case(state)
s0:led<=led0;
s1:led<=led1;
s2:led<=led2;
s3:led<=led3;
s4:led<=led4;
s5:led<=led5;
s6:led<=led6;
s7:led<=led7;
s8:led<=led8;
s9:led<=led9;
s10:led<=led10;
s11:led<=led11;
default:led<=led0;
endcase
end
endmodule
6、方法六:Mealy状态机实现跑马灯,源代码:
///////在DEO开发板上已经跑通了//////////
module Led10flash_DEO(clk,rst,led);
input clk,rst;
output[9:0] led;
reg[9:0] led;
reg[6:0] state;
//parameter s0='d0,s1='d1,s2='d2,s3='d3,s4='d4,s5='d5,s6='d6,s7='d7,
// s8='d8,s9='d9,s10='d10,s11='d11,s12='d12,s13='d13,s14='d14,
// s15='d15,s16='d16,s17='d17,s18='d18,s19='d19,s20='d20,s21='d21,
// s22='d22,s23='d23,s24='d24,s25='d25,s26='d26,s27='d27,s28='d28,s29='d29;
parameter s0='d0,s1='d1,s2='d2,s3='d3,s4='d4,s5='d5,s6='d6,s7='d7,s8='d8,s9='d9,s10='d10,s11='d11,s12='d12, s13='d13,s14='d14,s15='d15,s16='d16,s17='d17,s18='d18,s19='d19,s20='d20,s21='d21,s22='d22,s23='d23, s24='d24,s25='d25,s26='d26,s27='d27,s28='d28,s29='d29;
parameter led0=10'b1111_1111_11,
led1=10'b0000_0000_00,
led2=10'b1000_0000_00,
led3=10'b0100_0000_00,
led4=10'b0010_0000_00,
led5=10'b0001_0000_00,
led6=10'b0000_1000_00,
led7=10'b0000_0100_00,
led8=10'b0000_0010_00,
led9=10'b0000_0001_00,
led10=10'b1010_1010_10,
led11=10'b0000_0000_01,
led12=10'b0000_1100_00,
led13=10'b0001_1110_00,
led14=10'b0011_1111_00,
led15=10'b0111_1111_10,
led16=10'b1111_1111_11,
led17=10'b1000_0000_01,
led18=10'b1100_0000_11,//中间到两边
led19=10'b1110_0001_11,
led20=10'b1111_0011_11,
led21=10'b1111_1111_11,
led22=10'b0111_1111_10,//两边到中间
led23=10'b0011_1111_00,
led24=10'b0001_1110_00,
led25=10'b0000_1100_00,
led26=10'b0011_1111_00,
led27=10'b0111_1111_10,
led28=10'b1010_1010_10,
led29=10'b0101_0101_01;
//parameter dely=250_000_000;//延时0.5s,25位,[24:0]
parameter dely=25_000_000;
reg[24:0] cnt;
reg[6:0] count;
wire clk2hz;
always @(posedge clk)
begin
if(!rst)cnt<=1'b0;
else if(cnt==dely) cnt<=1'b0;//延时5s,频率为0.2hz一定要计算好,计数25_000_000次,25位
else cnt<=cnt+1;//若要分频成为4hz的时钟,则需要计数12500_000次,24位
end
assign clk2hz=cnt[24];
always @(posedge clk2hz )
begin
if(!rst) state<=s0;
else if(count==29) begin count<=0;end
// else if(cnt==dely) begin //count<=count+1'b1;
else
case(state)
s0:begin if(count==0)begin count<= count+1'd1;state<=s1;end
end
s1:begin if(count==1)begin count<= count+1'd1; state<=s2;end
else state<=s1;
end
s2:begin if(count==2)begin count<= count+1'd1; state<=s3;end
else state<=s2;
end
s3:begin if(count==3)begin count<= count+1'd1; state<=s4;end
else state<=s3;
end
s4:begin if(count==4)begin count<= count+1'd1; state<=s5;end
else state<=s4;
end
s5:begin if(count==5)begin count<= count+1'd1; state<=s6;end
else state<=s5;
end
s6:begin if(count==6)begin count<= count+1'd1; state<=s7;end
else state<=s6;
end
s7:begin if(count==7)begin count<= count+1'd1; state<=s8;end
else state<=s7;
end
s8:begin if(count==8)begin count<= count+1'd1; state<=s9;end
else state<=s8;
end
s9:begin if(count==9)begin count<= count+1'd1; state<=s10;end
else state<=s9;
end
s10:begin if(count==10)begin count<= count+1'd1; state<=s11;end
else state<=s10;
end
s11:begin if(count==11)begin count<= count+1'd1; state<=s12;end
else state<=s11;
end
s12:begin if(count==12)begin count<= count+1'd1; state<=s13;end
else state<=s12;
end
s13:begin if(count==13)begin count<= count+1'd1; state<=s14;end
else state<=s13;
end
s14:begin if(count==14)begin count<= count+1'd1; state<=s15;end
else state<=s14;
end
s15:begin if(count==15)begin count<= count+1'd1; state<=s16;end
else state<=s15;
end
s16:begin if(count==16)begin count<= count+1'd1; state<=s17;end
else state<=s16;
end
s17:begin if(count==17)begin count<= count+1'd1; state<=s18;end
else state<=s17;
end
s18:begin if(count==18)begin count<= count+1'd1; state<=s19;end
else state<=s18;
end
s19:begin if(count==19)begin count<= count+1'd1; state<=s20;end
else state<=s19;
end
s20:begin if(count==20)begin count<= count+1'd1; state<=s21;end
else state<=s20;
end
s21:begin if(count==21)begin count<= count+1'd1; state<=s22;end
else state<=s21;
end
s22:begin if(count==22)begin count<= count+1'd1; state<=s23;end
else state<=s22;
end
s23:begin if(count==23)begin count<= count+1'd1; state<=s24;end
else state<=s23;
end
s24:begin if(count==24)begin count<= count+1'd1; state<=s25;end
else state<=s24;
end
s25:begin if(count==25)begin count<= count+1'd1; state<=s26;end
else state<=s25;
end
s26:begin if(count==26)begin count<= count+1'd1; state<=s27;end
else state<=s26;
end
s27:begin if(count==27)begin count<= count+1'd1; state<=s28;end
else state<=s27;
end
s28:begin if(count==28)begin count<= count+1'd1; state<=s29;end
else state<=s28;
end
s29:begin if(count==29)begin count<= 0;state<=s0;end //可以直接begin count<=0;state<=s0;end
else begin state<=s0;count<=0;end
end
default:begin state<=s0;end
endcase
end
always @(state)
begin
case(state)
s0:led<=led0;
s1:led<=led1;
s2:led<=led2;
s3:led<=led3;
s4:led<=led4;
s5:led<=led5;
s6:led<=led6;
s7:led<=led7;
s8:led<=led8;
s9:led<=led9;
s10:led<=led10;
s11:led<=led11;
s12:led<=led12;
s13:led<=led13;
s14:led<=led14;
s15:led<=led15;
s16:led<=led16;
s17:led<=led17;
s18:led<=led18;
s19:led<=led19;
s20:led<=led20;
s21:led<=led21;
s22:led<=led22;
s23:led<=led23;
s24:led<=led24;
s25:led<=led25;
s26:led<=led26;
s27:led<=led27;
s28:led<=led28;
s29:led<=led29;
default:led<=led0;
endcase
end
endmodule