I've got five SPI SRAM chips, which I want to control off a single Arduino. I've seen setups like this, which share SCLK
, MOSI
and MISO
, with separate SS
pins:
(image courtesty of Wikipedia)
However, I'd like to avoid using five pins for separate SS
enables. I'm considering using a counter to select the different slaves, so I can cut the count down to 2 pins.
The idea is to use two pins for the counter's direction and clock. The code would look something like this:
int currentSlave = 1; // on setup I'll set the counter to 1
void SelectSlave(int id)
{
// error checking
if (id < 1 || id > 5) Serial.writeln("Invalid slave ID passed to SelectSlave.");
// calculate the ID difference
int diff = abs(id - currentSlave);
if (diff == 0) return; // no need to do anything
if (id > currentSlave) set(CTR_DIRECTION); // increment
if (id < currentSlave) clear(CTR_DIRECTION); // decrement
// calculate the number of clock pulses to send to the counter
int pulses = 0;
if (id > currentSlave) pulses = (1 << (id - 1)) - (1 << (currentSlave - 1));
if (id < currentSlave) pulses = (1 << (currentSlave - 1)) - (1 << (id - 1));
// send the clock pulses
for(int i = 0; i < pulses; i++)
{
set(CTR_CLOCK);
delay(1);
clear(CTR_CLOCK);
delay(1);
}
}
I have a few questions:
- Are there any issues related to turning multiple
SS
pins on and off during the interim counting period? - Can I rely on (most) counters being set to zero when it is first powered on?
- Are there other/better ways to cut pin counts in this kind of setup?
~n
instead ofn
. So instead of1, 2, 4, 8, 16
, the pulse counts would be30, 29, 27, 23, 15
. \$\endgroup\$