RTC to 4Bit LCD

Hi All,

Im in need of some help as im tearing my hair out!!!!

I have a 2 wire RTC and a 4x16 4Bit LCD.

I have been trying to alter every single bit of serial.print source code i can find to allow me to show the time and date on my LCD.

Everything i try and everything i read just is not working. Ive been on this for about 5 days now and i just wish i had bought a serial LCD ( Newb Mistake!!! )

Here is what i have so far:

I have been trying to alter Maurice Ribble's code:

#include <LCD4Bit.h>
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
LCD4Bit lcd = LCD4Bit(2);

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// Stops the DS1307, but it has the side effect of setting seconds to 0
// Probably only want to use this for testing
/void stopDs1307()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(0x80);
Wire.endTransmission();
}
/

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}

void setup()
{
lcd.init();
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(9600);

// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 45;
minute = 15;
hour = 12;
dayOfWeek = 3;
dayOfMonth = 21;
month = 1;
year = 9;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day_of_week:");
Serial.println(dayOfWeek, DEC);

delay(1000);
}

Every time i try to alter any of the Serial.print to lcd.print i get compiler errors.

Please could someone take a look and help me alter this code from serial to show on my LCD. I swear im going bald and im only 29!!!! :o

VR

Could you post the exact wording of the compiler error message, please?

So here is where i try to later the sketch to output the time and date to my lcd:

lcd.clear();
lcd.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day_of_week:");
Serial.println(dayOfWeek, DEC);

And this is the compiler error im getting:

In function 'void loop()':
error: invalid conversion from 'const char*' to 'int

This is my first lcd and rtc project and its really hurting my head!

VR

I was able to compile the code in your first post without any compiler error. I did comment out the lcd calls - you may want to try doing that to see if the problem is in that code. I use the liquidCrystal library that is distributed with version 0012, any reason you aren't using that?

Ok so have i got my wires crossed here somewhere?

Im a newb and this is my first programming project so excuse my niaevety.

I have downloaded the 4 bit lcd library after following ladyada's post on lcds, that then allowed me to get my lcd using 4 bits instead of 8.

The above post does work fine when you compile it but all i can do is see the time and date on the serial monitor. I was trying to adjust the code to allow it to show on my lcd instead.

if im making some real newb errors i can understand. I got the rest of the things working on my LCD but then the time and date has been impossible!!!

Ok so now i have just gone to anrduino on my laptop and opened up the "hello world" from the liquidcrystal lib and it wont compile??? same with any of the other example sketches from the liquid lib??

what am i doing wrong? The libs are stored on the arduino/hardware/library files on my hard drive???????

The compiler error reads:

error: no matching function for call to 'LiquidCrystal::LiquidCrystal(int, int, int, int, int, int, int)'hardware\libraries\LiquidCrystal/LiquidCrystal.h:8: note: candidates are: LiquidCrystal::LiquidCrystal()

hardware\libraries\LiquidCrystal/LiquidCrystal.h:6: note: LiquidCrystal::LiquidCrystal(const LiquidCrystal&)

In function 'void setup()':

OK, that error message is what you would get if you supply a string (in quotes) when the function is expecting an integer (a whole number). It probably refers to your line 'lcd.print(":");'. Now, maybe the member function 'print()' of the object 'lcd' simply doesn't support character strings for some reason? And you need to use a different member function? Is there a 'println()' function? The example code from the Playground seems to use 'printIn' (with an 'I'):

#include <LCD4Bit.h> 
LCD4Bit lcd = LCD4Bit(1);   //create a 1-line display.
lcd.clear();
delay(1000);
lcd.printIn("arduino");

Is that example correct?

i have no problem writing text to my display in my main program i have written. I just cant seem to interface the I2C RTC to it.

I cant load up any of the liquidCrystal examples without the compiler coming up with an error message.

The LiquidCrystal library and the LCD4Bit libraries are different. If you have the LCD4Bit example working then stick with that library for now.

Make sure that the lcd code in the i2c sketch is the same as the working lcd example. Add a line in your setup function to print a string to the lcd - you can copy the lcd print code from the example sketch that works.

