2

I want a counter that the Most significant bit toggles every 2 seconds, and gets values 0 and 1.So for example it will have 0 for 2 seconds and after 1 for another 2 seconds etc.. I need it like that because I will connect the most significant bit to a decoder which will show results on FPGA 3starter (50MHZ/20ns) Does this will work?

 library ieee ;

use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

----------------------------------------------------

entity counter is

generic(27: natural :=2);
port(   clock:  in std_logic;
    rst:    in std_logic;
    count:  in std_logic;
    Q:  out std_logic_vector(27-1 downto 0)
);
end counter;

----------------------------------------------------

architecture behv of counter is           

    signal Pre_Q: std_logic_vector(27-1 downto 0);

begin

    -- behavior describe the counter

    process(clock, count, clear)
    begin
    if clear = '1' then
        Pre_Q <= Pre_Q - Pre_Q;
    elsif (clock='1' and clock'event) then
        if count = '1' then
        Pre_Q <= Pre_Q + 1;
        end if;
    end if;
    end process;    

    -- concurrent assignment statement
    Q <= Pre_Q(27-1);

end behv;
user
  • 35
  • 4
  • depends on your clock frequency. NOt the best way to code a counter –  Aug 31 '14 at 15:35
  • @JonRB my inside clock on FPGA is 50Mhz/20ns – user Aug 31 '14 at 15:41
  • @JonRB no it's not duplicate.I am not the same user. – user Aug 31 '14 at 17:29
  • It isn't necessary that you are the same user, I think it is sufficient that it is the same question. The idea of the stackexchange wiki format is to have a good set of questions and answers, and avoid duplicate questions. The question @JohnRB links to seems to be the same as this question. – gbulmer Aug 31 '14 at 17:32
  • @gbulmer Here It's not 2Hz but 0.5 Hz – user Aug 31 '14 at 17:43
  • Agreed. In one it may say max_count: natural := 100_000_000 and in the other max_count: natural := 400_000_000. However that alone doesn't seem enough difference to justify a new question. It might have been helpful to ask for a review of your VHDL, e.g. "Review VHDL for 2Hz counter", instead of ask a very similar question, and risking a 'close'. Just my $0.02 – gbulmer Aug 31 '14 at 17:53
  • @user to code to make a 2Hz strobe and a 0.5Hz strobe or any arbitary stroke is not that much different once the method is known –  Aug 31 '14 at 18:21
  • @JonRB Okay but actually I didn't wanted to divide clock..I wanted a counter that MSB will toggle every 2 seconds. It's not the same. – user Aug 31 '14 at 18:31
  • @user clock divider, counter... result is the same. you end up with a strobe at a lower rate. A counter is one way todo it: See: http://stackoverflow.com/questions/19708301/making-a-clock-divider http://stackoverflow.com/questions/15053008/vhdl-clock-divider-counter-duty-cycle http://electronics.stackexchange.com/questions/97301/clock-divider-vhdl –  Aug 31 '14 at 18:40

1 Answers1

1

You're very close, so I'm going to give you the answer here. Let me know if you have any further questions.

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

----------------------------------------------------

entity counter is

generic (
  width: natural := 27;
  max_count: natural := 100_000_000     -- 50 MHz / 0.5 Hz
);
port (
  clock:  in std_logic;
  rst:    in std_logic;
  count:  in std_logic;
  Q:      out std_logic
);
end counter;

----------------------------------------------------

architecture behv of counter is           

  signal prescaler: std_logic_vector(width-1 downto 0);
  signal pre_Q : std_logic;

begin

    -- behavior describe the counter

  process (clock, count, clear)
  begin
    if clear = '1' then
      prescaler <= (others => '0');
      pre_Q <= '0';
    elsif (clock='1' and clock'event) then
      if count = '1' then
        if prescaler = max_count then
          -- This happens every 2 seconds; toggle the output flip-flop.
          prescaler <= (others => '0');
          pre_Q <= not pre_Q;
        else
          prescaler <= prescaler + 1;
        end if;
      end if;
    end if;
  end process;    

    -- concurrent assignment statement
    Q <= pre_Q;

end behv;
Dave Tweed
  • 172,781
  • 17
  • 234
  • 402
  • The count input was in your original code, so I kept it. It functions as an "enable" signal for the counting, so you would ordinarily just tie it high. Yes, Q is the output that toggles every 2 seconds. – Dave Tweed Aug 31 '14 at 17:44
  • 1
  • Yes but I have edited it. – user Aug 31 '14 at 18:00
  • BUT the principal is the same. asking for a 2Hz strobe isn't much different from asking for a 0.5Hz strobe –  Aug 31 '14 at 18:22
  • @DaveTweed are you sure that this will work ?? it will show 1 or 0 because I want to get the MSB for seven segment displays. – user Sep 01 '14 at 15:14
  • I'm sorry -- since you said "toggle", I assumed that MSB meant "most significant bit". If you meant something different, you need to be clearer in your original problem statement about what the output actually needs to be. Are you saying that you want a 4-bit value that increments every 2 seconds? – Dave Tweed Sep 01 '14 at 15:30
  • Don't use acronyms, please spell it out. Edit your question to show exactly what output you expect from this module. – Dave Tweed Sep 01 '14 at 21:19
  • OK, then yes, the module in my answer does what you want. The Q output toggles every 2 seconds. – Dave Tweed Sep 02 '14 at 10:42
  • @DaveTweed may I ask you something I have an inside enable signal that during the process of the calculation of my results is U when the results are done it gets value 1.But because I am going to connect this enable signal to my seven segment component (fpga,decoder) I will have any problem with that U??(if yes) Is there any way to make the signal stays at 0 during the process? – user Sep 02 '14 at 13:01
  • You need to assign a value to that enable signal when reset is asserted. – Dave Tweed Sep 02 '14 at 13:55