I'd like to know if it is possible to share pins 51(MOSI) and 52(SCK) on the Arduino Mega 2560 between an SPI bus (Touch screen with SD Card) and TLC5940 Shift Register (Other/LED's).
The TLC5940 requires pin 51 on the mega to connect to its pin 26(SIN) and pin 52 to pin 25(SCLK).
Would this cause me issues? Is the communication handled through hardware automatically somehow?
I'd like to know if it is possible to share pins 51(MOSI) and 52(SCK) on the Arduino Mega 2560 between an SPI bus (Touch screen with SD Card) and TLC5940 Shift Register (Other/LED's).
No. If the pin is being used for SPI is can not be used for ANY other purpose.
You have 50 plus pins. If that is not enough, you can consider the use of port expanders (I2c/SPI) or serial /parallel (or vice versa) shift registers.
As you already have an SPI device, it makes sense to me to add more if needed.
I have a lot of available pins on the Mega, but from my understanding the TLC5940 library does not allow you to change those 2 pins (51 and 52).
Here are 2 snippets from the library, one from the Arduino_Mega.h file telling you configurable pins are in the tlc_config.h file, and that file telling you not to edit them:
Alright, well since nobody is replying I'll just carry on as it might help someone down the road.
So I managed to change the 2 pins by bit-banging after finding a post about it online on github. The post says to change information in 3 files as seen below.
After doing that and moving pin 52 to 2, and 51 to 7, I ran my sketch and all the leds worked as intended, but at random times when they were supposed to be off some of them would flicker, which did not happen previously.
My guess is the code above might be a little old and needs updating or it has something to do with the timers not working properly with the pins they are on. The change the post states to makes in the ATmega_xx8.h file seems outdated. It states they are using version r014 of the TLC5940 library, but I am using r014_2, so there's probably been a change.
I went over the changes and tried different pins/settings, but the flicker persists. I checked the port mappings thanks to this post, and everything seems right, so I'm at a loss as to where the flicker is coming from or how to fix it.
I know the Mega has 6 timers, and the MOSI pin is using timer4 on pin 7 while SCK pin is using timer3 on pin 2. Could there be some sort of conflict there?
Somewhere along the line I had a knight_rider effect example code from the internet and had added the SD data logging to it with no ill effects so I knew it was possible in my other projects as well.
Yesterday I added an SD breakout to a project to log data and began having crazy LED flickering issues right off the bat when the arduino interacted with the SD card.
I started picking through the code and found that the only difference between my main project and the knight_rider example is the use of delay() after Tlc.update() and writting to the SD card. Below is some sample code where I stripped out everything I could and was still able to turn the issue on/off by only commenting out the delay(1);
For reference, a pic of my shield is attached to shows you which pins I am sharing. I am using a MEGA 2560.
I can't explain why this works for me. I don't like that it feels like a hack to me but I really hope that you (or someone later) finds this helpful. Maybe someone with more experience/knowledge can bring to light why the delay(1) works..?
I logged on tonight with the sole purpose of posting this since I could not find a solution anywhere online. If this helps you, please let me know!
Cheers!
//libraries
#include <SD.h>
#include <SPI.h>
#include "Tlc5940.h"
//Chipselect pin for SD breakout
const int chipSelect = 46;
File SDlogfile; //Construct the SD object
void setup(){
// Call Tlc.init() to setup the tlc. You can optionally pass an initial PWM value (0 - 4095) for all channels
Tlc.init();
// start serial port
Serial.begin(9600);
pinMode(SS, OUTPUT);
//*******************************************
//https://www.arduino.cc/en/Reference/SDbegin
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//*******************************************
//create a new file
char SDLogFileName[] = "BrewSD00.CSV";
for (uint8_t i = 0; i < 100; i++) {
SDLogFileName[6] = i/10 + '0';
SDLogFileName[7] = i%10 + '0';
if (! SD.exists(SDLogFileName)) {
// only open a new file if it doesn't exist
// https://www.arduino.cc/en/Reference/SDopen
SDlogfile = SD.open(SDLogFileName, FILE_WRITE);
break; // leave the loop!
}
}
}
void loop(){
// Tlc.update() sends the data to the TLCs. This is when the LEDs will actually change
Tlc.update();
//-----------------------------------------------------------------
//I don't know why this works... but adding a delay between Tlc.update() & when data is written
//to the SD card resolved my flickering issues
delay(1); //Comment out this delay and flickering occurs with my setup
//-----------------------------------------------------------------
//Writes some data to the logfile and flushes
SDlogfile.println(millis());
SDlogfile.flush();
}