Forum Discussion

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

VHDL timer

hi,

how can I write a program for a timer which Measures a pulse width that represents of range.i'm using un ultrasonic sensor,

The distance can be calculated using the scale factor of 147uS per inch.

thanks in advance

20 Replies

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

    you need a counter, and the size of the count to trigger a "20 seconds elapsed" signal will depend on your clock speed.

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

    Hi

    I want to design traffic light controller using AHDL so I writ the code for traffic controller and for the timers and all is successful individual but when I design the circuit for all and connected to gather I have three error but I do not know how to fix it so please can some one help me and tell me what must I do. Thank you

    this is the error

    [1] Error: Port "100_cnt" does not exist in macrofunction "inst20".

    [2] Error: Quartus II Analysis & Synthesis was unsuccessful. 1 error, 11 warnings.

    [3] Error: Quartus II Full Compilation was unsuccessful. 3 errors, 11 warnings.

    What dose mean about macrofunction.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You have a component called "inst20" in your design, and when you instantiate it in your code you are trying to connect a signal to one of its ports that you called "100_cnt" but this port doesn't exist.

    Do you have any reason why you are using the deprecated AHDL and not VHDL?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Ok I wrote this code for timer 2 second in Ahdl so please how can I convert it to VHDL

    SUBDESIGN 2_sec_cnt

    (

    clk ,enl: INPUT;

    2_second : OUTPUT;

    )

    VARIABLE

    count[25..0] : DFF;

    BEGIN

    count[].clk = clk;

    IF (enl)Then

    IF ((count[].q == 48000000)) THEN

    count[].d = 0;

    2_second = VCC;

    ELSE

    count[].d = count[].q + 1;

    2_second = GND;

    END IF;

    Else

    2_second = GND;

    END IF;

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

    Yes I understood what is my circuit it is about traffic light controller so this timer for the yellow light in traffic and I wrote the Vhdl code for the Traffic controller but still I have the timer and this AHDL 2 second timer for traffic also so how can I convert it to VHDL.

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

    The code logic and structure by itself is the same so, you can almost just take each line and change it to it's VHDL equivalent. Why don't you try it and show us what you wrote, if you have more questions? It is really basic VHDL.

    The only big difference in VHDL is that you will have to put everything in a process, and specify that you want it executed on the rising edge of clk.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Ok this is what I wrote in VHDL and when I run the code (in quartus II) I got success put when I go to the RTL viewer to draw the circuit I gut only the pin for input and output but when I do the same for AHDL code I have very big and different circuit.

    library IEEE;

    use IEEE.STD_LOGIC_1164.all;

    entity two is

    Port (

    clk : in std_logic;

    enl : in std_logic;

    second : out std_logic

    );

    end two;

    architecture Timer of two is

    constant count : natural := 48000000;

    -- I used 24MHz clock

    begin

    process (clk, enl)

    variable count : natural range 0 to count;

    begin

    if (enl ='1') then

    if (count = 48000000) then

    count := 0 ;

    second <= '1';

    else

    count := count+1;

    second <= '0';

    end if ;

    second <= '0';

    end if ;

    end process;

    end Timer;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    First I suggest that you rename your constant "count" to "count_max" for example, to avoid confusion between the constant and the variable.

    Then in VHDL you need to explicitely say that you want something done on each rising edge of the clock:
    process (clk, enl)
    variable count : natural range 0 to count;
    begin
    if rising_edge(clk) then
      if (enl ='1') then
        if (count = 48000000) then
          count := 0 ;
          second <= '1';
        else
          count := count+1;
          second <= '0';
        end if ;
        second <= '0';
      end if ;
    end if;
    end process;
    And finally, you have a problem with the line I've put in red. It will be executed after the "if (count = 48000000)" block, so it will overwrite the value '1' you put when the counter reached its maximal value. Either delete this line, or put it in an else block to put 'second' to 0 when enl is 0 (which I assume is what you wanted to do in the first place)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hie i am a newbie ..

    i managed to create a program that read input from the PIR sensor from the gpio and when sensor sense movement it will turn on and LED on the DE2 board. how can i write the timer that stays on for 5 to 10 minutes.

    here i attcehed my code...

    library IEEE;

    use ieee.std_logic_1164.all;

    use IEEE.numeric_std.all;

    entity ltestinggpiowithdelay is

    port( GPIO_0 : in std_logic_vector(2 downto 0);

    q : out std_logic_vector(2 downto 0));

    end ltestinggpiowithdelay ;

    architecture bufferwithGpio_arc of ltestinggpiowithdelay is

    begin

    process(GPIO_0)

    begin

    if (GPIO_0(0)='1') then q<=GPIO_0(2 downto 0);

    else q<= "000";

    end if;

    end process;

    end bufferwithGpio_arc;