2014年10月25日 星期六

LCD I2C模組使用.

http://www.geeetech.com/wiki/index.php/Serial_I2C_1602_16%C3%972_Character_LCD_Module
1602I2C 1.jpg 1602I2C 2.jpg
This is another great blue/yellow backlight LCD display. As the pin resources of Arduino controller is limited, your project may be not able to use normal LCD shield after connected with a certain quantity of sensors or SD card. However, with this I2C interface LCD module, you will be able to realize data display via only 2 wires. If you already has I2C devices in your project, this LCD module actually cost no more resources at all. It is fantastic for Arduino based project.

  • Interface: I2C
  • I2C Address: 0x27
  • Pin Definition : VCC、GND、SDA、SCL
  • Back lit (Green with white char color)
  • Supply voltage: 5V
  • Size : 27.7mm×42.6mm
  • Contrast Adjust : Through Potentiometer
  • Only employ two I/O interface
Here is pic shows how to connect an Arduino 1602 I2C module.The following is a table describing which pins on the Arduino should be connected to 1602 I2C LCD module.
1602I2C 4.jpg
1602I2C table.jpg
1602I2C 3.jpg
Spinnig the potentimeter clockwise is to increase contrast ,spinning unclockwise is to decrease it

Arduino 1602 I2C library only Arduino IDE 023!
Arduino 1602 I2C library for Arduino IDE 1.0   http://www.geeetech.com/Documents/LiquidCrystal_I2Cv1-1.rar
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
 lcd.init();                      // initialize the lcd 
 // Print a message to the LCD.
 lcd.backlight();
 lcd.print("Hello, world!");
}
void loop()
{
}


=========================================================
來源: 




由於最後我們的作品希望可以展示時間和各種需要顯示的狀態,因此決定安裝一塊LCD模組,不過剛剛有提到過,我們有許多各種需要顯示的東西,因此表示我們的Arduino UNO已經沒有辦法再提供豐富的腳位給我們的LCD使用。



在Cooper Maa大大中的一篇文中"Arduino 筆記 – Lab9 在 2x16 LCD 上顯示 "Hello World" 訊息",我們可以知道即使使用了最少IO port的做法,也得要有四條data線、RS暫存器選擇線和E致能線,加上電源兩條,總共就得消耗八條線路,其中六條還是用去了digital,以UNO來說,幾乎用去了大半,這樣一來我可能得再購買一塊arduino分別控制LCD和其他裝置,這是相當不方便的,不僅要花錢還得另外進行兩塊UNO的連線,否則就是要購買mega來使用。

(內心:...........但是我沒這麼多錢,而且我懶.............我就是只有一塊UNO,究竟有沒有辦法不要佔去這麼多digital腳位,又可以讓我使用需要很多腳位的LCD呢?)



碰巧被我找到這麼一塊好東西,這樣一來我連一根digital腳位都沒有消耗,搭配RTC模組使用,甚至可以說,我一根腳位都沒有浪費的使用,那就是I2C模組拉QWQ!!!!!(QWQ,這是一種網路表情符號,表示很感動的意思。)


本篇最一開始就是LCD I2C模組的圖片
http://www.geeetech.com/wiki/index.php/Serial_I2C_1602_16%C3%972_Character_LCD_Module


不過外觀看起來和一般的LCD沒什麼太多的不同,唯一有不同的就是多插了一塊小IC晶片PCF8574,他是I2C的8bits擴充IC,利用他就可以將Arduino的腳位擴充,或許將來的專題我可以使用它來做許多多腳位的裝置。

PCF8574的data sheet
http://www.datasheetcatalog.org/datasheet2/b/0fjjhr6h643gldhx3o1rgkyk7ucy.pdf

關於PCF8574有個專業的教學網站在說明這塊IC怎麼使用
http://garagelab.com/profiles/blogs/tutorial-arduino-i-o-port-expander-with-pcf8574
他還提到怎麼應用在一般的LCD上面,使一般的LCD也變成I2C的LCD模組
http://garagelab.com/profiles/blogs/tutorial-lcd-using-only-2-arduino-pins-with-pcf8574-and-i2c


接下來這邊來說說實作方式


在剛剛的網站裡面有下載檔案,我將下載網址連過來。
http://www.geeetech.com/wiki/index.php/Serial_I2C_1602_16%C3%972_Character_LCD_Module

總共分成0023和1.0版本可以使用
Arduino IDE 023
Arduino IDE 1.0


開啟之後可以稍微看看範例檔案,下列的程式我有稍微修改過。

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
// 0x27是LCD模組的地址,16表示有十六欄位,2表示總共有2欄
LiquidCrystal_I2C lcd(0x27,16,2);  
void setup()
{
    //LCD初始化
    lcd.init();   
    lcd.init();
    //開啟背光
    lcd.backlight();
    //清除先前畫面
    lcd.clear();
    //設定第一個字顯示座標
    lcd.setCursor(0,0);
    //顯示字串
    lcd.print("Hello, world!");
}
void loop()
{
}

在導入.h檔和定義好LCD I2C之後,在setup那邊需要初始化LCD和開啟背板光源,之後只要設定座標和使用lcd.print即可顯示我們想要顯示的資料,用起來實在是相當的方便,以後只要Serial.print可以顯示,LCD上面一樣可以顯示。

舉例來說,我將Arduino的第一個基礎範例analogReadSerial來進行修改。

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
  Serial.begin(9600);
  lcd.init();   // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.clear();
}

void loop() {
  int sensorValue = analogRead(A0);
  lcd.setCursor(0,0);
  lcd.println(sensorValue, DEC);
}

我將該設定的部分設定好之後,把Serial.println換成lcd.print即可顯示。

這東西實在是太方便了~~~~

不過雖然Arduino的模組們用起來很方便,但是等到會用了之後,還是建議大家回到最初使用LCD的方式玩看看,才能夠真正的了解為什麼可以用兩隻腳就可以操控LCD是一件多麼讓人感動的事情,而且也可以同時了解四位元和八位元的意義是什麼,這樣回過頭來去尋找源頭的過程中,就像是考古學家一樣,會發現許多前人留下的有趣故事唷。

沒有留言:

張貼留言