2515 and TFT not working together

Hi All...

Bit (lot)stuck and not sure where to go .

My circuit comprises an Adafruit 2.8" TFT and a generic CAN bus breakout with a 2515 chip. The processor is a ESP32S3 -wroom-1 .

IMG_5197

THe processor is 3.3v and the 2515 is running at 5v ( due to its on board 2550).
I have made the connections for SPI using the default ESP pins, but with a idffrertnt pin for CS on the display . Here is my circuit. I have a potential divider on the MISO pin of the CAN devise as it outputs at 5v

If the CAN and TFT are plugged into the board the CAN with not initalise. The CAN on its own will however. The TFT will work in either situation.
Here is the first bit of my code, where the issues occurs:

#include <EEPROM.h>
#include <mcp_can.h>
#include <SPI.h>
#include "Adafruit_ILI9341.h"
#include "Adafruit_GFX.h"

#include <Fonts/FreeSerif9pt7b.h> // font used for text
#include <Fonts/FreeSansBold12pt7b.h> // like this font 
#include <Fonts/FreeSans9pt7b.h>
#define TFT_GRAY 0x8410


//for pro mini32 testing use
/*
  #define TFT_DC 10
  #define TFT_CS 12
  #define TFT_MOSI 11
  #define TFT_MISO 9
  #define TFT_CLK 7
  #define TFT_RST 0

*/


//"EEPROM" (actually flash but ...)
#define EEPROM_SIZE 20  // number of bytes needed

const bool Verbose = 1; //used in debug for serial monitor printing.
const int TESTled=7;
// setup parameters:
bool FirstRun;
int CANBaudRate = 500;
int CanBaseNumber = 0x290;
byte firstrunCode = 121;

// RF and input type:
byte COMSchoiceTx;
byte COMSchoiceRx;
const int COMSaddressRx = 10; // address in flash (EEPROM) wehere these are stored.
const int COMSaddressTx = 11;

// coms stuff: variables to hold the parsed data from PC for rest of program:
const byte numChars = 20;
char messageFromPC[numChars] = {0};
int DesiredFillTime = 0;
int TimerStatusFromPC = 0;
bool newData = false;
bool freshData = false;
bool RUN = false;
int Status;  // used for tranmitting info to host PC , sends battery , shutdown status. see voltage and shutdown function and void DataTx()
int Battery_Status = 2; // (2==good)
int Switchoff_Status = 1;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

// display type:
bool DisplayType; // 0 =enery prioroty ,1 =timer priority
const int DisplayTypeAddr = 12; // where data for display view is stored
#define CAN0_INT 47 // IO 47 process pin to show buffer data present.

// eeprom sytax:   EEPROM.write(addr, val);
//                 EEPROM.read(addr);
// EEPROM Locations:

byte  FirstRunSave = 1;
const int  BaudrateSave = 2; //(and 3!)
const int CanBaseNumberAdd = 4;//&5
// Data Areas:
const int timeAdd = 6;
const int DeadManHandle = 5; // fuel control lever. IO5
const int Flow = 4;

// 2515 setup
const int CanCS = 14; //IO14;
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; //128  // Array to store serial string
char SpeedString[25];

MCP_CAN CAN0(CanCS);

// input Buttons
bool Up;
bool Down;
bool Enter;
bool Across;

const byte Uppin = 40;// IO40
const byte Downpin = 39; //IO39
const byte Enterpin = 38; //IO38
const byte Acrosspin = 42; //IO42 , pin35

//Power control:
byte PowerlockinPin = 48; // IO48 used for switch on to hold unit on.
bool PowerlockinPinState;
long AutoSwitchoffTime = 900000; // time to switch off Ms
long RunTime; //  millis() time since switch on, reset by any operation event

//outputs to display etc:
long Volval;//flow rate
long Massval;
long StartReading;
int Temperature;
long  totalVol;  //from can bus
long TotalVolinRun;
long Filltime;  // the duration of a fuel fill
long StartFilltime;
long EndFilltime;
long TimerupdateInterval = 200; // how often to update the display
unsigned long currentMillis;

// Second serial port used for debugging:
#define RXD2 16  //default for these
#define TXD2 17

// start display.
//For PCB use: these are just the default  SPI pins for esp32, but different cs for display.
#define TFT_DC 10
#define TFT_CS 21 // pin 23 on the chip
#define TFT_MOSI 11
#define TFT_MISO 13
#define TFT_CLK 12
#define TFT_RST 0

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

