検索
連載

よろしいならばダイナミック点灯だ――FPGAでLEDをダイナミックにLチカさせるMAX 10 FPGAで学ぶFPGA開発入門(5)(2/6 ページ)

単純なLチカならばFPGAでもそう難しくない。ただ、ダイナミック点灯やそれに伴うソースの最適化については“ならでは”のポイントが散見される。今回も連載で利用している「MAX 10 FPGA 評価キット」の読者プレゼントをご用意。

Share
Tweet
LINE
Hatena
module top(
 
    //Clock from oscillator
    input Clock,
 
    //Arduino I/Os
    inout Arduino_IO5,
    inout Arduino_IO6,
    inout Arduino_IO7,
    inout Arduino_IO8,
    inout Arduino_IO9,
    inout Arduino_IO10,
    inout Arduino_IO11,
    inout Arduino_IO12,
    inout Arduino_IO13
    
 
);
 
    reg[18:0] div_cntr1;
    reg[9:0] div_cntr2;
    reg[3:0] dec_cntr;
    reg[3:0] pos_cntr;
    reg[6:0] seg_cntr;
    reg[7:0] disp_cntr;
    reg Hi;
 
    initial begin
	div_cntr1 = 0;
	div_cntr2 = 0;
	dec_cntr = 0;
	seg_cntr = 0;
	pos_cntr = 0;
	Hi = 1;
	end
	
    
    always@(posedge Clock) begin
	div_cntr1 <= div_cntr1 + 1;
	if (div_cntr1 == 50000) begin
	    div_cntr1 <= 0;
	    div_cntr2 <= div_cntr2 + 1;
	    if (div_cntr2 == 1000) begin
		div_cntr2 <= 0;
		case (dec_cntr)
		    0 : seg_cntr <= 7'b0000100; // 1111011
		    1 : seg_cntr <= 7'b1100111; // 0011000
		    2 : seg_cntr <= 7'b1001000; // 0110111
		    3 : seg_cntr <= 7'b1000001; // 0111110
		    4 : seg_cntr <= 7'b0100011; // 1011100
		    5 : seg_cntr <= 7'b0010001; // 1101110
		    6 : seg_cntr <= 7'b0010000; // 1101111
		    7 : seg_cntr <= 7'b1000111; // 0111000
		    8 : seg_cntr <= 7'b0000000; // 1111111
		    9 : seg_cntr <= 7'b0000001; // 1111110
		    default : seg_cntr <= 7'b1111111; // 0000000
		endcase
		dec_cntr <= dec_cntr + 1;
		if (dec_cntr == 9) begin
		    dec_cntr <= 0;
		end
	    end
	    else begin
		pos_cntr <= pos_cntr + 1;
		case (pos_cntr)
		    0 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= seg_cntr[6];
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= 1;
			 end
		    1 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= seg_cntr[5] ;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= 1;
			 end
		    2 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= seg_cntr[4] ;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= 1;
			 end
		    3 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= seg_cntr[3] ;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= 1;
			 end
		    4 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= seg_cntr[2] ;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= 1;
			 end
		    5 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= seg_cntr[1] ;
			    disp_cntr[0] <= 1;
			 end
		    6 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= seg_cntr[0] ;
			 end
		    6 : begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= seg_cntr[0] ;
			 end
		    7 : begin
			    disp_cntr[7] <= dec_cntr[0] ;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= seg_cntr[0] ;
			 end
		    default: begin
			    disp_cntr[7] <= 1;
			    disp_cntr[6] <= 1;
			    disp_cntr[5] <= 1;
			    disp_cntr[4] <= 1;
			    disp_cntr[3] <= 1;
			    disp_cntr[2] <= 1;
			    disp_cntr[1] <= 1;
			    disp_cntr[0] <= 1;
			 end
		endcase
	    end
	end
    end
 
    assign Arduino_IO5 = Hi ;
    assign Arduino_IO6 = disp_cntr[6] ;
    assign Arduino_IO7 = disp_cntr[5] ;
    assign Arduino_IO8 = disp_cntr[4] ;
    assign Arduino_IO9 = disp_cntr[7] ;
    assign Arduino_IO10 = disp_cntr[3] ;
    assign Arduino_IO11 = disp_cntr[2] ;
    assign Arduino_IO12 = disp_cntr[1] ;
    assign Arduino_IO13 = disp_cntr[0] ;
 
endmodule
List1

Copyright © ITmedia, Inc. All Rights Reserved.

ページトップに戻る