如何在DB410C上打招呼,DB410c的芯片选择引脚很高。这是arduino mega的主人[关闭]

问题描述 投票:-1回答:1

我在树莓面上使用spidev这里是我的示例代码将10和11发送到arduino但现在我想在raspberry bi上收到“hello”

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 pis注意:我想从arduino到raspberry pi接收“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

  }  

先感谢您

python c arduino raspberry-pi spi
1个回答
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?

© www.soinside.com 2019 - 2024. All rights reserved.