Go Down

Topic: Setting up the second SPI port on the Arduino Uno (Read 2740 times) previous topic - next topic

diesel

Nick,

OK, I will look around and see if someone has done something like that. Thanks for your help!!

-diesel

Nick Gammon

Here's an example:

Code: [Select]

// bit banged SPI pins
const byte MSPIM_SCK = 4;  // port D bit 4
const byte MSPIM_SS  = 5;  // port D bit 5
const byte BB_MISO   = 6;  // port D bit 6
const byte BB_MOSI   = 7;  // port D bit 7

// for fast port access (Atmega328)
#define BB_MISO_PORT PIND
#define BB_MOSI_PORT PORTD
#define BB_SCK_PORT PORTD
const byte BB_SCK_BIT = 4;
const byte BB_MISO_BIT = 6;
const byte BB_MOSI_BIT = 7;

// control speed of programming
const byte BB_DELAY_MICROSECONDS = 4;


// Bit Banged SPI transfer
byte BB_SPITransfer (byte c)
{       
  byte bit;
   
  for (bit = 0; bit < 8; bit++)
    {
    // write MOSI on falling edge of previous clock
    if (c & 0x80)
        BB_MOSI_PORT |= _BV (BB_MOSI_BIT);
    else
        BB_MOSI_PORT &= ~_BV (BB_MOSI_BIT);
    c <<= 1;

    // read MISO
    c |= (BB_MISO_PORT & _BV (BB_MISO_BIT)) != 0;

   // clock high
    BB_SCK_PORT |= _BV (BB_SCK_BIT);

    // delay between rise and fall of clock
    delayMicroseconds (BB_DELAY_MICROSECONDS);

    // clock low
    BB_SCK_PORT &= ~_BV (BB_SCK_BIT);

    // delay between rise and fall of clock
    delayMicroseconds (BB_DELAY_MICROSECONDS);
    }
   
  return c;
  }  // end of BB_SPITransfer


void setup () { }
void loop () { }


You'll have to change the constants to suit.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

diesel

Nick,

Thanks I will give that a try. I confirmed it was xck0 pin not being accessible that was the problem on the arduino mega. Bought a seeduino mega board that has the xck0(pe2) accessible, got spi on uart working on this board.

Another question, can you switch uart line back over to serial mode by just doing Serial.begin(#)? I would think you could. Should you make any other register changes?

Thanks,

-diesel


Nick Gammon

Serial.begin should do it, as far as I know.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up