Nick,
Thanks for posting the code to create SPI port on uart pins. I am, however, having a problem with it. I tried setting up a simple test. I am using an Arduino Uno w/no devices attached. Since I was having a problem, I tried this simple test using std SPI library, works as you would expect. I am using FTDI breakout from Sparkfun to read. hello there, hello there....
#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial DebugPort = SoftwareSerial(2,3);
char mystring[100]="hello there";
char mystring2[100]="";
void setup() {
// put your setup code here, to run once:
DebugPort.begin(9600);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setDataMode(SPI_MODE0);
}
void loop() {
uint8_t x=0;
digitalWrite(SS,LOW);
for(x;x<(strlen(mystring));x++){
mystring2[ x ] = SPI.transfer( mystring [ x ] ) ;
}
digitalWrite(SS,HIGH);
mystring2[x+1]='\0';
DebugPort.println(mystring2);
}
*************
Then I tried a similar setup using the code you posted. On this I get an output of ......,......,...... (yes dots). Do you know what may be the problem?
************
#include <SoftwareSerial.h>
SoftwareSerial DebugPort = SoftwareSerial(2,3);
const byte MSPIM_SCK = 4;
const byte MSPIM_SS = 5;
char mystring[100]="hello there";
char mystring2[100]="";
// sends/receives one byte
char MSPIMTransfer (char c)
{
// wait for transmitter ready
while ((UCSR0A & _BV (UDRE0)) == 0)
{}
// send byte
UDR0 = c;
// wait for receiver ready
while ((UCSR0A & _BV (RXC0)) == 0)
{}
// receive byte, return it
return UDR0;
} // end of MSPIMTransfer
// select slave, write a string, wait for transfer to complete, deselect slave
void spiWriteString (const char * str)
{
if (!str) return; // Sanity Clause
char c;
// enable slave select
digitalWrite (MSPIM_SS, LOW);
// const char * mystr = str;
uint8_t count =0;
// send the string
while (c = *str++){
mystring[count] =MSPIMTransfer (c);
}
// wait for all transmissions to finish
while ((UCSR0A & _BV (TXC0)) == 0)
{}
// disable slave select
digitalWrite (MSPIM_SS, HIGH);
} // end of spiWriteString
void setup()
{
DebugPort.begin(9600);
pinMode (MSPIM_SS, OUTPUT); // SS
// must be zero before enabling the transmitter
UBRR0 = 0;
UCSR0A = _BV (TXC0); // any old transmit now complete
pinMode (MSPIM_SCK, OUTPUT); // set XCK pin as output to enable master mode
UCSR0C = _BV (UMSEL00) | _BV (UMSEL01); // Master SPI mode
UCSR0B = _BV (TXEN0) | _BV (RXEN0); // transmit enable and receive enable
// must be done last, see page 206
UBRR0 =3;// 3; // 2 Mhz clock rate
} // end of setup
void loop()
{
uint8_t x=0;
digitalWrite(MSPIM_SS,LOW);
for(x;x<(strlen(mystring));x++){
mystring2 [ x ] =MSPIMTransfer( mystring [ x ] );
}
digitalWrite(MSPIM_SS,HIGH);
mystring2[x+1]='\0';
DebugPort.println(&mystring2[0]);
}
Thanks,
-diesel
Moderator edit: [code] ... [/code] tags added. (Nick Gammon)