Arduino Can';t不要同时使用两个SPI设备

Arduino Can';t不要同时使用两个SPI设备,arduino,Arduino,我可以使用以下代码使用MFRC522: #include <SPI.h> #include <MFRC522.h> MFRC522 mfrc522(10, 9); void setup() { SPI.begin(); mfrc522.PCD_Init(); } void loop() { if ( mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial

我可以使用以下代码使用MFRC522:

#include <SPI.h>
#include <MFRC522.h>

MFRC522 mfrc522(10, 9);

void setup() {
    SPI.begin();
    mfrc522.PCD_Init(); 
}

void loop() {
    if (  mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
         // Do stuff
    }
}
#include "LedControl.h"

LedControl lc = LedControl(12,11,8,1);

void setup() {
    lc.shutdown(0,false);
    lc.setIntensity(0,3);
    lc.clearDisplay(0);

    lc.setLed(0,2,5,true);
    lc.setLed(0,5,5,true);

    lc.setLed(0,2,2,true);
    lc.setLed(0,3,1,true);
    lc.setLed(0,4,1,true);
    lc.setLed(0,5,2,true);
}

void loop() {
}
#include <SPI.h>
#include <MFRC522.h>
#include "LedControl.h"

LedControl lc = LedControl(12,11,8,1);
MFRC522 mfrc522(10, 9);

void setup() {
    SPI.begin();
    mfrc522.PCD_Init();

    lc.shutdown(0,false);
    lc.setIntensity(0,3);
    lc.clearDisplay(0);

    lc.setLed(0,2,5,true);
    lc.setLed(0,5,5,true);

    lc.setLed(0,2,2,true);
    lc.setLed(0,3,1,true);
    lc.setLed(0,4,1,true);
    lc.setLed(0,5,2,true); 
}

void loop() {
    if (  mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
         // Do stuff
    }
}
而且效果也不错。但是,当我尝试使用以下代码同时使用它们时:

#include <SPI.h>
#include <MFRC522.h>

MFRC522 mfrc522(10, 9);

void setup() {
    SPI.begin();
    mfrc522.PCD_Init(); 
}

void loop() {
    if (  mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
         // Do stuff
    }
}
#include "LedControl.h"

LedControl lc = LedControl(12,11,8,1);

void setup() {
    lc.shutdown(0,false);
    lc.setIntensity(0,3);
    lc.clearDisplay(0);

    lc.setLed(0,2,5,true);
    lc.setLed(0,5,5,true);

    lc.setLed(0,2,2,true);
    lc.setLed(0,3,1,true);
    lc.setLed(0,4,1,true);
    lc.setLed(0,5,2,true);
}

void loop() {
}
#include <SPI.h>
#include <MFRC522.h>
#include "LedControl.h"

LedControl lc = LedControl(12,11,8,1);
MFRC522 mfrc522(10, 9);

void setup() {
    SPI.begin();
    mfrc522.PCD_Init();

    lc.shutdown(0,false);
    lc.setIntensity(0,3);
    lc.clearDisplay(0);

    lc.setLed(0,2,5,true);
    lc.setLed(0,5,5,true);

    lc.setLed(0,2,2,true);
    lc.setLed(0,3,1,true);
    lc.setLed(0,4,1,true);
    lc.setLed(0,5,2,true); 
}

void loop() {
    if (  mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
         // Do stuff
    }
}
#包括
#包括
#包括“LedControl.h”
LedControl lc=LedControl(12,11,8,1);
MFRC522 MFRC522(10,9);
无效设置(){
SPI.begin();
mfrc522.PCD_Init();
lc.停机(0,假);
lc.设定强度(0,3);
lc.清晰显示(0);
lc.设定值(0,2,5,正确);
lc.设定值(0,5,5,正确);
lc.设定值(0,2,2,真);
lc.设定值(0,3,1,真);
lc.设定值(0,4,1,真);
lc.设定值(0,5,2,正确);
}
void循环(){
if(mfrc522.PICC_IsNewCardPresent()&mfrc522.PICC_ReadCardSerial()){
//做事
}
}

在这种情况下,它们中只有一个起作用(MFRC522)。我知道,由于它们是在SPI模式下连接的,它们需要有不同的SS引脚,所以我使用引脚10表示MFRC522,引脚8表示点阵。那么,怎么了?为什么dot matrix与MFRC522不能在同一代码上工作?如果手头没有数据表,我首先怀疑这两个SPI设备的时钟频率不兼容。您需要找到每台设备的时钟频率,或者关闭两个不同的计时器,或者在单个计时器上切换计时,为当前选定的设备提供正确的时钟频率。不兼容的时钟频率是我在SPI设备上遇到的唯一问题。

他也在上发布了它,这是因为MRF库使用HW
SPI
LedControl
使用SW
SPI
。硬件一阻止(至少)引脚
12
digitalWrite
shiftOut
功能中使用的
digitalWrite
进行设置/重置。这肯定是一个更具决定性的答案。我会投赞成票我对SPI问题的唯一体验是硬件SPI设备,它们需要非常不同的SCK速率。HW SPI CLK引脚为13,对于LED控制,他使用了11。