# 4-bit up/down counter fault

Status
Not open for further replies.

#### miroseh

##### Newbie level 2 Hi everyone,
I am writing a block code via the gate level model for a 4-bit up/down synchronous counter.

The counter should be able to count up to a certain number (which is not larger than 15) predefined by a 4-bit input, and to count down from a certain number as well.

I used TFF in my design, in which I define D[3:0] as the counting bound, as mentioned above, eg D = 1010. A flag Up denotes up or down counting.

Using the Karnaugh map, I obtained these:

Clear[] = ~up + (Q^D) + (Q^D) + (Q^D) + (Q^D);
Preset = up + ~D + Q + Q + Q + Q;

To illustrate, when I put up = 1 to count up to D = 4'b1010, if Q = 1010 we have clear[] = 0 while preset still gets 1; so the next Q = 0000.
Similarly, when I want to count down from D = 0111, whenever Q reach 0000, it will be set to 0111 because Clear = 1111 and Preset = 1000.

When I implement the code to Quartus, using functional simulation, it give good result for many cases.

But in several cases, eg, counting up to D = 0111, it just counts from 0 to 3 instead.

I remain stuck with this problem and I dunno where is the mistake.