Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino 如何观察最低有效位的变化?_Arduino - Fatal编程技术网

Arduino 如何观察最低有效位的变化?

Arduino 如何观察最低有效位的变化?,arduino,Arduino,我正在与Arduino合作,并开始与端口寄存器合作。我喜欢速度的提高和同时更换多个端口的能力。然而,我不知道如何使用端口寄存器观察单个管脚的变化。(我想可以用它来做,但我甚至不知道如何开始。) 因此,当我检查端口寄存器时,我应该得到如下结果: PINB = B000xxxxx 其中x是我的pin值。这些引脚中的任何一个都可能发生变化。我想知道最右边(最不重要?)的位何时发生了变化。如何使用bitmath检查最后一个是否已从0切换到1?“bitmath”确实是问题的答案。在您的情况下:x&0x0

我正在与Arduino合作,并开始与端口寄存器合作。我喜欢速度的提高和同时更换多个端口的能力。然而,我不知道如何使用端口寄存器观察单个管脚的变化。(我想可以用它来做,但我甚至不知道如何开始。)

因此,当我检查端口寄存器时,我应该得到如下结果:

PINB = B000xxxxx
其中
x
是我的pin值。这些引脚中的任何一个都可能发生变化。我想知道最右边(最不重要?)的位何时发生了变化。如何使用bitmath检查最后一个是否已从
0
切换到
1

“bitmath”确实是问题的答案。在您的情况下:
x&0x01
将“屏蔽”除最低位以外的所有位。根据您的意愿,可以将结果与
0
1
进行比较

常见的习语有:

x & 0x01    // get only the lowest bit
x & ~0x01   // clear only the lowest bit
x & 0xFE    // same: clear only the lowest bit
x | 0x01    // set the lowest bit (others keep their state)

要找出位是否已更改,您需要上一个值,正如其他人所说,您可以将其掩盖--

然后在代码中执行

int currentValue = PINB & 0x01;
获取当前引脚值的LSB

要确定要使用“异或”(^)运算符的位是否发生了更改,当且仅当两个位不同时才为“真”

if (lastValue ^ currentValue) {
  // Code to execute goes here

  // Now save "last" as "current" so you can detect the next change
  lastValue = currentValue;
}

if(value&1){…}else{…}
Yes,直到需要边缘检测。在这种情况下,你需要一个(或两个)相同条件下的while循环!谢谢你的例子。第一个“清晰”的例子是你想要坚持的。第二个示例(x&0xFE)是一个仅8位的示例——如果有任何更高的位,这些位也将被清除。
if (lastValue ^ currentValue) {
  // Code to execute goes here

  // Now save "last" as "current" so you can detect the next change
  lastValue = currentValue;
}