Hola buenas, necesito conectar un LCD 2x16 y un RTC1307 al arduino por I2C, en el Lcd ya dispongo de la interface I2C, es decir que solo uso Vcc, Gnd, SDA y SCL para controlar el LCD, mi problema es únicamente de software ya que los tengo conectados correctamente pero no se como sería el código para utilizar los dos por I2C, si alguien tiene algo de código con estos dispositivos o me pueda explicar como sería se lo agradecería enormemente.
Un saludo a todos.
y conectas los dos al mismo bus, y colocas las librerias+code para los dos dispositivos.
Naruto128:
y conectas los dos al mismo bus, y colocas las librerias+code para los dos dispositivos.
Sería mucho pedir si me puedes mostrar un código ejemplo?
system
April 4, 2014, 12:28pm
4
No está probado, pero sería algo como esto
#include <Wire.h> // Reloj
#include <SLCD.h> // Librería de la pantalla LCD
#define WIRE_BUS 0x68
SLCD serialLCD = SLCD( 2, 16 );
// Conversion de datos decimal a hexadecimal
byte decABcd(byte val) {
return ( (val/10*16) + (val%10) );
} // p.ej. 25 -> 37
byte bcdADec(byte val) {
return ( (val/16*10) + (val%16) );
} // p.ej. 37 -> 25
void ponReloj( byte segundo, // 0-59
byte minuto, // 0-59
byte hora, // 1-23
byte diaSem, // 1 Lunes-7 Domingo
byte dia, // 1-28/29/30/31
byte mes, // 1-12
byte anio) // 0-99
{
Wire.beginTransmission( WIRE_BUS );
Wire.send(0);
Wire.send(decABcd(segundo)); // 0 to bit 7 starts the clock
Wire.send(decABcd(minuto));
Wire.send(decABcd(hora));
Wire.send(decABcd(diaSem));
Wire.send(decABcd(dia));
Wire.send(decABcd(mes));
Wire.send(decABcd(anio));
Wire.send(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
// Toma la fecha y la hora del ds1307
void tomaHora( byte *segundo,
byte *minuto,
byte *hora,
byte *diaSem,
byte *dia,
byte *mes,
byte *anio)
{
// Resetea el registro puntero
Wire.beginTransmission( WIRE_BUS );
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom( WIRE_BUS, 7);
// Alguno de estos necesitan enmascarar porque ciertos bits son bits de control
*segundo = bcdADec(Wire.receive() & 0x7f);
*minuto = bcdADec(Wire.receive());
*hora = bcdADec(Wire.receive() & 0x3f); // Need to change this if 12 hora am/pm
*diaSem = bcdADec(Wire.receive());
*dia = bcdADec(Wire.receive());
*mes = bcdADec(Wire.receive());
*anio = bcdADec(Wire.receive());
}
void setup() {
Wire.begin();
serialLCD.init();
Serial.begin(9600);
// Si se quiere inicializar el reloj
ponReloj( 0, 0, 12, 5, 4, 4, 14 ) // 12:00 Viernes 4/4/2014
}
void loop() {
byte ss, mm, hh, dS, dd, nn, aa;
long tiempo;
tomaHora( &ss, &nn, &&hh, &dS, &dd, &mm, &aa );
Serial.print( aa, DEC );
Serial.print( "-" );
Serial.print( mm, DEC );
Serial.print( "-" );
Serial.print( dd, DEC );
Serial.print( " " );
Serial.print( &hh, DEC );
Serial.print( ":" );
Serial.println( &nn, DEC );
delay( 5000 ); // Actualiza la hora cada cinco segundos
}
Debería funcionar si no avisa
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup () {
Serial.begin(9600);
RTC.begin();
lcd.begin(16, 2);
//Retirar "//" para ajustar hora con PC
//RTC.adjust(DateTime(__DATE__, __TIME__));
}
void loop () {
DateTime now = RTC.now();
//Linea Uno
lcd.setCursor(0,0);
lcd.print("Hora: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
//Linea Dos
lcd.setCursor(0,1);
lcd.print("Fecha: ");
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
delay(1000);
}