<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          Altera MAX10: 交通燈控制

          作者: 時(shí)間:2023-11-17 來源:電子森林 收藏

          本文引用地址:http://www.ex-cimer.com/article/202311/453010.htm

          上圖為十字路口交通示意圖分之路與主路,要求如下:

          • 主路上綠燈持續(xù)15s的時(shí)間,黃燈3s的時(shí)間,紅燈10s的時(shí)間;
          • 交通燈支路上綠燈持續(xù)7s的時(shí)間, 黃燈持續(xù)3秒的時(shí)間,紅燈18秒的時(shí)間;

          根據(jù)上述要求,設(shè)計(jì)框架分析如下:

          • S1:主路綠燈點(diǎn)亮,支路紅燈點(diǎn)亮,持續(xù)15s的時(shí)間;
          • S2:主路黃燈點(diǎn)亮,支路紅燈點(diǎn)亮,持續(xù)3s的時(shí)間;
          • S3:主路紅燈點(diǎn)亮,支路綠燈點(diǎn)亮,持續(xù)10s的時(shí)間;
          • S4:主路紅燈點(diǎn)亮,支路黃燈點(diǎn)亮,持續(xù)3s的時(shí)間;

          首先是時(shí)鐘分頻部分:

          //********************************************************
          //   Copyright(c)2016, STEP  
          //   All rights reserved
          //   File name       :   divide.v
          //   Module name     :   divide
          //   Author          :   STEP
          //   Email           :   info@stepfpga.com
          //   Data            :   2016/08/01
          //   Version         :   V1.0
          //   Description     :   
          //
          //   Modification history
          //   ----------------------------------------------------------------------------
          // Version       
          // Description
          //
          //******************************************************** 
          //*******************
          //DEFINE MODULE PORT
          //*******************
          module divide(
          	//INPUT
          	clk			,
          	rst_n			,
          	//OUTPUT
          	clkout			
          );
          	//*******************
          	//DEFINE PARAMETER
          	//*******************
          	parameter			WIDTH	=	3;
          	parameter			N		=	5; 	//*******************
          	//DEFINE INPUT
          	//*******************
          	input 	clk,rst_n;          //*******************
          	//DEFINE OUTPUT
          	//*******************
          	output	clkout; 	//********************
          	//OUTPUT ATTRIBUTE
          	//********************
          	//REGS
          	reg 	[WIDTH-1:0]		cnt_p,cnt_n;
          	reg						clk_p,clk_n; 	
          	assign clkout = (N==1)?clk:(N[0])?(clk_p&clk_n):clk_p; 	//Sequential logic style
          	always @ (posedge clk)
          		begin
          			if(!rst_n)
          				cnt_p<=0;
          			else if (cnt_p==(N-1))
          				cnt_p<=0;
          			else cnt_p<=cnt_p+1;
          		end 	always @ (negedge clk)
          		begin
          			if(!rst_n)
          				cnt_n<=0;
          			else if (cnt_n==(N-1))
          				cnt_n<=0;
          			else cnt_n<=cnt_n+1;
          		end 	always @ (posedge clk)
          		begin
          			if(!rst_n)
          				clk_p<=0;
          			else if (cnt_p<(N>>1))  
          				clk_p<=0;
          			else 
          				clk_p<=1;
          		end 	always @ (negedge clk)
          		begin
          			if(!rst_n)
          				clk_n<=0;
          			else if (cnt_n<(N>>1))  
          				clk_n<=0;
          			else 
          				clk_n<=1;
          		end
          		endmodule


          接下來就是利用三段式狀態(tài)機(jī)實(shí)現(xiàn)的交通燈部分:

          //********************************************************
          //   Copyright(c)2016, STEP  
          //   All rights reserved
          //   File name       :   traffic.v
          //   Module name     :   traffic
          //   Author          :   STEP
          //   Email           :   info@stepfpga.com
          //   Data            :   2016/08/01
          //   Version         :   V1.0
          //   Description     :   
          ////   Modification history
          //   ----------------------------------------------------------------------------
          // Version       
          // Description
          //
          //******************************************************** 
          //*******************
          //DEFINE MODULE PORT
          //*******************
          module traffic(
          	//INPUT
          	clk			,
          	rst_n		,
          	//OUTPUT
          	out			
          );
          	//*******************
          	//DEFINE INPUT
          	//*******************
          	input 	clk,rst_n;          //*******************
          	//DEFINE OUTPUT
          	//*******************
          	output	reg[5:0]	out; 	//*******************
          	//DEFINE PARAMETER
          	//*******************    	parameter      	S1 = 4'b00,    
          					S2 = 4'b01,
          					S3 = 4'b10,
          					S4 = 4'b11; 	parameter		time_s1 = 4'd15,
          					time_s2 = 4'd3,
          					time_s3 = 4'd10,
          					time_s4 = 4'd3; 	parameter		led_s1 = 6'b101011,
          					led_s2 = 6'b001011,
          					led_s3 = 6'b011101,
          					led_s4 = 6'b011001; 	//*********************
          	//INNER SIGNAL DECLARATION
          	//*********************
          	//REGS	
          	reg 	[3:0] 	timecont;
          	reg 	[1:0] 	cur_state,next_state;  	//WIRES
          	wire			clk1h; 
          	divide #(.WIDTH(32),.N(12000000)) CLK1H (
          								.clk(clk),
          								.rst_n(rst_n),
          								.clkout(clk1h));
          	//Sequential logic style
          	always @ (posedge clk1h)
          	begin
          		if(!rst_n) cur_state <= S1;
                  else cur_state <= next_state;
          	end 	always @ (cur_state or rst_n or timecont)
          	begin
          		if(!rst_n) begin
          			next_state = S1;
          			end
          		else begin
          			case(cur_state)
          				S1:begin
          					if(timecont==1) next_state = S2;
          					else next_state = S1;
          				end 
                          S2:begin
          					if(timecont==1) next_state = S3;
          					else next_state = S2;
          				end 
                          S3:begin
          					if(timecont==1) next_state = S4;
          					else next_state = S3;
          				end 
                          S4:begin
          					if(timecont==1) next_state = S1;
          					else next_state = S4;
          				end 				default: next_state = S1;
          			endcase
          		end
          	end 	always @ (posedge clk1h)
          	begin
          		if(!rst_n) begin
          			out <= led_s1;
          			timecont <= time_s1;
          		end else begin
          			case(next_state)
          				S1:begin
          					out <= led_s1;
          					if(timecont == 1) timecont <= time_s1;
          					else timecont <= timecont - 1;
          				end 
          				S2:begin
          					out <= led_s2;
          					if(timecont == 1) timecont <= time_s2;
          					else timecont <= timecont - 1;
          				end 
          				S3:begin
          					out <= led_s3;
          					if(timecont == 1) timecont <= time_s3;
          					else timecont <= timecont - 1;
          				end 
          				S4:begin
          					out <= led_s4;
          					if(timecont == 1) timecont <= time_s4;
          					else timecont <= timecont - 1;
          				end 				default:begin
          					out <= led_s1;
          					end
          			endcase
          		end
          	end
          	endmodule

          上正好有4路按鍵和4路開關(guān),可以用來作為輸入信號(hào)分別控制數(shù)碼管的輸出。按照下面表格定義輸入輸出信號(hào)

          信號(hào)引腳信號(hào)引腳
          clkJ5rstJ9
          out[0]E14out[1]E15
          out[2]G15out[3]D12
          out[4]C14out[5]C15

          配置好以后編譯下載程序。您也可以試試修改程序,觀察修改代碼對(duì)于內(nèi)部電路所造成的影響。

          狀態(tài)機(jī)是一類很重要的時(shí)序邏輯電路,是許多數(shù)字系統(tǒng)的核心部件,掌握狀態(tài)機(jī)的使用是利用FPGA與CPLD進(jìn)行開發(fā)的一項(xiàng)必會(huì)技能,本小節(jié)的交通燈程序即是利用三段式狀態(tài)機(jī)描述方法實(shí)現(xiàn)的,希望讀者能夠快速掌握這項(xiàng)技能。



          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();