2016-09-24 122 views
0

進出口試圖對接ESP12-E模塊ILI9341顯示320 * 240和PN532 RFID閱讀器相同的SPI總線上。我在不同的GPIO上分配了SS引腳。多個從SPI上ESP8266 - PN532和ILI9341

林無法與兩者進行通信。顯示在任何情況下都能完美運行但是,一旦我與ILI9341通信,PN532停止工作,它將不會響應,直到我重新啓動設備,即使我重新初始化它。

任何幫助將高度讚賞 我的代碼:

#include <Wire.h> 
#include <SPI.h> 
#include <Adafruit_PN532.h> 
#include <UTFT.h> 
UTFT lcd(ILI9341_S5P,15,D1,D3); 

// If using the breakout with SPI, define the pins for SPI communication. 
#define PN532_SS (D4) 
#define PN532_SCK (D5) 
#define PN532_MOSI (D7) 
#define PN532_MISO (D6) 

// If using the breakout or shield with I2C, define just the pins connected 
// to the IRQ and reset lines. Use the values below (2, 3) for the shield! 
#define PN532_IRQ (2) 
#define PN532_RESET (3) // Not connected by default on the NFC Shield 

// Uncomment just _one_ line below depending on how your breakout or shield 
// is connected to the Arduino: 

// Use this line for a breakout with a SPI connection: 
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS); 

// Use this line for a breakout with a hardware SPI connection. Note that 
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's 
// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are 
// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin. 
//Adafruit_PN532 nfc(PN532_SS); 

// Or use this line for a breakout or shield with an I2C connection: 
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); 
extern uint8_t BigFont[]; 

void setup(void) { 
    Serial.begin(115200); 
    Serial.println("Hello!"); 
    nfc.begin(); 

    uint32_t versiondata = nfc.getFirmwareVersion(); 
    if (! versiondata) { 
    Serial.print("Didn't find PN53x board"); 
    while (1); // halt 
    } 

    // Got ok data, print it out! 
    Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
    Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
    Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); 
    lcd.InitLCD(); 
    lcd.setColor (0, 0, 0); 
    lcd.fillRect(1,1,319,239); 
    lcd.setColor (255, 255, 255); 
    lcd.fillRect(100,100,220,140); 
    lcd.setFont (BigFont); 
    lcd.print(String("Scanning"),0,0); 
    // Set the max number of retry attempts to read from a card 
    // This prevents us from waiting forever for a card, which is 
    // the default behaviour of the PN532. 
    nfc.setPassiveActivationRetries(0xFF); 

    // configure board to read RFID tags 
    nfc.SAMConfig(); 

    Serial.println("Waiting for an ISO14443A card"); 
} 

void loop(void) { 
    boolean success; 
    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID 
    uint8_t uidLength;    // Length of the UID (4 or 7 bytes depending on ISO14443A card type) 

    // Wait for an ISO14443A type cards (Mifare, etc.). When one is found 
    // 'uid' will be populated with the UID, and uidLength will indicate 
    // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) 
    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,25); 

    if (success) { 
    Serial.println("Found a card!"); 
    Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); 
    Serial.print("UID Value: "); 
    for (uint8_t i=0; i < uidLength; i++) 
    { 
     Serial.print(" 0x");Serial.print(uid[i], HEX); 
    } 
    Serial.println(""); 
    // Wait 1 second before continuing 
    delay(1000); 
    } 
    else 
    { 
    // PN532 probably timed out waiting for a card 
    //Serial.println("Timed out waiting for a card"); 
    } 
} 

回答

0

我已經測試了很多想通了 - 該SPI位順序是不同的,所以你不能開車在這兩種設備相同的SPI。

但是你可以使用SW-SPI(bitbanging)的NFC模塊,因爲這是沒有必要的駕駛它快。而TFT必須快速更新。

的一個問題是仍然在我的設置打開: 顯示屏沒有顯示內容,如果你有一個開放的串行終端。但是當它關​​閉時,TFT和NFC都可以正常工作。

這裏是我的代碼:

