dohzer
Member level 1
vhdl increment
I'm writing a simple program (as my first VHDL program) to take a clock signal and divide it by powers of two (1,2,4,8,....). Basically I have a signal that counts up, and depending on the divider input, one of the signal's bits is used as the output.
Here's my current code:
There are two processes; one to increment the count signal, and the other to select which bit to use as the output based on the divider select input.
I wanted the clock input to "pass straight through" when the divider select input is '00000', and at first I thought I would do it by having the count signal increment on both the rising and falling edges, but the program didn't compile when I tried to do that. I got the following error:
I know I could just use the clock signal without manipulating it if I want a "divide by 1" signal, but I was just wondering why using both edges give me trouble.
Can someone explain if there is a way to use both edges, or if not, explain why not.
Also, if there is any other mistake I have made in my code, let me know.
I'm writing a simple program (as my first VHDL program) to take a clock signal and divide it by powers of two (1,2,4,8,....). Basically I have a signal that counts up, and depending on the divider input, one of the signal's bits is used as the output.
Here's my current code:
Code:
--Clock Divider
library ieee;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use IEEE.numeric_std.all;
entity clock_divider is
port( RESET: in std_logic;
CLK_IN: in std_logic;
DIV_SEL: in std_logic_vector(4 downto 0);
CLK_OUT: out std_logic
);
end clock_divider;
architecture clock_divider of clock_divider is
signal count: unsigned(31 downto 0);
begin
mux: process(count,DIV_SEL)
begin
--Select output bit
case DIV_SEL is
when "00000" => CLK_OUT <= std_logic(count(0));
when "00001" => CLK_OUT <= std_logic(count(1));
when "00010" => CLK_OUT <= std_logic(count(2));
when "00011" => CLK_OUT <= std_logic(count(3));
when "00100" => CLK_OUT <= std_logic(count(4));
when "00101" => CLK_OUT <= std_logic(count(5));
when "00110" => CLK_OUT <= std_logic(count(6));
when "00111" => CLK_OUT <= std_logic(count(7));
when "01000" => CLK_OUT <= std_logic(count(8));
when "01001" => CLK_OUT <= std_logic(count(9));
when "01010" => CLK_OUT <= std_logic(count(10));
when "01011" => CLK_OUT <= std_logic(count(11));
when "01100" => CLK_OUT <= std_logic(count(12));
when "01101" => CLK_OUT <= std_logic(count(13));
when "01110" => CLK_OUT <= std_logic(count(14));
when "01111" => CLK_OUT <= std_logic(count(15));
when "10000" => CLK_OUT <= std_logic(count(16));
when "10001" => CLK_OUT <= std_logic(count(17));
when "10010" => CLK_OUT <= std_logic(count(18));
when "10011" => CLK_OUT <= std_logic(count(19));
when "10100" => CLK_OUT <= std_logic(count(20));
when "10101" => CLK_OUT <= std_logic(count(21));
when "10110" => CLK_OUT <= std_logic(count(22));
when "10111" => CLK_OUT <= std_logic(count(23));
when "11000" => CLK_OUT <= std_logic(count(24));
when "11001" => CLK_OUT <= std_logic(count(25));
when "11010" => CLK_OUT <= std_logic(count(26));
when "11011" => CLK_OUT <= std_logic(count(27));
when "11100" => CLK_OUT <= std_logic(count(28));
when "11101" => CLK_OUT <= std_logic(count(29));
when "11110" => CLK_OUT <= std_logic(count(30));
when "11111" => CLK_OUT <= std_logic(count(31));
when others => CLK_OUT <= 'Z';
end case;
end process mux;
increment: process(RESET,CLK_IN) is
begin
--Reset the counter if reset is high
if (RESET = '1') then
count <= (others => '0');
--Increment the counter on each clock edge
elsif (rising_edge(CLK_IN)) then
count <= count + 1;
elsif (falling_edge(CLK_IN)) then
count <= count + 1;
end if;
end process increment;
end architecture;
There are two processes; one to increment the count signal, and the other to select which bit to use as the output based on the divider select input.
I wanted the clock input to "pass straight through" when the divider select input is '00000', and at first I thought I would do it by having the count signal increment on both the rising and falling edges, but the program didn't compile when I tried to do that. I got the following error:
Error (10819): Netlist error at clock_divider.vhd(66): can't infer register for count[0] because it changes value on both rising and falling edges of the clock
I know I could just use the clock signal without manipulating it if I want a "divide by 1" signal, but I was just wondering why using both edges give me trouble.
Can someone explain if there is a way to use both edges, or if not, explain why not.
Also, if there is any other mistake I have made in my code, let me know.