Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramming a ripple counter in C with JK flip flops
    primarykey
    data
    text
    <p>I've decided to have a go at programming flip flops in C. I've had an attempt at both a D and JK flip flop (without preset and clear sections yet).</p> <p>I'm testing if by cascading them, I can get them to produce a simple 4 bit ripple counter. After writing my code and running it, it seems to produce some really weird results in the form of:</p> <p>Clk: 01010101010101010101</p> <p>OuA: 01100110011001100110</p> <p>OuB: 01000100010001000100</p> <p>OuC: 01111000011110000111</p> <p>OuD: 01010000010100000101</p> <p>Where Clk is the input clock, OuA is output A, OuB, output B etc. As you can see, OuA and OuC seem somewhat acceptable with the ratio of on to off but B and D seem really odd!</p> <p>My code is:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void DFF(int Clk, int D, int *Q, int *NQ) { if(Clk) { *Q = D; *NQ = !*Q; } } void JKF(int Clk, int J, int K, int *Q, int *NQ) { if(Clk&amp;J&amp;(!K)) { *Q = 1; *NQ = 0; } if(Clk&amp;K&amp;(!J)) { *Q = 0; *NQ = 1; } if(Clk&amp;J&amp;K) { *Q = *NQ; *NQ = !*Q; } } int main() { FILE *fptr; const int Len = 20; int Clk = 1, ClkA[Len]; int n, OA[Len], OB[Len], OC[Len], OD[Len]; int Q = 0, NQ = 1; int Q2 = 0, NQ2 = 1; int Q3 = 0, NQ3 = 1; int Q4 = 0, NQ4 = 1; for(n=0; n&lt;Len; n++) { Clk^=1; JKF(Clk, 1, 1, &amp;Q, &amp;NQ); JKF(Q, 1, 1, &amp;Q2, &amp;NQ2); JKF(Q2, 1, 1, &amp;Q3, &amp;NQ3); JKF(Q3, 1, 1, &amp;Q4, &amp;NQ4); ClkA[n] = Clk; OA[n] = Q; OB[n] = Q2; OC[n] = Q3; OD[n] = Q4; } fptr = fopen("c:/ff.txt", "w"); fprintf(fptr, "Clk: "); for(n = 0; n&lt;Len; n++) fprintf(fptr, "%d", ClkA[n]); fprintf(fptr, "\nOuA: "); for(n = 0; n&lt;Len; n++) fprintf(fptr, "%d", OA[n]); fprintf(fptr, "\nOuB: "); for(n = 0; n&lt;Len; n++) fprintf(fptr, "%d", OB[n]); fprintf(fptr, "\nOuC: "); for(n = 0; n&lt;Len; n++) fprintf(fptr, "%d", OC[n]); fprintf(fptr, "\nOuD: "); for(n = 0; n&lt;Len; n++) fprintf(fptr, "%d", OD[n]); fclose(fptr); return 0; } </code></pre> <p>I apologise about the clarity and I know its probably not particularly efficient but could someone please clear up what is happening with the outputs?</p> <p>Cheers!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload