SlideShare a Scribd company logo
3台以上のArduinoでのSPI通信 ver.1 = 2017-07-20
以下の記事を参考にした
How do you use SPI on an Arduino? - Arduino Stack Exchange
https://arduino.stackexchange.com/questions/16348/how-do-you-use-spi-on-an-arduino
1totでの接続はSS=10が基本
いくつかの例題を見ると、SS=10, MOSI=11, MISO=12, SCK=13がよく用いられる。
1つのMasterに複数のSlaveをつなげる一つの方法はSSを増やすこと。
3台のArduinoでMaster-Slavesをやる場合:
2台のSalveをつなげる場合は、例えば、SS1=10,SS2=9とする。
それぞれのSlaveがMasterとSSを共有する。
SCK,MISO,MOSIは3台で共有できる。
GNDは繋いだ方が良いかもしれない。
サンプルプログラム ( Arduino Codes)
ここではSlave1=SS=10, Slave2=SS=9とした
Master:
#include <SPI.h>
void setup (void)
{
Serial.begin (115200); // Serialの通信スピード
Serial.println ();
digitalWrite(10, HIGH); // ensure SS=10 stays high for now
digitalWrite(9, HIGH); // ensure SS=9 stays high for now
SPI.begin ();
// Slow down the master a bit
SPI.setClockDivider(SPI_CLOCK_DIV8);
} // end of setup
byte transferAndWait (const byte what)
{
byte a = SPI.transfer (what);
delayMicroseconds (20);
return a;
} // end of transferAndWait
void loop (void)
{
byte a, b, c, d;
// enable Slave Select
digitalWrite(10, LOW); // Slave1=SS=10
digitalWrite(9, LOW); // Slave2=SS=9
transferAndWait ('a'); // add command
transferAndWait (10);
a = transferAndWait (17);
b = transferAndWait (33);
c = transferAndWait (42);
d = transferAndWait (0);
// disable Slave Select
digitalWrite(10, HIGH); // Slave1=SS=10, slave2=SS=9
Serial.println ("Adding results:");
Serial.println (a, DEC);
Serial.println (b, DEC);
Serial.println (c, DEC);
Serial.println (d, DEC);
// enable Slave Select
digitalWrite(10, LOW); // Slave1=SS=10
transferAndWait ('s'); // subtract command
transferAndWait (10);
a = transferAndWait (17);
b = transferAndWait (33);
c = transferAndWait (42);
d = transferAndWait (0);
// disable Slave Select
digitalWrite(9, HIGH); // Slave1=SS=9
Serial.println ("Subtracting results:");
Serial.println (a, DEC);
Serial.println (b, DEC);
Serial.println (c, DEC);
Serial.println (d, DEC);
delay (1000); // 1 second delay
} // end of loop
Slave1:
// what to do with incoming data
volatile byte command = 0;
void setup (void)
{
Serial.begin (115200); // Serialの通信スピード
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// turn on interrupts
SPCR |= _BV(SPIE);
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR;
switch (command)
{
// no command? then this is the command
case 0:
command = c;
SPDR = 0;
break;
// add to incoming byte, return result
case 'a':
SPDR = c + 15; // add 15
break;
// subtract from incoming byte, return result
case 's':
SPDR = c - 8; // subtract 8
break;
} // end of switch
} // end of interrupt service routine (ISR) SPI_STC_vect
void loop (void)
{
// if SPI not active, clear current command
if (digitalRead (10) == HIGH){ // slave1=SS=10
Serial.println ("called by MASTER, slave01");
command = 0;
}
} // end of loop
Slave2:
// what to do with incoming data
volatile byte command = 0;
void setup (void)
{
Serial.begin (115200); // Serialの通信スピード
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// turn on interrupts
SPCR |= _BV(SPIE);
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR;
switch (command)
{
// no command? then this is the command
case 0:
command = c;
SPDR = 0;
break;
// add to incoming byte, return result
case 'a':
SPDR = c + 15; // add 15
break;
// subtract from incoming byte, return result
case 's':
SPDR = c - 8; // subtract 8
break;
} // end of switch
} // end of interrupt service routine (ISR) SPI_STC_vect
void loop (void)
{
// if SPI not active, clear current command
if (digitalRead (9) == HIGH){ // slave2=SS=9
Serial.println ("called by MASTER, slave02");
delay(500);
command = 0;
}
} // end of loop
MacでのArduinoとのシリアル通信
以下のコマンドをターミナルで実行すると、Arduinoがつながったポートがわかる:
$ ls /dev/tty.*
/dev/tty.Bluetooth-Incoming-Port /dev/tty.usbmodem14121
/dev/tty.iPad-WirelessiAP /dev/tty.usbmodem14131
/dev/tty.lpss-serial1 /dev/tty.usbmodem14141
/dev/tty.lpss-serial2
ここで
/dev/tty.usbmodem14121, /dev/tty.usbmodem14131, /dev/tty.usbmodem14141
がArduinoが接続されたポート
Arduino IDEを使っても、確認できる
シリアルポートからデータを読み込むpythonプログラム (1) read_serial01.py
import serial, sys
strPort = sys.argv[1]
#ser = serial.Serial(
# port=strPort,
# baudrate=sys.argv[2],
# parity=serial.PARITY_NONE,
# stopbits=serial.STOPBITS_ONE,
# bytesize=serial.EIGHTBITS,
# timeout=0)
ser=serial.Serial(strPort, sys.argv[2])
print("connected to: " + ser.portstr)
count=1
while True:
line = ser.readline()
print(str(count) + str(': ') + line )
count = count+1
ser.close()
使い方:
ターミナルでArduinoがつながったシリアルポートを確認して、例えば、ターミナルで次のように実行
する
$ python read_serial01.py "/dev/tty.usbmodem14121" 115200
"/dev/tty.usbmodem14121"がポートナンバー。115200は通信スピード。
1つのウィンドウで1つのポートを読み込む。2つ以上は、2つ以上のウィンドウを開いて、一つず
つ表示。
3つのシリアルポートからデータを読み込むpythonプログラム (2) read_serial02.py
import serial, sys
#ser = serial.Serial(
# port=strPort,
# baudrate=sys.argv[2],
# parity=serial.PARITY_NONE,
# stopbits=serial.STOPBITS_ONE,
# bytesize=serial.EIGHTBITS,
# timeout=0)
ser1=serial.Serial(sys.argv[1], sys.argv[4])
ser2=serial.Serial(sys.argv[2], sys.argv[4])
ser3=serial.Serial(sys.argv[3], sys.argv[4])
print("connected to: " + ser1.portstr)
print("connected to: " + ser2.portstr)
print("connected to: " + ser3.portstr)
count1=1
count2=1
count3=1
while True:
line1 = ser1.readline()
line2 = ser2.readline()
line3 = ser3.readline()
print(ser1.portstr+str('= ')+str(count1) + str(': ') + line1 )
print(ser2.portstr+str('= ')+str(count2) + str(': ') + line2 )
print(ser3.portstr+str('= ')+str(count3) + str(': ') + line3 )
count1 = count1+1
count2 = count2+1
count3 = count3+1
ser1.close()
ser2.close()
ser3.close()
使い方:
ターミナルでArduinoがつながったシリアルポートを確認して、例えば、ターミナルで次のように実行
する
$ $ python read_serial02.py "/dev/tty.usbmodem14121" "/dev/tty.usbmodem14131" "/dev/tty.usbmodem14141"
115200
"/dev/tty.usbmodem14121", "/dev/tty.usbmodem14131", "/dev/tty.usbmodem14141"がポートナンバー。115200
は通信スピード。
1つのウィンドウで3つのポートを読み込む。

