【问题标题】:A program that would test two input gates (AND, OR, NAND, NOR, and XOR)测试两个输入门(AND、OR、NAND、NOR 和 XOR)的程序
【发布时间】:2013-12-06 20:36:49
【问题描述】:

我正在尝试设计一个符合 these 参数的 Arduino 程序。起初我认为这不会那么难,但我不知道解决这个问题的逻辑或方法是什么。现在我有点卡住了。无论如何,here 是我目前所拥有的。非常感谢任何帮助和输入。谢谢。

【问题讨论】:

    标签: boolean arduino logic ttl digital-logic


    【解决方案1】:

    基本逻辑是:

    - For out in 3, 6, 8, 11,
      - is_and = is_or = is_xor = is_nand = is_nor = true.
    
      - For p in 0, -1,
        - Set pin out-2 to (p ? HIGH : LOW).
        - For q in 0, -1,
          - Set pin out-1 to (q ? HIGH : LOW).
          - Read pin out.
          - If the value read isn't equal (p & q ? HIGH : LOW),
            - is_and = false.
          - If the value read isn't equal (p | q ? HIGH : LOW),
            - is_or = false.
          - If the value read isn't equal (p ^ q ? HIGH : LOW),
            - is_xor = false.
          - If the value read isn't equal (p & q ? LOW : HIGH),
            - is_nand = false.
          - If the value read isn't equal (p | q ? LOW : HIGH),
            - is_nor = false.
    
      - If is_and,
        - Print "AND".
      - Else if is_or,
        - Print "OR".
      - Else if is_xor,
        - Print "XOR".
      - Else if is_nand,
        - Print "NAND".
      - Else if is_nor,
        - Print "NOR".
      - Else
        - Print "???".
    

    int out_pins[4] = { 3, 6, 8, 11 };
    const int num_out_pins = sizeof(out_pins) / sizeof(out_pins[0]);
    
    void setup() {
       int i = num_out_pins;
       while (i--) {
          pinMode(out_pins[i],   OUTPUT);
          pinMode(out_pins[i-1], INPUT);
          pinMode(out_pins[i-2], INPUT);
       }
    }
    
    void test_gate(int out_pin) {
       boolean is_and  = true;
       boolean is_or   = true;
       boolean is_xor  = true;
       boolean is_nand = true;
       boolean is_nor  = true;
    
       for (int p=0; p>-2; --p) {
          digitalWrite(out_pin-2, (p ? HIGH : LOW))
          for (int q=0; p>-2; --p) {
             digitalWrite(out_pin-1, (q ? HIGH : LOW))
             // Is a delay needed here?
             int out = digitalRead(out_pin);
             if (out == (p & q ? HIGH : LOW)) is_nand = false; else is_and = false;
             if (out == (p | q ? HIGH : LOW)) is_nor  = false; else is_or  = false;
             if (out != (p ^ q ? HIGH : LOW)) is_xor  = false;
          }
       }
    
       if (is_and) {
          printf("%d is the output of an AND gate\n", out_pin);
       }
       else if (is_or) {
          printf("%d is the output of an OR gate\n", out_pin);
       }
       else if (is_xor) {
          printf("%d is the output of an XOR gate\n", out_pin);
       }
       else if (is_nand) {
          printf("%d is the output of a NAND gate\n", out_pin);
       }
       else if (is_nor) {
          printf("%d is the output of a NOR gate\n", out_pin);
       }
       else {
          printf("%d is the output of an unrecognized gate\n", out_pin);
       }
    }
    
    void test_chip() {
       for (int i=0; i<num_out_pins; ++i) {
          test_gate(i, out_pins[i]);
       }
    }
    
    void loop() {
       test_chip();
       delay(1000);
    }
    

    【讨论】:

    • 池上,你能告诉我这会是什么样子吗?抱歉,对于 Arduino 经验很少的人来说,这有点太模糊了。
    • 它看起来和我发布的非常相似。你只需要使用正确的语法。您已经知道如何读取和设置引脚,从而留下 foreach 循环。我什至不知道你的代码是什么语言,所以我不能告诉你如何在其中做一个 foreach 循环。读一读!
    • 这是 Arduino。 Arduino 语言只是一组可以从代码中调用的 C/C++ 函数。我只熟悉java。另外,“foreach”循环是什么意思?
    • 你好,池上。所以我得到了this,但是当我去运行程序时,我收到一条错误消息,指出“在'{'令牌之前需要不合格的ID”。你认为可能是什么问题?一如既往地感谢您的帮助。
    • Foreach 循环:在每个元素上循环一个列表的循环。如果您的语言没有,您可以将引脚编号放在一个数组中并循环遍历数组的索引。
    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多