Go Down

Topic: LED Diplay in different digital ports (Read 540 times) previous topic - next topic

squizzatoandrea

Hi,

I used three four-digit, seven-segment LED display that has an SPI interface to show some numbers on them.
http://tronixstuff.wordpress.com/2011/05/13/tutorial-arduino-and-the-spi-bus/
I used the code below, and all went fine.

Now I'm trying to move data pins between D2 and D7 (I need to expand my project adding a shield that use ports from D8 to D13).
I tried many changes, but it seems when I move SPICLOCK from D13 to any other, all doesn't work.

What's wrong?
Thanks.

Andrea

Code: [Select]
/*
Example 34.3 - SPI bus demo using SFE 4-digit LED display [http://bit.ly/ixQdbT]
http://tronixstuff.com/tutorials > chapter 34
Based on code by Quazar & Busaboi on Arduio forum - http://bit.ly/iecYBQ
*/

/*
WIRING
Board GND to Arduino GND
Board VCC to Arduino 5V
Board SCK to Arduino D13
Board SI to Arduino D11
Board CSN to Arduino Dx
*/

#define DATAOUT 11 //MOSI
#define SPICLOCK 13 //sck
#define SLAVESELECT1 8 //ss1
#define SLAVESELECT2 9 //ss2
#define SLAVESELECT3 10 //ss3

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}

void setup()
{
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(SPICLOCK, OUTPUT);
  pinMode(SLAVESELECT1, OUTPUT);
  pinMode(SLAVESELECT2, OUTPUT);
  pinMode(SLAVESELECT3, OUTPUT);
  digitalWrite(SLAVESELECT1, HIGH); //disable device
  digitalWrite(SLAVESELECT2, HIGH); //disable device
  digitalWrite(SLAVESELECT3, HIGH); //disable device
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1);
  clr=SPSR;
  clr=SPDR;
  delay(10);
  write_led_numbers(0x78,0x78,0x78,0x78,SLAVESELECT1); //Blank display
  write_led_numbers(0x78,0x78,0x78,0x78,SLAVESELECT2); //Blank display
  write_led_numbers(0x78,0x78,0x78,0x78,SLAVESELECT3); //Blank display
  write_led_decimals(0x00,SLAVESELECT1); // All decimal points off
  write_led_decimals(0x00,SLAVESELECT2); // All decimal points off
  write_led_decimals(0x00,SLAVESELECT3); // All decimal points off
 
  delay(500);
  write_led_numbers(1,1,1,1,SLAVESELECT1);
  delay(500);
  write_led_numbers(2,2,2,2,SLAVESELECT2);
  delay(500);
  write_led_numbers(3,3,3,3,SLAVESELECT3);
  delay(500);
  write_led_numbers(9,9,9,9,12); 
}

void write_led_decimals(int value, int slave)
{
  digitalWrite(slave, LOW);
  delay(10);
  spi_transfer(0x77);     // Decimal Point OpCode
  spi_transfer(value);    // Decimal Point Values
  digitalWrite(slave, HIGH); //release chip, signal end transfer
}
void write_led_numbers(int digit1, int digit2, int digit3, int digit4, int slave)
{
  digitalWrite(slave, LOW);
  delay(10);
  if(digit1)
    spi_transfer(digit1);    // Thousands Digit
  else
    spi_transfer(0x78);    // Thousands Digit
 
  spi_transfer(digit2);    // Hundreds Digit
  spi_transfer(digit3);    // Tens Digit
  spi_transfer(digit4);    // Ones Digit
  digitalWrite(slave, HIGH); //release chip, signal end transfer
}

void loop()

}


floresta

Quote
I tried many changes, but it seems when I move SPICLOCK from D13 to any other, all doesn't work.

What's wrong?

The SPI interface uses a hardware subsystem built into the microprocessor.  Several of the pins are fixed and cannot be changed.  

The only way around this would be to use a 'Soft SPI' library.


Don

squizzatoandrea

Thanks Don.

I will take a look to that library.

Andrea

CrossRoads

"adding a shield that use ports from D8 to D13"
Does that shield also use SPI?
If so, both devices will connect D11-12-13, and each will need unique SS pins.

Code: [Select]

#define SLAVESELECT1 8 //ss1
#define SLAVESELECT2 9 //ss2
#define SLAVESELECT3 10 //ss3

These can go to your other pins.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up