# DS3234 + Arduino TFT screen using SPI

**URL:** <https://forum.arduino.cc/t/ds3234-arduino-tft-screen-using-spi/469743>\
**Category:** Programming\
**Created:** [July 12, 2017, 7:42pm UTC](https://forum.arduino.cc/t/ds3234-arduino-tft-screen-using-spi/469743 "2017-07-12T19:42:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandeshakya](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sandeshakya](https://forum.arduino.cc/u/sandeshakya)\
**Post date:** [July 12, 2017, 7:42pm UTC](https://forum.arduino.cc/t/ds3234-arduino-tft-screen-using-spi/469743/1 "2017-07-12T19:42:13Z")

</div>

Hey folks! I have an Arduino Mega 2560, a DS3234 RTC, and an arduino TFT screen (this one: [https://www.arduino.cc/en/Guide/TFT](https://www.arduino.cc/en/Guide/TFT)). They both work fine independently, but I'm having problems with the RTC when I connect the lcd screen as well. I'm not too familiar with SPI so I was wondering if someone here could help me with this issue.

The pin connections for RTC are:  
CLK - 52  
MISO - 50  
MOSI - 51  
SS - 44

for the lcd screen:  
MISO - 50  
SCK - 52  
MOSI - 51  
CS-LD - 28  
DC-LD - 26  
RESET - 24

If I understand correctly, the MISO, MOSI and CLK pins should be the same for both devices, only the chip select pin needs to be different. The devices that I want to communicate with needs to have its CS pin LOW, and the other device needs to have its HIGH.

I wrote a simple program that sets the RTC time, and displays the second. I have set the CS, DC and RESET pins for the display HIGH, but whenever I connect the display to the breadboard, both the minute and second value become 0. It starts working as soon as I remove the display.

Code:

```cpp
#include <SPI.h>

int chipSelect = 44;
int intFreq = 3;

/*display pins*/
int cs = 28;
int dc = 26;
int rst = 24;

#define WRITE_CONTROL_REG 0x8E
#define READ_CONTROL_REG 0x0E
#define WRITE_TIME_REG 0x80
#define READ_TIME_REG 0x00

typedef struct 
{
 uint8_t ss;
 uint8_t mm;
 uint8_t hh;
 uint8_t dy;
 uint8_t d;
 uint8_t m;
 uint8_t y;
}timeParams;

void rtc_init(int chipSelect, int intFreq)
{
 /*
  * output frequencies:
  * 0: 1hz
  * 1: 1.024 kHz
  * 2: 4.096 kHz
  * 3: 8.192 kHz
  * 4: off
  */

 /*set up chip select pin*/
 pinMode(chipSelect, OUTPUT);
 //initialize comm
 digitalWrite(chipSelect, LOW);
 
 //Only change bit 2,3,4,6
 SPI.transfer(READ_CONTROL_REG);
 byte originalConfig = SPI.transfer(0x00);
 //end comm
 digitalWrite(chipSelect, HIGH);
 delay(10);

 byte configModifier;
 byte newConfig;

 //if intFreq is 4, turn square wave off
 if(intFreq == 4)
 {
   configModifier = 0b10111111;
   newConfig = configModifier & originalConfig;
 }
 else if (intFreq < 4)
 {
   configModifier = (intFreq << 3) | 0b01000000; // bit 6 has to be on
   newConfig = configModifier | (originalConfig & 0b11100011); //only change bits 2,3,4,6
 }

 digitalWrite(chipSelect, LOW);
 SPI.transfer(WRITE_CONTROL_REG);
 SPI.transfer(newConfig);
 digitalWrite(chipSelect, HIGH);
}

static uint8_t convertValGet(uint8_t value)
{
 uint8_t convertedValue = value - 6 * (value >> 4);
 return convertedValue; 
}

static uint8_t convertValSet(uint8_t value)
{
 uint8_t convertedValue = value + 6 * (value / 10);
 return convertedValue;
}

void setTime(int chipSelect, timeParams *timeVals)
{
 pinMode(chipSelect, OUTPUT);
 
 digitalWrite(chipSelect, LOW);
 SPI.transfer(WRITE_TIME_REG);
 SPI.transfer(convertValSet(timeVals->ss));
 SPI.transfer(convertValSet(timeVals->mm));
 SPI.transfer(convertValSet(timeVals->hh));
 SPI.transfer(convertValSet(timeVals->dy));
 SPI.transfer(convertValSet(timeVals->d));
 SPI.transfer(convertValSet(timeVals->m));
 SPI.transfer(convertValSet(timeVals->y));
 digitalWrite(chipSelect, HIGH);
 
 delay(10);
}

void getTime(int chipSelect, timeParams *timeVals)
{
 pinMode(chipSelect, OUTPUT);

 digitalWrite(chipSelect, LOW);
 SPI.transfer(READ_TIME_REG);
 timeVals->ss = convertValGet(SPI.transfer(READ_TIME_REG));
 timeVals->mm = convertValGet(SPI.transfer(READ_TIME_REG));
 timeVals->hh = convertValGet(SPI.transfer(READ_TIME_REG));
 timeVals->dy = convertValGet(SPI.transfer(READ_TIME_REG));
 timeVals->d = convertValGet(SPI.transfer(READ_TIME_REG));
 timeVals->m = convertValGet(SPI.transfer(READ_TIME_REG));
 timeVals->y = convertValGet(SPI.transfer(READ_TIME_REG));
 digitalWrite(chipSelect, HIGH);
 delay(10);
}

timeParams currentTime = {
 39,
 29,
 11,
 4,
 5,
 7,
 17
};

void setup() {
 // put your setup code here, to run once:
 pinMode(cs, OUTPUT);
 pinMode(dc, OUTPUT);
 pinMode(rst, OUTPUT);
 pinMode(chipSelect, OUTPUT);

 digitalWrite(cs, HIGH);
 digitalWrite(dc, HIGH);
 digitalWrite(rst, HIGH);
 
 SPI.begin();
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE1);
 Serial.begin(9600);

 rtc_init(chipSelect, intFreq);
 setTime(chipSelect, &currentTime);

}

void loop() {
 // put your main code here, to run repeatedly:
 delay(1000);
 getTime(chipSelect, &currentTime);
 Serial.println(currentTime.ss);

}

```

What is it that I'm doing wrong here?

I found out that the RTC keeps count even though the LCD is connected, but displays 0. If I plug in the LCD at second 2, for 3 seconds, and unplug, the count resumes are 5.

Also found out that the problem is with the MISO pin. I tried plugging in each pin of the LCD individually, and found out that the MISO pin causes the time to be 0.

---

<div class="post-metadata">

**Author:** ![bDeters](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@bDeters](https://forum.arduino.cc/u/bDeters)\
**Post date:** [July 12, 2017, 9:04pm UTC](https://forum.arduino.cc/t/ds3234-arduino-tft-screen-using-spi/469743/2 "2017-07-12T21:04:51Z")

</div>

Since you're not even referencing the TFT code yet, I'd guess there's a wiring issue. Can you hook the TFT pins up one at a time until you find the offender?

---

<div class="post-metadata">

**Author:** ![sandeshakya](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sandeshakya](https://forum.arduino.cc/u/sandeshakya)\
**Post date:** [July 13, 2017, 6:01pm UTC](https://forum.arduino.cc/t/ds3234-arduino-tft-screen-using-spi/469743/3 "2017-07-13T18:01:51Z")

</div>

Yeah, found out that the MISO pin was the culprit. Since the LCD will not be sending any data to the master, I disconnected that pin, and now the LCD and RTC work together fine 🙂 . However, I'm still curious as to why that happened, and if there is a software way to solve the issue.

---

<div class="post-metadata">

**Author:** ![bDeters](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@bDeters](https://forum.arduino.cc/u/bDeters)\
**Post date:** [July 13, 2017, 7:11pm UTC](https://forum.arduino.cc/t/ds3234-arduino-tft-screen-using-spi/469743/4 "2017-07-13T19:11:55Z")

</div>

Perhaps the idle states on each device's MISO are at odds, one pulling high and the other low. Could that be mucking up the start condition, perhaps?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:37pm UTC](https://forum.arduino.cc/t/ds3234-arduino-tft-screen-using-spi/469743/5 "2021-05-05T22:37:50Z")

</div>

Closed on May 5, 2021, 10:37pm UTC


