名称:数字显示自动记分器VHDL代码Quartus仿真(文末获取)
软件:Quartus
语言:VHDL
代码功能:
数字显示自动记分器
1.设计一个射击用的数字显示自动计分器,电路共设18个模拟开关,K1~K18,
分别代表射击者射中的目标,射中K1~K9分别得1~9分,射中K10~K18分别得10~
90分;
2.要求每射中一次(K1~K18开关拔动一次)单发计数显示器能自动显示得分数,
显示2秒后自动消失
3.每射中一次均应将射击的得分数累计并显示;
4.电路应具有复位功能,即每局结束后,应将各计数器自动清0,为下一次记分作好准备(手动复位)
5.单发计数显示和累加计数显示应分开;
6.脉神信号源(自行设计)为1KHz频率
1. 工程文件
2. 顶层原理图
3. 程序文件
4. 程序编译
5. RTL图
状态图
6. 仿真图
部分代码展示:
LIBRARY ieee; USE ieee.std_logic_1164.all; USE ieee.std_logic_unsigned.all; --控制模块 ENTITY score_ctrl IS PORT ( clk : IN STD_LOGIC;--时钟 rst_p : IN STD_LOGIC;--复位 k1_p : IN STD_LOGIC;--按键1~18 k2_p : IN STD_LOGIC; k3_p : IN STD_LOGIC; k4_p : IN STD_LOGIC; k5_p : IN STD_LOGIC; k6_p : IN STD_LOGIC; k7_p : IN STD_LOGIC; k8_p : IN STD_LOGIC; k9_p : IN STD_LOGIC; k10_p : IN STD_LOGIC; k11_p : IN STD_LOGIC; k12_p : IN STD_LOGIC; k13_p : IN STD_LOGIC; k14_p : IN STD_LOGIC; k15_p : IN STD_LOGIC; k16_p : IN STD_LOGIC; k17_p : IN STD_LOGIC; k18_p : IN STD_LOGIC; display_en : OUT STD_LOGIC;--显示使能(显示持续2秒) cur_score_o : OUT STD_LOGIC_VECTOR(7 DOWNTO 0);--当前分数 total_score_o : OUT STD_LOGIC_VECTOR(7 DOWNTO 0) --总分数 ); END score_ctrl; ARCHITECTURE trans OF score_ctrl IS SIGNAL key_valid : STD_LOGIC; type state_type is ( s_idle , s_key , s_wait , s_end ); SIGNAL state:state_type; SIGNAL time_cnt : INTEGER := 0; SIGNAL cur_score : STD_LOGIC_VECTOR(7 DOWNTO 0) := "00000000"; SIGNAL total_score : STD_LOGIC_VECTOR(7 DOWNTO 0) := "00000000"; BEGIN key_valid <= k1_p OR k2_p OR k3_p OR k4_p OR k5_p OR k6_p OR k7_p OR k8_p OR k9_p OR k10_p OR k11_p OR k12_p OR k13_p OR k14_p OR k15_p OR k16_p OR k17_p OR k18_p;--按键是否按键 --状态机 PROCESS (clk, rst_p) BEGIN IF (rst_p = '1') THEN state <= s_idle;--复位 ELSIF (clk'EVENT AND clk = '1') THEN CASE state IS WHEN s_idle =>--初始状态 IF (key_valid = '1') THEN--按键按下 state <= s_key; ELSE state <= s_idle; END IF; WHEN s_key =>--按键按下状态 state <= s_wait; WHEN s_wait =>--计时2秒状态 IF (time_cnt >= 200) THEN--1KHz计时2000就是2秒,仿真时把2000改小为200便于观察 state <= s_end; END IF; WHEN s_end =>--结束 state <= s_idle; WHEN OTHERS => END CASE; END IF; END PROCESS; --计时 PROCESS (clk, rst_p) BEGIN IF (rst_p = '1') THEN time_cnt <= 0;--复位 ELSIF (clk'EVENT AND clk = '1') THEN IF (state = s_wait) THEN--计时2秒状态下进行计数 time_cnt <= time_cnt + 1;--计数 ELSE time_cnt <= 0; END IF; END IF; END PROCESS; --计分 PROCESS (clk, rst_p) BEGIN IF (rst_p = '1') THEN--复位 cur_score <= "00000000";---当前分 total_score <= "00000000";--总分 ELSIF (clk'EVENT AND clk = '1') THEN IF (state = s_key) THEN IF (k1_p = '1') THEN cur_score <= "00000001";--1分 total_score <= total_score + "00000001"; ELSIF (k2_p = '1') THEN cur_score <= "00000010";--2分 total_score <= total_score + "00000010"; ELSIF (k3_p = '1') THEN cur_score <= "00000011";--3分 total_score <= total_score + "00000011"; ELSIF (k4_p = '1') THEN cur_score <= "00000100";--4分 total_score <= total_score + "00000100"; ELSIF (k5_p = '1') THEN cur_score <= "00000101";--5分 total_score <= total_score + "00000101"; ELSIF (k6_p = '1') THEN cur_score <= "00000110";--6分 total_score <= total_score + "00000110"; ELSIF (k7_p = '1') THEN cur_score <= "00000111";--7分 total_score <= total_score + "00000111"; ELSIF (k8_p = '1') THEN cur_score <= "00001000";--8分 total_score <= total_score + "00001000"; ELSIF (k9_p = '1') THEN cur_score <= "00001001";--9分 total_score <= total_score + "00001001"; ELSIF (k10_p = '1') THEN cur_score <= "00001010";--10分 total_score <= total_score + "00001010"; ELSIF (k11_p = '1') THEN cur_score <= "00010100";--20分 total_score <= total_score + "00010100"; ELSIF (k12_p = '1') THEN cur_score <= "00011110";--30分 total_score <= total_score + "00011110"; ELSIF (k13_p = '1') THEN cur_score <= "00101000";--40分 total_score <= total_score + "00101000"; ELSIF (k14_p = '1') THEN cur_score <= "00110010";--50分 total_score <= total_score + "00110010"; ELSIF (k15_p = '1') THEN cur_score <= "00111100";--60分 total_score <= total_score + "00111100"; ELSIF (k16_p = '1') THEN cur_score <= "01000110";--70分 total_score <= total_score + "01000110"; ELSIF (k17_p = '1') THEN cur_score <= "01010000";--80分 total_score <= total_score + "01010000"; ELSIF (k18_p = '1') THEN cur_score <= "01011010";--90分 total_score <= total_score + "01011010"; END IF; END IF; END IF; END PROCESS; --显示使能,持续2秒 PROCESS (clk, rst_p) BEGIN IF (rst_p = '1') THEN--复位 display_en <= '0'; ELSIF (clk'EVENT AND clk = '1') THEN IF (state = s_wait) THEN display_en <= '1'; ELSE display_en <= '0'; END IF; END IF; END PROCESS; cur_score_o <= cur_score;--输出单发计分 total_score_o <= total_score;--输出总分 END trans;
源代码
扫描文章末尾的公众号二维码