Forum Discussion

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

Push-button counter

hi again

this has got me stumped, yet I think it should be so easy !

I'm trying to display a horizontal bar in VGA (which I can do) and then have the bar rotate by 1 degree every time I push a button. I've created a sine and cosine array of values from 0 to 90 degrees for this, and I want to step through these arrays with an integer 'k' every time I press the button (active high). Following is the code I'm using to increment k :-

entity bar_gen is

port (

vidon : in std_logic ; -- vidon=1 when hc and vc are in the visible display area, else =0.

pbut : in std_logic:='0'; -- push button to rotate bar by 1 degree for every button push. Connected to Button input in top-level entity by pbut<=Button.

hc : in unsigned (9 downto 0); -- hc , horizontal pixel count from vga_640x480 generator

vc : in unsigned (9 downto 0); -- vc , vertical " " "

rgb : out unsigned(2 downto 0) -- holds 3 colours, rgb(2)=red, rgb(1)=green, rgb(0)=blue

);

end bar_gen;

process(vidon,hc,vc,pbut)

if vidon = '1' then -- cursor is in the visible area of the display

if pbut='1' then -- pushbutton not pushed

k:=k+1;

end if;

...... use the value of k to select an element from the array and draw new line.

end process;

This produced 'inferred latch' warnings for 'k'. Reading up on this on the internet, I thought I understood that this was caused by the code requiring a latch to hold the new value of 'k' , which is not recommended ! The solution seems to be to introduce a previous value for k also, which I call prevk, also an integer. I've tried dozens of permutations of the following, but I still get the same 'inferred latch' warnings. This is what I thought should work, but doesn't. What am I missing ?

process(vidon,hc,vc,pbut)

if vidon = '1' then

if pbut='0' then

k:=prevk;

else

k:=prevk + 1;

end if;

prevk:=k;

...... draw new line, etc.

end process;

Note that I don't have a clock source for this entity.

Thanks in advance for any help.

5 Replies

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

    Just noticed a typing error,in the first batch of code the comment --'pushbutton not pushed' should read 'pushbutton pushed', after 'if pbut='1' then'.

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

    --- Quote Start ---

    Note that I don't have a clock source for this entity.

    --- Quote End ---

    If you don't provide a clock, how is your code supposed to increment k?

    Whenever the if condition is true, k is continuously incremented according to your code, but this is not physically synthesizable.

    You definitely need a clock to step one by one the increment process.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hi

    I thought the pushbutton incremented 'k', via the 'if pbut='1'.........' statement.

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

    In this case you should use pbut as a clock and rather test the rising edge condition, not the level:

    if (pbut'event and pbut = '1') then
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hi

    using your suggestion and a bit of tweaking of k and prevk, I managed to get it to work.

    Thanks