之前在网上看了很多数字七段管的相关代码,发现都或多或少有些不足,有些赘余,导致代码报错。
这里我给大家分析一下我写的代码,希望对大家在硬件学习,开发上有帮助。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity ShowNum is
port(
clk:in std_logic;--------------------------------时钟信号
s:out std_logic_vector(7 downto 0);--------------数码管
q:out std_logic_vector(6 downto 0));-------------段位
end ShowNum;
architecture one of ShowNum is
signal clk1:std_logic;---------------分频
signal cnt1:integer range 0 to 7;---------------计数
signal count1:integer range 0 to 49999;
begin
process(clk)
begin
if clk'event and clk = '1' then
if count1 = 49999 then
clk1 <= '1';-------------------------------------1K
count1 <= 0;
else
count1 <= count1+1;
clk1 <= '0';
end if;
end if;
end process;
-------------------------------------------------------
process(clk1)-------主要是让计算两个数码管的数值
begin
if clk1'event and clk1 = '1' then
if cnt1 = 7 then cnt1 <= 0;
else
cnt1 <= cnt1+1;
end if;
end if;
end process;
-------------------------------------------------------------
process(cnt1)------------------只是显示在某种状态下的段位显示,不参与计算
begin
case cnt1 is
when 0 => q <= "1111110";s <="01111111";------------cnt是1时,q显示0,依次选通s
when 1 => q <= "1111110";s <="10111111"
when 2 => q <= "1111110";s <="11011111";
when 3 => q <= "1111110";s <="11101111";
when 4 => q <= "1111110";s <="11110111";
when 5 => q <= "1111110";s <="11111011";
when 6 => q <= 1111110";s <="11111101";
when 7 => q <= "1111110";s <="11111110";
when others => null;
end case;
end process;
----------------------------------------------------------
end one;
实现功能:
在八位七段数码管上显示固定数字,具体数字可修改q后面的数字实现。
管脚分配:
q(0~6) 分配数码管A到G。
s(0~7)分配数码管八个使能端。