`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 05/30/2014 // Design Name: led // Module Name: led // Project Name: led // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1d // Description: 使用过程赋值语句间隔点亮LED // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led(rst_n,clk,led1,led2,led3,led4); input clk;//50MHz 总线时钟 input rst_n;//异步复位信号,低电平有效 output led1;//输出LED1,低电平点亮 output led2;//输出LED2,低电平点亮 output led3;//输出LED3,低电平点亮 output led4;//输出LED4,低电平点亮 reg led1; reg led2; reg led3; reg led4; always@(posedge clk or negedge rst_n) begin if(!rst_n) begin // 复位,4个LED 均不点亮 led1 <= 1'b1; led2 <= 1'b1; led3 <= 1'b1; led4 <= 1'b1; end else begin led1 <= 1'b1; led2 <= 1'b0;//点亮LED2 led3 <= 1'b1; led4 <= 1'b0;//点亮LED4 end end endmodule
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/04/2014 // Design Name: led_flicker // Module Name: led_flicker // Project Name: led_flicker // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1d // Description: led_flicker // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_flicker(clk,rst_n,led1,led2,led3,led4); input clk;//50 MHz 鎬荤嚎鏃堕挓 input rst_n;//寮傛澶嶄綅 output led1;//LED1,浣庣數骞崇偣浜 output led2;//LED2,浣庣數骞崇偣浜 output led3;//LED3,浣庣數骞崇偣浜 output led4;//LED4,浣庣數骞崇偣浜 reg led1; reg led2; reg led3; reg led4; ///////////////////////////////////////////////////////////////////////////////// reg [27:0]cnt; always@(posedge clk or negedge rst_n) begin //鍒嗛,50HMz 鈫1Hz if(!rst_n) cnt <= 28'h0000000; else if(cnt == 28'd49999999) cnt <= 28'h0000000; else cnt <= cnt + 1'b1; end always@(posedge clk or negedge rst_n) begin if(!rst_n) begin led1 <= 1'b1; led2 <= 1'b1; led3 <= 1'b1; led4 <= 1'b1; end else if(cnt == 28'd49999999)//浣縇ED 姣忛殧1s 浜伃涓€娆 begin led1 <= ~led1; led2 <= ~led2; led3 <= ~led3; led4 <= ~led4; end else begin led1 <= led1; led2 <= led2; led3 <= led3; led4 <= led4; end end endmodule
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/04/2014 // Design Name: led_water // Module Name: led_water // Project Name: led_water // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1d // Description: 4个LED流水灯,向左流水点亮 // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_water(clk,rst_n,led1,led2,led3,led4); input clk;//50Mhz 总线时钟 input rst_n;//异步复位,低电平有效 output led1;//输出LED1 低电平点亮 output led2;//输出LED2 低电平点亮 output led3;//输出LED3 低电平点亮 output led4;//输出LED4 低电平点亮 reg led1; reg led2; reg led3; reg led4; ///////////////////////////////////////////////////////////////////////////////// reg [27:0] cnt; always@(posedge clk or negedge rst_n) begin //分频 50MHz → 1Hz if(!rst_n) cnt <= 28'h0000000; else if(cnt == 28'd49999999) cnt <= 28'h0000000; else cnt <= cnt + 1'b1; end always@(posedge clk or negedge rst_n) begin if(!rst_n) //复位 begin led1 <= 1'b1; led2 <= 1'b1; led3 <= 1'b1; led4 <= 1'b1; end else if(cnt == 28'd49999999) begin if({led1,led2,led3,led4} != 4'b0000) {led1,led2,led3,led4} <= {led1,led2,led3,led4} << 1'b1; //LED 灯向左流水灯 else begin led1 <= 1'b1; led2 <= 1'b1; led3 <= 1'b1; led4 <= 1'b0; end end else begin led1 <= led1; led2 <= led2; led3 <= led3; led4 <= led4; end end endmodule
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/04/2014 // Design Name: led_run // Module Name: led_run // Project Name: led_run // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1d // Description: 4个LED向右的跑马灯 // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_run(clk,rst_n,led1,led2,led3,led4); input clk; input rst_n; output led1; output led2; output led3; output led4; reg led1; reg led2; reg led3; reg led4; ////////////////////////////////////////////////////////////////////////////////// reg [27:0]cnt; always@(posedge clk or negedge rst_n) begin //分频50Mhz → 1Hz if(!rst_n) cnt <= 28'h0000000; else if(cnt == 28'd49999999) cnt <= 28'h0000000; else cnt <= cnt + 1'b1; end always@(posedge clk or negedge rst_n) begin if(!rst_n) begin led1 <= 1'b0; led2 <= 1'b1; led3 <= 1'b1; led4 <= 1'b1; end else if(cnt == 28'd49999999)//LED 向右跑马灯,时间间隔1s begin {led4,led3,led2,led1} <= {led3,led2,led1,led4}; end else begin led1 <= led1; led2 <= led2; led3 <= led3; led4 <= led4; end end endmodule
下面是testbench(对于上面四个project使用的同一个testbench,仿真结果OK,只是不知道符不符合规范):
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/04/2014 // Design Name: // Module Name: // Project Name: // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1d // Description: // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_water_tb(); reg clk; reg rst_n; wire led1; wire led2; wire led3; wire led4; ///////////////////////////////////////////////////////////////////////////////// led_water water(clk,rst_n,led1,led2,led3,led4); initial begin #10 clk = 1'b0; #20 rst_n = 1'b0; #10 rst_n = 1'b1; forever #10 clk = ~ clk; end endmodule
请老师指点谬误,谢谢!