Search code examples
vhdlfpgaxilinxspartan

Counter with push button switch design using VHDL and Xilinx


I'm very new to VHDL and XILINX ISE. I use the version 13.2 for Xilinx ISE.

I want to design a very simple counter with the following inputs:

  • Direction
  • Count

The count input will be assigned to a button and I want the counter to count up or down according to direction input when the button is pressed. I have written a sample VHDL before this one. It had a clock input and It was counting according to the clock input. Now I want it to count when I press the button instead of counting synchronously.

Here's my VHDL code (please tell me if my code have a logical or any other flaw):

entity counter is
    Port ( COUNT_EN : in  STD_LOGIC;
           DIRECTION : in  STD_LOGIC;
           COUNT_OUT : out  STD_LOGIC_VECTOR (3 downto 0));
end counter;

architecture Behavioral of counter is

signal count_int : std_logic_vector(3 downto 0) := "0000";
begin
process 
begin
    if COUNT_EN='1' then
        if DIRECTION='1' then   
            count_int <= count_int + 1;
        else
            count_int <= count_int - 1;
        end if;
    end if;
end process;
COUNT_OUT <= count_int;
end Behavioral;

I use Spartan xc3s500e and I placed the inputs accordingly. Below is my .ucf file:

#Created by Constraints Editor (xc3s500e-fg320-5) - 2013/03/18
NET "COUNT_EN" LOC = K17;
NET "COUNT_OUT[0]" LOC = F12;
NET "COUNT_OUT[1]" LOC = E12;
NET "COUNT_OUT[2]" LOC = E11;
NET "COUNT_OUT[3]" LOC = F11;
NET "DIRECTION" LOC = L13;
#Created by Constraints Editor (xc3s500e-fg320-5) - 2013/03/18
NET "COUNT_EN" CLOCK_DEDICATED_ROUTE = FALSE;

I needed to change the last line because I was getting the error:

This will not allow the use of the fast path between the IO and the Clock...

After having this error gone, I programmed the device. But the output (leds) acted crazy. They sometimes stood still for a few seconds, sometimes just flashed very fast. I could not figure out where my mistake is. I would appreciate any help, some beginner tutorials are greatly appreciated (the links i found directed me to xilinx's documentations and they seemed quite complicated for a beginner).


Solution

  • From your description I understand that you are not looking for an Asynchronous Counter.

    What you need is counter that counts on trigger from PushButton Switch. The below RTL should work:

    enter image description here

    If any difficulty in HDL coding let me know.