- 我只想测试 arduino uno 和 dspic33ep512mc502 之间的 spi 通信,我使用串口查看从微控制器接收数据;-arduino uno 是主机 -dspic33ep512mc502 是从机
这是我的 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 通信):
初始化:
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;
SPI1中断:
void __attribute__((__interrupt__)) _SPI1Interrupt(void)
{
IFS0bits.SPI1IF = 0;
SPI1STATbits.SPIROV = 0;
SPI1BUF=3;
};
我希望在串行监视器上看到值 3,但我只看到值 7 或 255。