I am using an Arduino Uno R3, DS3234 RTC and TFT display (KMR-1.8 SPI). The schematic looks like this.
I have tested the RTC and TFT using separate sketches and they work fine.
DS3234 code:
#define rtc_cs 10
#include <SPI.h>
#include <RtcDS3234.h>
RtcDS3234<SPIClass> Rtc(SPI, rtc_cs);
void setup ()
{
Serial.begin(9600);
SPI.begin();
Rtc.Begin();
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
delay(1000);
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Day() ,
dt.Month() ,
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
I used an elaborate sketch initially to set the RTC date and time and later shortened it to the above format to just initiate the RTC (with battery) and get the time.
TFT Code:
#include <SPI.h>
#include <TFT.h>
#define tft_cs 6
#define rst 8
#define dc 9
TFT myscreen = TFT(tft_cs,dc,rst);
void setup()
{
myscreen.begin();
myscreen.setRotation(1);
myscreen.background(0,0,0);
myscreen.stroke(0,255,0);
myscreen.fill(0,0,225);
myscreen.rect(3,5,150,18);
myscreen.setTextSize(1);
myscreen.text("Sample Text",10,10);
}
void loop() {
myscreen.stroke(255, 255, 255);
myscreen.text("RTC Time:", 10, 30);
delay(1000);
myscreen.stroke(0, 0, 0);
myscreen.text("RTC Time:", 10, 30);
}
I tried combining both the sketches to project the RTC date and time in the TFT display. The RTC values still appear in the serial monitor using the combined sketch but the TFT projects unrecognizable characters in the place of date and time.
RTC+TFT code:
#include <SPI.h>
#include <RtcDS3234.h>
#include <TFT.h>
#define rtc_cs 10
#define tft_cs 6
#define rst 8
#define dc 9
RtcDS3234<SPIClass> Rtc(SPI, rtc_cs);
TFT myscreen = TFT(tft_cs,dc,rst);
void setup()
{
Serial.begin(9600);
SPI.begin;
Rtc.Begin();
myscreen.begin();
myscreen.setRotation(1);
myscreen.background(0,0,0);
myscreen.stroke(0,255,0);
myscreen.fill(0,0,225);
myscreen.rect(3,5,150,18);
myscreen.setTextSize(1);
myscreen.text("Sample Text",10,10);
}
void loop() {
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
myscreen.stroke(255, 255, 255);
myscreen.text(now, 10, 30);
delay(1000);
myscreen.stroke(0, 0, 0);
myscreen.text(now, 10, 30);
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Day(),
dt.Month(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
PS: I am new to Arduino and programming. Kindly bear with me if there is a blatant mistake.