0
  • i just want to test spi comunication between arduino uno and dspic33ep512mc502, i use serial port to see receiving data from microcontroller; -arduino uno is master -dspic33ep512mc502 is slave

here is my code for Arduino Uno :

#include<SPI.h>

byte y=1;
void setup() {

Serial.begin(9600);
SPI.begin(); 
SPI.setDataMode(SPI_MODE1);     
 digitalWrite(SS,HIGH);
// SPI.setBitOrder( LSBFIRST);
 SPI.setBitOrder( MSBFIRST);
}

int trnasfer_spi(int d)
{
digitalWrite(SS,LOW);  
byte r=SPI.transfer(7); 
digitalWrite(SS,HIGH);
return r; 
}

void loop() {

 if (Serial.available() > 0) {
     // read the incoming byte:
        int incomingByte = Serial.read();
        y=trnasfer_spi(incomingByte);
         }       

Serial.print(y, DEC);
delay(1000);


}

here is dspic33ep512mc502 code(just for spic comunication):

initialization :

ANSELBbits.ANSB0 = 0; 
TRISBbits.TRISB0 = 1;  
TRISBbits.TRISB7 = 1;  
TRISBbits.TRISB8 = 0;  
TRISBbits.TRISB9 = 1;  
SPI1CON1bits.DISSCK = 0;
SPI1CON1bits.DISSDO = 0; 
SPI1CON1bits.MODE16 = 0; 
SPI1CON1bits.SMP = 0;    
SPI1CON1bits.CKP = 0;    
SPI1CON1bits.CKE = 1;    
SPI1CON1bits.MSTEN = 0;  
SPI1CON1bits.SSEN = 1;   

//Setari registru SPI1STAT
SPI1STATbits.SPIROV = 0; 
SPI1STATbits.SPIEN  = 1; 

IFS0bits.SPI1IF = 0; 
IEC0bits.SPI1IE = 1; 
IPC2bits.SPI1IP = 6; 
SPI1BUF =3; 

SPI1Interrupt:

 void __attribute__((__interrupt__)) _SPI1Interrupt(void)
 {

    IFS0bits.SPI1IF = 0;           
    SPI1STATbits.SPIROV = 0;       
    SPI1BUF=3;


    };

i expect to see value 3 on serial monitor, but i see only value 7 or 255.

kederrac
  • 16,819
  • 6
  • 32
  • 55
  • I have no idea, but doesn't SS pin have to be configured as output using pinMode()? – Palo Jul 05 '15 at 18:25
  • spi library already has configure all necessary pins (including SS) – kederrac Jul 05 '15 at 18:29
  • https://www.arduino.cc/en/Reference/SPI – kederrac Jul 05 '15 at 18:29
  • Why do you think so? "All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative." It seems to be the job of the library user to set the pin as output. – Palo Jul 06 '15 at 14:08

1 Answers1

1

SPI reads and writes at the same time, so a write to the register must be followed by a read, otherwise the next read won't work.

So try to read spi1buf after writing it.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89