Forum Discussion

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

Code for a pushbutton to produce an enable signal for a time delay generator

hi all

i’m trying to realize a very small Project, for further and more complex projects . I wrote a synthesizable code to accomplish this – when i hit a pushbutton fpga should light a led and start a timer at the same time. After a predetermined time delay led is goint to off and timer will reset and wait for a new pushbutton command. (i have a terasic D0 board with cyclone iii).

For this i have 2 design files. One for time delay generator , with a generic variable named “delay” which is used for to produce time delay with a 50 Mhz clock . And the other design file is my top level entity which captures the hit to the pushbutton and send it as the enable signal for counter of time delay generator.

i get lots of error messages until i reach to this final code but i couldn’t get over the last error whic is error (10822): hdl error at buton.vhd(23): couldn't implement registers for assignments on this clock edge.

somewhere in the forum said that it is impossible to use synchronous and asynchronous signal at the same time at the same device. I feel this is my fault but how can i fix it?


library ieee;
use ieee.std_logic_1164.all;
entity buton is 
port (clk,buton : in std_logic; 
led_delay_out std_logic);
end buton;
architecture behav of buton is
signal deljenen : std_logic;
signal deljenout: std_logic;
begin
inst_5s: entity work.delay_jen generic map (delay => 250000000) port map (clk,deljenen,deljenout);
led_delay<=deljenout;
process (buton)
begin
if falling_edge (buton) then 
deljenen <='1'; else
deljenen <='0';
end if;
end process;
end behav;


library ieee;
use ieee.std_logic_1164.all;
entity delay_jen is 
generic (delay : integer);
port( clk : in std_logic;
en : in std_logic;
flag: out std_logic);
end delay_jen;
architecture behv of delay_jen is
begin
process(clk)
variable count:integer:=0;
begin
if (en) then 
flag<='1'; count:=count+1;
if (count=delay) then count:=0; flag<='0'; end if;
end if;
end process;
end behv;

3 Replies

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

    There are a few changes I would recommend.

    First - I highly suggest you do not use the button as a clock. It will be very bouncy and generate a lot of edges, as well as having timing issues. You need to synchronise the button press into the clock domain and then detect the edge (using an edge detector, not a rising edge VHDL statement).

    Secondly, your process in the delay_jen entity is not a clocked process. Just including the clock in the sensitivity list does not make it a clocked process. You need to follow the template. I also suggest you forget about variables for now - they will only get you in to trouble. Use signals instead.

    
    signal count    : integer;
    process(clk)
        
    begin
        if rising_edge(clk) then
            if (en) then 
                flag<='1'; 
                count <= count+1;
                
                if (count=delay) then 
                    count:=0; 
                    flag<='0'; 
                end if;
            end if;
         end if         
    end process;
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    HDL is supposed to describe the behaviour of a system which will be eventually synthesized into actual logic gates.

    Consider what you are requesting to the synthesis tool with the following lines you wrote:

    if falling_edge (buton) then

    deljenen <='1'; else

    deljenen <='0';

    So, deljenen is high only when buton presents a falling edge. In all other cases is low !

    Then, how long does deljenen pulse last?!? How can you implement this with flip-flops and/or logic gates?

    The correct way of operation is using clk to sample buton signal into a register and use it to detect the falling edge.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    what was i thinking while i was writing such a code :)) dont know. You have right , i will adhere to both of your advices. Thanks.