簡體   English   中英

Arduino Uno和Raspberry Pi之間的SPI通信使用connectioningPi

[英]SPI communication between Arduino Uno and Raspberry Pi using wiringPi

我一直在嘗試將Raspberry Pi 2 B +(主機)與Arduino Uno Rev3(從機)之間的SPI通信實現不成功。

我使用了該教程: http : //mitchtech.net/raspberry-pi-arduino-spi/

但是,那里提供的代碼無法正常工作。 我已經在整個互聯網上搜索了一種解決方案,但是找不到。 我在網站上對arduino使用相同的代碼,但對樹莓派使用以下代碼:

/**
 *  Hello, SPI!
 */

 #include <stdio.h> // printf()
 #include <signal.h> // signal()
 #include <errno.h> // strerro
 #include <string.h>

 #include <wiringPi.h> // GPIO
 #include <wiringPiSPI.h> // SPI

 int volatile interrupt = 0;
 #define len_max 100
 int volatile len = 0;
 unsigned char buffer[len_max];
 static const int speed = 500000;

 int const CE0 = 0;

 void sig_handler(int signo)
 {
   if(signo == SIGINT)
   {
     interrupt = 1;
   }
 }

 void setup(void)
 {
   signal(SIGINT, sig_handler);

   wiringPiSetupGpio () ;

   if(wiringPiSPISetup(CE0, speed) < 0)
   {
     printf("SPI setup failed: %s\n", strerror(errno));
     interrupt = 1;
   }

   printf("System ready.\n");
 }

 void loop(void)
 {
   memcpy(buffer, "Hello world!\n", sizeof buffer);
   len = 12;
   if( wiringPiSPIDataRW (CE0, buffer, len) < 0)
     printf("Error while recieving mesage\n");
   printf("Received mesage: %s \n", buffer);
   delay(1000);
 }

 void close(void)
 {
   printf("Ending activities.\n");
 }

 int main(void)
 {
   setup();
   while(!interrupt) loop();
   close();
   return 0;
 }

這段代碼只會給我帶來垃圾。 我不知道該怎么辦。

如果要發布執行代碼時得到的輸出,這將有助於找出問題所在。 另外,看起來您所參考的教程使用的是pi模型B的原理圖設置。我相信b +具有15個額外的gpio引腳,因此引腳分配也不同,因此您可能需要仔細檢查所有物理連接是否正確制作。

以下Arduino的C代碼也受到您相同代碼的啟發。 一定要為Arduino實現SPI從設備。 我將USB .NET兼容設備NUSBIO用作MASTER SPI,並與Arduino進行了交談。

該代碼的確以10Kbyte / s的速度運行。 就目前而言,我正在丟失數據。

`

 include <Adafruit_NeoPixel.h>
 include <SPI.h>
 include <fArduino.h>

#define API_SIZE 128
char buf[API_SIZE];

volatile int bufCheckSum = 0;
volatile int pos = 0;
volatile boolean process_it = false;

void spiSetup(void)
{
    // Move from SPI_CLOCK_DIV4 to SPI_CLOCK_DIV2
    //SPI.setClockDivider(SPI_CLOCK_DIV2);

    SPCR |= bit(SPE); // turn on SPI in slave mode
    pinMode(MISO, OUTPUT); // have to send on master in, *slave out*
    pos = 0;
    process_it = false;
    SPI.attachInterrupt(); // now turn on interrupts
}

// SPI interrupt routine
ISR(SPI_STC_vect)
{
    byte c = SPDR;        // Grab byte from SPI Data Register

    if (pos < sizeof buf) // Add to buffer if room
    {
        if (c == '\r') { // Performance test

            bufCheckSum = 0;
            for (int i = 0; i < pos; i++) {
                bufCheckSum += buf[i];
            }
            pos = 0; // Reset buffer
            SPDR = 1;
            process_it = true;
        }
        else {
            buf[pos++] = c; // Store c in buffer
            SPDR = c;
        }
    }
    else {
        pos = 0;
    }
}

void setup() {

    Board.Delay(1000 * 2); // Wait 1.5 second before initializing the serial com, so  I can start the ArduinoWindowsConsole on the Windows machine
    Board.InitializeComputerCommunication(9600, "Arduino Spi Slave +");
    Board.SetPinMode(10, INPUT);
    Board.SetPinMode(2, INPUT); // JUST FOR TESTING
    spiSetup();
}

void loop() {

    if (process_it) {

        process_it = false;
        if (bufCheckSum != 8255) {
            Serial.println("ko ");
        }
        bufCheckSum = -1;
    }
}

`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM