单片机实现74HC148优先编码器功能的程序
发布网友
发布时间:2022-05-16 19:21
我来回答
共1个回答
热心网友
时间:2024-02-29 17:57
//在arudino 中调试通过
//将A0,A1,A2,接入IO 4,7,8
//如数据不稳定可以将digitalRead改为digitalReadt程序消抖进行对比。
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //电脑反馈运行状态变量值
#define HC148A0 8
pinMode (4, INPUT);
pinMode (7, INPUT);
pinMode (8, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int a = 0;
Serial.print(" A2 ");
Serial.print("A1");
Serial.print(" A0 ");
int n8 = digitalRead(8);
delay(2);
int n7 = digitalRead(7);
delay(2);
int n4 = digitalRead(4);
bitWrite(a, 0, n4);
bitWrite(a, 1, n7);
bitWrite(a, 2, n8);
Serial.print(n8, BIN); Serial.print(" "); Serial.print(n7, BIN); Serial.print(" "); Serial.print(n4, BIN);Serial.print(" = "); Serial.println(a);
// Serial.println("<------------------------------->");
delay(100);
}
bool digitalReadt(int testpin) {
bool digval = digitalRead(testpin); //消抖
delay(5);
while (digval != digitalRead(testpin)) {
delay(5);
digval = digitalRead(testpin);
delay(5);
}
return digval;
}