Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

counter using vhdl and implementation in fpga

Status
Not open for further replies.

p11

Banned
Joined
Jan 25, 2014
Messages
177
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Activity points
0
Code:
 ----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date:    17:38:49 05/13/2015 
-- Design Name: 
-- Module Name:    COUNTLED - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
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 COUNTLED is
    Port ( CLK : in  STD_LOGIC;
	        X : in  std_logic_vector(1 DOWNTO 0);
           Y : out  std_logic_vector(1 DOWNTO 0));
end COUNTLED;

architecture Behavioral of COUNTLED is  


begin
process(X,clk)
  
 
  VARIABLE COUNT :integer := 04;
  
  variable INT : std_logic_vector(1 DOWNTO 0);
  
  variable b : std_logic :='1';
  variable c: std_logic;
  variable z : std_logic_vector (1 downto 0);
  
  
  BEGIN
    int (1 DOWNTO 0):= x (1 DOWNTO 0);
  
  L1: loop
  
  if  (rising_edge (clk)) then 
  
  z(0) := int(0) xor b;
  c := int(0) and  b;
  
  y(0) <=  z(0);
  
  z(1):= (c xor int(1));
  
  y(1) <= z(1);
  
 int (0) := z(0);
 
 int (1) := z(1);
 else 
 
 end if ;
  
COUNT :=COUNT -1;
EXIT L1 WHEN COUNT =0 ;
	 
	 
END LOOP L1;
		 
  
  end process;
 
       
end Behavioral;


here i have designed a 2 bit counter that gives 00,01,10,11 if initialized from outside (input port of fpga) as 00.here , the loop continues unless it counts 4 times , after which the loop ends . here for every rising edge of clock the count begins . I am new in this fiels , please help and let me know if there is any mistake in my desi
 

std_match

Advanced Member level 4
Joined
Jul 9, 2010
Messages
1,187
Helped
444
Reputation
888
Reaction score
409
Trophy points
1,363
Location
Sweden
Activity points
9,162
You are on the wrong track. You need a VHDL text book and a simulator. Start with working examples and then try to modify them.
Don't use variables until you have a better understanding.

Get a text book that uses numeric_std and stay away from the non-IEEE packages std_logic_arith, std_logic_unsigned etc.
 

lucbra

Advanced Member level 2
Joined
Oct 30, 2003
Messages
514
Helped
73
Reputation
146
Reaction score
63
Trophy points
1,308
Location
Belgium
Activity points
3,251
The problem is that someone might find this code using google and start using it.

Isn't there a possibility to scrap this code?
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,236
Helped
1,151
Reputation
2,321
Reaction score
1,127
Trophy points
1,403
Location
Brazil
Activity points
53,763
The question seems quite trivial, being almost a behavioral description of what you want, however the code above uses a structural implementation which use too much lines of code and is harder to debug.
 

vGoodtimes

Advanced Member level 4
Joined
Feb 16, 2015
Messages
1,089
Helped
307
Reputation
614
Reaction score
302
Trophy points
83
Activity points
8,730
Curiously, I decided to just google this subject "vhdl counter example".

link 1: ftp://www.cs.uregina.ca/pub/class/301/counter/lecture.html
Uses "after 50ns", although this will synthesize correctly, well other than the lack of a conversion to slv.

link 2: https://mosaic.cnfolio.com/VHDLfiles/files.xml?action=download&file=Example_Entity_0to9_Counter.pdf
This is how someone might actually write the code. Everyone might not agree on the style, but it is at least believable as something written for synthesis.

https://www2.cs.siu.edu/~cs320/four-bit-counter.php
This actually reinvents "rising_edge(clk)" using variables.

Somehow the worst code examples are getting pushed to the top of google search.
 
  • Like
Reactions: p11

    p11

    Points: 2
    Helpful Answer Positive Rating

ads-ee

Super Moderator
Staff member
Joined
Sep 10, 2013
Messages
7,805
Helped
1,809
Reputation
3,628
Reaction score
1,768
Trophy points
1,393
Location
USA
Activity points
58,912
link 2: https://mosaic.cnfolio.com/VHDLfiles/files.xml?action=download&file=Example_Entity_0to9_Counter.pdf
This is how someone might actually write the code. Everyone might not agree on the style, but it is at least believable as something written for synthesis.
It's using the synopsys packages, so right there is a knock against it qualifying as "good" example code. ;-)

That other link to Southern Illinois university makes me wonder what they are trying to teach the engineering students taking the class that is using that as a counter example. 8-O

Somehow the worst code examples are getting pushed to the top of google search.
I guess this shows how Google has transitioned from doing no evil to doing evil on a regular basis. ;-)

My third link was the following:
https://www.asic-world.com/examples/vhdl/up_counter_with_load.htmland we can blame A. Pham for not using the numeric_std package. though they did use rising_edge(clk) instead of the usual clk'event and clk = '1' stuff.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top