EmbDev.net

Forum: FPGA, VHDL & Verilog A VHDL Counter


von Resha L. (Company: Resige) (tahmod)


Rate this post
useful
not useful
I want to understand this code. Can anyone tell help me, how does it 
work?
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use ieee.numeric_std.all;
4
use ieee.std_logic_arith.all;
5
6
7
-- Uncomment the following library declaration if using
8
-- arithmetic functions with Signed or Unsigned values
9
--use IEEE.NUMERIC_STD.ALL;
10
11
-- Uncomment the following library declaration if instantiating
12
-- any Xilinx primitives in this code.
13
--library UNISIM;
14
--use UNISIM.VComponents.all;
15
16
entity leds2_8 is
17
GENERIC (n:positive := 2**22);
18
    Port ( clkin : in  STD_LOGIC;
19
           clkout : out  STD_LOGIC;
20
           leds : out  STD_LOGIC_VECTOR (7 downto 0));
21
end leds2_8;
22
23
architecture Behavioral of leds2_8 is 
24
begin
25
PROCESS (clkin)
26
VARIABLE count: INTEGER RANGE 0 to n;
27
variable counter : integer range 0 to 255;
28
29
BEGIN
30
IF(clkin'EVENT AND clkin='1') THEN
31
count :=count + 1;
32
IF(count=n/2) THEN
33
clkout <='1';
34
ELSIF (count=n) THEN
35
clkout <='0';
36
37
count :=0;
38
counter :=counter+1;
39
leds <=conv_std_logic_vector(counter,8);
40
END IF;
41
END IF;
42
END PROCESS;
43
end  Behavioral;

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


Rate this post
useful
not useful
This code is ugly and bad formatted.


But have a look at my comments:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use ieee.numeric_std.all;
4
--use ieee.std_logic_arith.all;     -- it is a stupid idea to include both arithmetic libraries!
5
6
entity leds2_8 is
7
  GENERIC (n:positive := 2**22);    -- define n as a very big number
8
    Port ( clkin : in  STD_LOGIC;   -- clkin i the oscillator input
9
           clkout : out  STD_LOGIC; -- read my comments to this signal down a few lines
10
           leds : out  STD_LOGIC_VECTOR (7 downto 0)); -- the led pins
11
end leds2_8;
12
13
architecture Behavioral of leds2_8 is 
14
begin
15
  PROCESS (clkin)      -- this process is sensitive to the clkin signal
16
  VARIABLE count: INTEGER RANGE 0 to n := 0;      -- why the heck does everybody use variables nowadays?
17
  variable counter : integer range 0 to 255 := 0; -- at least the variables MUST be initialized!
18
                                                  -- otherwise the simulation will not work!
19
  BEGIN
20
    IF(clkin'EVENT AND clkin='1') THEN  -- you could use the rising_edge() insted
21
      count :=count + 1;                -- count up the prescaler every clock
22
      IF(count=n/2) THEN                -- here comes a way to get fired immediately: if you present me a clock
23
        clkout <='1';                   -- generated this way you will work somewhere else next day!
24
                                        -- clocks are generated with clock managers on a FPGA!
25
      ELSIF (count=n) THEN              -- ok, lets go on. here the prescaler reaches its end value
26
        clkout <='0';                   
27
        count :=0;                      -- reset the prescaler-counter
28
        counter :=counter+1;            -- and increment the led-counter
29
        leds <=conv_std_logic_vector(counter,8); -- transfer the led counter to the leds
30
      END IF;
31
    END IF;
32
  END PROCESS;
33
end  Behavioral;

: Edited by Moderator
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.