Controlling DAC Chip & Touch Screen using SPI Question


I have programmed a capacitive touch screen so that the touch screen aspect works. And separately I successfully programmed a resistive touch screen to work with a DAC chip. I used some of the example code that the capacitive screen manufacturers provided to get the touch screen working, and then added all of my initial code.

I am trying to get the capacitive touch screen to work with the DAC chip. But whenever I power on the arduino, and the DAC chip, the screen freezes and starts outputting nonsense. The DAC Chip is the Max5250. Capacitive touch screen is this one -

Any help or insight would be greatly appreciated.

#include <SPI.h>
#include <Wire.h>
#include <stdint.h>

uint8_t addr  = 0x38;

#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
#include "MCUFRIEND_kbv.h"
MCUFRIEND_kbv ftf;
//LCD:hardware SPI  CTP:hardware IIC
//Arduino DUE,Arduino mega2560,Arduino UNO
#define RA8875_INT 4
#define RA8875_CS 10 
#define RA8875_RESET 9
#define FT5206_WAKE 6
#define FT5206_INT   7    
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET);
uint16_t tx, ty;

tsPoint_t       _tsLCDPoints[3];
tsPoint_t       _tsTSPoints[3];
tsMatrix_t      _tsMatrix;

enum {
  eNORMAL = 0,
  eTEST   = 0x04,
  eSYSTEM = 0x01

struct TouchLocation
  uint16_t x;
  uint16_t y;

TouchLocation touchLocations[0];

uint8_t readFT5206TouchRegister( uint8_t reg );
uint8_t readFT5206TouchLocation( TouchLocation * pLoc, uint8_t num );
uint8_t readFT5206TouchAddr( uint8_t regAddr, uint8_t * pBuf, uint8_t len );

uint32_t dist(const TouchLocation & loc);
uint32_t dist(const TouchLocation & loc1, const TouchLocation & loc2);

bool sameLoc( const TouchLocation & loc, const TouchLocation & loc2 );

uint8_t buf[30];

//Defining SPI connections for DAC
#define SELPIN 53 //Slave select Pin 
#define DATAOUT 50//MISO
#define DATAIN  51//MOSI 
#define QCLOCK  52//Clock
#define CSPIN 47 //Connects to CS

//Defining colors
#define NAVY 0x000F
#define BLACK 0x0000
#define WHITE 0xFFFF
#define DARKGREY 0x7BEF
#define DARKCYAN 0x03EF
#define BLUE 0x001F
#define RED 0xF800

int sstrb;
bool wasHigh = false;
byte TB1 = B11101111;
byte RB1;
byte RB2;
byte RB3;
byte RB4;

uint16_t both;
uint16_t combined;
uint16_t combined2;
double dacvar;
double dacvout;
double vdac;
uint16_t newvdac;
byte low;
byte high;
byte low2;
byte high2;

void voltmeterScrn() {
  tft.fillRect(0, 0, 350, 80, DARKCYAN);
  tft.fillRect(0, 200, 350, 80, DARKCYAN);
  tft.setCursor(150, 40);
  tft.setCursor(150, 125);
  //variable that will be updated.
  tft.setCursor(150, 220);

void setup() 
  Serial.println("RA8875 start");
  Wire.begin();        // join i2c bus (address optional for master)

  pinMode     (FT5206_WAKE, INPUT);
  digitalWrite(FT5206_WAKE, HIGH );
  writeFT5206TouchRegister(0, eNORMAL); // device mode = Normal
  uint8_t periodMonitor = readFT5206TouchRegister(0x89);
  uint8_t  lenLibVersion = readFT5206TouchAddr(0x0a1, buf, 2 );
  uint8_t firmwareId = readFT5206TouchRegister( 0xa6 );
  Serial.print("firmware ID = ");
  Serial.println( firmwareId);

  tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  showmsgXY(140, 210, 2, "Senior Design Team 11");
  showmsgXY(40, 240, 2, "Multi-Function Instrument Calibrator");
  //CL Pin.
  pinMode(31, OUTPUT);
  pinMode(33, OUTPUT);
  //Power Supply: Inputs/Outputs
  pinMode(CSPIN, OUTPUT);
  pinMode(SELPIN, OUTPUT);
  pinMode(DATAOUT, INPUT);
  pinMode(DATAIN, OUTPUT);
  pinMode(QCLOCK, OUTPUT);
  //disable device to start with
  digitalWrite(CSPIN, HIGH);
  digitalWrite(SELPIN, HIGH);
  digitalWrite(QCLOCK, LOW);
  digitalWrite(DATAIN, LOW);
  currentPage = '0';
  onepressed = 0;
  twopressed = 0;
  threepressed = 0;
  digitalWrite(31, HIGH);
  digitalWrite(33, HIGH);

void loop(){
  pinMode     (FT5206_INT, INPUT);
  uint8_t count = readFT5206TouchLocation( touchLocations, 1 );
  if (currentPage == '0'){
    static TouchLocation lastTouch = touchLocations[0];
    if (count) {
      x = touchLocations[0].x;
      y = touchLocations[0].y;
        //Serial.print("\r\nX="); Serial.print(x);
        //Serial.print("\r\nY="); Serial.print(y);
        if ((x >= 0) && (x <= 400) && (y >= 35) && (y <= 90)) {
          currentPage = '1';
        else if ((x >= 0) && (x <= 400) && (y >= 110) && (y <= 180)) {
          currentPage = '2';
        //Was 230.
        else if ((x >= 0) && (x <= 400) && (y >= 200) && (y <= 280)) {
          currentPage = '3';
 if (currentPage == '1'){
 if (count) {
      x = touchLocations[0].x;
      y = touchLocations[0].y;
        tft.fillRect(0, 80, 350, 80, BLACK);
        tft.setCursor(150, 125);
        vcounter = vcounter + 0.1;
        dacvar = vcounter;
        dacvout = vcounter;
        vdac = dacvout/2;
        newvdac = int((vdac/2.5) * 1024);
        both = newvdac;
        //Fill left of 1 with zeroes?.
        Serial.println(both, BIN);
        for (int i = 2; i < 12 ; i++){
          bitWrite(combined, i, bitRead(both, i));
        bitWrite(combined, 12, 1);
        bitWrite(combined, 13, 1);
        bitWrite(combined, 14, 0);
        bitWrite(combined, 15, 0);
        bitWrite(combined, 0, 0);
        bitWrite(combined, 1, 0);
        Serial.println(combined, BIN);
        //Transmit to DAC.
        SPI.beginTransaction(SPISettings(10000, MSBFIRST, SPI_MODE0));
        digitalWrite(SELPIN, LOW);
        digitalWrite(CSPIN, LOW);
        //Send 16 bits to DIN (DAC).
        high = combined >> 8;
        low = combined; 
        //left first, then right.
        //Send another to DAC.
        bitWrite(combined2, 12, 0);
        bitWrite(combined2, 13, 1);
        bitWrite(combined2, 14, 1);
        bitWrite(combined2, 15, 1);
        high2 = combined2 >> 8;
        low2 = combined2;
        //left first, then right.
        digitalWrite(CSPIN, HIGH);

That code doesn't compile.

Don't use the hardware SPI interface and use the same pins for a bit-bang interface! Use the SPI interface too to communicate with your DAC.

Oh I didn't include all the front end code. I just edited it, and added to it. But how would I switch the screen to not depend on the hardware SPI? The example code for the screen included it that way, and I wasn't sure how to change it.

But how would I switch the screen to not depend on the hardware SPI?

Wrong way! Get rid of the bit-bang code, always use the hardware SPI for SPI devices.

I see, I didn't even realize I wasn't using hardware SPI to communicate with the DAC. I'll fix that. On another note, I believe I fixed the issue i initially reported. I discovered that the RA8875 conflicts with other devices that communicate with SPI (Not sure if this was ever fixed, but I went along with that presumption. So I attached some 1k resistors to MOSI, MISO, etc. And that seems to have solved the issue.

I discovered that the RA8875 conflicts with other devices that communicate with SPI

Usually that’s because of a level converter not SPI compatible and not because of the RA8875. Do you use the Adafruit board?