More Related Content

What's hot

Introduction to ARM
Introduction to ARMIntroduction to ARM
Introduction to ARM
Puja Pramudya
 
Introduction to msp430
Introduction to msp430Introduction to msp430
Introduction to msp430
Harsha herle
 
Types of encoders and decoders with truth tables
Types of encoders and decoders with truth tablesTypes of encoders and decoders with truth tables
Types of encoders and decoders with truth tables
Abdullah khawar
 
Read & write
Read & writeRead & write
Read & write
malaybpramanik
 
UNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGNUNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGN
Dr.YNM
 
Pipeline desdobramento escalonamento
Pipeline desdobramento escalonamentoPipeline desdobramento escalonamento
Pipeline desdobramento escalonamento
Elaine Cecília Gatto
 
01 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.201601 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.2016
Mohamed Fawzy
 
Architecture Of TMS320C50 DSP Processor
Architecture Of TMS320C50 DSP ProcessorArchitecture Of TMS320C50 DSP Processor
Architecture Of TMS320C50 DSP Processor
Janelle Martinez
 
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauriArduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Gaurav Pandey
 
Digital fundamentals 8th edition by Thomas Floyd
Digital fundamentals 8th edition by Thomas Floyd Digital fundamentals 8th edition by Thomas Floyd
Digital fundamentals 8th edition by Thomas Floyd
Dawood Aqlan
 
