苏州专业网站建设设计公司,珠海市城乡住房建设局网站,企业标准型手机网站,平面设计人才网从计数器到可控制线性序列机——LED实验进化六部曲一#xff1a;让LED灯按照亮0.25s#xff0c;灭0.75s的状态循环亮灭二#xff1a;让LED灯按照亮0.25s#xff0c;灭0.5s#xff0c;亮0.75s#xff0c;灭1s的状态循环亮灭三#xff1a;让LED灯按照指定的亮灭模式亮灭让LED灯按照亮0.25s灭0.75s的状态循环亮灭二让LED灯按照亮0.25s灭0.5s亮0.75s灭1s的状态循环亮灭三让LED灯按照指定的亮灭模式亮灭亮灭模式未知由用户随机指定。以0.25s为一个变化周期8个变化状态为一个循环四让LED灯按照指定的亮灭模式亮灭亮灭模式未知由用户随机指定。8个变化状态为一个循环每个变化状态的时间值可以根据不同的应用场景选择五让多个LED灯按照设置的模式各自在一个变化循环内独立亮灭变化六每隔10ms让LED灯的一个8状态循环执行一次每个状态的变化时间值小一点方便测试比如设置为10us一让LED灯按照亮0.25s灭0.75s的状态循环亮灭
源文件 module counter_led_0(input clk,input reset,output reg led);reg [25:0] cnt;parameter MCNT50000000;always(posedge clk or negedge reset)beginif(!reset)cnt0;else if(cntMCNT-1)cnt0;else cntcnt1b1;endalways(posedge clk or negedge reset)beginif(!reset)led0;else if(cnt(MCNT/4MCNT/2)-1)led1;else if(cntMCNT-1)led0;else ledled;end
endmodule
测试文件
timescale 1ns / 1nsmodule counter_led_0_tb();reg clk;reg reset_n;wire led;counter_led_0 counter_led_0(.clk(clk),.reset(reset_n),.led(led));defparam counter_led_0.MCNT26d25000;initial beginclk0;endalways#10 clk!clk;initial beginreset_n0;#201;reset_n1;#40000000;$stop;end
endmodule
仿真截图
收获 1用parameter定义变量的时候对变量除以2还是很容易实现的因为其对应的是一个常量的值但是若是一个变量在过程中会实时变化的话直接/2是会有一些问题的~ 2需要这道题需要注意亮0.25s不代表是要在0的基础上计数到0.25s在变为1而是在1的基础上保留的时间是0.25s这里也是需要转过弯儿的地方~
二让LED灯按照亮0.25s灭0.5s亮0.75s灭1s的状态循环亮灭
源代码 module counter_led_1(input clk,input reset,output reg led);reg [26:0] cnt;parameter MCNT125000000;always(posedge clk or negedge reset)beginif(!reset)cnt0;else if(cntMCNT-1)cnt0;else cntcnt1b1;endalways(posedge clk or negedge reset)beginif(!reset)led1;else if(cntMCNT/10-1)led0;else if(cntMCNT/10MCNT/5-1)led1;else if(cnt(MCNT/10MCNT/5)*2-1)led0;else if(cntMCNT-1)led1;elseledled;end
endmodule
仿真截图 收获 1这里为了使亮灭计数好想设置了复位状态时led灯为灭~
三让LED灯按照指定的亮灭模式亮灭亮灭模式未知由用户随机指定。以0.25s为一个变化周期8个变化状态为一个循环
源代码
//该模块的功能是让LED灯按照指定的亮灭模式亮灭亮灭模式未知由用户随机指定。以0.25s为一个变化周期8个变化状态为一个循环module counter_led_2(input clk,input reset,input [7:0]ctrl,output reg led);reg [26:0] cnt;parameter MCNT100000000;always(posedge clk or negedge reset)beginif(!reset)cnt0;else if(cntMCNT-1)cnt0;else cntcnt1b1;endalways(posedge clk or negedge reset)beginif(!reset)led0;else if(cntMCNT/8-1)ledctrl[0];else if(cntMCNT*2/8-1)ledctrl[1];else if(cntMCNT*3/8-1)ledctrl[2];else if(cntMCNT*4/8-1)ledctrl[3];else if(cntMCNT*5/8-1)ledctrl[4];else if(cntMCNT*6/8-1)ledctrl[5];else if(cntMCNT*7/8-1)ledctrl[6];else if(cntMCNT*8/8-1)ledctrl[7];else ledled;end
endmodule
测试文件
timescale 1ns / 1nsmodule counter_led_2_tb();reg clk;reg reset_n;reg [7:0]ctrl;wire led;counter_led_2 counter_led_2(.clk(clk),.reset(reset_n),.ctrl(ctrl),.led(led));defparam counter_led_2.MCNT100000;initial beginclk0;endalways#10 clk!clk;initial beginreset_n0;ctrl0;#201;reset_n1;#200;ctrl8b1001_0001;#40000000;$stop;end
endmodule
仿真图
四让LED灯按照指定的亮灭模式亮灭亮灭模式未知由用户随机指定。8个变化状态为一个循环每个变化状态的时间值可以根据不同的应用场景选择
思路在原有的基础上因为时间是用户自己设定的所以设置times变量作为输入因为MCNT变量以往都是固定值但是times会随时变化所以这里就又定义了一个cnt2记到1——8每到一个值灯就会反应出对应的亮度 源代码
//该模块的功能是让LED灯按照指定的亮灭模式亮灭亮灭模式未知由用户随机指定。8个变化状态为一个循环每个变化状态的时间值可以根据不同的应用场景选择module counter_led_3(input clk,input reset,input [31:0]times,input [7:0]ctrl,output reg led);reg [31:0] cnt;always(posedge clk or negedge reset)beginif(!reset)cnt0;else if(cnttimes-1)cnt0;else cntcnt1b1;endreg [2:0]cnt2;always(posedge clk or negedge reset)beginif(!reset)cnt23b0;else if(cnttimes-1)cnt2cnt21;endalways(posedge clk or negedge reset)beginif(!reset)led0;else begincase(cnt2)0:ledctrl[0];1:ledctrl[1];2:ledctrl[2];3:ledctrl[3];4:ledctrl[4];5:ledctrl[5];6:ledctrl[6]; 7:ledctrl[7]; default ledled;endcaseendend
endmodule
测试文件
timescale 1ns / 1nsmodule counter_led_3_tb();reg clk;reg reset_n;reg [7:0]ctrl;reg [31:0]times;wire led;counter_led_3 counter_led_3(.clk(clk),.reset(reset_n),.ctrl(ctrl),.times(times),.led(led));initial beginclk0;endalways#10 clk!clk;initial beginreset_n0;ctrl0;times0;#201;reset_n1;#200;ctrl8b1001_0001;#201;times2500;#2000001;times25000;#400000000;$stop;end
endmodule
仿真截图
五让多个LED灯按照设置的模式各自在一个变化循环内独立亮灭变化
答在四的基础上增加led的位宽以及设置多个控制信号进行赋值
module counter_led_5(Clk,Reset_n,CtrlA,CtrlB,Time,Led
);input Clk;input Reset_n;input [7:0]CtrlA;input [7:0]CtrlB;input [31:0]Time;output reg [1:0]Led;reg [31:0]counter;always(posedge Clk or negedge Reset_n)if(!Reset_n)counter 0;else if(counter Time - 1)counter 0;elsecounter counter 1b1;reg [2:0]counter2;always(posedge Clk or negedge Reset_n)if(!Reset_n) counter2 0; else if(counter Time - 1)counter2 counter2 1b1;always(posedge Clk or negedge Reset_n)if(!Reset_n)Led 2d0;else case(counter2)0:begin Led[0] CtrlA[0]; Led[1] CtrlB[0]; end1:begin Led[0] CtrlA[1]; Led[1] CtrlB[1]; end2:begin Led[0] CtrlA[2]; Led[1] CtrlB[2]; end3:begin Led[0] CtrlA[3]; Led[1] CtrlB[3]; end4:begin Led[0] CtrlA[4]; Led[1] CtrlB[4]; end5:begin Led[0] CtrlA[5]; Led[1] CtrlB[5]; end6:begin Led[0] CtrlA[6]; Led[1] CtrlB[6]; end7:begin Led[0] CtrlA[7]; Led[1] CtrlB[7]; enddefault:begin Led[0] Led; Led[1] Led[1]; endendcaseendmodule测试文件
timescale 1ns/1nsmodule counter_led_5_tb;reg Clk;reg Reset_n;reg [7:0]CtrlA,CtrlB;reg [31:0]Time;wire [1:0]Led;counter_led_5 counter_led_5(.Clk(Clk),.Reset_n(Reset_n),.CtrlA(CtrlA),.CtrlB(CtrlB),.Time(Time),.Led(Led));initial Clk 1;always #10 Clk !Clk;initial beginReset_n 0;CtrlA 0;CtrlB 0;Time 0;#201;Reset_n 1;#2000;Time 2500;CtrlA 8b1000_0110;CtrlB 8b1101_0010;#20000000; Time 25000;CtrlA 8b1010_0110;CtrlB 8b1100_1010;#20000000; $stop;endendmodule
六每隔10ms让LED灯的一个8状态循环执行一次每个状态的变化时间值小一点方便测试比如设置为10us 最开始自己的理解是在8个循环之后间隔10ms所以才有了以下代码的理解但是后来才懂原来周期是10msemmm有些许思想出路
注以下代码的逻辑思想正确即在原有计数的基础上又加了一个整体的计数周期8*times10ms但是这里的8需要对输入变量做乘法在运算时很有可能一个周期运算不出来就会出现一些错误所以这里还是要谨慎的使用小梅哥采用的是设置en进行控制自己没有写出来这里看完他的代码后自行默写了一下~
//该模块的功能是每隔10ms让LED灯的一个8状态循环执行一次每个状态的变化时间值小一点方便测试比如设置为10usmodule counter_led_4(input clk,input reset,input [31:0]times,input [7:0]ctrl,output reg led);reg [31:0] cnt;reg en;//设置使能只有在en为1的时候parameter count5000;always(posedge clk or negedge reset)beginif(!reset)cnt0;else if(cnttimes-1)cnt0;else cntcnt1b1;endreg [2:0]cnt2;always(posedge clk or negedge reset)beginif(!reset)cnt23b0;else if(cnttimes-1)cnt2cnt21;endreg [35:0] cnt3;always(posedge clk or negedge reset)beginif(!reset)cnt30;else if(cnt3times*8-1count)cnt30;else cnt3cnt31b1;endalways(posedge clk or negedge reset)beginif(!reset)led0;else if(cnt38*times-1)begincase(cnt2)0:ledctrl[0];1:ledctrl[1];2:ledctrl[2];3:ledctrl[3];4:ledctrl[4];5:ledctrl[5];6:ledctrl[6]; 7:ledctrl[7]; default ledled;endcaseendelse if(cnt38*times-1count)beginled0;endend
endmodule
timescale 1ns / 1nsmodule counter_led_4_tb();reg clk;reg reset_n;reg [7:0]ctrl;reg [31:0]times;wire led;counter_led_4 counter_led_4(.clk(clk),.reset(reset_n),.ctrl(ctrl),.times(times),.led(led));initial beginclk0;endalways#10 clk!clk;initial beginreset_n0;ctrl0;times0;#201;reset_n1;#200;ctrl8b1001_0001;#201;times2500;#2000001;times25000;#40000000;$stop;end
endmodule 小梅哥的官方源码
module counter_led_4(Clk,Reset_n,Ctrl,Time,Led
);input Clk;input Reset_n;input [7:0]Ctrl;input [31:0]Time;output reg Led;reg [31:0]counter;reg EN;reg [18:0]counter0;//10msڶʱalways(posedge Clk or negedge Reset_n)if(!Reset_n)counter0 0;else if(counter0 500000 - 1)counter0 0;elsecounter0 counter0 1b1;always(posedge Clk or negedge Reset_n)if(!Reset_n)EN 0;else if(counter0 0)EN 1;else if((counter2 7) (counter Time - 1))EN 0;always(posedge Clk or negedge Reset_n)if(!Reset_n)counter 0;else if(EN)beginif(counter Time - 1)counter 0;elsecounter counter 1b1;endelsecounter 0;reg [3:0]counter2;always(posedge Clk or negedge Reset_n)if(!Reset_n) counter2 0; else if(EN)beginif(counter Time - 1)counter2 counter2 1b1;endelsecounter2 4d8; always(posedge Clk or negedge Reset_n)if(!Reset_n)Led 0;else case(counter2)0:Led Ctrl[0];1:Led Ctrl[1];2:Led Ctrl[2];3:Led Ctrl[3];4:Led Ctrl[4];5:Led Ctrl[5];6:Led Ctrl[6];7:Led Ctrl[7];8:Led 0;default:Led Led;endcaseendmodule测试文件
timescale 1ns / 1nsmodule counter_led_4_tb();reg clk;reg reset_n;reg [7:0]ctrl;reg [31:0]times;wire led;counter_led_4 counter_led_4(.Clk(clk),.Reset_n(reset_n),.Ctrl(ctrl),.Time(times),.Led(led));initial beginclk0;endalways#10 clk!clk;initial beginreset_n0;ctrl0;times0;#201;reset_n1;#2000;ctrl8b1000_0111;times2500;#20000001;times25000;ctrl8b1010_0110;#20000000;$stop;end
endmodule
仿真截图 收获 1锻炼了自己分析波形的能力自己调试最初小梅哥的代码在间隔10ms的时间里会一直保持ctrl[0]的状态让波形看的很乱找不到是什么时候开始算起的于是我对counter2进行计数更改当当前处于不计数的状态时不为0.因为为0 的话需要在后面case循环中进行判断等于ctrl[0]于是我把不计数的状态设为不在判断范围内的8使其为0这样使得波形比较规律易懂 2学会了添加模块中其他参数的方法好开心~这样查错就更容易了
文章转载自: http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.mtymb.cn.gov.cn.mtymb.cn http://www.morning.gjlxn.cn.gov.cn.gjlxn.cn http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.nccqs.cn.gov.cn.nccqs.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.fosfox.com.gov.cn.fosfox.com http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.mbfj.cn.gov.cn.mbfj.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.wqfj.cn.gov.cn.wqfj.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.fqmcc.cn.gov.cn.fqmcc.cn http://www.morning.rhzzf.cn.gov.cn.rhzzf.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.gstmn.cn.gov.cn.gstmn.cn http://www.morning.wljzr.cn.gov.cn.wljzr.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.rqbr.cn.gov.cn.rqbr.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.gfmpk.cn.gov.cn.gfmpk.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn http://www.morning.zxgzp.cn.gov.cn.zxgzp.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.mdgb.cn.gov.cn.mdgb.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn