Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Up & Down Counter Using Push Buttons

Hi,

im trying to implement a counter that counts from 0 to 100 and vise versa. the counter must be controlled using 2 push buttons, one for incrementing and the other for decrementing for example, if 07 is displayed, pressing the increment button (push_button_1) should result in 08 being displayed or pressing the decrement button (push_button_2) should result in 06 being displayed. I tried implementing the counter using the code below (attached as well) but Quartus cannot compile it because apparently im using two distinctive clocks. Please guys, run the code n pls help me sort this problem out?

Regards

Dante

(i only used values 00 to 07 for testing purposes) isnt there a shorter way of implementing this? otherwise i have to write statements for each and every number till 100:confused:

LIBRARY IEEE;

USE IEEE.STD_LOGIC_1164.ALL;

USE IEEE.STD_LOGIC_ARITH.ALL;

USE IEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITY BUTTON IS

PORT( PUSH_BUTTON_1 : IN STD_LOGIC;

PUSH_BUTTON_2 : IN STD_LOGIC;

MSD_driver : OUT STD_LOGIC_VECTOR(6 DOWNTO 0);

LSD_driver : OUT STD_LOGIC_VECTOR(6 DOWNTO 0));

END BUTTON;

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

ARCHITECTURE a OF BUTTON IS

SIGNAL PUSH_COUNTER1 :STD_LOGIC_VECTOR(2 DOWNTO 0):="000";

BEGIN

PROCESS(PUSH_BUTTON_1, PUSH_BUTTON_2)

BEGIN

IF (PUSH_BUTTON_1'EVENT AND PUSH_BUTTON_1 ='0') THEN

IF PUSH_COUNTER1 = "111" THEN

PUSH_COUNTER1 <= "111";

ELSE

PUSH_COUNTER1 <= PUSH_COUNTER1 + 1;

END IF;

ELSIF (PUSH_BUTTON_2'EVENT AND PUSH_BUTTON_2 ='0') THEN

IF PUSH_COUNTER1 = "000" THEN

PUSH_COUNTER1 <= "000";

ELSE

PUSH_COUNTER1 <= PUSH_COUNTER1 - 1;

END IF;

END IF;

END PROCESS;

PROCESS(PUSH_COUNTER1)

BEGIN

CASE PUSH_COUNTER1 IS

WHEN "000" => -- O=00

LSD_driver <= "1000000";

MSD_driver <= "1000000";

WHEN "001" => -- A=01

LSD_driver <= "1111001";

MSD_driver <= "1000000";

WHEN "010" => -- B=02

LSD_driver <= "0100100";

MSD_driver <= "1000000";

WHEN "011" => -- C=03

LSD_driver <= "0110000";

MSD_driver <= "1000000";

--

WHEN "100" => -- D=04

LSD_driver <= "0011001";

MSD_driver <= "1000000";

--

WHEN "101" => -- E=05

LSD_driver <= "0010010";

MSD_driver <= "1000000";

--

WHEN "110" => -- F=06

LSD_driver <= "0000010";

MSD_driver <= "1000000";

--

WHEN "111" => -- G=07

LSD_driver <= "1111000";

MSD_driver <= "1000000";

WHEN OTHERS => --23

LSD_driver <= null; --2

MSD_driver <= null; --3

END CASE;

END PROCESS;

END a;

12 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You can give me the source count from 00 to 99 and vice versa. (With button count up and count down.)

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The up1 and dw1 signals are used to store the previous values of the up and down input signals. Using the current and previous values, you can detect a status change, in order to count up and down only once when the button is pressed. If you just act on the current value of 'up' and 'down', then you will count on each clock cycle during which the button is pressed.