joi, 9 mai 2013

Interfata i2c la LCD pentru Arduino

    Deoarece am incarcat de pe mai multe calculatoare programioarele (sketch-urile) pentru Arduino, am avut surpriza neplacuta sa constat ca uneori apar probele cu librariile si mai ales cu porturile, adresele... la interfata i2c dintre Arduino si afisajul LCD cu 165 coloane si 2 randuri, pe care o folosisem la afisarea temperaturii si umiditatii pe un LCD cu 16 coloane si 2 randuri (16x2), vezi Ministatie meteo cu senzorul DHT11 si.. Arduino.

   Conectarea e simpla:

   Am cautat ceva informatii si am gasit pe forumul Arduino o discutie despre aceasta problema, care facea printre altele trimitere la un sketch numit "i2c scanner":



// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// 
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//

#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
}


   Trebuie sa descarcam o librarie pentru afisaje LCD care include si acest protocol de comunicare, pe care o gasim la https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads.
   Pe site-ul Arduino, se recomanda descarcarea librariei de la http://hmario.home.xs4all.nl/arduino/LiquidCrystal_I2C/
apoi am folosit primul sketch de la http://blog.gotencool.com/2012/03/arduino-lcd-via-i2c.html pe care-l mai testasem:


#include <Wire.h>
  
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // 0x27 es la direccion del LCD 16x2
void setup(){
    lcd.init();
    lcd.backlight(); //enciende la iluminacion
    lcd.setCursor(0, 0);
    lcd.print("Probando i2c");
}
void loop(){
    delay(1000);
}

am schimbat adresa in 0x20 din 0x27 si apoi am inceput sa ma joc cu sketch-ul:


#include <Wire.h>
  
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2); // 0x20 is adresss for LCC 16x2
void setup(){
   lcd.init(); 
  lcd.backlight(); //backlight is now ON
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a logo message to the LCD.
  lcd.print("www.tehnic.go.ro");  
  lcd.setCursor(0, 1);
  lcd.print("creat de niq_ro");
  delay (2500);
  lcd.clear();
   
  // Print a permanent message to the LCD.
  lcd.print(" teste interfata");
  lcd.setCursor(0, 1);
  lcd.print("i2c pt. LCD 16x2");
  delay (2500);
}
void loop(){
    delay(1000);
}


9.iul.2013
   Conectand placuta Arduino impreuna cu afisajul LCD si interfata i2c pe un calculator nou cu Windows 7, am constatat ca la programul de test nu se aprinde afisajul, asa ca am rulat si programiorul de verificare a adresei.. am (re)constatat ca adresa interfetei i2c pentru LCD1602 este 0x20...

Niciun comentariu:

Trimiteți un comentariu