名稱:狀態(tài)機控制的交通燈設(shè)計Verilog代碼Quartus仿真
軟件:Quartus
語言:Verilog
代碼功能:
2、輸入輸出
clk:輸入時鐘信號,頻率為50MH
rst_n:復(fù)位信號.低電平有效,高電平系統(tǒng)開始工作。輸出
r_sn:南北方向紅燈,高電平點亮,低電平熄滅y_sn:南北方向黃燈.高電平點亮,低電平熄滅g_sn:南北方向綠燈,高電平點亮,低電平熄滅r_ew:東西方向紅燈,高電平點亮,低電平熄滅y_ew:東西方向黃燈,高電平點亮,低電平熄滅g_ew:東西方向綠燈,高電平點亮,低電平熄滅
3、設(shè)計要求
使用 verilog HDL語言編寫, Quartus、 Modelsim軟件開發(fā)復(fù)位信號有效,南北方向與東西方向均為紅燈。復(fù)位信號無效,系統(tǒng)正常工作時,南北方向:1-27秒綠燈亮,第28、29、30秒閃爍黃燈.31-60秒變紅燈;東西方向:1~30秒紅燈亮,31~57綠燈亮,第58、59、60秒閃爍黃燈;1-60秒如此循環(huán)。
4、設(shè)計思路
根據(jù)設(shè)計要求,劃分狀態(tài)機,確定狀態(tài)個數(shù)、狀態(tài)名稱;確定控制信號,狀態(tài)轉(zhuǎn)移條件;確定輸岀信號;繪制狀態(tài)轉(zhuǎn)移圖;根據(jù)狀態(tài)轉(zhuǎn)移圖進行代碼編寫、仿真驗證。
使用計數(shù)器實現(xiàn)計時功能,也可作為狀態(tài)轉(zhuǎn)移條件
FPGA代碼Verilog/VHDL代碼資源下載:www.hdlcode.com
演示視頻:
設(shè)計文檔:
1. 工程文件
2. 程序文件
3. 程序編譯
4. RTL圖
狀態(tài)圖
5. Testbench
6. 仿真圖
部分代碼展示:
紅->綠?綠->黃?黃->紅 1、紅--計時main_red_times------------------------綠--計時main_green_times---main_yellow_times黃燈---------------紅 2、綠--計時branch_green_times---branch_yellow_times黃燈--------------------紅--計時branch_reg_times-------------------綠 */ module?traffic_light( input?clk,//50MMhz input?rst_n,//復(fù)位 output?r_sn,//主路燈 output?g_sn,//主路燈-- output?y_sn,//主路燈-- output?r_ew,//支路燈-- output?g_ew,//支路燈-- output?y_ew//支路燈-- ); wire?clk_1Hz; reg?main_red;//主路燈 reg?main_green;// reg?main_yellow;// reg?branch_red;//支路燈 reg?branch_green;// reg?branch_yellow; //分頻到1Hz reg?[31:0]?cnt='d0; reg?clk_1hz=0; assign?clk_1Hz=clk_1hz; always@(posedge?clk) if(cnt==32'd25)?begin//分頻50000000,得1HZ,25000000,仿真改小為25 cnt<=32'd0; clk_1hz<=~clk_1hz; end else?begin ???cnt<=cnt+32'd1; clk_1hz<=clk_1hz; end //定義狀態(tài) parameter?all_red=3'd0; parameter?main_green_state=3'd1; parameter?main_yellow_state=3'd2; parameter?branch_green_state=3'd3; parameter?branch_yellow_state=3'd4; wire?[7:0]?main_green_time; wire?[7:0]?main_yellow_time; wire?[7:0]?branch_green_time; wire?[7:0]?branch_yellow_time; //定義路口個燈持續(xù)時間,修改此處時間 assign?main_green_time=8'd27;//主路黃燈時間設(shè)置為27秒 assign?main_yellow_time=8'd3;//主路黃燈時間設(shè)置為3秒 assign?branch_yellow_time=8'd3;//支路黃燈時間設(shè)置為3秒 assign?branch_green_time=8'd27;//主路黃燈時間設(shè)置為27秒 //主路綠燈+主路黃燈=支路紅燈時間 //支路綠燈+支路黃燈=主路紅燈時間 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; //主路綠燈+主路黃燈=支路紅燈時間 //支路綠燈+支路黃燈=主路紅燈時間 always@(posedge?clk_1Hz?or?negedge?rst_n) if(!rst_n) state<=all_red;//rst_n else 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;//計數(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;//計數(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;//計數(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;//計數(shù)到后到下一狀態(tài) ??????branch_yellow_cnt<='d1; end
點擊鏈接獲取代碼文件:http://www.hdlcode.com/index.php?m=home&c=View&a=index&aid=543