void setup()//********** start of setup***************
{
  pinMode (TESTled, OUTPUT);
  if (Verbose == 1) //used for debug
  {
    Serial.begin(115200);
    delay(100);
    Serial.print(F( "PSH/24 Ver:"));
    Serial.println(Ver);
  }
  pinMode(Flow, INPUT_PULLUP);
  pinMode(DeadManHandle, INPUT_PULLUP);
  pinMode(CAN0_INT, INPUT);
  //pinMode (CanCS, OUTPUT);

  // input buttons buttons:

  pinMode (Uppin, INPUT);
  pinMode (Downpin, INPUT);
  pinMode (Enterpin, INPUT);
  pinMode( Acrosspin, INPUT);  //these are all active low, with external pull ups

  // lock in power
  pinMode(PowerlockinPin, OUTPUT); //HOLDS POWER ON AFTER BUTTON PRESS
  digitalWrite(PowerlockinPin, HIGH);
  PowerlockinPinState = 1;

  EEPROM.begin(EEPROM_SIZE);
  Retrieveparameters(1); // GET SETUP CONSTANTS

  //setup receiver:
  //Set up receiver (we start as a receiver):
  Serial.print(F("ATR")); //ASCII
  Serial.write(COMSchoiceRx); // decimal byte values
  Serial.write(numChars); // length of data string ("<----data,length--->"

  //*****************************************************

  // Initialize MCP2515 running at 16MHz with a baudrate of CANBaudRate and the masks and filters disabled.
  // if (CAN0.begin(MCP_ANY, SpeedString, MCP_16MHZ) == CAN_OK)
  if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)  // default method
  {
    if (Verbose == 1)
    {
      Serial.println("MCP2515 Initialized Successfully!");
    }
  }
  else
  {
    if (Verbose == 1)
    {
      Serial.println("Error Initializing MCP2515...");
    }
  }
  CAN0.setMode(MCP_NORMAL);
  
  delay(2000); // TIME TO READ ABOVE MSG

  tft.begin();
  tft.fillScreen(ILI9341_BLACK); // blank screen out.
  tft.setRotation(1);// rotate display
  Retrieveparameters(1);  //restore saved values

  //*****************************************
  RunTime = millis(); // start up time recorded

  if (Verbose == 1)
  {
    Serial.println (F(" INTO Loop...")) ;
  }

}//*******end of setup**********
 

messages are :

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0
PSH/24 Ver:0.85
Run code is (121)121
 Setup values retrieved
ATR**Entering Configuration Mode Failure...**
**Error Initializing MCP2515...**
Run code is (121)121
 Setup values retrieved
 INTO Loop...
 CANBUS SECTION...  Can int= 1
Standard ID: 0x000       DLC: 0  Data:
                        HEADER:0
into display3

Its interesting that posting on here clears the mind...

I have swapped the voltage divider resistors to 820R and 1k - it now works ok .

This pinout of a ESP32-S3-board shows a different IO-pin-assigment
SPI-CLK is IO-pin 13 (you define 12)
and SPI-MISO is IO-pin 12 (you define 13)

best regards Stefan

Interesting ....

I've worked from the chip manufacturers data sheet ( as I'm using the bare chip ESP32S3-WROOM-1) which gives, after not bending your neck.... The chips are different, mine has 40 pins, that one only 38.

IMG_5198

Do us a favor. Make sure that your pictures can be read without turning the monitor 90 degrees

Sorry M8 , I do try :slight_smile:
IMG_5199

That is "interesting" too.
Because with 1k = 1000 Ohm and 820 Ohm the HIGH-voltage is
5 / (1000 + 820) * 1000 = 2.74 V

oh dear my typo.. 1500 and 820; 3.23 volts.

thx for pointing out

I've removed the "solution" flag as I thought as it initialised ok all was well.... But on looking at CAN messages the 2515 is comming back with a rebbish result . If I remove the line "TFT.begin" , then the 2515 receives can messages ok.
I read from Google that there is a problem sharing SPI with the Adafruit ILI9341 but the solutions of changing lines in the adafruit library just wont me then allow to compile ( redefinition of "... ILI934).
I adapted the example CAN receive to add the tft as below

// CAN Receive Example (modified with TFT)
//

#include <mcp_can.h>
#include <SPI.h>
#include "Adafruit_ILI9341.h"
#include "Adafruit_GFX.h"
#include <Fonts/FreeSerif9pt7b.h> // font used for text
#include <Fonts/FreeSansBold12pt7b.h> // like this font 
#include <Fonts/FreeSans9pt7b.h>
#define TFT_GRAY 0x8410
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];                        // Array to store serial string