DC-DC Converter.pptx
DC-DC Converter.pptxDC-DC Converter.pptx
DC-DC Converter.pptx
anuradhapateriya1
 
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
endokayle
 
Approximation techniques used for general purpose algorithms
Approximation techniques used for general purpose algorithmsApproximation techniques used for general purpose algorithms
Approximation techniques used for general purpose algorithms
Sabidur Rahman
 
Design of -- Two phase non overlapping low frequency clock generator using Ca...
Design of -- Two phase non overlapping low frequency clock generator using Ca...Design of -- Two phase non overlapping low frequency clock generator using Ca...
Design of -- Two phase non overlapping low frequency clock generator using Ca...
Prashantkumar R
 
FPGA Based Implementation of Electronic Safe Lock
FPGA Based Implementation of Electronic Safe LockFPGA Based Implementation of Electronic Safe Lock
FPGA Based Implementation of Electronic Safe Lock
IOSR Journals
 
Static Noise margin
Static Noise margin Static Noise margin
Static Noise margin
VLSI SYSTEM Design
 
OpenFOAMによるミルククラウンのシミュレーション
OpenFOAMによるミルククラウンのシミュレーションOpenFOAMによるミルククラウンのシミュレーション
OpenFOAMによるミルククラウンのシミュレーション
日本アムスコ
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
SABBIR AHMED
 
Ziegler-Nichols methods.pptx
Ziegler-Nichols methods.pptxZiegler-Nichols methods.pptx
Ziegler-Nichols methods.pptx
kaustubhshedbalkar1
 
Application of code composer studio in digital signal processing
Application of code composer studio in digital signal processingApplication of code composer studio in digital signal processing
Application of code composer studio in digital signal processing
IAEME Publication
 

What's hot (20)

Introduction to ARM
Introduction to ARMIntroduction to ARM
Introduction to ARM
 
Introduction to msp430
Introduction to msp430Introduction to msp430
Introduction to msp430
 
Types of encoders and decoders with truth tables
Types of encoders and decoders with truth tablesTypes of encoders and decoders with truth tables
Types of encoders and decoders with truth tables
 
Read & write
Read & writeRead & write
Read & write
 
UNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGNUNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGN
 
Pipeline desdobramento escalonamento
Pipeline desdobramento escalonamentoPipeline desdobramento escalonamento
Pipeline desdobramento escalonamento
 
01 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.201601 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.2016
 
Architecture Of TMS320C50 DSP Processor
Architecture Of TMS320C50 DSP ProcessorArchitecture Of TMS320C50 DSP Processor
Architecture Of TMS320C50 DSP Processor
 
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauriArduino for beginners- Introduction to Arduino (presentation) - codewithgauri
Arduino for beginners- Introduction to Arduino (presentation) - codewithgauri
 
Digital fundamentals 8th edition by Thomas Floyd
Digital fundamentals 8th edition by Thomas Floyd Digital fundamentals 8th edition by Thomas Floyd
Digital fundamentals 8th edition by Thomas Floyd
 
