在用 C 编写 Arduino 时,我们如何将两个字符并排写入?

When programming an Arduino in C, how do we write two characters next to each other?

我的 arduino 有以下代码,但是 adafruit 液晶显示器只打印向下箭头字符,而不是向上箭头然后是向下箭头。 (循环函数是空的,所以我没有包含它)。

#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

#define UP_ARROW 0
byte up[] = {4, 14, 31, 4, 4, 4, 0, 0};
#define DOWN_ARROW 1
byte down[] = {0, 0, 4, 4, 4, 31, 14, 4};

void setup() {
  lcd.clear();
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.createChar(UP_ARROW, up);
  lcd.write(UP_ARROW);
  lcd.setCursor(1,0);
  lcd.createChar(DOWN_ARROW, down);
  lcd.write(DOWN_ARROW);
}

根据您图书馆的源代码,

void Adafruit_RGBLCDShield::createChar(uint8_t location, uint8_t charmap[]) {
  location &= 0x7; // we only have 8 locations 0-7
  command(LCD_SETCGRAMADDR | (location << 3));
  for (int i=0; i<8; i++) {
    write(charmap[i]);
  }
  command(LCD_SETDDRAMADDR);  // unfortunately resets the location to 0,0
}

如果我是你,我会在程序的开头创建字符,然后定位光标并根据需要编写它们。

来自Arduino reference

NB : When referencing custom character "0", if it is not in a variable, you need to cast it as a byte, otherwise the compiler throws an error. See the example below.

在您的示例中,UP_ARROW 将替换为 0 in lcd.write(UP_ARROW);

也许试试:lcd.write(byte(UP_ARROW));

希望对您有所帮助。

原来我的问题是创建一个字符将光标位置重置为 (0,0),所以当我将光标设置为 (0,1) 并创建向下箭头时,它会将光标重置为 (0 ,0).我的解决方案是先创建自定义字符,然后设置光标并写入它们。