#define CAN0_INT 47                             // Set INT to pin 2
MCP_CAN CAN0(14);                               // Set CS to pin 10

// Second serial port used for debugging:
#define RXD2 16  //default for these
#define TXD2 17

// start display.
//For PCB use: these are just the default  SPI pins for esp32, but different cs for display.
#define TFT_DC 10
#define TFT_CS 21 // pin 23
#define TFT_MOSI 11
#define TFT_MISO 13
#define TFT_CLK 12
#define TFT_RST 0

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);

void setup()
{
  Serial.begin(115200);
  
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");
  
  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.

  pinMode(CAN0_INT, INPUT_PULLUP);                            // Configuring pin for /INT input
   tft.begin();
  Serial.println("MCP2515 Library Receive Example...");
   Serial.println("into display 0");
    tft.fillScreen(ILI9341_BLUE);
    tft.setFont(&FreeSans9pt7b);
    tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(1);
    tft.setCursor(10, 20);
    tft.println("In print hello ");
    delay(1500);
}

void loop()
{
  if(!digitalRead(CAN0_INT))                         // If CAN0_INT pin is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)
    
    if((rxId & 0x80000000) == 0x80000000)     // Determine if ID is standard (11 bits) or extended (29 bits)
      sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
    else
      sprintf(msgString, "Standard ID: 0x%.3lX       DLC: %1d  Data:", rxId, len);
  
    Serial.print(msgString);
  
    if((rxId & 0x40000000) == 0x40000000){    // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
      Serial.print(msgString);
    } else {
      for(byte i = 0; i<len; i++){
        sprintf(msgString, " 0x%.2X", rxBuf[i]);
        Serial.print(msgString);
      }
    }
        
    Serial.println();
  }
}

The output running this is:

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0
Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
MCP2515 Library Receive Example...
into display 0
Standard ID: 0x000       DLC: 0  Data:
Standard ID: 0x000       DLC: 0  Data:
Standard ID: 0x000       DLC: 0  Data:
Standard ID: 0x000       DLC: 0  Data:
Standard ID: 0x000       DLC: 0  Data:
Standard ID: 0x000       DLC: 0  Data:
Standard ID: 0x000       DLC: 0  Data:
Standard ID: 0x000       DLC: 0  

If I take out the TFT.begin line I get the result I'm looking for:

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbd8
load:0x403cc700,len:0x2a80
entry 0x403c98d0
Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Standard ID: 0x290       DLC: 8  Data: 0x93 0xA6 0x19 0x00 0x42 0x85 0x14 0x00
Standard ID: 0x291       DLC: 8  Data: 0x00 0x00 0x00 0x7B 0x00 0xD8 0x00 0x00
Standard ID: 0x294       DLC: 8  Data: 0x40 0xE2 0x01 0x00 0x00 0x00 0x00 0x00
Standard ID: 0x290       DLC: 8  Data: 0xAF 0xB2 0x19 0x00 0xF2 0x8E 0x14 0x00
Standard ID: 0x291       DLC: 8  Data: 0x00 0x00 0x00 0x7B 0x00 0xD8 0x00 0x00
Standard ID: 0x294       DLC: 8  Data: 0xD1 0xE8 0x01 0x00 0x00 0x00 0x00 0x00
Standard ID: 0x290       DLC: 8  Data: 0xAF 0xB2 0x19 0x00 0xF2 0x8E 0x14 0x00
Standard ID: 0x291       DLC: 8  Data: 0x00 0x00 0x00 0x7B 0x00 0xD8 0x00 0x00
Standard ID: 0x294       DLC: 8  Data: 0x65 0xEF 0x01 0x00 0x00 0x00 0x00 0x00
Standard ID: 0x290       DLC: 8  Data: 0x2F 0xBF 0x19 0x00 0xF2 0x98 0x14 0x00
Standard ID: 0x291       DLC: 8  Data: 0x00 0x00 0x00 0x7B 0x00 0xD8 0x0

On its own the TFT works fine !

If anyone has seen this or knows of a solution I would be exteremly grateful !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.