EmbDev.net

Forum: FPGA, VHDL & Verilog Debounce Code Correction for a Decimal Counter Project in 1Hz counting Speed


von Ali A. (ali_a184)


Attached files:

Rate this post
useful
not useful
I am completly new for the FPGA and basys3 development board. I have a 
project for Counter on the 7 segment displays on the board. We got 3 
different layers as a design.

cntr  cntr_rtl  cntr_top  cntr_top_struc  io_ctrl  io_ctrl_rtl 

And in the project it has to diplay on the 7 segment controlled by the 
switches : count up/count down  hold  reset options:

The priorities for these switches are:

reset

hold

count direction

top level VHDL file cntr_top.vhd

Port Name Direction Description

clk_i In System clock (100 MHz)

reset_i In Asynchronous high active reset

sw_i(15:0) In 16 switches

pb_i(3:0) In 4 buttons

ss_o(7:0) Out Contain the value for all 7-segment digits

ss_sel_o(3:0) Out Select a 7-segment digit

io_ctrl clk_i In System clock (100 MHz)

reset_i In Asynchronous high active reset

cntr0_i(n:0) In Digit 0 (from internal logic)

cntr1_i(n:0) In Digit 1 (from internal logic)

cntr2_i(n:0) In Digit 2 (from internal logic)

cntr3_i(n:0) In Digit 3 (from internal logic)

sw_i(15:0) In 16 switches (from FPGA board)

pb_i(3:0) In 4 buttons (from FPGA board)

ss_o(7:0) Out to 7-segment displays of the FPGA board

ss_sel_o(3:0) Out Selection of a 7-segment digit

swclean_o(15:0) Out 16 switches (to internal logic)

pbclean_o(3:0) Out 4 buttons (to internal logic)

cntr.vhd

clk_i In System clock (100 MHz)

reset_i In Asynchronous high active reset

cntrup_i In Counts up if signal is ‘1’

cntrdown_i In Counts down if signal is ‘1’

cntrreset_i In Sets counter to 0x0 if signal is ‘1’

cntrhold_i In Holds count value if signal is ‘1’

cntr0_o(n:0) Out Digit 0 (from internal logic)

cntr1_o(n:0) Out Digit 1 (from internal logic)

cntr2_o(n:0) Out Digit 2 (from internal logic)

cntr3_o(n:0) Out Digit 3 (from internal logic)

I will attach also the file to the attachment. Now my code is working 
and do all the funcitionality correct but there is only one issue which 
is the DEBOUNCE code part.

I didnt use the clk signal for the code and i have to change it. The 
certain given clock signal has to be used.

So can any be give me suggestions how i can correct the debounce concept 
in the code.

Please waiting for your suggestions.

Any help would be great appricated thanks for all.

here down below example for debounce but couldnt find to way to 
implement.
1
-----------------------------------------------------------------------------
2
--
3
-- Debounce buttons and switches
4
--
5
-----------------------------------------------------------------------------
6
p_debounce: process (clk_i, reset_i)
7
begin -- process debounce
8
if reset_i = '1' then -- asynchronous reset (active high)
9
elsif clk_i'event and clk_i = '1' then -- rising clock edge
10
end if;
11
end process p_debounce;
12
swsync_o <= swsync;
13
pbsync_o <= pbsync;
14
------------------------------------------------------------

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Ali A. schrieb:
> And in the project it has to diplay on the 7 segment controlled by the
> switches : count up/count down  hold  reset
Nice job for basic hardware design.

> here down below example for debounce
That is only an empty template with absolutely nothing in it.

> but couldnt find to way to implement.
How would you implement such a debouncing in real hardware with 
flipflops and counters and logic? When you have an idea, then simply 
describe it with the hardware description language VHDL.

You can have a look how others do it: see the data sheet of the MAX6818. 
Understand, what the device is doing! When you understand, what the 
device is doing, its kind of simple to implement that function in VHDL:
http://www.lothar-miller.de/s9y/archives/4-Tasterentprellung-wie-MAX6816.html

> We got 3 different layers as a design.
> cntr  cntr_rtl  cntr_top  cntr_top_struc  io_ctrl  io_ctrl_rtl
Crude and chatty academic style.
Not a nice job to be forced to solve easy problems that cumbersome way.


> here down below example
Pls use the [vhdl] tags as described above each text box...

: Edited by Moderator
von Ali (Guest)


Rate this post
useful
not useful
Here is the my code for debounce.
My tutor told me that i need to do it with the clock which it is already 
been for the system.

But my code uses some sort of extra generated signal so i need to change 
the implementation of the lines down below according to the empty 
skript.
1
-----------------------------------------------------------------------------
2
--
3
-- Generate 1 KHz enable signal.
4
--
5
-----------------------------------------------------------------------------
6
p_slowen: process (clk_i, reset_i)
7
begin
8
if reset_i = '1' then    
9
  s_enctr <= (others => '0');
