《FPGA Verilog篇》Part 1 跑马灯例程的实现方法锦集

来源:互联网 发布:windows好用的v翻墙 编辑:程序博客网 时间:2024/05/17 05:08

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) begincount<=18'd0;endelse if(count==dely1ms) begin count<=16'd0; endelse begin count<=count+1'd1;endendalways @(posedge clk or negedge rstn)beginif(!rstn) begin  cnt<=0;endelse if(cnt==9) begin cnt<=0;endelse if(count==dely1ms) begin cnt<=cnt+1'd1;endend//移位操作实现跑马灯reg[9:0] shiftR;always @(posedge clk or negedge rstn)beginif(!rstn) begin shiftR<=9'h01;end//初始化为9'h01很重要else if(cnt==9) begin shiftR<={shiftR[0],shiftR[9:1]};endelse begin shiftR<=shiftR;endendassign 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) begincount<=18'd0;endelse if(count==dely1ms) begin count<=16'd0; endelse begin count<=count+1'd1;endendalways @(posedge clk or negedge rstn)beginif(!rstn) begin  cnt<=0;endelse if(cnt==9) begin cnt<=0;endelse if(count==dely1ms) begin cnt<=cnt+1'd1;endend//移位实现跑马灯reg[9:0] shiftL;always @(posedge clk)begin if(!rstn)begin shiftL<=9'h01;endelse if(cnt==9)begin shiftL<={shiftL<<1};end//移位运算符else if(shiftL==10'h400)begin shiftL<=10'h01;end//endelse beginshiftL<=shiftL;endendassign 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) begincount<=18'd0;endelse if(count==dely1ms) begin count<=16'd0; endelse begin count<=count+1'd1;endendalways @(posedge clk or negedge rstn)beginif(!rstn) begin  cnt<=0;endelse if(cnt==9) begin cnt<=0;endelse if(count==dely1ms) begin cnt<=cnt+1'd1;endendreg[9:0]shiftL;reg[3:0]i;always@(posedge clk or negedge rstn)beginif(!rstn)begin shiftL<=10'h01;i<=4'd0;endelse begincase(i)//case语句循环4'd0:begin if(cnt==9)begin i<=i+4'd1;shiftL<=10'h02;end else begin shiftL<=10'h01;endend4'd1:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h04;end else begin shiftL<=10'h02;endend4'd2:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h08;end  else begin shiftL<=10'h04;endend4'd3:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h10;end  else begin shiftL<=10'h08;endend4'd4:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h20;end  else begin shiftL<=10'h10;endend4'd5:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h40;end  else begin shiftL<=10'h20;endend4'd6:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h80;end  else begin shiftL<=10'h40;endend4'd7:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h100;end  else begin shiftL<=10'h80;endend4'd8:beginif(cnt==9)begin i<=i+4'd1;shiftL<=10'h200;end  else begin shiftL<=10'h100;endend4'd9:beginif(cnt==9)begin i<=4'd0;shiftL<=10'h01;end  else begin shiftL<=10'h200;endenddefault:begin shiftL<=10'h01;i=4'd0;endendcaseendendassign 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)beginif(!rstn) begin count<=18'd0;endelse if(count==dely)begin count<=18'd0;endelse count<=count+1'd1;endalways@(posedge clk or negedge rstn)beginif(!rstn) begin cnt<=3'd0;endelse if(cnt==9) begin cnt<=3'd0;endelse if(count==dely)begin cnt<=cnt+1'd1;endendreg[9:0] shiftLR;reg[2:0] i;//连接符移位操作实现左右循环跑always @(posedge clk or negedge rstn)beginif(!rstn)begin i<=3'd0;shiftLR<=10'h01;endelse 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;//右移endend3'd1:begin shiftLR<={shiftLR[0],shiftLR[9:1]};//右移//if(cnt==9)  if(shiftLR==10'h01)begin i=3'd0;shiftLR<=10'h02;//左移endenddefault:begin i<=3'd0; shiftLR<=10'h01;endendcaseendendassign 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)beginif(!rst) cnt<=1'b0;else if(cnt==dely) cnt<=1'b0;else cnt<=cnt+1'b1;endassign 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;endelse state<=s1;end s2:begin if(count==2)begin count<= count+1'd1; state<=s3;endelse state<=s2;end s3:begin if(count==3)begin count<= count+1'd1; state<=s4;endelse state<=s3;end s4:begin if(count==4)begin count<= count+1'd1; state<=s5;endelse state<=s4;end s5:begin if(count==5)begin count<= count+1'd1; state<=s6;endelse state<=s5;end s6:begin if(count==6)begin count<= count+1'd1; state<=s7;endelse state<=s6;end s7:begin if(count==7)begin count<= count+1'd1; state<=s8;endelse state<=s7;end s8:begin if(count==8)begin count<= count+1'd1; state<=s9;endelse state<=s8;end s9:begin if(count==9)begin count<= count+1'd1; state<=s10;endelse state<=s9;end s10:begin if(count==10)begin count<= count+1'd1; state<=s11;endelse state<=s10;end s11:begin if(count==11)begin count<= count+1'd1; state<=s0;endelse state<=s0;enddefault:state<=s0;endcaseendalways @(state)begincase(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;endcaseendendmodule 

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位endassign 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;endelse state<=s1;end s2:begin if(count==2)begin count<= count+1'd1; state<=s3;endelse state<=s2;end s3:begin if(count==3)begin count<= count+1'd1; state<=s4;endelse state<=s3;end s4:begin if(count==4)begin count<= count+1'd1; state<=s5;endelse state<=s4;end s5:begin if(count==5)begin count<= count+1'd1; state<=s6;endelse state<=s5;end s6:begin if(count==6)begin count<= count+1'd1; state<=s7;endelse state<=s6;end s7:begin if(count==7)begin count<= count+1'd1; state<=s8;endelse state<=s7;end s8:begin if(count==8)begin count<= count+1'd1; state<=s9;endelse state<=s8;end s9:begin if(count==9)begin count<= count+1'd1; state<=s10;endelse state<=s9;end s10:begin if(count==10)begin count<= count+1'd1; state<=s11;endelse state<=s10;end s11:begin if(count==11)begin count<= count+1'd1; state<=s12;endelse state<=s11;end s12:begin if(count==12)begin count<= count+1'd1; state<=s13;endelse state<=s12;ends13:begin if(count==13)begin count<= count+1'd1; state<=s14;endelse state<=s13;ends14:begin if(count==14)begin count<= count+1'd1; state<=s15;endelse state<=s14;ends15:begin if(count==15)begin count<= count+1'd1; state<=s16;endelse state<=s15;ends16:begin if(count==16)begin count<= count+1'd1; state<=s17;endelse state<=s16;ends17:begin if(count==17)begin count<= count+1'd1; state<=s18;endelse state<=s17;ends18:begin if(count==18)begin count<= count+1'd1; state<=s19;endelse state<=s18;ends19:begin if(count==19)begin count<= count+1'd1; state<=s20;endelse state<=s19;ends20:begin if(count==20)begin count<= count+1'd1; state<=s21;endelse state<=s20;ends21:begin if(count==21)begin count<= count+1'd1; state<=s22;endelse state<=s21;ends22:begin if(count==22)begin count<= count+1'd1; state<=s23;endelse state<=s22;ends23:begin if(count==23)begin count<= count+1'd1; state<=s24;endelse state<=s23;ends24:begin if(count==24)begin count<= count+1'd1; state<=s25;endelse state<=s24;ends25:begin if(count==25)begin count<= count+1'd1; state<=s26;endelse state<=s25;ends26:begin if(count==26)begin count<= count+1'd1; state<=s27;endelse state<=s26;ends27:begin if(count==27)begin count<= count+1'd1; state<=s28;endelse state<=s27;ends28:begin if(count==28)begin count<= count+1'd1; state<=s29;endelse state<=s28;ends29:begin if(count==29)begin count<= 0;state<=s0;end//可以直接begin count<=0;state<=s0;endelse begin state<=s0;count<=0;endenddefault:begin state<=s0;endendcaseendalways @(state)begincase(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;endcaseendendmodule


0 0
原创粉丝点击