VHDL学习笔记——模10计数器

模10计数器

在这里插入图片描述

VHDL 模10计数器
-- mod10counter
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity mod10counter is --实体名与文件名一致
	generic(
		cnt_mod_value: integer:=10 --定义模值
	);
	port(
		i_lamp_clk:in std_logic;--时钟频率 1Hz时钟
		i_sys_res:in std_logic;--复位
		o_lamp_val:out std_logic_vector(3 downto 0)--输出为4维向量
	);
end entity mod10counter;

architecture behave of mod10counter is--定义实体行为
signal r_lamp_count:std_logic_vector(3 downto 0);--中间变量
begin
	process(i_lamp_clk)
		begin
			if(i_lamp_clk'event and i_lamp_clk='1') then
			if(i_sys_res='1') then--复位时从0000开始
					r_lamp_count<="0000";
				elsif (r_lamp_count="1001")then--到达最后一个状态 从0000开始重复
					r_lamp_count<="0000";
				else
					r_lamp_count<=unsigned(r_lamp_count) + '1';
				end if;
			end if;
	end process;
o_lamp_val <= r_lamp_count;--结果传给out变量
end architecture behave;

在这里插入图片描述

好的,我很乐意为您介绍如何使用VHDL设计一个单时钟10可逆计数器VHDL是一种硬件描述语言,用于设计和拟电子系统。一个10可逆计数器可以计数0到9,并可以递增或递减。以下是一个简单的VHDL代码示例,展示了如何实现这样的计数器: ```vhdl library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity Decade_Counter is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC; up_down : in STD_LOGIC; count : out STD_LOGIC_VECTOR (3 downto 0)); end Decade_Counter; architecture Behavioral of Decade_Counter is signal temp_count : STD_LOGIC_VECTOR (3 downto 0) := "0000"; begin process(clk, reset) begin if reset = '1' then temp_count <= "0000"; elsif rising_edge(clk) then if up_down = '1' then if temp_count = "1001" then temp_count <= "0000"; else temp_count <= temp_count + 1; end if; else if temp_count = "0000" then temp_count <= "1001"; else temp_count <= temp_count - 1; end if; end if; end if; end process; count <= temp_count; end Behavioral; ``` 这段代码的解释: 1. 我们定义了一个名为"Decade_Counter"的实体,它有四个端口: - clk: 时钟输入 - reset: 重置信号 - up_down: 方向控制信号('1'表示递增,'0'表示递减) - count: 4位输出,表示当前计数值 2. 在架构部分,我们定义了一个信号temp_count来存储当前计数值。 3. 在process块中,我们实现了计数器的逻辑: - 当reset为高电平时,计数器被重置为0。 - 在每个时钟上升沿,如果up_down为'1',计数器递增;如果为'0',计数器递减。 - 当计数器达到9(十进制)时,下一个计数值会回到0(循环)。 - 当计数器递减到0时,下一个计数值会变为9,实现循环。 4. 最后,我们将temp_count的值赋给输出端口count。 这个设计实现了基本的10可逆计数功能。根据具体需求,您可能需要添加更多的功能或修改某些部分。例如,您可以添加使能信号(enable)来控制计数器的运行,或者添加进位和借位输出以便于级联多个计数器
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值