Testing LOGIC, am I doing this correctly?

japreja

Active member
I am trying to remake a project I did quite a while ago on a different microcontroller. Basicaly just emulating LOGIC ic's. Input pins are 8 and 9, output pin is 16 and connected to an LED for testing.

Does this look correct?

Code:
void setup() {
  //wait a moment
  delay(1000);


  //setup the output pins
  for(int i = 16; i <= 23; i++){
    pinMode(i, OUTPUT);
  }
}


void loop() {
  // un-comment only one of the following to test
  
  // gate_NOT(16,8);
  // gate_AND(8, 9, 16);
  // gate_OR(8, 9, 16);
  // gate_NAND(8, 9, 16);
  // gate_NOR(8, 9, 16);
  // gate_XOR(8, 9, 16);
  gate_XNOR(8, 9, 16);
}


void gate_NOT(uint8_t outPin, uint8_t inPin){
  digitalWrite(outPin, !digitalRead(inPin));
}


void gate_AND(uint8_t inPin_A, uint8_t inPin_B, uint8_t outPin){
  digitalWrite(outPin, digitalRead(inPin_A) & digitalRead(inPin_B));
}


void gate_OR(uint8_t inPin_A, uint8_t inPin_B, uint8_t outPin){
  digitalWrite(outPin, digitalRead(inPin_A) | digitalRead(inPin_B));
}


void gate_NAND(uint8_t inPin_A, uint8_t inPin_B, uint8_t outPin){
  digitalWrite(outPin, !(digitalRead(inPin_A) & digitalRead(inPin_B)));
}


void gate_NOR(uint8_t inPin_A, uint8_t inPin_B, uint8_t outPin){
  digitalWrite(outPin, !(digitalRead(inPin_A) | digitalRead(inPin_B)));
}


void gate_XOR(uint8_t inPin_A, uint8_t inPin_B, uint8_t outPin){
  digitalWrite(outPin, (digitalRead(inPin_A) ^ digitalRead(inPin_B)));
}


void gate_XNOR(uint8_t inPin_A, uint8_t inPin_B, uint8_t outPin){
  digitalWrite(outPin, !(digitalRead(inPin_A) ^ digitalRead(inPin_B)));
}
 
You need to set the mode of the input pins too.
Code:
    // setup the input pins
    for(int i = 8; i <= 9; i++){
      pinMode(i, INPUT_PULLUP);
    }
Do you have a resistor in series with the output LED? If you only need one output you could just use the builtin led which is set up with
pinMode(LED_BUILTIN, OUTPUT);

Pete
 
Yes I am using a 330 Ohm resistor on the output LED. This is just the tip of the iceburg of code for this project. I am also doing Multiplexers, de-Multiplexers, counters, etc... as I did in an old project. Only difference is this project is in C which I am just beginning to learn more of. My old code was for the P8X32A micro, which is a propriatery language by Parallax. Here is a link to my old code https://forums.parallax.com/discussion/160846/chipulater-propeller-logic-emulator#latest
 
Back
Top