I2C - 使用 LCD 并在 LCD 上显示资料

材料准备

  • AmebaD [AMB21 / AMB22 / AMB23 / BW16] x 1

  • I2C 2×16 LCD

范例准备

一般LCD有许多pin脚, 以手上这片LCD为例有16个pin

../../../../_images/image154.png

直接控制LCD的话会用掉许多pin, 在资料处理上也很麻烦, 所以一般LCD会有额外的控制晶片帮忙处理, 并且使用I2C介面控制这块控制晶片。

AMB21 / AMB22 接线图:

../../../../_images/image249.png

AMB23 接线图:

../../../../_images/image2-14.png

BW16 接线图:

../../../../_images/image2-32.png

BW16-TypeC Wiring Diagram:

../../../../_images/image2-41.png

Open the example in “File” “Examples” “AmebaWire” “LCD_HelloWorld”. Compile and upload to Ameba, then press the reset button. Then you can see “Hello World” in the first line, and “Ameba” in the second line displayed on the LCD screen.

../../../../_images/image31.jpeg

等8秒之后出现提示可以从Serial Monitor输入要显示的字串:

../../../../_images/image41.jpeg

在Serial Monitor 输入“123456789” 之后按下“Send”, 显示在LCD上:

../../../../_images/image53.jpeg

程式码说明

每款LCD的I2C控制板需要设定的内容都不一样,范例里使用的constructor:

LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs,
                  uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,
                  uint8_t backlighPin, t_backlighPol pol);

设定的内容如下:

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

其中I2C的address是0x27, 后面八个参数代表1个byte里, 每个bit代表的意义, 其中En为Bit 2, Rw为Bit 1, Rs为Bit 0, d4为bit 4, 以此类推每款LCD需要设定的内容可能不一样, 要参考datasheet做设定。

Call backlight() to light the screen, Call setCursor(0, 0) to set the position of the cursor. LCD inherits the Print class, so we can use lcd.print() to output string on the screen.