只有晚上才有时间研究和敲代码。熬夜敲代码的孩纸悲催呀。
以下是使用4位拨码开关控制数码管并进行0~F显示源代码,经过testbench 验证和下载调试。
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/08/2014 // Design Name: led_display_dip // Module Name: led_display_dip // Project Name: led_display_dip // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1c // Description: 使用4位拨码开关控制数码管并进行0~F显示 // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_display_dip(clk,rst_n,sw,led_seg,led_bit); input clk;//50MHz 总线时钟 input rst_n;//异步复位,低电平复位 input [3:0]sw;//4位拨码开关 output [7:0]led_seg;//8段LED数码管段选,共阳极数码管 output [7:0]led_bit;//8位LED 位选,低电平选中数码管 reg [7:0]led_seg; ////////////////////////////////////////////////////////////////////////////////// parameter seg0 = 8'hc0, // "0" seg1 = 8'hf9, // "1" seg2 = 8'ha4, // "2" seg3 = 8'hb0, // "3" seg4 = 8'h99, // "4" seg5 = 8'h92, // "5" seg6 = 8'h82, // "6" seg7 = 8'hf8, // "7" seg8 = 8'h80, // "8" seg9 = 8'h90, // "9" sega = 8'h88, // "a" segb = 8'h83, // "b" segc = 8'hc6, // "c" segd = 8'ha1, // "d" sege = 8'h86, // "e" segf = 8'h8e; // "f" ///////////////////////////////////////////////////////////////////////////////// reg [7:0]led_seg_r; always@(posedge clk or negedge rst_n) begin if(!rst_n) led_seg <= 8'hff; else led_seg <= led_seg_r; end always@(sw) begin //数码管显示 case(sw) 4'b0000: led_seg_r <= seg0; 4'b0001: led_seg_r <= seg1; 4'b0010: led_seg_r <= seg2; 4'b0011: led_seg_r <= seg3; 4'b0100: led_seg_r <= seg4; 4'b0101: led_seg_r <= seg5; 4'b0110: led_seg_r <= seg6; 4'b0111: led_seg_r <= seg7; 4'b1000: led_seg_r <= seg8; 4'b1001: led_seg_r <= seg9; 4'b1010: led_seg_r <= sega; 4'b1011: led_seg_r <= segb; 4'b1100: led_seg_r <= segc; 4'b1101: led_seg_r <= segd; 4'b1110: led_seg_r <= sege; 4'b1111: led_seg_r <= segf; default: led_seg_r <= 8'hff;//默认不点亮LED数码管 endcase end assign led_bit = 8'b1111_1110;//只选中一个LED数码管 endmodule
testbench
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/08/2014 // Design Name: led_display_dip_tb // Module Name: led_display_dip_tb // Project Name: led_display_dip_tb // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.0c // Description: ??4?????????????0~F?? testbench // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_display_dip_tb(); reg clk; reg rst_n; reg [3:0]sw; wire [7:0]led_seg; wire [7:0]led_bit; reg [3:0]i; led_display_dip display_dip(clk,rst_n,sw,led_seg,led_bit); ////////////////////////////////////////////////////////////////////////////////// initial begin sw = 4'b0000; clk = 1'b0; #10 rst_n = 1'b0; #20 rst_n = 1'b1; #260 sw = 4'b1011; forever #10 clk = ~clk; end endmodule
模为60的计数器,计数结果动态显示在数码管上作业源代码,代码经过功能仿真和下载到开发板调试
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/10/2014 // Design Name: led_counter60 // Module Name: led_counter60 // Project Name: led_counter60 // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1c // Description: 模为60的计数器,计数结果动态显示在数码管上 // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_counter60(clk,rst_n,led_bit,led_seg); input clk;//50Mhz总线时钟 input rst_n;//异步复位,低电平复位 output [7:0]led_seg;//LED 数码管段选 output [7:0]led_bit;//LED 数码管位选 reg [7:0]led_seg; reg [7:0]led_bit; ////////////////////////////////////////////////////////////////////////////////// reg [15:0]delay_sel;//位选计数寄存器 always@(posedge clk or negedge rst_n) //动态扫描分频模块,50MHZ → 1Khz begin if(!rst_n) delay_sel <= 16'h0000; else if(delay_sel == 16'd49999) delay_sel <= 16'h0000; else delay_sel <= delay_sel + 1'b1; end reg [27:0]delay_cnt;//计数器分频寄存器 always@(posedge clk or negedge rst_n)//计数器分频模块,50MHz → 1Hz begin if(!rst_n) delay_cnt <= 28'h0000000; else if(delay_cnt == 28'd49999999) delay_cnt <= 28'h0000000; else delay_cnt <= delay_cnt + 1'b1; end ////////////////////////////////////////////////////////////////////////////////// reg bit_select;//位选寄存器 always@(posedge clk or negedge rst_n)//LED 数码管位选模块 begin if(!rst_n) bit_select <= 1'b0; else if(delay_sel == 16'd49999) bit_select <= bit_select + 1'b1; else bit_select <= bit_select; end always@(bit_select) begin case(bit_select) 1'b0: led_bit <= 8'b1111_1110; 1'b1: led_bit <= 8'b1111_1101; default: led_bit <= 8'b1111_1111; endcase end ////////////////////////////////////////////////////////////////////////////////// reg [2:0]decade_cnt;//模60计数器的十位计数寄存器 reg [3:0]unit_cnt;//模60计数器的个位计数寄存器 always@(posedge clk or negedge rst_n)//模60计数器计数模块 begin if(!rst_n) begin decade_cnt <= 3'b000; unit_cnt <= 4'b0000; end else if(decade_cnt == 3'b101 && unit_cnt == 4'b1001 && delay_cnt == 28'd49999999) begin decade_cnt <= 3'b000; unit_cnt <= 4'b0000; end else if(unit_cnt == 4'b1001 && delay_cnt == 28'd49999999) begin decade_cnt <= decade_cnt + 1'b1; unit_cnt <= 4'b0000; end else if(delay_cnt == 28'd49999999) begin decade_cnt <= decade_cnt; unit_cnt <= unit_cnt + 1'b1; end else begin decade_cnt <= decade_cnt; unit_cnt <= unit_cnt; end end ///////////////////////////////////////////////////////////////////////////////// parameter //数码管定义 seg0 = 8'hc0, // "0" seg1 = 8'hf9, // "1" seg2 = 8'ha4, // "2" seg3 = 8'hb0, // "3" seg4 = 8'h99, // "4" seg5 = 8'h92, // "5" seg6 = 8'h82, // "6" seg7 = 8'hf8, // "7" seg8 = 8'h80, // "8" seg9 = 8'h90, // "9" sega = 8'h88, // "a" segb = 8'h83, // "b" segc = 8'hc6, // "c" segd = 8'ha1, // "d" sege = 8'h86, // "e" segf = 8'h8e; // "f" ////////////////////////////////////////////////////////////////////////////////// reg [7:0]led_seg_r;//数码管段选寄存器 reg [7:0]led_seg_unit;//数码管个位段选寄存器 reg [7:0]led_seg_decade;//数码管十位段选寄存器 always@(posedge clk or negedge rst_n) begin if(!rst_n) led_seg <= 8'hff; else led_seg <= led_seg_r; end always@(led_bit) begin case(led_bit) 8'b1111_1110: led_seg_r <= led_seg_unit; 8'b1111_1101: led_seg_r <= led_seg_decade; default : led_seg_r <= 8'hff; endcase end always@(unit_cnt)//数码管个位显示模块 begin case(unit_cnt) 4'b0000: led_seg_unit <= seg0; 4'b0001: led_seg_unit <= seg1; 4'b0010: led_seg_unit <= seg2; 4'b0011: led_seg_unit <= seg3; 4'b0100: led_seg_unit <= seg4; 4'b0101: led_seg_unit <= seg5; 4'b0110: led_seg_unit <= seg6; 4'b0111: led_seg_unit <= seg7; 4'b1000: led_seg_unit <= seg8; 4'b1001: led_seg_unit <= seg9; default: led_seg_unit <= 8'hff; endcase end always@(decade_cnt)//数码管十位显示模块 begin case(decade_cnt) 3'b000: led_seg_decade <= seg0; 3'b001: led_seg_decade <= seg1; 3'b010: led_seg_decade <= seg2; 3'b011: led_seg_decade <= seg3; 3'b100: led_seg_decade <= seg4; 3'b101: led_seg_decade <= seg5; default: led_seg_decade <= 8'hff; endcase end endmodule
testbench代码:
`timescale 1ns / 1ps ////////////////////////////////////////////////////////////////////////////////// // Company: // Engineer: Charles Wang // // Create Date: 06/10/2014 // Design Name: led_counter60_tb // Module Name: led_counter60_tb // Project Name: led_counter60_tb // Target Devices: EP4CE6E22C8 // Tool versions: Quartus II 13.1 & Modelsim 10.1c // Description: ??60????,????????????? testbench // // Dependencies: // // Revision: // Revision 0.01 - File Created // Additional Comments: // ////////////////////////////////////////////////////////////////////////////////// module led_counter60_tb(); reg clk; reg rst_n; wire [7:0]led_bit; wire [7:0]led_seg; led_counter60 counter60(.clk(clk),.rst_n(rst_n),.led_bit(led_bit),.led_seg(led_seg)); ///////////////////////////////////////////////////////////////////////////////// initial begin #10 clk = 1'b0; #20 rst_n= 1'b0; #10 rst_n= 1'b1; forever #10 clk = ~clk; end endmodule