FPGA(7)--有限状态机--交通灯
文章目錄
- 一、實(shí)驗(yàn)?zāi)康?/li>
- 二、實(shí)驗(yàn)內(nèi)容
- 三、實(shí)驗(yàn)設(shè)計(jì)與結(jié)果
- 四、實(shí)驗(yàn)思考與小結(jié)
一、實(shí)驗(yàn)?zāi)康?/h2>
掌握有限狀態(tài)機(jī)的編程和使用方法。
二、實(shí)驗(yàn)內(nèi)容
設(shè)計(jì)一個(gè)十字路口交通燈控制器,東西(b)、南北(a)方向有紅燈、黃燈、綠燈,持續(xù)時(shí)間分別為45、5、40秒,仿真驗(yàn)證其功能。
三、實(shí)驗(yàn)設(shè)計(jì)與結(jié)果
1.整體設(shè)計(jì)思路:根據(jù)題目要求可知,需要用VHDL描述設(shè)計(jì)不同的進(jìn)程分別進(jìn)行“復(fù)位清零”、“狀態(tài)定義及轉(zhuǎn)化”、“時(shí)間定義及控制”、“計(jì)時(shí)”的功能。而又因?yàn)榧t綠燈需要持續(xù)40秒,而黃燈只需要持續(xù)5秒,因此需要將“時(shí)間的定義及控制”、“計(jì)時(shí)”的功能各自都分開成兩個(gè)部分。細(xì)節(jié):需要注意復(fù)位清零的異步性、雙向紅綠燈存在的4種不同狀態(tài)、計(jì)時(shí)功能與狀態(tài)選擇的銜接等。VHDL描述代碼如下。
①引入與定義:
②異步復(fù)位清零:
process(clk,rst)beginif rst = '1' then presentstate <= S0;elsif clk'event and clk='1' thenpresentstate<=nextstate;end if;end process;③40秒計(jì)時(shí):
process(clk,rst40,en40) begin if rst = '1' then tmp40<="000000";elsif rst40='1' then tmp40<="000000";elsif clk'event and clk='1' then if en40='1' thenif tmp40="100111" then tmp40<="000000";else tmp40<=tmp40+1;end if;end if;end if;if tmp40="100111" then time40<='1';else time40<='0';end if;end process;④5秒計(jì)時(shí):
process(clk,rst5,en5) begin if rst = '1' then tmp5<="000";elsif rst5='1' then tmp5<="000";elsif clk 'event and clk='1' then if en5='1' thenif tmp5="100" then tmp5<="000";else tmp5<=tmp5+1;end if;end if;end if;if tmp5="100" then time5<='1';else time5<='0';end if;end process;⑤時(shí)間與狀態(tài)銜接:
process(presentstate,time40,time5)begincase presentstate iswhen S0 => rst40 <='0';en40<='1';rst5<='1';en5<='0';if time40= '1' then nextstate<= S1;else nextstate<= S0; end if;when S1 => rst5 <='0';en5 <='1';rst40<='1';en40<='0';if time5= '1' then nextstate<= S2;else nextstate<= S1; end if; when S2 => rst40 <='0';en40<='1';rst5<='1';en5<='0';if time40= '1' then nextstate<= S3;else nextstate<= S2; end if; when S3 => rst5 <='0';en5<='1';rst40<='1';en40<='0';if time5= '1' then nextstate<= S0;else nextstate<= S3; end if; end case;end process;⑥狀態(tài)對(duì)應(yīng)轉(zhuǎn)化:
process(presentstate)begincase presentstate is when S0 => Ra<='0'; Ya<= '0'; Ga<='1'; Rb<='1'; Yb<= '0'; Gb<='0'; when S1 => Ra<='0'; Ya<= '1'; Ga<='0'; Rb<='1'; Yb<= '0'; Gb<='0'; when S2 => Ra<='1'; Ya<= '0'; Ga<='0'; Rb<='0'; Yb<= '0'; Gb<='1'; when S3 => Ra<='1'; Ya<= '0'; Ga<='0'; Rb<='0'; Yb<= '1'; Gb<='0';end case;end process;2.仿真實(shí)驗(yàn):輸入:clk、rst,輸出:Ra、Rb、Ga、Gb、Ya、Yb以及狀態(tài)中間輸出:presentstate、tmp40、tmp5。
3.狀態(tài)機(jī)轉(zhuǎn)換圖。
4.根據(jù)引腳配置完成接線,下載至FPGA芯片中,在實(shí)驗(yàn)箱上完成驗(yàn)證。如下圖為相關(guān)的實(shí)驗(yàn)驗(yàn)證。
四、實(shí)驗(yàn)思考與小結(jié)
1.VHDL描述的模塊化,將實(shí)現(xiàn)不同功能的VHDL描述分開在不同的進(jìn)程里面,實(shí)現(xiàn)VHDL的“高內(nèi)聚低耦合”。
2. 需要明確不同的狀態(tài)機(jī)它們之間的轉(zhuǎn)換條件。
3. 狀態(tài)機(jī)由狀態(tài)寄存器和組合邏輯電路構(gòu)成,能夠根據(jù)控制信號(hào)按照預(yù)先設(shè)定的狀態(tài)進(jìn)行狀態(tài)轉(zhuǎn)移,是協(xié)調(diào)相關(guān)信號(hào)動(dòng)作、完成特定操作的控制中心。交通信號(hào)燈可以用狀態(tài)機(jī)的原理來實(shí)現(xiàn),因?yàn)樾盘?hào)燈的變化狀態(tài)是有限的,各個(gè)狀態(tài)可以列舉出來,狀態(tài)間切換由計(jì)數(shù)器決定。
總結(jié)
以上是生活随笔為你收集整理的FPGA(7)--有限状态机--交通灯的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 公路商店如何注销
- 下一篇: 山东移动APP怎么帮别人买流量(山东省人