天天看點

FPGA-數位管顯示

今天來看看入門級必備代碼之–數位管顯示。

簡單的說,了解了原理,哪種語言都可以描述,第一次接觸數位管是在學習單片機的時候,後來學習verolog HDL也就順其自然的會了。

FPGA-數位管顯示

點亮數位管原理:

輸入相應的電平點亮一根根小火柴a-b-c-d-e-f-g-dp。如果數位管是共陰極,給高電平1即可相應點亮,反之如果是共陽極,給低電平0即可相應點亮。是以才有:

/******數字0-9的顯示******/
case (digit)
		0:a_to_g<=7'b0000001;
		1:a_to_g<=7'b1001111;
		2:a_to_g<=7'b0010010;
		3:a_to_g<=7'b0000110;
		4:a_to_g<=7'b1001100;
		5:a_to_g<=7'b0100100;
		6:a_to_g<=7'b0100000;
		7:a_to_g<=7'b0001111;
		8:a_to_g<=7'b0000000;
		9:a_to_g<=7'b0000100;
		default:a_to_g<=7'b0000001;
endcase
           

除此之外,我們要知道動态掃描比靜态掃描更節省資源,靜态掃描占用的端口多,是以我們一般都采用動态掃描方式點亮數位管。

那麼掃描的時間,我這邊給的是190HZ,不固定,你開心給多少都行,一般時間設定小于40ms即可。

這裡提供的代碼是四個數位管動态顯示的。

/**************共陽極數位管**************/
module digital_number(
	input	   [3:0] minute_o ,
	input	   [3:0] minute_t ,
	input	   [3:0] hour_o   ,
	input	   [3:0] hour_t   ,
	input 	  		 mclk     ,
	input 	    	 rst_n    ,
	output reg [6:0] a_to_g   ,                 //段選
	output reg [3:0] an       ,					//位選
	output 			 dp							//小數點
		);
	reg  [1:0]  s     ;
	wire [3:0]  aen   ;
	reg         clk190;
	reg  [17:0] cnt	  ;
	reg  [3:0]  digit ;
	assign dp = 1;		
	assign aen = 4'b1111;
	
	//分頻190hz
	parameter T	=18'd263157;					// 50_000_000除以190
	[email protected](posedge mclk or negedge rst_n)
	begin
		if(!rst_n) 
			cnt<=0;
		else if(cnt == T-1) 
			cnt<=0;
		else 
			cnt<=cnt+1;
	end	
	
	always @(posedge mclk or negedge rst_n)
	begin
		if(!rst_n)	  
			clk190<=0;
		else if(cnt == T-1)
			clk190<=1;
		else
			clk190<=0;
	end
	
	//計數
	[email protected](posedge clk190 or negedge rst_n)
	begin
		if(!rst_n)	s<=0;
		else		s<=s+1;
	end
	
	//數字選擇
	always @(*)
	begin
		an <= 4'b1111;               			//全部滅
		if(aen[s] == 1)	an[s]<=0;				//動态掃描亮,在一個clk190時鐘為周期
	end
	
	//4選一
	always @(*)
	case(s)
		0:digit	<= hour_t[3:0];
		1:digit	<= hour_o[3:0];
		2:digit	<= minute_t[3:0];
		3:digit	<= minute_o[3:0];
		default:digit <= 4'b0000;
	endcase
	
	//7段數位顯示管
	always @(*)
	case (digit)
		0:a_to_g<=7'b0000001;
		1:a_to_g<=7'b1001111;
		2:a_to_g<=7'b0010010;
		3:a_to_g<=7'b0000110;
		4:a_to_g<=7'b1001100;
		5:a_to_g<=7'b0100100;
		6:a_to_g<=7'b0100000;
		7:a_to_g<=7'b0001111;
		8:a_to_g<=7'b0000000;
		9:a_to_g<=7'b0000100;
		default:a_to_g<=7'b0000001;
	endcase
	
endmodule
           

這裡就不給仿真了,不過資源利用還是貼出來,大家可以互相學習。

FPGA-數位管顯示

PS: 一如年輕時模樣。