10
  s_2khzen <= '0';
11
  
12
elsif clk_i'event and clk_i = '1' then 
13
  if s_enctr = COUNTVALUE then  -- When the terminal counter is reached, set the release flag and reset the counter
14
    s_enctr <= (others => '0');
15
    s_2khzen <= '1';
16
    s_2khzcount <= std_logic_vector(to_unsigned(to_integer(unsigned( s_2khzcount )) + 1, 4));
17
  else 
18
    s_enctr <= std_logic_vector(to_unsigned(to_integer(unsigned( s_enctr )) + 1, 17)); -- As long as the terminal count is not reached: increment the counter.
19
    if  s_2khzen = '1' then 
20
    s_2khzen <= '0';
21
    end if; 
22
    end if;
23
  if s_2khzcount = "1010" then
24
    s_1hzen <= not s_1hzen;
25
    s_2khzcount <= "0000";
26
  end if;
27
end if;
28
end process p_slowen;
29
-----------------------------------------------------------------------------
30
--
31
-- Debounce buttons and switches
32
--
33
-----------------------------------------------------------------------------
34
p_debounce: process (s_1hzen, reset_i)
35
variable dbouncecntr : integer:=0;
36
begin 
37
if reset_i = '1' then 
38
    swdebounced <= "0000000000000000";
39
    pbdebounced <= "0000";
40
    dbouncecntr :=0;                 -- Change clocking the process with signal from sens list.
41
else
42
    if (dbouncecntr = 0) then
43
                swtmp <= swsync1;
44
                pbtmp <= pbsync1;
45
                dbouncecntr := dbouncecntr + 1;
46
    elsif (dbouncecntr = 1) then
47
                if (swtmp = swsync1) then
48
                    swdebounced <= swsync1;
49
                end if;
50
                if (pbtmp = pbsync1) then
51
                    pbdebounced <= pbsync1;
52
                end if;
53
                dbouncecntr := 0;
54
    
55
    end if;
56
   end if;
57
end process p_debounce;
58
swclean_o <= swdebounced;
59
pbclean_o <= pbdebounced;


You can also check the hole files that i upload for the vhdl folder.

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Ali wrote:
> p_debounce: process (s_1hzen, reset_i)
Congratulation: you formed a combinatorial loop.
1
 variable dbouncecntr : integer:=0;
2
 begin
3
 if reset_i = '1' then                      
4
     :                  
5
 else
6
     :
7
        dbouncecntr := dbouncecntr + 1; ---- here it is: a counter with no clock!

I would write your REALLY BIG enable process somehow that way by using 
integer as counters:
1
p_slowen: process (clk_i, reset_i) -- can't see no need for the reset...
2
begin
3
if clk_i'event and clk_i = '1' then
4
5
  s_2khzen <= '0';                   -- the default value is '0' but it
6
  s_1hzen  <= '0';                   -- maybe overwritten furtherdown
7
8
  if s_enctr < COUNTVALUE then       --  COUNTVALUE = (f_clk_i/2kHz)-1
9
    s_enctr <= s_enctr + 1;
10
  else 
11
    s_enctr <= 0;
12
    s_2khzen <= '1';                 -- 2kHz clock enable
13
    if s_2khzcount<1999 then
14
       s_2khzcount <= s_2khzcount+1;
15
    else    
16
       s_1hzen <= '1';               -- 1Hz clock enable
17
    end if; 
18
  end if;
19
20
end if;
21
end process p_slowen;
While editing I was heavily wondering, what the s_2khzen is for.
But I would use it for debouncing:
1
p_debounce: process (clk_i, reset_i) -- USE OLNY ONE AND THE SAME CLOCK IN THE WHOLE DESIGN
2
                                     -- again absolutely no need for the reset....
3
variable cntpb, cntsw : integer range 0 to 15 := 0;
4
5
begin 
6
if clk_i'event and clk_i = '1' then
7
    if s_2khzen = '1' then
8
       -- debounce sw
9
       if swsync1 /= swdebounced then 
10
          cntsw<=0;
11
       else
12
          cntsw <= cntsw+1;
13
       end if;
14
       if cntsw = 15 then 
15
         swdebounced <= swsync1; 
16
      end if;
17
      
18
      --- same for pd
19
      
20
   end if;
21
end process;
22
swclean_o <= swdebounced;
23
pbclean_o <= pbdebounced;


> You can also check the hole files that i upload for the vhdl folder.
I can't open rar files here. Why not simply attaching the VHDL flies 
plus getting a bonus like sytax higlighting?


BTW: check the use of the vhdl tags...
1
Rules — please read before posting
2
  ...
3
    Post long source code as attachment, not in the text
4
5
Formatting options
6
  ...
7
    [vhdl]VHDL code[/vhdl]

: Edited by Moderator
von Ali (Guest)


Rate this post
useful
not useful
Thanks a lot i think i solved the issue. Thanks for your contribution.
Wish you a nice day.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.