我是Linux上的C ++编程新手。 我尝试使用旋转编码器来控制液晶显示屏上的值。 根据我使用的wirePi设置例程,只有中断或lcd工作,但不能同时进行。 我导出了所有nessersary GPIO,因此不需要sudo。
#Interrupt Pins Encoder
gpio edge 23 both
gpio edge 24 both
gpio mode 4 up
gpio mode 5 up
#Pins LCD
gpio export 17 out
gpio export 18 out
gpio export 27 out
gpio export 22 out
gpio export 8 out
gpio export 7 out
这里是C ++代码:
#include <wiringPi.h>
#include <lcd.h>
#include <stdio.h>
#define A 23
#define B 24
char a = 0;
char b = 0;
const char a1 = 1, a2 = 2, a3 = 3, b1 = 4, b2 = 5, b3 = 6;
char state = 0;
volatile int counter = 0;
volatile int counter_l = 0;
void interrupt();
int main(void)
{
printf("wiringPiSetup\n");
//wiringPiSetupSys(); //Interrupts working
wiringPiSetup(); //LCD working
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(27, OUTPUT);
pinMode(22, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
int fd = lcdInit(2, 16, 4, 11, 10, 0, 1, 2, 3, 0, 0, 0, 0);
lcdClear(fd);
lcdPosition(fd, 0, 0);
lcdPrintf(fd,"Test");
wiringPiISR(A, INT_EDGE_BOTH, &interrupt);
wiringPiISR(B, INT_EDGE_BOTH, &interrupt);
pinMode(A, INPUT);
pinMode(B, INPUT);
while (true)
{
if (counter != counter_l)
{
counter_l = counter;
printf("%i\n", counter);
lcdPosition(fd, 0, 0);
lcdPrintf(fd, "%i ", counter);
}
}
return 0;
}
void interrupt(){
a = !digitalRead(A);
b = !digitalRead(B);
switch (state)
{
case a1:
if (!a)
state = 0;
else if (b)
state = a2;
break;
case a2:
if (!a&&b)
state = a3;
else if (a&&!b)
state = a1;
break;
case a3:
if (a&&b)
state = a2;
else if (!a&&!b) {
state = 0;
counter++;
}
break;
case b1:
if (!b)
state = 0;
else if (a)
state = b2;
break;
case b2:
if (!b&&a)
state = b3;
else if (b&&!a)
state = b1;
break;
case b3:
if (b&&a)
state = b2;
else if (!b&&!a) {
state = 0;
counter--;
}
break;
default:
if (a&&b)
state = 0;
else if (a)
state = a1;
else if (b)
state = b1;
break;
}
}
现在问题是为什么它表现得像那样,我能做什么/应该做什么。
答案 0 :(得分:0)
我找到了与设置功能一起使用的不同编号的解决方案。 WirinpPi有自己的Pin编号,还有原生的BCM编号。解决方案是将lcdInit函数中的编号更改为BCM编号。
//wiringPi Nr.
pinMode(17, OUTPUT); //0
pinMode(18, OUTPUT); //1
pinMode(27, OUTPUT); //2
pinMode(22, OUTPUT); //3
pinMode(8, OUTPUT); //10
pinMode(7, OUTPUT); //11
//int fd = lcdInit(2, 16, 4, 11, 10, 0, 1, 2, 3, 0, 0, 0, 0); //wiringPi Nr.
int fd = lcdInit(2, 16, 4, 7, 8, 17, 18, 27, 22, 0, 0, 0, 0); //BCM Nr.