如何在arduino uno和dspic33ep512mc502之间建立spi连接?

时间:2015-07-05 18:10:36

标签: arduino-uno microchip dspic

  • 我只想测试arduino uno和dspic33ep512mc502之间的spi通信,我使用串口看微控制器接收数据; -arduino uno是大师 -dspic33ep512mc502是slave

这是我的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);


}

这里是dspic33ep512mc502代码(仅适用于spic comunication):

初始化:

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;


    };

我希望在串口监视器上看到值3,但我只看到值7或255.

1 个答案:

答案 0 :(得分:1)

SPI同时读写,因此写入寄存器后必须读取,否则下一次读取不会起作用。

因此在编写之后尝试阅读spi1buf。