从Raspberry Slave从Raspberry PI PI SPI读取与wireingPI2?

猪油概念

我已经在NOOBS Raspbian PI发行版上安装connectionpi2twitterpi2 python包装器Adafruit 4通道逻辑电平转换器使PI不受5v的影响,并且在PI端将数据发送到Arduino如此简单:

import wiringpi2
wiringpi2.wiringPiSPISetup(1,5000)
wiringpi2.wiringPiSPIDataRW(1,'HELLO WORLD\n')

以及相应的Arduino代码[3]。

编辑:抱歉-从现在开始,我不能再发布我精心添加的链接以显示我的工作,源代码和示例代码。您必须使用Google并感谢2链接规则。

因此,我知道接线的工作原理。但这不是我真正想要的方式-我想读取Arduino到PI的引脚。

Arduino SPI参考指出:

该库允许您以Arduino为主要设备与SPI设备进行通信

PI必须是主设备。我以为自己注定要失败,直到阅读了Nick Gammon关于SPI的精彩页面,该页面演示了2 Arduinii互相交谈。

另外,该SPI transfer()命令建议您可以从Arduino编写。

我现在处于Google的前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
}
}

在PI方面:

import wiringpi2
wiringpi2.wiringPiSPISetup(1,5000)
stuff = wiringpi2.wiringPiSPIDataRW(1,'\n')
print stuff

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.
}

记住,您是GOTTAGOFAST。如果arduino在下一条SPI消息到达之前没有退出该中断服务程序,则一切都会陷入困境。

另外,还要检查以确保Pi和Arduino之间的时钟极性和相位相同(否则称为模式0-3)。

| 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)

因此要打开SPI,SPI中断并将极性设置为...无论是什么...

SPCR |= _BV(SPIE) | _BV(SPE) | _BV(CPOL) ;

无论如何,我花了几天时间与Arduino SPI交流,这就是我学到的东西。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章