Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)

Understanding a counter

Altera_Forum
Honored Contributor II
1,403 Views

I want to understand this code. Can anyone tell help me, how does it work? 

 

library IEEE; 

use IEEE.STD_LOGIC_1164.ALL; 

use ieee.numeric_std.all; 

use ieee.std_logic_arith.all;-- Uncomment the following library declaration if using-- arithmetic functions with Signed or Unsigned values--use  

IEEE.NUMERIC_STD.ALL;-- Uncomment the following library declaration if instantiating-- any Xilinx primitives in this code.--library UNISIM;--use UNISIM.VComponents.all; 

entity leds2_8 is 

GENERIC (n: positive := 2**22);  

 

Port ( clkin : in STD_LOGIC; 

clkout : out STD_LOGIC; 

leds : out STD_LOGIC_VECTOR (7 downto 0)); 

end leds2_8; 

architecture Behavioral of leds2_8 is 

begin 

PROCESS (clkin) 

VARIABLE count: INTEGER RANGE 0 to n; 

variable counter : integer range 0 to 255; 

 

BEGIN 

IF(clkin'EVENT AND clkin='1') THEN 

count :=count + 1; 

IF(count=n/2) THEN 

clkout <='1'; 

ELSIF (count=n) THEN 

clkout <='0'; 

 

count :=0; 

counter :=counter+1; 

leds <=conv_std_logic_vector(counter,8); 

END IF; 

END IF; 

END PROCESS; 

end Behavioral;
0 Kudos
1 Reply
Altera_Forum
Honored Contributor II
634 Views

you got two counters (count 0 ~ 2^22 and counter 0~255). 

count outputs clkout at n/2 and n (not exactly right, should be n/2-1 and n-1 due to zero start value), it counts clocks 

counter drives led output and is enabled when count is max 

 

both counters can be free running as they are binary range(0~255,0~ 2^22-1) if you correct your range.
0 Kudos
Reply