Arduino Mega2560 的PG3 ( TOSC2 )和PG4 ( TOSC1 )引脚怎么配置为普通的IO口,使其输出高低电平?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino Mega2560 的PG3 ( TOSC2 )和PG4 ( TOSC1 )引脚怎么配置为普通的IO口,使其输出高低电平?相关的知识,希望对你有一定的参考价值。

Arduino Mega2560 的PG3 ( TOSC2 )和PG4 ( TOSC1 )引脚怎么配置为普通的IO口,使其输出高低电平?

参考技术A 直接在setup里
pinMode(TOSC2 , OUTPUT);
后面就可以
digitalWrite(TOSC2 , 1);

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

我在树莓面上使用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 == '
')
  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

  }  

先感谢您

答案

在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 == '
')
      {
        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?

以上是关于Arduino Mega2560 的PG3 ( TOSC2 )和PG4 ( TOSC1 )引脚怎么配置为普通的IO口,使其输出高低电平?的主要内容,如果未能解决你的问题,请参考以下文章

arduino mega2560是多少位的单片机

(Arduino Mega2560)键盘密码检查

Arduino Mega 2560 - 成功执行多次后在 while 循环中停止代码执行

Arduino MEGA 2560找不到驱动怎么办

arduino UNO r3和arduino mega2560 用esp8266 01的WiFi模块可以接入百度天工物联网平台吗?

Python 没有接收到来自 Arduino Mega 2560 的第一行串行数据,而是接收到所有后续数据,为啥会发生这种情况?