1. 程式人生 > >IIC/I2C/介面LCD1602轉接板使用心得

IIC/I2C/介面LCD1602轉接板使用心得

首先下載庫檔案

https://pan.baidu.com/s/1zLYSlDuxTMvxjclyk94hNg

 

注意:如果是從淘寶店下載的庫檔案有可能出現只顯示一個首字元的現象,此時需要修改 LiquidCrystal_I2C.cpp 檔案中的一個語句就可以了,更改如下:

inline size_t LiquidCrystal_I2C::write(uint8_t value) {

send(value, Rs);

return 0; 改為 return 1;

}

 

 

按如下方式接線

 

下面是地址檢測程式碼

#include <Wire.h>

void setup()

{

  Wire.begin();

  Serial.begin(9600);

  Serial.println("\nI2C Scanner");

}

void loop()

{

  byte error, address;

  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;

  for(address = 1; address < 127; address++ ) 

  {

    // The i2c_scanner uses the return value of

    // the Write.endTransmisstion to see if

    // a device did acknowledge to the address.

    Wire.beginTransmission(address);

    error = Wire.endTransmission();

    if (error == 0)

    {

      Serial.print("I2C device found at address 0x");

      if (address<16) 

        Serial.print("0");

      Serial.print(address,HEX);

      Serial.println("  !");

      nDevices++;

    }

    else if (error==4) 

    {

      Serial.print("Unknow error at address 0x");

      if (address<16) 

        Serial.print("0");

      Serial.println(address,HEX);

    }    

  }

  if (nDevices == 0)

    Serial.println("No I2C devices found\n");

  else

    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan

}

 

編譯執行後,找出模組的起始地址。

 

下面是顯示字元的程式碼:

 

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,4); // 此處0x27改成剛才檢測到的地址即可。

void setup()

{

lcd.init();  // initialize the lcd 

lcd.backlight();

lcd.setCursor(0, 0); 

lcd.setCursor(0, 0); 

lcd.print("Success!");

lcd.setCursor(0, 1); 

lcd.print("Hello, world!");

}

void loop()

{}