如何在arduino uno和dspic33ep512mc502之间建立spi连接?
how to make spi connection between arduino uno and dspic33ep512mc502?
- 我只是想测试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。
SPI是同时读和写的,所以写寄存器必须先读,否则下一次读不成功。
所以尝试写完spi1buf再读
- 我只是想测试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。
SPI是同时读和写的,所以写寄存器必须先读,否则下一次读不成功。
所以尝试写完spi1buf再读