前回までの記事で、Arduinoのデジタルおよびアナログ入出力関数を取り上げてきましたが、今回は、その他の入出力関数について解説したいと思います。
siftOut
siftOutは、1バイト分のデータを1ビットずつシフトアウトします。MSB(最上位ビット)とLSB(最下位ビット)のどちらからもデータを送る事ができます。各ビットは、まずdataPinに出力され、その後clockPinが反転して、そのビットが有効になったことが示されます。 この機能はソフトウエアで実現されていますますので、高速な動作が必要な場合はハードウエアで実現されているSPIライブラリの使用した方が良いかも知れません。設定パラメータは以下の通りです。
dataPin: ビット出力ピン
clockPin: クロック出力ピン。dataPinに正しい値がセットされる毎に、このピンが1回反転します
bitOrder: MSBFIRSTまたはLSBFIRSTを指定します。(*)
value: 送信したいデータ (byte)
(*)備考
Most Significant Bit First: MSB(最上位ビット)から送信
Least Significant Bit First: LSB(最下位ビット)から送信
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
//Arduino Sketch Example: shiftOut //Date: 2015.1.18 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax shiftOut(dataPin, clockPin, bitOrder, value) //Parameters //dataPin: the pin on which to output each bit (int) //clockPin: the pin to toggle once the dataPin has been set to the //correct value (int) //bitOrder: which order to shift out the bits; either MSBFIRST or LSBFIRST. //(Most Significant Bit First, or, Least Significant Bit First) //value: the data to shift out. (byte) //Note //The dataPin and clockPin must already be configured as outputs by a call to pinMode(). //shiftOut is currently written to output 1 byte (8 bits) so it requires a two step operation to output values larger than 255. // Do this for MSBFIRST serial int data = 500; // shift out highbyte shiftOut(dataPin, clock, MSBFIRST, (data >> 8)); // shift out lowbyte shiftOut(dataPin, clock, MSBFIRST, data); // Or do this for LSBFIRST serial data = 500; // shift out lowbyte shiftOut(dataPin, clock, LSBFIRST, data); // shift out highbyte shiftOut(dataPin, clock, LSBFIRST, (data >> 8)); |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:shiftOut()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、int型データを送っています。データサイズが2バイトなので、シフト演算子と組み合せて2回、shiftOutを使用しています。なお、dataPinとclockPinは、pinMode関数によってOUTPUT(出力)に設定されている必要があります。
siftIn
shifInは、1バイトのデータを1ビットずつシフトインします。MSB(最上位ビット)とLSB(最下位ビット)のどちらからでも開始できます。最初に、clockPinがHIGHになり、dataPinからデータが読み込まれ、clockPinがLOWに戻ります。設定パラメータは、次の通りです。
dataPin: 入力ピン
clockPin: クロック出力ピン
bitOrder: MSBFIRSTまたはLSBFIRSTを指定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Arduino Sketch Example: shiftIn //Date: 2015.1.18 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax byte incoming = shiftIn(dataPin, clockPin, bitOrder) //Parameters //dataPin: the pin on which to input each bit (int) //clockPin: the pin to toggle to signal a read from dataPin //bitOrder: which order to shift in the bits; either MSBFIRST or LSBFIRST. //(Most Significant Bit First, or, Least Significant Bit First) //Returns: the value read (byte) |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:shiftIn()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
pulseIn
pulseInは、ピンに入力されるパルスを検出します。設定パラメータは次の通りです。
pin: パルス入力のピンの番号
value: 測定するパルスの種類。HIGHまたはLOW
timeout(省略可): タイムアウトまでの時間(単位・マイクロ秒)。デフォルトは1秒 (unsigned long)
戻り値は、パルスの長さ(マイクロ秒)で、パルスがスタートする前にタイムアウトとなった場合は0 (unsigned long)を返します。この関数で計測可能な時間は、およそ10マイクロ秒から3分で、あまりに長いパルスは、エラーとなる可能性があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//Arduino Sketch Example: pulseIn //Date: 2015.1.18 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax pulseIn(pin, value) pulseIn(pin, value, timeout) //Parameters //pin: the number of the pin on which you want to read the pulse.(int) //value: type of pulse to read: either HIGH or LOW. (int) //timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long) //Returns //the length of the pulse (in microseconds) or 0 if no pulse started before the timeout (unsigned long) //Example int pin = 7; unsigned long duration; void setup() { pinMode(pin, INPUT); } void loop() { duration = pulseIn(pin, HIGH); } |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:pulseIn()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
上の例では、パルスの種類(value)をHIGHに指定しているので、入力がHIGHに変わると時間計測を始め、またLOWに戻ったら、そこまでの時間(マイクロ秒単位)がdurationに格納されます。
tone
toneは、指定周波数の矩形波(50%デューティ)を発生させます。durationを指定しなかった場合、noTone()を実行するまで動作を続けます。設定パラメータは次の通りです。
pin: トーンの出力ピン
frequency: 周波数(Hz)
duration: 出力時間(ミリ秒)、オプションで省略可能。
なお、この関数を使用すると、3と11番ピンのPWM出力が使用できなくなるので注意して下さい。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Arduino Sketch Example: tone //Date: 2015.1.18 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax tone(pin, frequency) tone(pin, frequency, duration) //Parameters //pin: the pin on which to generate the tone //frequency: the frequency of the tone in hertz - unsigned int //duration: the duration of the tone in milliseconds (optional) - unsigned long |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:tone()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
toneを利用すると、出力ピンに圧電ブザーやスピーカに接続すれば、一定ピッチの音を再生できます。 ただし、同時に生成できるのは1音だけで、既に他で、tone()が実行されている場合、次のtone()は効果がありません。また、同じピンに対してtone()を実行すると周波数が変化します。
noTone
noToneは、toneで開始された矩形波を停止します。toneが実行されていない場合は何も起こりません。設定パラメータは次の通りです。
pin: トーンを停止するピン番号
1 2 3 4 5 6 7 8 9 10 |
//Arduino Sketch Example: noTone //Date: 2015.1.18 //Edited and Modified by: easy labo //Original Source: Arduino Reference (http://arduino.cc/en/Reference/HomePage) //Syntax noTone(pin) //Parameters //pin: the pin on which to stop generating the tone |
Creative Commons Attribution-ShareAlike 3.0 License (CC BY-SA 3.0)
“Arduino Reference:noTone()” by Arduino Team, used under CC BY-SA 3.0/ easy labo made some changes and comments to the original
Sponsored Link