微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > 计数计数模块设计遇到的问题

计数计数模块设计遇到的问题

时间:10-02 整理:3721RD 点击:
我正在用CPLD设计一个计时计数模块:按下reset清零,按一下start开始计时,按一下stop停止计时把结果传给单片机,现在就是控制不了stop,就是仿真时start为高电平1时计数器工作,为0时停止计数,stop控制不了,请问应该怎样才能控制呢?

用状态机控制吧

用状态机控制吧

能具体点吗?状态机我不是很熟

程序是
entity count  is
   port(
            clk,start,stop,reset : in std_logic;
             cout : out  std_logic_vector(7 downto 0)
          );
end count;
architecture behav of count
begin
process(clk,start,stop,reset)
  variable c : std_logic_vector(7 downto 0);
begin
if reset='1' then
c:="00000000";
if clk'event and clk='1'  then
if start'event and start='1'  then
c:=c+1
elsif stop'event and stop='1'  then
cout<=c;
end if;
end if;
cout<=c;
end process;
end behav
编译后出现错误:can't infer register for "c[0]" at count.vhd,because it does not hold its value outside the clock edge



回复 1# woshaogang123

非常谢谢

非常感谢



library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity count is
  port(
    clk:in std_logic;
    reset : in std_logic;
    start: in std_logic;
    stop: in std_logic;
    cout : out std_logic_vector(7 downto 0)
  );
end count;
architecture behav of count is
begin
  process(clk,reset)
    variable c : std_logic_vector(7 downto 0);
  begin
    if reset='1' then
      c:="00000000";
      cout<="00000000";
    elsif clk'event and clk='1' then
      if start='1' then
        c:=c+1;
      elsif stop='1' then
        cout<=c;
      end if;
    end if;
  end process;
end behav;


我做的是一个激光测距仪,用CPLD作时间间隔测量,就是测量激光从发射出去到碰到目标反射回来的时间,start是一PWM信号,作启动计数器的开门信号.stop就是回波信号,作关闭计数器的关门信号,我的思路是检测start信号的第一个上升沿作开门信号,stop的第一个上升沿作关门信号



    我在Quartus II 里面建波形文件怎么仿真不出来呢?


那你需要先取start和stop的上升沿。
因为逻辑简单,就根据start和stop的上升沿产生计数的状态(cnt_status)。复杂时用状态机。
QuartusII不能仿真的原因不是很清楚,应该是软件没设置好,建议再看看帮助文档。我这里没有QuartusII所以没法给你截图。
下面是参考代码。
   

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.std_logic_unsigned.all;

  4. entity count is
  5. port(
  6. clk:in std_logic;
  7. reset : in std_logic;
  8. start: in std_logic;
  9. stop: in std_logic;
  10. cout : out std_logic_vector(7 downto 0)
  11. );
  12. end count;

  13. architecture behav of count is
  14. signal start_dly1 : std_logic;
  15. signal start_dly2 : std_logic;
  16. signal start_rising_edge : std_logic;
  17. signal stop_dly1 : std_logic;
  18. signal stop_dly2 : std_logic;
  19. signal stop_rising_edge : std_logic;
  20. signal cnt_status : std_logic;
  21. begin

  22. process(clk,reset)
  23. begin
  24. if reset='1' then
  25. start_dly1 <= '0';
  26. start_dly2 <= '0';
  27. elsif clk'event and clk='1' then
  28. start_dly1 <= start;
  29. start_dly2 <= start_dly1;
  30. end if;
  31. end process;

  32. start_rising_edge <= not(start_dly2) and start_dly1;

  33. process(clk,reset)
  34. begin
  35. if reset='1' then
  36. stop_dly1 <= '0';
  37. stop_dly2 <= '0';
  38. elsif clk'event and clk='1' then
  39. stop_dly1 <= stop;
  40. stop_dly2 <= stop_dly1;
  41. end if;
  42. end process;

  43. stop_rising_edge <= not(stop_dly2) and stop_dly1;

  44. -- status
  45. process(clk,reset)
  46. begin
  47. if reset='1' then
  48. cnt_status <= '0';
  49. elsif clk'event and clk='1' then
  50. if start_rising_edge = '1' then
  51. cnt_status <= '1';
  52. elsif stop_rising_edge = '1' then
  53. cnt_status <= '0';
  54. end if;
  55. end if;
  56. end process;

  57. process(clk,reset)
  58. variable c : std_logic_vector(7 downto 0);
  59. begin
  60. if reset='1' then
  61. c:="00000000";
  62. cout<="00000000";
  63. elsif clk'event and clk='1' then
  64. if cnt_status = '1' then
  65. c:=c+1;
  66. else
  67. cout<=c;
  68. end if;
  69. end if;
  70. end process;

  71. end behav;

复制代码




Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top