Forum: FPGA, VHDL & Verilog TTL encoder signal (easy question)

von frank s. (Company: 4StepsAhead B.V.) (frank_19)

Attached files:

Rate this post
0 useful
not useful
Hi all,

At the moment am programming my first VHDL program within 'ise project 
navigator' from xilinx. (using the coolrunner 2 board from digilent)

I am trying to generate an TTL encoder signal like this:
I have an kflop encoder card which counts this pulses.

The idea for my test:
1. A push button add 100 to an counter.
2. Every X microsecond: while the counter is higher than 0 it gives a 
puls and decrease the counter with one.

I wrote this program: (Also attached: Test.zip)
library IEEE;

entity dts_signals is
    Port (
        clk_in : in  STD_LOGIC;
      btn0 : in  STD_LOGIC;
      A0   : out  STD_LOGIC;
      A0_N : out  STD_LOGIC;
      A1   : out  STD_LOGIC;
      A1_N : out  STD_LOGIC      
end dts_signals;

architecture Behavioral of dts_signals is
    signal c_TTL     : integer range 0 to 81910 := 0; -- 2^13  reset after 10 cycle
    signal position  : integer range 0 to 8191 := 0; -- 2^13
   signal A0_M      : std_logic := '0';
   signal A1_M      : std_logic := '0';
   signal btn0_M    : std_logic := '0';
   signal c_TTL_M  : std_logic := '0';
    dts_signals: process (clk_in) begin
      if rising_edge(clk_in) then      
          if (btn0 = '1' and btn0_M = '0' and position < 5000)  then
            position <= (position + 200);
            btn0_M <= '1';  
          end if;
          if (btn0 = '0' and btn0_M = '1') then     
            btn0_M <= '0';  --Memory rising_edge button
          end if;
          if c_TTL = 60000 then --at very lo speed 60000 counts of 20Mhz cristal
            c_TTL <= 0;
            if position > 2000 then
              if (A0_M='0' and A1_M='0' and c_TTL_M='0') then A1_M<='1';c_TTL_M<='1'; end if;
              if (A0_M='0' and A1_M='1' and c_TTL_M='0') then A0_M<='1';c_TTL_M<='1'; end if;
              if (A0_M='1' and A1_M='0' and c_TTL_M='0') then A0_M<='0';c_TTL_M<='1'; end if;
              if (A0_M='1' and A1_M='1' and c_TTL_M='0') then A1_M<='0';c_TTL_M<='1'; end if;  
              if (A0_M = '1') then A0_N<='0';A0<='1'; else A0_N<='1';A0<='0';end if;
              if (A1_M = '1') then A1_N<='0';A1<='1'; else A1_N<='1';A1<='0';end if;
              position <= (position - 1);
            end if;
            c_TTL <= c_TTL + 1;
          end if;
        end if;
    end process;
end Behavioral;

But when I test te program this is wat happen:
10x push button0:  nothing happen --good because of position > 2000
5x push button0: kflop board counts position: 500 pulses --good
3x push button0: stil position 500 no change ... --false
1x push button0: position 572 -- very strange! I expect it to be a 
multiply of 100.
reset position of the kflop counter.
13x push button0: position 1300 --good
1x push button0: position 1300 --wrong
1x push button0: position 1372 --wrong!

Some how the program send's 0 pulses or 72 pulses or 100 pulses after 
every push of button 0. I would expect 100 pulses every push...

Do you know what i do wrong?


von Olga (Guest)

Rate this post
1 useful
not useful
How is the behaviour in your simulation?

von Lothar M. (lkmiller) (Moderator)

Rate this post
2 useful
not useful
The btn_0 is an asynchonous input and therfore must be synchronized to 
the clock before being used in a FSM. Try this with google translator 
(its German):

von frank s. (Company: 4StepsAhead B.V.) (frank_19)

Rate this post
1 useful
not useful
Hi Olga and Ikmiller,

Thank you for your reply,

I started using the simulation and learned the difference of signals and 
variables. Also fixed some mistakes after the simulation.

The input is an asynchonous input indeed. I don't know how to 
synchronize and debounce it, so I removed the input from the program. 
When I use an clock to trigger the "position <= (position + 200);" it 
works good!

Thank you for your help,



Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.