February 24, 2021

A hardware design for variable output frequency using an n-bit counter

The DE1-SoC from Terasic is a great board for hardware design and prototyping. The following VHDL process is from a hardware design that I created for the Terasic DE1-SoC FPGA. The ten switches and four buttons on the FPGA are used as an n-bit counter with an adjustable multiplier to increase the output frequency of one or more output pins at a 50% duty cycle.

As the switches are moved or the buttons are pressed, the seven-segment display is updated to reflect the numeric output frequency, and the output pin(s) are driven at the desired frequency.  The on-board clock runs at 50MHz and the signal on the output pins is set on the rising edge of the clock input signal (positive edge triggered).  At 50MHz, the output pins can be toggled at a maximum rate of 50 million cycles per second or 25 million rising edges of the clock per second.  In other words, an LED attached to one of the output pins would blink 25 million times per second, which is not recognizable to the human eye.

scaler <= compute_prescaler((to_integer(unsigned( SW )))*scaler_mlt);
gpiopulse_process : process(CLOCK_50, KEY(0))
    if (KEY(0) = '0') then  -- async reset
        count <= 0;
    elsif rising_edge(CLOCK_50) then
        if (count = scaler - 1) then
            state <= not state;
            count <= 0;
        elsif (count = clk50divider) then -- auto reset
            count <= 0;
            count <= count + 1;
        end if;
    end if;
end process gpiopulse_process;