newbie needs help

Hello,

I start playing with a Spartan 3 demo board. I want to program a 8 bits counter with one button that increments the counter, one button that decrements the counter and one reset button that erases the counter. Output of the counter is connected to the 8 leds.

Here is the very naive code:

library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating

---- any Xilinx primitives in this code.

--library UNISIM;

--use UNISIM.VComponents.all;

entity test is port ( inc: in std_logic; dec: in std_logic; reset: in std_logic; led_value: out std_logic_vector(7 downto 0) ); end test;

architecture Behavioral of test is begin process (inc, dec, reset) variable counter_value: std_logic_vector(7 downto 0); variable flag_inc: std_logic; variable flag_dec: std_logic; begin if (reset='1') then counter_value := "00000000"; flag_inc := '0'; flag_dec := '0'; end if;

if (inc='1' and flag_inc='0') then flag_inc := '1'; counter_value := counter_value + 1; end if;

if (dec='1' and flag_dec='0') then flag_dec := '1'; counter_value := counter_value - 1; end if; if (inc='0') then flag_inc := '0'; end if; if (dec='0') then flag_dec := '0'; end if;

led_value

Reply to
Olivier Scalbert
Loading thread data ...

Reply to
Peter Alfke

Are you coming from a software background? Electronics hobbyist?

First, buttons need to be understood. The mechanical reality of switches is that they bounce. The contacts don't physically go from 100% open to 100% closed and back reliably. To accommodate the bounce, analog or digital "debounce" circuits are used to give you one event for one action.

Second, modern digital electronics work well with a single system clock that takes inputs from the outside world through synchronizing blocks such that each input event is seen in the system clock domain as a single event, much like the debounce; this is most important when many changes occur at the same time such as when 8 commuters (one byte of information) are trying to enter a train at the same time the doors (system clock) are closing (hitting the posedge). Not every commuter can get on the train if they all show up right when the door closes.

A system clock will allow the design to see a switch transition and lock out a change in that switch until a specified time goes by. The counter - which wants a single clock - will then have the ability to go up or down depending on the debounced switch settings once per switch press.

It almost sounds like when I wanted to do a simple Microsoft Windows program (having done much command-line stuff before) and had to deal with all the infrastructure to produce the windows and their somewhat involved behavior. But at least the nuances in electronics are fewer. Switch debounce for digital circuits is usually one of the first lessons.

- John_H

Reply to
John_H

A typical switch will bounce for a few tens to a few thousands of hits, depending on the switch and other circuitry (a small cap works wonders but will not eliminate the bounce). As to those numbers, I have measured them - one particularly bad switch gave me over 2000 hits on a switch event.

Perhaps the OP needs to learn some electronics basics (suggestion : post to s.e.b. asking about switch bounce and possible solutions)

Cheers

PeteS

Reply to
PeteS

Hello! Thanks for your help.

With the following code, with the 50MHz clock, I have only few bouncing contacts. So Xilinx switches are good !

Of course, bouncing contacts disappear if I reduce the clock frequency.

However something must be wrong with my previous code and bouncing contacts do not explain everything ...

Any code improvement is welcomed !

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

-- Company:

-- Engineer:

--

-- Create Date: 12:24:58 01/03/07

-- Design Name:

-- Module Name: test - Behavioral

-- Project Name:

-- Target Device:

-- Tool versions:

-- Description:

--

-- Dependencies:

--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
Reply to
Olivier Scalbert

John_H gave you the strongest hint: In current digital technologies, and particularly in FPGAs, it is a VERY bad idea to try to describe asynchronous state machines. Essentially you had something like this...

process(button) begin if button = '1' then if button_seen = '0' then button_seen

Reply to
Jonathan Bromley

Yes, easy to guess !

Electronics hobbyist? Yes after leaving electronics for 20 years ...

I have bouncing contacts problem on my trs80 late in 1978 ... ;)

Ok

Yes, doing Windows, java, Qt or Gtk programs need an "important" infrastructure. They also use the event driven approach which can be a big paradigm shift from command-line programming. By the way, the event-driven approach is perhaps nearer the way of thinking in electronics. I am currently trying to create a kind of makefile that produce and download binary file to the fpga board from a set of vhdl files and for a newcomer it is not so easy ... There are so much parameters ... If you go to Xilinx to Altera or XXX, I think you have to learn new softwares. Debugging electronics can also be difficult as debugging concurrent programs (race conditions) can be difficult. So I am not sure that the nuances in electronics are fewer. There are different.

Olivier

Reply to
Olivier Scalbert

Reply to
Olivier Scalbert

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.