Hi,
I am struggling to get the time displayed on TFT.
3.3 -> + of DS3231
GND -> - of DS3231
SDA -> D of DS3231
SLC -> C of DS3231
TFT shield on top of UNO [ soldered ectra header pins to connect DS3231
Result - The screen is White
Problem area -
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
}
If the above is commented, the TFT doisplays the patterns/designs.
Note: I am writing to serial monitor for testing, but final version is to write on TFT.
Can anyone help sort this issue. thanks
Full Code
// All the mcufriend.com UNO shields have the same pinout.
// i.e. control pins A0-A4. Data D2-D9. microSD D10-D13.
// Touchscreens are normally A1, A2, D7, D6 but the order varies
//
// This demo should work with most Adafruit TFT libraries
// If you are not using a shield, use a full Adafruit constructor()
// e.g. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <SPI.h> // f.k. for Arduino-1.5.2
#include "Adafruit_GFX.h"// Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
//#include <Adafruit_TFTLCD.h>
//Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
void setup(void);
void loop(void);
unsigned long testFillScreen();
unsigned long testText();
unsigned long testLines(uint16_t color);
unsigned long testFastLines(uint16_t color1, uint16_t color2);
void progmemPrint(const char *str);
void progmemPrintln(const char *str);
uint16_t g_identifier;
extern const uint8_t hanzi[];
void showhanzi(unsigned int x, unsigned int y, unsigned char index)
{
uint8_t i, j, c, first = 1;
uint8_t *temp = (uint8_t*)hanzi;
uint16_t color;
tft.setAddrWindow(x, y, x + 31, y + 31); //设置区域
temp += index * 128;
for (j = 0; j < 128; j++)
{
c = pgm_read_byte(temp);
for (i = 0; i < 8; i++)
{
if ((c & (1 << i)) != 0)
{
color = RED;
}
else
{
color = BLACK;
}
tft.pushColors(&color, 1, first);
first = 0;
}
temp++;
}
}
void setup(void) {
Serial.begin(9600);
uint32_t when = millis();
// while (!Serial) ; //hangs a Leonardo until you connect a Serial
if (!Serial) delay(5000); //allow some time for Leonardo
Serial.println("Serial took " + String((millis() - when)) + "ms to start");
// tft.reset(); //hardware reset
uint16_t ID = tft.readID(); //
Serial.print("ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9481; // write-only shield
// ID = 0x9329; // force ID
tft.begin(ID);
}
#if defined(MCUFRIEND_KBV_H_)
uint16_t scrollbuf[320]; // my biggest screen is 320x480
#define READGRAM(x, y, buf, w, h) tft.readGRAM(x, y, buf, w, h)
#else
uint16_t scrollbuf[320]; // Adafruit only does 240x320
// Adafruit can read a block by one pixel at a time
int16_t READGRAM(int16_t x, int16_t y, uint16_t *block, int16_t w, int16_t h)
{
uint16_t *p;
for (int row = 0; row < h; row++) {
p = block + row * w;
for (int col = 0; col < w; col++) {
*p++ = tft.readPixel(x + col, y + row);
}
}
}
#endif
void windowScroll(int16_t x, int16_t y, int16_t wid, int16_t ht, int16_t dx, int16_t dy, uint16_t *buf)
{
if (dx) for (int16_t row = 0; row < ht; row++) {
READGRAM(x, y + row, buf, wid, 1);
tft.setAddrWindow(x, y + row, x + wid - 1, y + row);
tft.pushColors(buf + dx, wid - dx, 1);
tft.pushColors(buf + 0, dx, 0);
}
if (dy) for (int16_t col = 0; col < wid; col++) {
READGRAM(x + col, y, buf, 1, ht);
tft.setAddrWindow(x + col, y, x + col, y + ht - 1);
tft.pushColors(buf + dy, ht - dy, 1);
tft.pushColors(buf + 0, dy, 0);
}
}
void printmsg(int row, const char *msg)
{
tft.setTextColor(YELLOW, BLACK);
tft.setCursor(0, row);
tft.println(msg);
}
typedef struct {
PGM_P msg;
uint32_t ms;
} TEST;
TEST result[12];
#define RUNTEST(n, str, test) { result[n].msg = PSTR(str); result[n].ms = test; delay(500); }
// Standard Adafruit tests. will adjust to screen size
unsigned long testFillScreen() {
unsigned long start = micros();
tft.fillScreen(BLACK);
tft.fillScreen(RED);
tft.fillScreen(BLACK);
return micros() - start;
}
unsigned long testText() {
unsigned long start;
tft.fillScreen(BLACK);
start = micros();
tft.setCursor(0, 0);
tft.setTextColor(WHITE); tft.setTextSize(1);
tft.println("Hello World!");
tft.setTextColor(YELLOW); tft.setTextSize(2);
tft.println(123.45);
tft.setTextColor(RED); tft.setTextSize(3);
tft.println(0xDEADBEEF, HEX);
tft.println();
tft.setTextColor(GREEN);
tft.setTextSize(5);
tft.println("Groop");
tft.setTextSize(2);
tft.println("I implore thee,");
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
// print2digits(tm.Hour);
// Serial.write(':');
// print2digits(tm.Minute);
// Serial.write(':');
// print2digits(tm.Second);
// Serial.print(", Date (D/M/Y) = ");
// Serial.print(tm.Day);
// Serial.write('/');
// Serial.print(tm.Month);
// Serial.write('/');
// Serial.print(tmYearToCalendar(tm.Year));
// Serial.println();
// } else {
// if (RTC.chipPresent()) {
// Serial.println("The DS1307 is stopped. Please run the SetTime");
// Serial.println("example to initialize the time and begin running.");
// Serial.println();
// } else {
// Serial.println("DS1307 read error! Please check the circuitry.");
// Serial.println();
// }
// delay(9000);
}
// tft.fillScreen(BLACK);
// tft.setTextSize(5);
// tft.println("Groop");
delay(1000);
tft.setTextSize(1);
return micros() - start;
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
void loop(void) {
uint8_t aspect;
uint16_t pixel;
const char *aspectname[] = {
"LANDSCAPE","PORTRAIT", "PORTRAIT_REV", "LANDSCAPE_REV"
};
const char *colorname[] = { "BLUE", "GREEN", "RED", "GRAY" };
uint16_t colormask[] = { 0x001F, 0x07E0, 0xF800, 0xFFFF };
uint16_t dx, rgb, n, wid, ht, msglin;
tft.setRotation(1);
delay(2000);
if (tft.height() > 64) {
for (uint8_t cnt = 0; cnt < 1; cnt++) {
aspect = (cnt + 0) & 3;
tft.setRotation(1);//aspect);
wid = tft.width();
ht = tft.height();
msglin = (ht > 160) ? 200 : 112;
testText();
}
}
}