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, 以此類

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.