gray_code

extras/gray_code.vhdl

Dependencies

None

Description

This package provides functions to convert between Gray code and binary. An example implementation of a Gray code counter is also included.

Example usage

-- Conversion functions
signal bin, gray, bin2 : std_ulogic_vector(7 downto 0);
...
bin  <= X"C5";
gray <= to_gray(bin);
bin2 <= to_binary(gray);

-- Gray code counter
gc: gray_counter
  port map (
    Clock       => clock,
    Reset       => reset,
    Load        => '0', -- Not using load feature
    Enable      => '1', -- Count continuously
    Binary_load => (binary_count'range => '0'), -- Unused load port
    Binary      => binary_count, -- Internal binary count value
    Gray        => gray_count    -- Gray coded count
);

Components

gray_counter

component gray_counter is generic ( RESET_ACTIVE_LEVEL : std_ulogic ); port ( --# {{clocks|}} Clock : in std_ulogic; Reset : in std_ulogic; --# {{control|}} Load : in std_ulogic; Enable : in std_ulogic; --# {{data|}} Binary_load : in unsigned; Binary : out unsigned; Gray : out unsigned ); end component;


gray_code.gray_counter

An example Gray code counter implementation. This counter maintains an internal binary register and converts its output to Gray code stored in a separate register.

Generics:
  • RESET_ACTIVE_LEVEL (std_ulogic) – Asynch. reset control level
Port:
  • Clock (in std_ulogic) – System clock
  • Reset (in std_ulogic) – Asynchronous reset
  • Load (in std_ulogic) – Synchronous load, active high
  • Enable (in std_ulogic) – Synchronous enable, active high
  • Binary_load (in unsigned) – Loadable binary value
  • Binary (out unsigned) – Binary count
  • Gray (out unsigned) – Gray code count

Subprograms

gray_code.to_gray (Binary : std_ulogic_vector) → std_ulogic_vector
Convert binary to Gray code.
Parameters:
  • Binary (std_ulogic_vector) – Binary value
Returns:

Gray-coded vector.

gray_code.to_gray (Binary : std_logic_vector) → std_logic_vector
Convert binary to Gray code.
Parameters:
  • Binary (std_logic_vector) – Binary value
Returns:

Gray-coded vector.

gray_code.to_gray (Binary : unsigned) → unsigned
Convert binary to Gray code.
Parameters:
  • Binary (unsigned) – Binary value
Returns:

Gray-coded vector.

gray_code.to_binary (Gray : std_ulogic_vector) → std_ulogic_vector
Convert Gray code to binary.
Parameters:
  • Binary – Gray-coded value
Returns:

Decoded binary value.

gray_code.to_binary (Gray : std_logic_vector) → std_logic_vector
Convert Gray code to binary.
Parameters:
  • Binary – Gray-coded value
Returns:

Decoded binary value.

gray_code.to_binary (Gray : unsigned) → unsigned
Convert Gray code to binary.
Parameters:
  • Binary – Gray-coded value
Returns:

Decoded binary value.