DC-DC Converter.pptx
DC-DC Converter.pptxDC-DC Converter.pptx
DC-DC Converter.pptx
 
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
Digital Systems Design Using Verilog 1st edition by Roth John Lee solution ma...
 
Approximation techniques used for general purpose algorithms
Approximation techniques used for general purpose algorithmsApproximation techniques used for general purpose algorithms
Approximation techniques used for general purpose algorithms
 
Design of -- Two phase non overlapping low frequency clock generator using Ca...
Design of -- Two phase non overlapping low frequency clock generator using Ca...Design of -- Two phase non overlapping low frequency clock generator using Ca...
Design of -- Two phase non overlapping low frequency clock generator using Ca...
 
FPGA Based Implementation of Electronic Safe Lock
FPGA Based Implementation of Electronic Safe LockFPGA Based Implementation of Electronic Safe Lock
FPGA Based Implementation of Electronic Safe Lock
 
Static Noise margin
Static Noise margin Static Noise margin
Static Noise margin
 
OpenFOAMによるミルククラウンのシミュレーション
OpenFOAMによるミルククラウンのシミュレーションOpenFOAMによるミルククラウンのシミュレーション
OpenFOAMによるミルククラウンのシミュレーション
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
Ziegler-Nichols methods.pptx
Ziegler-Nichols methods.pptxZiegler-Nichols methods.pptx
Ziegler-Nichols methods.pptx
 
Application of code composer studio in digital signal processing
Application of code composer studio in digital signal processingApplication of code composer studio in digital signal processing
Application of code composer studio in digital signal processing
 

Similar to 3台以上のarduinoでのspi通信 2017 07-20

coma Study Room vol.2 Arduino Workshop
coma Study Room vol.2 Arduino Workshopcoma Study Room vol.2 Arduino Workshop
coma Study Room vol.2 Arduino Workshop
Eto Haruhiko
 
Dive into RTS - another side
Dive into RTS - another sideDive into RTS - another side
Dive into RTS - another sideKiwamu Okabe
 
平成25年社会人講座 Arduinoによるマイコン入門講座
平成25年社会人講座 Arduinoによるマイコン入門講座平成25年社会人講座 Arduinoによるマイコン入門講座
平成25年社会人講座 Arduinoによるマイコン入門講座
Katsuhiro Morishita
 
Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)
ashigirl ZareGoto
 
What is Metasepi?
What is Metasepi?What is Metasepi?
What is Metasepi?
Kiwamu Okabe
 
FPGA workshop (2012f): Network Tester
FPGA workshop (2012f): Network TesterFPGA workshop (2012f): Network Tester
FPGA workshop (2012f): Network Testerykuga
 
DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519
DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519
DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519Yasuhiro Ishii
 
Apacheの展望とmod_perlの超絶技巧 #yapcasia
Apacheの展望とmod_perlの超絶技巧 #yapcasiaApacheの展望とmod_perlの超絶技巧 #yapcasia
Apacheの展望とmod_perlの超絶技巧 #yapcasia
鉄次 尾形
 
Arduino 習作工坊 - Lesson 5 機械之夜
Arduino 習作工坊 - Lesson 5 機械之夜Arduino 習作工坊 - Lesson 5 機械之夜
Arduino 習作工坊 - Lesson 5 機械之夜
CAVEDU Education
 
スタートアップ機能の等価回路モデル
スタートアップ機能の等価回路モデルスタートアップ機能の等価回路モデル
スタートアップ機能の等価回路モデル
マルツエレック株式会社 marutsuelec
 
Arduino jenkins
Arduino jenkinsArduino jenkins
Arduino jenkins
Kiro Harada
 
ものづくりプロジェクトII 一日でわかるArduino入門
ものづくりプロジェクトII 一日でわかるArduino入門ものづくりプロジェクトII 一日でわかるArduino入門
ものづくりプロジェクトII 一日でわかるArduino入門
Yoichi Yamazaki
 
Delayの等価回路モデル
Delayの等価回路モデルDelayの等価回路モデル
Delayの等価回路モデル
マルツエレック株式会社 marutsuelec
 
