请帮我。我正在尝试通过 SPI 将 74HC165(8 位输入移位寄存器)连接到 Arduino UNO 以检查 8 个按钮的状态。响应将被处理以更改变量“控件”,但它无法正常工作。
这是代码:
#include <SPI.h>
/* Latch contact */
enum { REG_LATCH = 8 };
int speed = 100;
/* Variable to store the Controls state for further transfer */
static uint8_t controls = 0;
void setup()
{
/* Turn ON the UART */
Serial.begin(9600);
/* Turn On the SPI */
SPI.begin();
pinMode(REG_LATCH, OUTPUT);
digitalWrite(REG_LATCH, HIGH);
}
void loop()
{
/* Storing the previous system state */
static uint8_t previous_input = 00000000;
digitalWrite(REG_LATCH, LOW);
digitalWrite(REG_LATCH, HIGH);
/* Read the inputs from shift register */
uint8_t input = SPI.transfer(0);
/* If anything has ben changed - report to UART */
if (input != previous_input)
{
/* Remebmer changed positions */
uint8_t changed = input ^ previous_input;
/* Remember current state */
previous_input = input;
Serial.println("Buttons State:\t\tControls State:");
/* Run trough all inputs from shift register */
for (int i = 0; i < 8; i++)
{
/* Print the state of currently checked input*/
Serial.print(input & 1);
/* If button is pressed and previously it was not, then */
if ((input & 1) & (changed & 1))
{
/* Highlight the current changes in system */
Serial.print("_");
/* Toggle controls in this position */
controls = controls ^ (1 << i);
}; /*Otherwise do nothing*/
/* Move to next bit of inputs. */
input >>= 1;
changed >>= 1;
};
Serial.print("\t\t");
for (int i = 0; i < 8; i++)
{
/* Print current control */
Serial.print(controls);
controls >>= 1;
}
Serial.println();
}
}
以及它写入串行的内容:
1) 按下按钮 1 后:
Buttons State: Controls State:
1_0000000 10000000
2) 发布时:
Buttons State: Controls State:
00000000 00000000
3) 按下按钮 7 后:
Buttons State: Controls State:
0000001_0 64321684210
4) 发布后:
Buttons State: Controls State:
00000000 00000000
5) 按下按钮 1 后:
Buttons State: Controls State:
1_0000000 10000000
6) 发布时:
Buttons State: Controls State:
00000000 00000000
它应该如何工作:
1) 按下按钮 1 后:
Buttons State: Controls State:
1_0000000 10000000
2) 发布后:
Buttons State: Controls State:
00000000 10000000
3) 按下按钮 7 后:
Buttons State: Controls State:
0000001_0 10000010
4) 发布后
Buttons State: Controls State:
00000000 10000010
5) 下一次按下按钮 1 后:
Buttons State: Controls State:
1_0000000 00000010
6) 发布后:
Buttons State: Controls State:
00000000 00000010
请帮帮我。我究竟做错了什么?