#include <ESP8266WiFi.h> 
#include <Wire.h> 
#include <SPI.h> 
#include <Adafruit_PN532.h> 
#include <UTFT.h> 

// NFC module 
#define PN532_SCK D1 
#define PN532_MOSI D2 
#define PN532_MISO D3 
#define PN532_SS D0 
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS); 

// TFT display 
// HSPI defines 
#define TFT_SCK  D5 
#define TFT_MOSI D7 
//#define TFT_MISO D6 (not connected) 
#define TFT_CS  D8 
#define TFT_DC  D4 
UTFT myGLCD(ILI9341_S5P, TFT_CS, -1, TFT_DC); 

// Declare which fonts we will be using 
extern uint8_t SmallFont[]; 
extern uint8_t BigFont[]; 

// forward declaration of helper function to get UID as HEX-String 
void byteToHexString(String &dataString, byte *uidBuffer, byte bufferSize, String strSeperator); 

void setup() { 
    Serial.begin(9600); 

    Serial.println("Initial nfc module"); 
    nfc.begin(); 
    uint32_t versiondata = nfc.getFirmwareVersion(); 
    if (! versiondata) { 
    Serial.print("Didn't find PN53x board"); 
    while (1); // halt 
    } 

    // Got ok data, print it out! 
    Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
    Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
    Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); 

    // Set the max number of retry attempts to read from a card 
    // This prevents us from waiting forever for a card, which is 
    // the default behaviour of the PN532. 
    nfc.setPassiveActivationRetries(0xFF); 

    // configure board to read RFID tags 
    nfc.SAMConfig(); 

    Serial.println("Waiting for an ISO14443A card"); 
    Serial.println("Initial tft display"); 

    myGLCD.InitLCD(); 
    myGLCD.setColor(0, 0, 0); 
    myGLCD.fillRect(1,1,319,239); 
    myGLCD.setColor(255, 255, 255); 
    myGLCD.setFont(BigFont); 
    myGLCD.print(String("Scanning"),0,0); 
} 

void loop(void) { 
    boolean success; 
    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID 
    uint8_t uidLength;    // Length of the UID (4 or 7 bytes depending on ISO14443A card type) 

    // Wait for an ISO14443A type cards (Mifare, etc.). When one is found 
    // 'uid' will be populated with the UID, and uidLength will indicate 
    // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) 
    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); 
    //success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,25); 

    if (success) { 
    Serial.println("Found a card!"); 
    Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); 
    Serial.print("UID Value: "); 
    String strUID; 
    // store UID as HEX-String to strUID and print it to display 
    byteToHexString(strUID, uid, uidLength, "-"); 
    Serial.println(""); 
    Serial.println(strUID); 
    myGLCD.print(strUID, CENTER, 50); 
    myGLCD.setColor (255, 0, 0); 
    myGLCD.setFont (BigFont); 
    myGLCD.print(String("Scanning"),0,0); 
    // Wait 1 second before continuing 
    delay(1000); 
    } 
    else 
    { 
    Serial.println("Timed out or waiting for a card"); 
    } 
} 

// helper function to get UID as HEX-String 
void byteToHexString(String &dataString, byte *uidBuffer, byte bufferSize, String strSeperator=":") { 
    dataString = ""; 
    for (byte i = 0; i < bufferSize; i++) { 
    if (i>0) { 
     dataString += strSeperator; 
     if (uidBuffer[i] < 0x10) 
     dataString += String("0"); 
    } 
    dataString += String(uidBuffer[i], HEX); 
    } 
    dataString.toUpperCase(); 
} 
0

感謝您的幫助。 我不是一個bitbanging SPI的忠實粉絲。 我已經嘗試了多種方法,並試圖重新初始化等 最後,我得到它解決。

我不知道爲什麼,但在閱讀卡後,當我重新初始化SPI硬件,這兩個模塊的完美的作品。

我只是閱讀卡後添加代碼:

lcd._hw_special_init(); 

,如:

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,0); 
    if (success) { 
    lcd._hw_special_init(); 
    //Do the rest 
    }