# sequential circuit with d flip flop

my truth table for this circuit:

ABX|AB

000 00

001 01

010 01

011 10

100 10

101 11

110 11

111 00

my code:

//x=pin 8 and clk=pin 9, A=pin 6 and B=pin 7 clk=pin 2

int a,b,c,d,e,f,g,h,i,j,k,x; int A,B; int clk;

void setup(){ A=0; B=0;

pinMode(6,OUTPUT); pinMode(7, OUTPUT); digitalWrite(6,LOW); digitalWrite(7,LOW); //delay(3000); // attachInterrupt(0,clock,RISING);

}

void loop(){ x=digitalRead(8); // clk=digitalRead(9); a=NAND(b,c); b=NOR(d,d); c=NAND(x,g); d=NAND(e,f); e=NAND(A,h); f=NAND(A,!B); g=NAND(A,!B); h=NOR(x,x); i=NAND(h,B); j=NAND(x,!B); k=NAND(i,j); int lasttemp =0; int temp = digitalRead(9); // gets the clock pulse delay(50); if ( (temp != lasttemp) && (temp == 1) && clk!=1) { A=dff(a,clk,A); B=dff(k,clk,B); clk =1; } else if (digitalRead(9)==0)clk = 0; lasttemp=temp;

if(clk){

} digitalWrite(6,A); digitalWrite(7,B); }

int NAND(int x, int y){ if(x && y) return(0); else return(1); }

int NOR(int x, int y){ if(!x && !y) return(1); else return(0); }

int dff(int d, int clk, int memory){ if (clk==1) return(d); else return(memory); }

void clock(){ //int a,b,c,d,e,f,g,h,i,j,k,x; //clk=digitalRead(9); // x=digitalRead(8); /* int temp = digitalRead(9); // gets the clock pulse delay(50); if ( (temp != lasttemp) && (temp == 1) ) clk = 1; else clk = 0; lasttemp=temp;

*/ }

you can probably see that I am attempting to fix the button bounce. I always have problems with my code in the A output especially the first 3 rows of the truth table. My question is can you fix my code to do what I want it to. I have played with the idea of button debounce and attachInterupt as well. I am at a loss, also I have been thinking that there is a problem with the rising edge of my switches, i don't understand how to fix this

``````if(clk){

}
``````

What were you trying to accomplish here?

I doubt you're going to be able to read a smiley face with much success. (This is what the CODE tags are there for).

ortonjon: can you fix my code to do what I want it to.

When is our assignment due by?

yeah sorry about the smilies and im not sure what you mean about when our assignment is due

ortonjon: yeah sorry about the smilies

Not sorry enough to fix it, though, I guess.

and im not sure what you mean about when our assignment is due

Rather than asking for help, you asked us to do the work. I just wanted to know when we had to do it by.

Go back to your first post. Hit the modify icon. Select the code, hit the # icon and then save it.

Why are you bothering to write all that logic simulation code? The whole thing is simply a look up table. Two lines of code at the most.

Unless as was said it is an assignment from a class that you want us to do for you.

Why the title? The question has nothing to do with a sequential circuit or a d type flip flop unless you are holding back on more stuff as well.

jon,

I know that you're trying to build a structural model of the circuit, but I realized that the behavioral model is fairly easy to build. I'll post the structural model in VHDL when I get the chance, but it may be a bit more work to model the circuit in C++.

``````/* Truth Table
PS  | NS
---------
ABx | AB
---------
000 | 00
001 | 01
010 | 01
011 | 10
100 | 10
101 | 11
110 | 11
111 | 00
*/

// I/O Pins:
// clock input is interrupt0 on pin 2
#define x 3 // x input on pin 3
#define A 4 // Flip-Flop A output on pin 4
#define B 5 // Flip-Flop B output on pin 5
boolean flag(0);
byte AB (B00); // initialize output of ffa & ffb to zero
//B00 means binary 00, for example B0101 = 5

void setup(){
pinMode(3,INPUT);  // x
pinMode(4,OUTPUT); // A
pinMode(5,OUTPUT); // B
attachInterrupt(0,trigger,RISING);
}

void loop(){
/* Write the outputs for ffa & ffb
using the value from AB: bit 0 holds the value for B
and bit 1 holds the value for A
*/
if (flag) {
delay(250); // delay to debounce the switch
flag--;     // reset the trigger to catch the next clock
if (digitalRead(3)==1) AB = ++AB % 4;
/*
no output changes unless x=1
notice that the next state for AB is always
one more than the current state, but never more than three
*/
}
}

void trigger(){
// update the trigger
if(!flag)flag++;
}
``````