Truth table
C,B,A=>Y3,Y2,Y1,Y0
--------------------
0,0,0 =>0,0,0,1
0,0,1 =>0,0,1,0
0,1,0 =>0,1,0,0
0,1,1 =>1,0,0,0
1,0,0 =>1,0,0,0
1,0,1 =>0,1,0,0
1,1,0 =>0,0,1,0
1,1,1 =>0,0,0,1
//---------Circuit--------------
#define A 2
#define B 3
#define C 4
#define Y0 5
#define Y1 6
#define Y2 7
#define Y3 8
void setup() {
Serial.begin(9600);
Serial.println("Chang Yung Fu <<<2012/9/18>>>");
Serial.println("Combining Logic using Arduino");
Serial.println("Truth Table example");
Serial.println("C,B,A=>Y3,Y2,Y1,Y0");
Serial.println("--------------------");
Serial.println("0,0,0 =>0,0,0,1");
Serial.println("0,0,1 =>0,0,1,0");
Serial.println("0,1,0 =>0,1,0,0");
Serial.println("0,1,1 =>1,0,0,0");
Serial.println("1,0,0 =>1,0,0,0");
Serial.println("1,0,1 =>0,1,0,0");
Serial.println("1,1,0 =>0,0,1,0");
Serial.println("1,1,1 =>0,0,0,1");
pinMode(A,INPUT);
pinMode(B,INPUT);
pinMode(C,INPUT);
pinMode(Y0,OUTPUT);
pinMode(Y1,OUTPUT);
pinMode(Y2,OUTPUT);
pinMode(Y3,OUTPUT);
}
void loop() {
int InData,InA,InB,InC,InD,OutData;
int Table[]={1,2,4,8,8,4,2,1};
//Input Pin Map InData
InA=digitalRead(A);
InB=digitalRead(B);
InC=digitalRead(C);
InD=digitalRead(D);
InData=(InA) | (InB<<1) | (InC<<2) | (InD<<3);
//Serial.print("InData(Bin)=");Serial.println(InData,BIN);
//Serial.println("------------------------------------");
//Table Map OutData
OutData=Table[InData];
//Serial.print("OutData(Bin)=");Serial.println(OutData,BIN);
//Serial.println("------------------------------------");
//OutData Map Output Pin
if ((OutData & 1)!=0) //-------------check bit 0
{digitalWrite(Y0,HIGH);}
else
{digitalWrite(Y0,LOW);}
if ((OutData & 2)!=0) //-------------check bit 1
{digitalWrite(Y1,HIGH);}
else
{digitalWrite(Y1,LOW);}
if ((OutData & 4)!=0) //-------------check bit 2
{digitalWrite(Y2,HIGH);}
else
{digitalWrite(Y2,LOW);}
if ((OutData & 8)!=0) //-------------check bit 3
{digitalWrite(Y3,HIGH);}
else
{digitalWrite(Y3,LOW);}
}
//-----------Simulate -------------------
//--------Instruction ------------#define Serial.beginSerial,printSerial.printlnpinModedigitalReaddigitalWriteif ....else....Set Var Bit=0,1 or notCheck Var Bit=0 or 1.
沒有留言:
張貼留言