不能同时使用两个SPI设备

时间:2017-05-25 19:44:43

标签: 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() ) {
         // Do stuff
    }
}

它很棒。我也可以使用以下代码使用点阵(8x8):

#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
    }
}

在这种情况下,只有其中一个有效(MFRC522)。我知道,由于它们以SPI模式连接,因此需要使用不同的SS引脚,因此我将引脚10用于MFRC522,将引脚8用于点阵。那么,有什么不对?为什么点阵不能与MFRC522相同的代码工作?

1 个答案:

答案 0 :(得分:1)

如果没有手头的数据表,我首先会怀疑这两个SPI设备的时钟速率是不兼容的。您需要找到每个时钟速率,并将它们从两个不同的定时器中取出,或者在单个定时器上切换定时,以便为当前所选设备提供正确的时钟速率。不兼容的时钟速率是我用SPI设备唯一的问题。