【国产FPGA高云GW1N-4系列开发板测评】——7、数码管显示秒表
[复制链接]
本帖最后由 gs001588 于 2021-12-26 00:03 编辑
【国产FPGA高云GW1N-4系列开发板测评】——7、数码管显示秒表
在上一贴基础上更改为数码管显示秒表功能,应该是比较简单的。(上一贴:【国产FPGA高云GW1N-4系列开发板测评】——6、数码管显示时钟(时、分、秒) https://bbs.eeworld.com.cn/thread-1190207-1-1.html)
使用SW1作为秒表开始(使能)和暂停,SW2作为秒表清零。
直接上代码:
--*******************************************************************************
--*-------------------------- file ---------------
--* name: segx4.vhd
--* ver : A
--* date:2021-12-9
--*------------------------------------------------
--*******************************************************************************
---------- LIB ----------------------------------------------------------------
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
----------ENTITY -------------------------------------------------------------
ENTITY segx4 IS
PORT (
-------------------------------------------------------------------------------
--系统全局时钟 CLK
-------------------------------------------------------------------------------
CLK_50M: IN STD_LOGIC; --50MHz系统时钟
-------------------------------------------------------------------------------
--系统全局复位 RESET
-------------------------------------------------------------------------------
NRESET: IN STD_LOGIC;
-------------------------------------------------------------------------------
--SEGx4
-------------------------------------------------------------------------------
DIS: OUT STD_LOGIC_VECTOR(7 DOWNTO 0);
SEL: OUT STD_LOGIC_VECTOR(4 DOWNTO 1);
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
--SWx4
-------------------------------------------------------------------------------
SW: IN STD_LOGIC_VECTOR(4 DOWNTO 1)
-------------------------------------------------------------------------------
);
END segx4;
-----------ARCHITECTURE-------------------------------------------------------
ARCHITECTURE MY_CODE OF segx4 IS
-----------------------------------------------------------------------
SIGNAL COUNTER1: integer range 0 to 62500;
SIGNAL CNT: integer range 0 to 8;
SIGNAL SCAN_CLK: STD_LOGIC;
TYPE DIS_Reg_Index IS ARRAY(1 to 4) OF STD_LOGIC_VECTOR(7 downto 0);
SIGNAL DIS_Reg: DIS_Reg_Index;
SIGNAL SEL_Reg: STD_LOGIC_VECTOR(4 downto 1);
-----------------------------------------------------------------------
TYPE SMG_INDEX IS ARRAY(0 to 9) OF STD_LOGIC_VECTOR(7 downto 0);
CONSTANT SMG: SMG_INDEX := ( X"C0", -- 0
X"F9", -- 1
X"A4", -- 2
X"B0", -- 3
X"99", -- 4
X"92", -- 5
X"82", -- 6
X"F8", -- 7
X"80", -- 8
X"90" -- 9
);
CONSTANT BLANK: STD_LOGIC_VECTOR(7 DOWNTO 0):=X"FF";
SIGNAL COUNTER2: integer range 0 to 500000;
SIGNAL SEC: integer range 0 to 99;
SIGNAL CLK_10MS: STD_LOGIC;
SIGNAL CNT_10MS: integer range 0 to 99;
--**********************************************************************************
--*****
--**********************************************************************************
BEGIN
PROCESS(NRESET,CLK_50M)
BEGIN
IF (NRESET = '0') THEN
COUNTER1 <= 0;
ELSIF (RISING_EDGE(CLK_50M)) THEN
IF (COUNTER1 < 62500-1) THEN
COUNTER1 <= COUNTER1 + 1;
ELSE
COUNTER1 <= 0;
END IF;
END IF;
END PROCESS;
PROCESS(NRESET,CLK_50M)
BEGIN
IF (NRESET = '0') THEN
SCAN_CLK <= '0';
ELSIF (RISING_EDGE(CLK_50M)) THEN
IF (COUNTER1 = 62500-1) THEN
SCAN_CLK <= NOT SCAN_CLK;
END IF;
END IF;
END PROCESS;
PROCESS(NRESET,SCAN_CLK)
BEGIN
IF (NRESET = '0') THEN
CNT <= 1;
ELSIF (RISING_EDGE(SCAN_CLK)) THEN
IF (CNT = 4) THEN
CNT <= 1;
ELSE
CNT <= CNT + 1;
END IF;
END IF;
END PROCESS;
SEL_Reg <= (1=>'0',others=>'1') WHEN CNT = 1 ELSE
(2=>'0',others=>'1') WHEN CNT = 2 ELSE
(3=>'0',others=>'1') WHEN CNT = 3 ELSE
(4=>'0',others=>'1') WHEN CNT = 4 ELSE
(others=>'1');
SEL <= SEL_Reg;
DIS <= DIS_Reg(CNT);
DIS_Reg(4) <= SMG(SEC / 10);
DIS_Reg(3) <= '0' & SMG(SEC REM 10)(6 DOWNTO 0);
DIS_Reg(2) <= SMG(CNT_10MS / 10);
DIS_Reg(1) <= SMG(CNT_10MS REM 10);
PROCESS(NRESET,CLK_50M)
BEGIN
IF (NRESET = '0') THEN
COUNTER2 <= 0;
ELSIF (RISING_EDGE(CLK_50M)) THEN
IF (COUNTER2 = 500000-1)THEN
COUNTER2 <= 0;
ELSE
COUNTER2 <= COUNTER2 + 1;
END IF;
END IF;
END PROCESS;
PROCESS(NRESET,CLK_50M)
BEGIN
IF (NRESET = '0') THEN
CLK_10MS <= '0';
ELSIF (RISING_EDGE(CLK_50M)) THEN
IF (COUNTER2 = 250000-1) OR (COUNTER2 = 500000-1)THEN
CLK_10MS <= NOT CLK_10MS;
END IF;
END IF;
END PROCESS;
PROCESS(NRESET,CLK_10MS)
BEGIN
IF (NRESET = '0' OR SW(2) = '1') THEN
SEC <= 00;
CNT_10MS <= 00;
ELSIF (RISING_EDGE(CLK_10MS) AND (SW(1) = '0')) THEN
IF (CNT_10MS = 99) THEN
CNT_10MS <= 0;
IF (SEC = 99) THEN
SEC <= 0;
ELSE
SEC <= SEC + 1;
END IF;
ELSE
CNT_10MS <= CNT_10MS + 1;
END IF;
END IF;
END PROCESS;
END MY_CODE;
123行之前代码功能,请参考上一贴,这里不再赘述。
125到129行,数码管4位分别显示秒表的秒十位、秒个位加点、100ms、10ms。
SEL的位选、DIS的显示段值正好都与CNT相关,每个位显示寄存器DIS_REG()实时赋值,DIS_Reg(CNT)查表显示。
131到142行,分频产生10ms定时节拍;
144行到153行,产生50%点空比,10ms脉冲时钟;
155行到172行为秒表主功能代码。SW(2)为‘1’时秒表清零;为‘0’时允许秒表计时。SW(1)为‘0’时秒表正常走,为‘1’时秒表暂停。最大计时99秒99。
显示效果如下图,未做动态视频。请参考以主代码或下载工程文件,验证功能。
工程文件:
segx4_miaobiao.rar
(58.38 KB, 下载次数: 0)
|