二进位制可逆计数器

二进位制可逆计数器,第1张

module counter(din,dout,up,down,load,reset,CO,BO);
input [3:0] din;
input up,down;
input reset;
input load;
output [3:0] dout;
output CO,BO;
reg [3:0] dout;
always@(posedge reset or posedge up)
begin
if(reset)
dout<=4'b0000;
else if (!load)
dout<=din;
else if(down)
dout<=dout+1;

这个问题实际很简单,楼主要保存的又不是每次计数的值,而是初始值。就算是要保存每次计数的值,可用电池呀,在每次掉电前将数据写入存储器就行了,单片机用内置eeprom的就行了。电池用锂电或超级电容就行。
检测输入用光耦隔离。

只能告诉你原理,先用一做计数器用你学号作为默认值,开关控制计数器增加;这个很简单,主要是显示,显示要用动态显示,数码管的位选要在四个数码管上快速的循环转移,这样能看出来四个数码管是都亮的效果,段选就比较简单了,通过一个译码动作

是用BCD码表示十进制吗?可以每四位分开看。
比如BCD码q(11 downto 0)可以表示0到999,前四位是个位,中四位是十位,后四位是百位。不知道对于溢出的有什么要求,我设成溢出后不做任何运算。
library ieee;
use ieeestd_logic_1164all;
use ieeestd_logic_unsignedall;
entity add_sub is
port(
clk : in std_logic;
clr : in std_logic;
sl : in std_logic;
q : out std_logic_vector(11 downto 0));

end add_sub;
architecture add_sub_arc of add_sub is

signal cnt : std_logic_vector(11 downto 0);

begin

process(clk,clr,cnt)
begin

if clr = '0' then

cnt <= (others => '0');

elsif clk = '1' and clk'event then

if sl = '0' then -- adder

if cnt /= "100110011001" then

if cnt(3 downto 0) = "1001" then

cnt(3 downto 0) <= (others => '0'); -- units
cnt(7 downto 4) <= cnt(7 downto 4) + '1'; -- tens

else

cnt(3 downto 0) <= cnt(3 downto 0) + '1'; -- units

end if;

if cnt(7 downto 4) = "1001" then -- tens

cnt(7 downto 4) <= (others => '0'); -- tens
cnt(11 downto 8) <= cnt(11 downto 8) + '1'; -- hundreds

end if;

else

cnt <= cnt;

end if;

else -- substractor

if cnt /= "000000000000" then

if cnt(3 downto 0) = "0000" then

cnt(3 downto 0) <= "1001";
cnt(7 downto 4) <= cnt (7 downto 4) - '1';

else

cnt(3 downto 0) <= cnt (3 downto 0) - '1';

end if;

if cnt(7 downto 4) = "0000" then

cnt(7 downto 4) <= "1001";
cnt(11 downto 8) <= cnt (11 downto 8) - '1';

end if;

end if;

end if;

q <= cnt;

end if;

end process;

end add_sub_arc;


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13167726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-06-15
下一篇 2023-06-15

发表评论

登录后才能评论

评论列表(0条)

保存