名稱:基于Basys2開(kāi)發(fā)板的交通燈控制器verilog紅綠燈倒計(jì)時(shí)(代碼在文末下載)
軟件:ISE
語(yǔ)言:Verilog
代碼功能:
1、實(shí)現(xiàn)一個(gè)十字路口交通燈,每條路有紅綠黃三色信號(hào)燈。
2、使用數(shù)碼管顯示倒計(jì)時(shí)。
3、可通過(guò)代碼修改通行時(shí)間。
本代碼的通行時(shí)間通過(guò)以下代碼修改:
FPGA代碼Verilog/VHDL代碼資源下載:www.hdlcode.com
本代碼已在Basys2開(kāi)發(fā)板驗(yàn)證,開(kāi)發(fā)板如下,其他開(kāi)發(fā)板可以修改管腳適配:
演示視頻:
設(shè)計(jì)文檔:
1. 工程文件
2. 程序文件
管腳約束
3. 程序編譯
4. Testbench
5. 仿真圖
整體仿真
代碼中定義了主路紅燈35秒,綠燈30秒,黃燈5秒
支路紅燈35秒,綠燈30秒,黃燈5秒
仿真圖對(duì)應(yīng)如下,下圖中用不同顏色標(biāo)記了不同燈
分頻模塊仿真
紅綠燈控制模塊
倒計(jì)時(shí)控制模塊
顯示模塊
部分代碼展示:
/* 紅->綠?綠->黃?黃->紅 1、紅--計(jì)時(shí)main_red_times------------------------綠--計(jì)時(shí)main_green_times---main_yellow_times黃燈---------------紅 2、綠--計(jì)時(shí)branch_green_times---branch_yellow_times黃燈--------------------紅--計(jì)時(shí)branch_reg_times-------------------綠 */ //紅綠燈控制模塊 module?led( input?clk_1Hz, input?[7:0]?main_green_time, input?[7:0]?main_yellow_time, input?[7:0]?branch_green_time, input?[7:0]?branch_yellow_time, output?reg?main_red,//主路燈 output?reg?main_green,// output?reg?main_yellow,// output?reg?branch_red,//支路燈 output?reg?branch_green,// output?reg?branch_yellow, output?reg?[7:0]?main_green_BCD, output?reg?[7:0]?main_yellow_BCD, output?reg?[7:0]?main_red_BCD, output?reg?[7:0]?branch_green_BCD, output?reg?[7:0]?branch_yellow_BCD, output?reg?[7:0]?branch_red_BCD ); parameter?main_green_state=3'd1; parameter?main_yellow_state=3'd2; parameter?branch_green_state=3'd3; parameter?branch_yellow_state=3'd4; //定義路口個(gè)燈持續(xù)時(shí)間,修改此處時(shí)間 //主路綠燈+主路黃燈=支路紅燈時(shí)間 //支路綠燈+支路黃燈=主路紅燈時(shí)間 reg?[2:0]?state=3'd0; reg?[7:0]?main_green_cnt=8'd1; reg?[7:0]?main_yellow_cnt=8'd1; reg?[7:0]?branch_green_cnt=8'd1; reg?[7:0]?branch_yellow_cnt=8'd1; //主路綠燈+主路黃燈=支路紅燈時(shí)間 //支路綠燈+支路黃燈=主路紅燈時(shí)間 always@(posedge?clk_1Hz) case(state) main_green_state: if(main_green_cnt<main_green_time)?begin//主路綠燈 state<=main_green_state; main_green_cnt<=main_green_cnt+'d1; end else?begin state<=main_yellow_state;//計(jì)數(shù)到后到下一狀態(tài) main_green_cnt<='d1; end main_yellow_state:??? ???if(main_yellow_cnt<main_yellow_time)?begin//主路黃燈 state<=main_yellow_state; ??????main_yellow_cnt<=main_yellow_cnt+'d1; end else?begin state<=branch_green_state;//計(jì)數(shù)到后到下一狀態(tài) ??????main_yellow_cnt<='d1; end ???branch_green_state: if(branch_green_cnt<branch_green_time)?begin//支路綠燈 state<=branch_green_state; branch_green_cnt<=branch_green_cnt+'d1; end else?begin state<=branch_yellow_state;//計(jì)數(shù)到后到下一狀態(tài) branch_green_cnt<='d1; end???? ???branch_yellow_state: ???if(branch_yellow_cnt<branch_yellow_time)?begin//支路3s黃燈 state<=branch_yellow_state; ??????branch_yellow_cnt<=branch_yellow_cnt+'d1; end else?begin state<=main_green_state;//計(jì)數(shù)到后到下一狀態(tài) ??????branch_yellow_cnt<='d1; end default:state<=main_green_state; endcase //交通燈狀態(tài)控制,state為相應(yīng)狀態(tài)時(shí)亮相應(yīng)燈 always@(posedge?clk_1Hz?) begin if(state==main_green_state) main_green<=1; else main_green<=0; if(state==main_yellow_state) main_yellow<=1; else main_yellow<=0; if(state==branch_green_state?|?state==branch_yellow_state?) main_red<=1; else main_red<=0; end //交通燈狀態(tài)控制,state為相應(yīng)狀態(tài)時(shí)亮相應(yīng)燈 always@(posedge?clk_1Hz?) begin if(state==branch_green_state) branch_green<=1; else branch_green<=0; if(state==branch_yellow_state) branch_yellow<=1; else branch_yellow<=0; if(state==main_green_state?|?state==main_yellow_state?) branch_red<=1; else branch_red<=0; end //計(jì)數(shù) //主路綠燈+主路黃燈=支路紅燈時(shí)間 //支路綠燈+支路黃燈=主路紅燈時(shí)間 //采用BCD碼計(jì)數(shù) always@(posedge?clk_1Hz?) begin if(state==branch_green_state) main_red_BCD<=branch_green_cnt; else?if(state==branch_yellow_state) main_red_BCD<=branch_yellow_cnt+branch_green_time;//支路綠燈+支路黃燈=主路紅燈時(shí)間 else ???main_red_BCD<='d0; if(state==main_green_state) branch_red_BCD<=main_green_cnt; else?if(state==main_yellow_state) branch_red_BCD<=main_yellow_cnt+main_green_time;//主路綠燈+主路黃燈=支路紅燈時(shí)間 else ???branch_red_BCD<='d0; ?main_green_BCD<=main_green_cnt; ?main_yellow_BCD<=main_yellow_cnt; ?branch_green_BCD<=branch_green_cnt; ?branch_yellow_BCD<=branch_yellow_cnt; end endmodule
點(diǎn)擊鏈接獲取代碼文件:http://www.hdlcode.com/index.php?m=home&c=View&a=index&aid=280