Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Raspberry PI SPI从Arduino从机读取wiringPI2?_Arduino_Raspberry Pi_Raspbian_Spi - Fatal编程技术网

Raspberry PI SPI从Arduino从机读取wiringPI2?

Raspberry PI SPI从Arduino从机读取wiringPI2?,arduino,raspberry-pi,raspbian,spi,Arduino,Raspberry Pi,Raspbian,Spi,我已经在NOOBS Raspbian PI发行版上安装了和。Adafruit 4通道逻辑电平转换器使PI在5v电压下保持安全,并在PI侧向Arduino发送数据就这么简单: import wiringpi2 wiringpi2.wiringPiSPISetup(1,5000) wiringpi2.wiringPiSPIDataRW(1,'HELLO WORLD\n') 以及相应的Arduino代码[3] 编辑:抱歉-从现在开始,我不能再发布我为展示我的工作、源代码和示例代码而精心添加的链接了。

我已经在NOOBS Raspbian PI发行版上安装了和。Adafruit 4通道逻辑电平转换器使PI在5v电压下保持安全,并在PI侧向Arduino发送数据就这么简单:

import wiringpi2
wiringpi2.wiringPiSPISetup(1,5000)
wiringpi2.wiringPiSPIDataRW(1,'HELLO WORLD\n')
以及相应的Arduino代码[3]

编辑:抱歉-从现在开始,我不能再发布我为展示我的工作、源代码和示例代码而精心添加的链接了。你必须用谷歌搜索它,并感谢双链接规则

所以,我知道接线是有效的。但这并不是我真正想要的方式——我想要读一个从Arduino到PI的pin

Arduino SPI参考说明:

此库允许您与SPI设备进行通信,将Arduino作为主设备。

PI必须是主设备。我以为我是命中注定的,直到我读到尼克·金蒙(Nick Gammon)关于SPI的精彩页面,该页面展示了两个Arduini相互交谈

另外,
SPI transfer()
命令建议您可以从Arduino进行写入

我现在正处于这样一个阶段,谷歌前4个结果页面的所有链接都显示为“跟随”——所以这并不是因为缺少谷歌搜索

从理论上讲,如果我在PI端使用READ方法,这不应该起作用吗?(注意:这只是众多尝试中的一次,而不是唯一一次!)

关于Arduino:

#include <SPI.h>
void setup (void)
{
  SPI.begin();
  pinMode(MISO, OUTPUT);

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);
}

void loop  (void) {
byte data[] = {0x00, 0x00, 0x00, 0x00};  // this is 24 bits (8bits/byte * 4 bytes)
// Transfer 24 bits of data
for (int i=0; i<4; i++) {
   SPI.transfer(data[i]);   // Send 8 bits
}
}
WiringPI说传入的数据将覆盖我的数据,而SPIDataRW只接受2个输入,所以我不应该得到“测试”吗


我错过了什么?非常感谢任何指针。

SPI库假定您希望arduino充当主控器。你不能用它让arduino充当奴隶。有时,你不得不从库中潜入芯片的数据表,看看事情是如何运作的。(然后,理想情况下,从你所有的烦恼中建立一个图书馆)

SPI从设备必须对发起通信的主设备作出反应

因此,作为SPI主机的Pi必须通过MOSI线路发送虚拟字节,并读取Arduino在MISO线路上的回复。也就是说,主人发起了沟通

在arduino端,您可以通过以下方式打开SPI中断:

SPCR |= _BV(SPIE);
它内置在atmega328芯片中。因此,在arduino端包含下一位以查看传入消息,并设置下一条消息的响应。当主设备发送消息时,arduino SPI从设备响应的数据是数据寄存器中的任何数据

int gCurrentSpiByte;  //or set up your a buffer or whatever
ISR (SPI_STC_vect)
{
  gCurrentSpiByte = SPDR;  // grab byte from SPI Data Register
  SPDR = buf[messageCount++]; //Set the data to be sent out on the NEXT message.
}
记住,你是哥达哥夫斯特。如果arduino在下一条SPI消息到来之前没有退出中断服务例程,那么一切都将化为乌有

此外,检查以确保Pi和Arduino之间的时钟极性和相位相同(也称为模式0-3)

所以要打开SPI,SPI中断,并将极性设置为。。。不管那是什么

SPCR |= _BV(SPIE) | _BV(SPE) | _BV(CPOL) ;
无论如何,我花了几天时间和Arduino SPI打交道,这就是我学到的

| 7    | 6    | 5    | 4    | 3    | 2    | 1    | 0    |
| SPIE | SPE  | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 |

SPIE - Enables the SPI interrupt when 1
SPE - Enables the SPI when 1
DORD - Sends data least Significant Bit First when 1, most Significant Bit first when 0
MSTR - Sets the Arduino in master mode when 1, slave mode when 0
CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0
CPHA - Samples data on the falling edge of the data clock when 1, rising edge when 0
SPR1 and SPR0 - Sets the SPI speed, 00 is fastest (4MHz) 11 is slowest (250KHz)
SPCR |= _BV(SPIE) | _BV(SPE) | _BV(CPOL) ;