Problems

11. Gray code counter

MediumWrite the moduleAbout 20 min
sequentialcountersgray code

Build a 4-bit counter whose output counts in Gray code: 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100 and so on, wrapping after 1000 (the code for 15) back to 0000.

gray must come straight from flip-flops (no logic between the registers and the output), so that it never shows a glitch while it changes.

On each rising clock edge: rst = 1 sets the count to 0 (gray = 0000); otherwise, when en = 1, the count advances by one; when en = 0 it holds. rst is synchronous and active high.

Ports

Module gray_counter

PortDirBitsWhat it is
clkin1Clock.
rstin1Synchronous reset, active high.
enin1Count enable.
grayout4The count in Gray code, registered.

Example

After reset gray = 0000. Four enabled edges later gray = 0110 (the code for 4). Exactly one bit changes on every step.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Veriloggray_counter.v
14px
Loading the editor…

Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.

Gray code counter: Verilog interview question | breadboardhub