我在树莓派上使用spidev,这是我的示例代码,将10和11发送到arduino,但现在我想在树莓派上收到“你好”
import time
import spidev
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=100000
while True:
resp = spi.xfer([0x31,0X31,0x0A])
print resp
time.sleep(1)
resp1 = spi.xfer([0x31,0x30,0x0A])
print resp1
time.sleep(1)
上面的代码是...。将10和11发送到arduino,下面给出的代码是写 7,8点 注意:我想收到从arduino到树莓派的“ hello”
#include <SPI.h>
#include <stdlib.h>
char buf [100];
volatile byte pos;
volatile boolean process_it;
void setup (void)
{
//Start the Serial for the debugging
Serial.begin (115200);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
pinMode(53,INPUT);
//Setting up the LED pin as OUTPUT
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR;
// grab byte from SPI Data Register
if(digitalRead(53)==0){
// add to buffer if room
if (pos < sizeof buf)
{
buf [pos++] = c;
// example: newline means time to process buffer
if (c == '\n')
process_it = true;
} // end of room available
}
} // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
int buff = atoi(buf);
Serial.println (buff);
switch(buff){
case 10:
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
break;
case 11:
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
break;
}
pos = 0;
process_it = false;
} // end of flag set
}
先谢谢您
答案 0 :(得分:1)
在SPI连接中,主机必须发送一些数据才能从机接收数据。就像循环缓冲区(See an image of "Data transmission" section)。
要完成所需的操作,主机必须再发送5个字节才能从从机获取“ h”,“ e”,“ l”,“ l”,“ o”。
这是素描的例子。
#include <SPI.h>
#include <stdlib.h>
char in_buf[100];
char out_buf[20] = "hello";
volatile int in_pos;
volatile int out_pos;
volatile boolean is_sending;
volatile boolean process_it;
void setup (void)
{
//Start the Serial for the debugging
Serial.begin (115200);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
pinMode(SS,INPUT);
//Setting up the LED pin as OUTPUT
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// get ready for an interrupt
in_pos = 0; // in_buf empty
out_pos = 0; // out_buf empty
is_sending = false;
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
if(is_sending == false){
// add to in_buf if room
if (in_pos < sizeof(in_buf))
{
in_buf[in_pos++] = c;
// example: newline means time to process buffer and to send out_buf
if (c == '\n')
{
is_sending = true;
process_it = true;
SPDR = out_buf[out_pos++]; // send first byte
}
} // end of room available
}
else
{
SPDR = out_buf[out_pos];
if(out_buf[out_pos] == 0 || ++out_pos >= sizeof(out_buf))
{
is_sending = false;
out_pos = 0;
}
}
} // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if(process_it)
{
in_buf[in_pos] = 0;
int buff = atoi(in_buf);
Serial.println(buff);
switch(buff){
case 10:
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
break;
case 11:
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
break;
}
in_pos = 0;
process_it = false;
} // end of flag set
}
这是python代码的示例。
import time
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 100000
while True:
resp = spi.xfer([0x31, 0X31, 0x0A, 0x01, 0x01, 0x01, 0x01, 0x01])
print(''.join([chr(_) for _ in resp[-5:]]))
time.sleep(1)
resp1 = spi.xfer([0x31, 0x30, 0x0A, 0x01, 0x01, 0x01, 0x01, 0x01])
print(''.join([chr(_) for _ in resp1[-5:]]))
time.sleep(1)
我认为以下以下网站对您也很有帮助。 How do I send a string from an Arduino Slave using SPI?