I've never used the LCD library that you're using, but I have been working with the Ds1307 code for the last couple of days.

Try casting the bytes to int before printing them:

Serial.print(int(minute), DEC);
Serial.print(":");

I didn't get compile errors from this, but I did find that Serial.print() would print unreadable characters without the cast to int...

Might help.
Tim

I tried that Tim and the compiler errors with:

29: error: macro "int" passed 2 arguments, but takes just 1 In function 'void loop()':

:frowning:

Reefer, which lcd library are you now using?

when i first started the thread i was using the LCD4Bit library after setting up my lcd as per:

http://www.ladyada.net/learn/arduino/lcd.html

I am now trying to get the 'Hello World' and 'Serialdisplay' examples to work using a 4bit lcd lib.

Am i just chasing my own tail here or can the above code be used in 4bit mode?

29: error: macro "int" passed 2 arguments, but takes just 1 In function 'void loop()':

Did you perhaps mistype this line:

Serial.print(int(minute), DEC);

That's the only explanation I can come up with for the error message about 2 parameters to int.

M

I think it will be easer to get your code running if you use the LiquidCrystal library that comes with the Arduino 0012. lcd4bit does not print numbers, you can add code to do that but because LiquidCrystal prints numbers exactly the same as Serial, its much easer to get working serial code functioning on the lcd.

To get one of the LiquidCrystal examples to run with your lcd panel, you need to make sure that the library is initialized with the correct pins.

Modify the example sketch so the pins number match your wiring:
LiquidCrystal(rs, r/w, enable, d4, d5, d6, d7)
See the the ladyada page you linked above to see which arduino pins are connected to the lcd pins

The LiquidCrystal hello world example sketch is wired as follows:

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// lcd d4, d5, d6, d7 on pins 5, 4, 3, 2

Note that the comment in the example sketch is wrong - the data lines should be d4 through d7, not d0 through d3

When you have the hello world working , add the following lines to setup:
byte minute = 30;
lcd.print(minute,DEC);

If you see the digits 30 after hello world, you are almost there. Modify the sketch in your first post so it includes LiquidCrystal instead of lcd. Remove the all the old references to lcd and replace them with the lcd code from the LiquidCrystal example. The add lcd.print statements as needed, following the model you used in the Serial.print statements.

Good luck!

Hi MEM,

I got the lib sorted and the hello world. now im just trying to map out which cusror position is which on my lcd and its driving me crazy.

i found this from your reply on another thread:

void LiquidCrystal::setCursor(int col, int row)
{
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
command(0x80 | (col + row_offsets[row]));
}

except it wont compile. Has anyone mapped out a 4x16 lcd before?

The worse thing is everytime i try and think of a nogical next step to find the cursor position it puts it somewhere different????????????????????????????????????????????????????????????????

Won't compile? If you have Arduino 0012 and this won't compile, there is something else wrong and it has nothing to do with your LCD. What's the error message?

Mikal

Oops sorry let me be abit clearer.

I got the RTC working and displaying on my LCD ( Thanks so much ).

No i want to map out the cursor positions on my lcd but everything i try just puts the cursor in random places.

here is my sample code:

#include <LiquidCrystal.h>

// LiquidCrystal display with:
// rs on pin 12
// rw on pin 11
// enable on pin 10
// d0, d1, d2, d3 on pins 5, 4, 3, 2
LiquidCrystal lcd(11, 13, 12, 7, 8, 9, 10);

void setup()
{
// Print a message to the LCD.
lcd.clear();
lcd.setCursor(54,5);
lcd.print("XX");
}

void loop()
{
}
now i expect it to go to row 4, col 5. But it places it row 2, col 1???

am i missing something here???

Am I missing something?

  1. Rows and cols are numbered beginning with 0.
  2. It's setCursor(column first then row)
  3. 54?

:slight_smile:

so i just put in:

lcd.clear();
lcd.setCursor(01,15);
lcd.print("XX");
}

and it places my cursor on row 4, col 6

any ideas why its not placing it on row 1, col 5????

If you want row 1, col 5, do

lcd.setCursor(4, 0);

not

lcd.setCursor(01, 15);