繁体   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