x86とコンテキストスイッチ
x86とコンテキストスイッチx86とコンテキストスイッチ
x86とコンテキストスイッチ
Masami Ichikawa
 
OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御
OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御
OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御
ROBOTIS Japan
 
ESP8266EXで位置推定
ESP8266EXで位置推定ESP8266EXで位置推定
ESP8266EXで位置推定
nishio
 
20220719_IoTLT_vol89_kitazaki_v1.pdf
20220719_IoTLT_vol89_kitazaki_v1.pdf20220719_IoTLT_vol89_kitazaki_v1.pdf
20220719_IoTLT_vol89_kitazaki_v1.pdf
Ayachika Kitazaki
 
20140910 Arduino for beginners
20140910 Arduino for beginners20140910 Arduino for beginners
20140910 Arduino for beginners
Kenichi Ohwada
 
Processing workshop v3.0
Processing workshop v3.0Processing workshop v3.0
Processing workshop v3.0
Wataru Kani
 

Similar to 3台以上のarduinoでのspi通信 2017 07-20 (20)

coma Study Room vol.2 Arduino Workshop
coma Study Room vol.2 Arduino Workshopcoma Study Room vol.2 Arduino Workshop
coma Study Room vol.2 Arduino Workshop
 
Dive into RTS - another side
Dive into RTS - another sideDive into RTS - another side
Dive into RTS - another side
 
Scapy presentation
Scapy presentationScapy presentation
Scapy presentation
 
平成25年社会人講座 Arduinoによるマイコン入門講座
平成25年社会人講座 Arduinoによるマイコン入門講座平成25年社会人講座 Arduinoによるマイコン入門講座
平成25年社会人講座 Arduinoによるマイコン入門講座
 
Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)
 
What is Metasepi?
What is Metasepi?What is Metasepi?
What is Metasepi?
 
FPGA workshop (2012f): Network Tester
FPGA workshop (2012f): Network TesterFPGA workshop (2012f): Network Tester
FPGA workshop (2012f): Network Tester
 
DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519
DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519
DE0でラジコンカー作ってみた 関西de0 fpga勉強会20120519
 
Apacheの展望とmod_perlの超絶技巧 #yapcasia
Apacheの展望とmod_perlの超絶技巧 #yapcasiaApacheの展望とmod_perlの超絶技巧 #yapcasia
Apacheの展望とmod_perlの超絶技巧 #yapcasia
 
Arduino 習作工坊 - Lesson 5 機械之夜
Arduino 習作工坊 - Lesson 5 機械之夜Arduino 習作工坊 - Lesson 5 機械之夜
Arduino 習作工坊 - Lesson 5 機械之夜
 
スタートアップ機能の等価回路モデル
スタートアップ機能の等価回路モデルスタートアップ機能の等価回路モデル
スタートアップ機能の等価回路モデル
 
Arduino jenkins
Arduino jenkinsArduino jenkins
Arduino jenkins
 
ものづくりプロジェクトII 一日でわかるArduino入門
ものづくりプロジェクトII 一日でわかるArduino入門ものづくりプロジェクトII 一日でわかるArduino入門
ものづくりプロジェクトII 一日でわかるArduino入門
 
Delayの等価回路モデル
Delayの等価回路モデルDelayの等価回路モデル
Delayの等価回路モデル
 
x86とコンテキストスイッチ
x86とコンテキストスイッチx86とコンテキストスイッチ
x86とコンテキストスイッチ
 
OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御
OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御
OpenCM IDE、OpenCM 485 EXPを用いてのDynamixel Xシリーズの制御
 
ESP8266EXで位置推定
ESP8266EXで位置推定ESP8266EXで位置推定
ESP8266EXで位置推定
 
20220719_IoTLT_vol89_kitazaki_v1.pdf
20220719_IoTLT_vol89_kitazaki_v1.pdf20220719_IoTLT_vol89_kitazaki_v1.pdf
20220719_IoTLT_vol89_kitazaki_v1.pdf
 
20140910 Arduino for beginners
20140910 Arduino for beginners20140910 Arduino for beginners
20140910 Arduino for beginners
 
