简体   繁体   English

带LCD的Arduino UNO,旋转编码器的读数异常

[英]Arduino UNO with LCD, strange readings from rotary encoder

I have an I2C 16x2 LCD display connected to an Arduino Uno's A4 (SDA) and A5 (SCL) pins. 我有一个I2C 16x2 LCD显示器连接到Arduino Uno的A4 (SDA)A5 (SCL)引脚。 No problem with the display, it works properly. 显示器没问题,它可以正常工作。

Then I have a rotary encoder connected to pins D3 (INT1) and D4 . 然后,我有一个旋转编码器连接到引脚D3 (INT1)D4 The INT1 pin is used as interrupt to read the encoder, and the reading is sent via Serial.print() to the Serial monitor. INT1引脚用作读取编码器的中断,并且读数通过Serial.print()发送到串行监视器。 There are debounce CAPs connected to the rotary encoder. 有防抖CAP连接到旋转编码器。 The encoder pins use the Arduino's internal pullups. 编码器引脚使用Arduino的内部上拉电阻。

The interrupt is attached to read encoderPinA when encoderPinB is falling from HIGH to LOW . encoderPinBHIGH下降到LOW时,该中断附加到读取的encoderPinA When turning the rotary clockwise, encoderPinA is LOW , and when turning it counter-clockwise, encoderPinA is HIGH . 顺时针旋转, encoderPinALOW ,逆时针旋转, encoderPinAHIGH

Now, when there is nothing in the main loop , I get ++++++++++ signs on the serial monitor when turning the rotary clockwise, and ---------- signs when turning it counter-clockwise, as I should. 现在,当主loop没有任何内容时,顺时针旋转旋钮时,串行监视器上会显示++++++++++标志,而逆时针旋转时会显示----------标志顺时针方向,如我所愿。

But if I uncomment those two lines that print to the LCD, I start to get erratic readings from the rotary encoder, like this: -++-++-++-+++-++-+++-++--+ . 但是,如果我取消注释打印到LCD的那两行,则开始从旋转编码器中获得不稳定的读数,例如: -++-++-++-+++-++-+++-++--+

What's going on? 这是怎么回事? Is the LCD interfering with interrupt pins? LCD是否干扰中断引脚?

#define encoderPinA 4
#define encoderPinB 3

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);
  attachInterrupt (digitalPinToInterrupt(encoderPinB), readEncoder, FALLING);
}

void loop() {
  //lcd.setCursor(0, 0);
  //lcd.print("test");
}

void readEncoder() {
  if (digitalRead(encoderPinA) == LOW) Serial.print("+");
  else Serial.print("-");
}

Sorry folks, this problem was clearly a cable related issue. 抱歉,这个问题显然是电缆相关的问题。

I was using the same non-shielded flat cable to carry out rotary and lcd signals, and there was some interference, because when I switch to separate cables, the erratic behaviour is gone. 我使用同一根非屏蔽扁平电缆来传输旋转信号和LCD信号,并且存在一些干扰,因为当我切换到单独的电缆时,不稳定的行为消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM