Hi All,
I try to communicate with an Arduino Pro Atmega 328, actually part of the Mongoose IMU. Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino. First I knew nothing about SPI and asked a question here http://arduino.cc/forum/index.php/topic,96326.0.html and Nick Gammon provided me with extremely usefull information (Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino) but I'm ashamed to say I still can't get it to work.
I want mij microprocessor (NetMF framework) to be the master and communicate with the Arduino. If the microprosessor sends out one byte "start updating me" the Arduino should send back the actual values.
I've created this on the Arduino:
void setup()
{
SPI.begin();
// SPI.setDataMode(SPI_MODE1);
// SPI.setBitOrder(MSBFIRST);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// turn on interrupts
SPCR |= _BV(SPIE);
}
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR;
if(c == 8)
{
SPDR = 16;
}
else
{
SPDR = 255;
}
} // end of interrupt service routine (ISR) SPI_STC_vect
And this on the NetMF:
SpiCom = new SPI(new SPI.Configuration(Cpu.Pin.GPIO_NONE,
false, 0,0, true, false, 4000, SPI.SPI_module.SPI1));
byte[] ReadBuffer = new byte[1];
// Create and fill write buffer
byte[] WriteBuffer = new byte[1];
WriteBuffer[0] = 4;
while (1 == 1)
{
SpiCom.WriteRead(WriteBuffer, ReadBuffer);
Debug.Print(ReadBuffer[0].ToString());
}
I would have expected 16 on the readbuffer if I would send 8 and 255 otherwise. But somehow It sends back 6 if I write 4 and 12 if I write 8. I can't figure out why. I don't use a slave select pin, there is only one slave. This is why I pass Cpu.Pin.GPIO_NONE as slave select pin on NetMf. ChipSelect active state = false, setuptime and hold time = 0, clock idle state = true, clock edge = false, ClockRate = 4000 Khz (I read somewhere that Arduino would be at 4Mhz?). SPI module = SPI1, whatever that might be. I also used about every possible other combination of the parameters on NetMF.
On Arduino I tried SPI.setDataMode(SPI_MODE1); and SPI.setBitOrder(MSBFIRST); as well, but no better results. I could also remove SPI.Begin(); and get the same results.
I did blink the status led within the ISR (SPI_STC_vect) routine and that seems to work. But the bytes don't match to what I would have expect. I thought SPDR would have been the byte incomming from the master and could be altered to represent the result from the slave.
I can't find anything on google about connecting NetMf to Arduino over SPI.
Thanks for taking the time to read this. Any information about what I'm doing wrong is very much appreciated. I don't have any Arduino skills.