Go Down

Topic: Controlling DAC Chip & Touch Screen using SPI Question (Read 234 times) previous topic - next topic

theMasonator

Nov 15, 2019, 01:21 am Last Edit: Nov 16, 2019, 04:06 am by theMasonator
Hello,

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 - https://www.buydisplay.com/default/4-3-inch-tft-capacitive-touchscreen-display-arduino-shield-480x272

Any help or insight would be greatly appreciated.

Code: [Select]

#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;


//DAC STUFF.
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.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.print("+");
  tft.setCursor(150, 125);
  //variable that will be updated.
  tft.print("0V");
  tft.setCursor(150, 220);
  tft.print("-");
  backBtn();
}

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

  readOriginValues();
  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.displayOn(true);
  tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  tft.PWM1out(255);
 
  tft.setRotation(1);
  //tft.invertDisplay(true);
  tft.fillScreen(BLACK);
  showmsgXY(140, 210, 2, "Senior Design Team 11");
  showmsgXY(40, 240, 2, "Multi-Function Instrument Calibrator");
  loading();
  //CL Pin.
  pinMode(31, OUTPUT);
  //PDL
  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);
  //SPI.begin();
  tft.fillScreen(WHITE);
  //tft.invertDisplay(false);
  homescreen();
  currentPage = '0';
  onepressed = 0;
  twopressed = 0;
  threepressed = 0;
  //CL
  digitalWrite(31, HIGH);
  //PDL
  digitalWrite(33, HIGH);
}

void loop(){
  pinMode     (FT5206_INT, INPUT);
  //Uneccessary.
  tft.touchEnable(true);
  uint8_t count = readFT5206TouchLocation( touchLocations, 1 );
  //Serial.print(count);
  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)) {
          tft.fillScreen(BLACK);
          currentPage = '1';
          voltmeterScrn();
        }
        else if ((x >= 0) && (x <= 400) && (y >= 110) && (y <= 180)) {
          tft.fillScreen(BLACK);
          currentPage = '2';
          //ammeterScrn();
        }
        //Was 230.
        else if ((x >= 0) && (x <= 400) && (y >= 200) && (y <= 280)) {
          tft.fillScreen(BLACK);
          currentPage = '3';
          //powerSupplyScrn();
        }
     }
  }
 if (currentPage == '1'){
 if (count) {
      x = touchLocations[0].x;
      y = touchLocations[0].y;
        tft.fillRect(0, 80, 350, 80, BLACK);
        tft.setCursor(150, 125);
        tft.setTextColor(WHITE);
        tft.setTextSize(3);
        //Temporary.
        vcounter = vcounter + 0.1;
        delay(100);
        tft.print(vcounter);
        tft.print("V");
        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);
        Serial.println(combined, BIN);
        //Transmit to DAC.
        SPI.beginTransaction(SPISettings(10000, MSBFIRST, SPI_MODE0));
        digitalWrite(SELPIN, LOW);
        digitalWrite(CSPIN, LOW);
        delay(0.1);
        //Send 16 bits to DIN (DAC).
        //Left.
        high = combined >> 8;
        //Right.
        low = combined;
        delay(0.1);
        //left first, then right.
        SPI.transfer(high);
        SPI.transfer(low);
        delay(0.1);
        //Send another to DAC.
        bitWrite(combined2, 12, 0);
        bitWrite(combined2, 13, 1);
        bitWrite(combined2, 14, 1);
        bitWrite(combined2, 15, 1);
        //Left.
        high2 = combined2 >> 8;
        //Right.
        low2 = combined2;
        //left first, then right.
        SPI.transfer(high2);
        SPI.transfer(low2);
        delay(0.1);
        digitalWrite(CSPIN, HIGH);
        SPI.endTransaction(); 
      }
  }
}

pylon

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.

theMasonator

#2
Nov 16, 2019, 04:07 am Last Edit: Nov 16, 2019, 05:49 am by theMasonator
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.

pylon

Quote
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.

theMasonator

#4
Nov 19, 2019, 04:35 am Last Edit: Nov 19, 2019, 04:36 am by theMasonator
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.

pylon

Quote
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?

Go Up