2016-09-24 123 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 
    }