EmbDev.net

Forum: FPGA, VHDL & Verilog Counter that goes up to 9 and down


Author: Jason Jellos (Company: nope) (liad1234)
Posted on:

Rate this post
0 useful
not useful
hello im new to FPGA and ive been trying to make a 4 bits counter that 
counts up to 9 and goes down to 0 and then again and again with no 
success... so im asking for help for how to do it from anyone who can 
help!  thank you. this is what i have right now after some failed 
attempts.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;


entity ElevatorCounter is
port (Clock, reset: in std_logic;
      Q            : out std_logic_vector (3 downto 0);
    
    end entity;
    
architecture arch_ElevatorCounter of ElevatorCounter is 
begin 
    process(clock, reset) 
          variable tempQ : std_logic_vector(3 downto 0);
  begin 
      if Reset='0' then 
        tempQ := "0000";
    elsif rising_edge(clock) then 
             tempQ :=tempQ + 1;    
        if tempQ= "1001" then 
          tempQ := tempQ - 1;
            wait until tempQ := "0000";
      
       
      end if;
            if tempQ= "0000" then
            tempQ := tempQ + 1;
          wait until tempQ := "1001";
          end if;  
        end if; 
      end if;
    Q <= tempQ;
  end process;
end architecture;

: Edited by Moderator
Author: Donni (Guest)
Posted on:

Rate this post
0 useful
not useful
I would use a simple state machine with two states UP & DOWN. If tmp is 
9 set state to DOWN, if tmp is 0 or you have the reset set state to UP.

Then check the state and increment or decrement.

Author: Lothar Miller (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Jason J. wrote:
> hello im new to FPGA and ive been trying to make a 4 bits counter that
> counts up to 9 and goes down to 0 and then again and again with no success
What about simulating this piece of code?

> wait until...
This will not work in real life here. You are not "programming" with 
VHDL. Only about 10% of VHDL are synthesizeble. And those 10% must be 
used in a way the synthesizer understands it. The easiest way is to do 
it like others do it, the other way is to read the synthesizers manual.

> variable tempQ : std_logic_vector(3 downto 0);
I urge you NOT to use any variables for storing elements. Use signals 
instead!

Author: Lothar Miller (lkmiller) (Moderator)
Posted on:
Attached files:

Rate this post
0 useful
not useful
My attempt would look like this:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity ElevatorCounter is
  port (clock : in  std_logic;
        floor : out std_logic_vector (3 downto 0));
end entity;
    
architecture arch_ElevatorCounter of ElevatorCounter is

signal d : std_logic := '0'; -- direction 0=up, 1=down
signal f : integer := 0;   -- start at the basement

begin 

  process begin 
    wait until rising_edge(clock);
    if d='0' then -- up
      if f<9 then
        f <= f+1;
      else
        d <= '1'; -- next: down
        f <= f-1;
      end if;
    else          -- down
      if f>0 then
        f <= f-1;
      else
        d <= '0'; -- next: up
        f <= f+1;
      end if;
    end if;
  end process;
  
  floor <= std_logic_vector(to_unsigned(f,4));
  
end architecture;
The result looks fine that far...

: Edited by Moderator
Author: Jason Jellos (Company: nope) (liad1234)
Posted on:

Rate this post
0 useful
not useful
weird when i try to compile that code i get these errors... help?



Error (10500): VHDL syntax error at elevatorcounter.vhd(9) near text 
"end";  expecting an identifier ("end" is a reserved keyword), or 
"constant", or "file", or "signal", or "variable"
Error (10500): VHDL syntax error at elevatorcounter.vhd(15) near text 
"begin";  expecting an identifier ("begin" is a reserved keyword), or 
"constant", or "file", or "signal", or "variable"
Error: Quartus II 64-Bit Create Symbol File was unsuccessful. 2 errors, 
0 warnings

Author: Lothar Miller (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Jason J. wrote:
>  i get these errors... help?
What I get with XST is
:
:

Final Register Report

Macro Statistics
# Registers              : 5
 Flip-Flops              : 5

:
:
Process "Synthesize - XST" completed successfully
And 5 registers is fine for the desing: 4 for the counter and 1 for the 
direction.

Same for Lattice synthesizing engine:
:
:

####### Begin Area Report (ElevatorCounter)###########
Number of register bits => 5 of 1604 (0 % )

:
:

Done: completed successfully
Same here, so its pretty sure that the Altera toolchain will be able to 
synthesize that code.

> when i try to compile that code
Pls. attach your VHDL file. Maybe something went wrong with copying...

: Edited by Moderator
Author: Markus F. (mfro)
Posted on:

Rate this post
0 useful
not useful
Lothar M. wrote:
> Same here, so its pretty sure that the Altera toolchain will be able to
> synthesize that code.

sure it does:
+---------------------------------------------------------------------------------+
; Flow Summary                                                                    ;
+------------------------------------+--------------------------------------------+
; Flow Status                        ; Successful - Thu Dec 14 12:44:06 2017      ;
; Quartus II 64-Bit Version          ; 13.1.4 Build 182 03/12/2014 SJ Web Edition ;
; Revision Name                      ; ElevatorCounter                            ;
; Top-level Entity Name              ; ElevatorCounter                            ;
; Family                             ; Cyclone III                                ;
; Device                             ; EP3C120F484C7                              ;
; Timing Models                      ; Final                                      ;
; Total logic elements               ; 8 / 119,088 ( < 1 % )                      ;
;     Total combinational functions  ; 8 / 119,088 ( < 1 % )                      ;
;     Dedicated logic registers      ; 5 / 119,088 ( < 1 % )                      ;
; Total registers                    ; 5                                          ;
; Total pins                         ; 5 / 284 ( 2 % )                            ;
; Total virtual pins                 ; 0                                          ;
; Total memory bits                  ; 0 / 3,981,312 ( 0 % )                      ;
; Embedded Multiplier 9-bit elements ; 0 / 576 ( 0 % )                            ;
; Total PLLs                         ; 0 / 4 ( 0 % )                              ;
+------------------------------------+--------------------------------------------+

Author: Jason Jellos (Company: nope) (liad1234)
Posted on:
Attached files:

Rate this post
0 useful
not useful
it happens when i try to compile this piece of code. i attached it. 
weird for me

Author: Andreas Schweigstill (Company: Schweigstill IT) (schweigstill)
Posted on:

Rate this post
0 useful
not useful
The port definition in your entity contains at least two big mistakes. 
Why would you expect the toolchain to compile or synthesize it?

Author: Jason Jellos (Company: nope) (liad1234)
Posted on:

Rate this post
-1 useful
not useful
I dont know because i didnt change much there it compiled it last time.. 
:(

Author: Andreas Schweigstill (Company: Schweigstill IT) (schweigstill)
Posted on:

Rate this post
0 useful
not useful
But I see THAT THERE ARE TWO MISTAKES. So you have to correct them. 
Probably you should take a formal VHDL specification in order to find 
out the right syntax.

Author: Lothar Miller (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Jason J. wrote:
> i didnt change much there it compiled it last time.. :(
So its kind of easy: the problem must be one of those few changes you 
made.

Jason J. wrote:
> it happens when i try to compile this piece of code. i attached it.
You simply must remove the errors reported by the toolchain. Start with 
that in line 9. Look how a port declaration must look like.
One hint: count all of the brackets. Match the opening brackets to the 
closing ones...

> weird for me
Its a little weird for me, that you don't find this little problem 
yourself by consulting a VHDL book or some code samples.

Author: Jason Jellos (Company: nope) (liad1234)
Posted on:

Rate this post
0 useful
not useful
yes my bad I've found the problem after looking at every line. Thank 
you!!

Author: John (Guest)
Posted on:

Rate this post
0 useful
not useful
>> ElevatorCounter.vhd
>> I dont know because i didnt change much there it compiled it last time..

I hope I'll never use this elevator accidentally. It might get stuck 
somewhere at 8.5 ...

Author: Jason Jellos (Company: nope) (liad1234)
Posted on:

Rate this post
0 useful
not useful
John wrote:
>>> ElevatorCounter.vhd
>>> I dont know because i didnt change much there it compiled it last time..
>
> I hope I'll never use this elevator accidentally. It might get stuck
> somewhere at 8.5 ...



LOOOOL probably hahhaah

Reply

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