Processing workshop v3.0
Processing workshop v3.0Processing workshop v3.0
Processing workshop v3.0
 

3台以上のarduinoでのspi通信 2017 07-20

  • 1. 3台以上のArduinoでのSPI通信 ver.1 = 2017-07-20 以下の記事を参考にした How do you use SPI on an Arduino? - Arduino Stack Exchange https://arduino.stackexchange.com/questions/16348/how-do-you-use-spi-on-an-arduino 1totでの接続はSS=10が基本 いくつかの例題を見ると、SS=10, MOSI=11, MISO=12, SCK=13がよく用いられる。 1つのMasterに複数のSlaveをつなげる一つの方法はSSを増やすこと。 3台のArduinoでMaster-Slavesをやる場合: 2台のSalveをつなげる場合は、例えば、SS1=10,SS2=9とする。 それぞれのSlaveがMasterとSSを共有する。 SCK,MISO,MOSIは3台で共有できる。 GNDは繋いだ方が良いかもしれない。 サンプルプログラム ( Arduino Codes) ここではSlave1=SS=10, Slave2=SS=9とした Master: #include <SPI.h> void setup (void) { Serial.begin (115200); // Serialの通信スピード Serial.println (); digitalWrite(10, HIGH); // ensure SS=10 stays high for now
  • 2. digitalWrite(9, HIGH); // ensure SS=9 stays high for now SPI.begin (); // Slow down the master a bit SPI.setClockDivider(SPI_CLOCK_DIV8); } // end of setup byte transferAndWait (const byte what) { byte a = SPI.transfer (what); delayMicroseconds (20); return a; } // end of transferAndWait void loop (void) { byte a, b, c, d; // enable Slave Select digitalWrite(10, LOW); // Slave1=SS=10 digitalWrite(9, LOW); // Slave2=SS=9 transferAndWait ('a'); // add command transferAndWait (10); a = transferAndWait (17); b = transferAndWait (33); c = transferAndWait (42); d = transferAndWait (0); // disable Slave Select digitalWrite(10, HIGH); // Slave1=SS=10, slave2=SS=9 Serial.println ("Adding results:"); Serial.println (a, DEC); Serial.println (b, DEC); Serial.println (c, DEC); Serial.println (d, DEC); // enable Slave Select digitalWrite(10, LOW); // Slave1=SS=10 transferAndWait ('s'); // subtract command transferAndWait (10); a = transferAndWait (17); b = transferAndWait (33); c = transferAndWait (42); d = transferAndWait (0); // disable Slave Select digitalWrite(9, HIGH); // Slave1=SS=9 Serial.println ("Subtracting results:"); Serial.println (a, DEC); Serial.println (b, DEC); Serial.println (c, DEC); Serial.println (d, DEC); delay (1000); // 1 second delay
  • 3. } // end of loop Slave1: // what to do with incoming data volatile byte command = 0; void setup (void) { Serial.begin (115200); // Serialの通信スピード // have to send on master in, *slave out* pinMode(MISO, OUTPUT); // turn on SPI in slave mode SPCR |= _BV(SPE); // turn on interrupts SPCR |= _BV(SPIE); } // end of setup // SPI interrupt routine ISR (SPI_STC_vect) { byte c = SPDR; switch (command) { // no command? then this is the command case 0: command = c; SPDR = 0; break; // add to incoming byte, return result case 'a': SPDR = c + 15; // add 15 break; // subtract from incoming byte, return result case 's': SPDR = c - 8; // subtract 8 break; } // end of switch } // end of interrupt service routine (ISR) SPI_STC_vect void loop (void) { // if SPI not active, clear current command if (digitalRead (10) == HIGH){ // slave1=SS=10 Serial.println ("called by MASTER, slave01");
  • 4. command = 0; } } // end of loop Slave2: // what to do with incoming data volatile byte command = 0; void setup (void) { Serial.begin (115200); // Serialの通信スピード // have to send on master in, *slave out* pinMode(MISO, OUTPUT); // turn on SPI in slave mode SPCR |= _BV(SPE); // turn on interrupts SPCR |= _BV(SPIE); } // end of setup // SPI interrupt routine ISR (SPI_STC_vect) { byte c = SPDR; switch (command) { // no command? then this is the command case 0: command = c; SPDR = 0; break; // add to incoming byte, return result case 'a': SPDR = c + 15; // add 15 break; // subtract from incoming byte, return result case 's': SPDR = c - 8; // subtract 8 break; } // end of switch } // end of interrupt service routine (ISR) SPI_STC_vect void loop (void) {
  • 5. // if SPI not active, clear current command if (digitalRead (9) == HIGH){ // slave2=SS=9 Serial.println ("called by MASTER, slave02"); delay(500); command = 0; } } // end of loop MacでのArduinoとのシリアル通信 以下のコマンドをターミナルで実行すると、Arduinoがつながったポートがわかる: $ ls /dev/tty.* /dev/tty.Bluetooth-Incoming-Port /dev/tty.usbmodem14121 /dev/tty.iPad-WirelessiAP /dev/tty.usbmodem14131 /dev/tty.lpss-serial1 /dev/tty.usbmodem14141 /dev/tty.lpss-serial2 ここで /dev/tty.usbmodem14121, /dev/tty.usbmodem14131, /dev/tty.usbmodem14141 がArduinoが接続されたポート Arduino IDEを使っても、確認できる
  • 6. シリアルポートからデータを読み込むpythonプログラム (1) read_serial01.py import serial, sys strPort = sys.argv[1] #ser = serial.Serial( # port=strPort, # baudrate=sys.argv[2], # parity=serial.PARITY_NONE, # stopbits=serial.STOPBITS_ONE, # bytesize=serial.EIGHTBITS, # timeout=0) ser=serial.Serial(strPort, sys.argv[2]) print("connected to: " + ser.portstr) count=1 while True: line = ser.readline() print(str(count) + str(': ') + line ) count = count+1 ser.close() 使い方: ターミナルでArduinoがつながったシリアルポートを確認して、例えば、ターミナルで次のように実行 する $ python read_serial01.py "/dev/tty.usbmodem14121" 115200 "/dev/tty.usbmodem14121"がポートナンバー。115200は通信スピード。 1つのウィンドウで1つのポートを読み込む。2つ以上は、2つ以上のウィンドウを開いて、一つず つ表示。
  • 7. 3つのシリアルポートからデータを読み込むpythonプログラム (2) read_serial02.py import serial, sys #ser = serial.Serial( # port=strPort, # baudrate=sys.argv[2], # parity=serial.PARITY_NONE, # stopbits=serial.STOPBITS_ONE, # bytesize=serial.EIGHTBITS, # timeout=0) ser1=serial.Serial(sys.argv[1], sys.argv[4]) ser2=serial.Serial(sys.argv[2], sys.argv[4]) ser3=serial.Serial(sys.argv[3], sys.argv[4]) print("connected to: " + ser1.portstr) print("connected to: " + ser2.portstr) print("connected to: " + ser3.portstr) count1=1 count2=1 count3=1 while True: line1 = ser1.readline()
  • 8. line2 = ser2.readline() line3 = ser3.readline() print(ser1.portstr+str('= ')+str(count1) + str(': ') + line1 ) print(ser2.portstr+str('= ')+str(count2) + str(': ') + line2 ) print(ser3.portstr+str('= ')+str(count3) + str(': ') + line3 ) count1 = count1+1 count2 = count2+1 count3 = count3+1 ser1.close() ser2.close() ser3.close() 使い方: ターミナルでArduinoがつながったシリアルポートを確認して、例えば、ターミナルで次のように実行 する $ $ python read_serial02.py "/dev/tty.usbmodem14121" "/dev/tty.usbmodem14131" "/dev/tty.usbmodem14141" 115200 "/dev/tty.usbmodem14121", "/dev/tty.usbmodem14131", "/dev/tty.usbmodem14141"がポートナンバー。115200 は通信スピード。 1つのウィンドウで3つのポートを読み込む。