Hey Guys
I need a little guidance with my current project that uses an RTC to hold the time and display it onto a LCD.
I have compiled the code fairly well it seems but my only error comes across as placing 5's after each unit of time. (see attachment)
Here is the code I used to upload the time to the RTC
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
void setDateDs1307(
byte second, //0-59
byte minute, //0-59
byte hour, //1-23
byte dayOfWeek, //1-7 1=Mon, 7=Sun
byte dayOfMonth, //1-28/29/30/31
byte month, //1-12
byte year //0-99
)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
pinMode(13, OUTPUT);
second = 30;
minute = 10;
hour = 16;
dayOfWeek = 3;
dayOfMonth = 4;
month = 1;
year = 17;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
And here is the code I used to display the time
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define DS1307_I2C_ADDRESS 0x68
#define I2C_ADDR 0x3F
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
void getDateDs1307(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
lcd.begin(16, 2);
}
void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
String s, m, d, mth, h;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
if (second < 10) { s = "0" + String(second); } else { s = String(second); }
if (minute < 10) { m = "0" + String(minute); } else { m = String(minute); }
h = String(hour);
if (dayOfMonth < 10) { d = "0" + String(dayOfMonth); } else { d = String(dayOfMonth); }
if (month < 10) { mth = "0" + String(month); } else { mth = String(month); }
char* days[] = { "NA", "Mon", "Tue", "Wed", "Thu", "Wed", "Thu", "Fri", "Sat", "Sun"};
lcd.clear();
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.setCursor(4,0);
lcd.print(h + ":" + m + ":" + s);
lcd.setCursor(1,1);
lcd.print(String(days[dayOfWeek]) + " " + d + "/" + mth + "/20" + year);
delay(1000);
}
Any help is much appreciated and Happy New Year!