Arduino cheat sheet: Difference between revisions

 
(53 intermediate revisions by the same user not shown)
Line 67: Line 67:
===AVR toolchain 配置===
===AVR toolchain 配置===
  [https://github.com/arduino/toolchain-avr  The AVR toolchain used by the Arduino IDE]
  [https://github.com/arduino/toolchain-avr  The AVR toolchain used by the Arduino IDE]
:arduino IDE 1.6.1 の場合は release 3.4.5 である。tag を指定して git clone する。
:arduino IDE 1.6.1 の場合は AVR toolchain release 3.4.5 である。tag を指定して git clone する。
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
git clone --depth=1 --branch 3.4.5 --single-branch https://github.com/arduino/toolchain-avr.git
git clone --depth=1 --branch 3.4.5 --single-branch https://github.com/arduino/toolchain-avr.git
Line 361: Line 361:


ano (ino) ではなく、こちらのほうが使い勝手が良いかも。
ano (ino) ではなく、こちらのほうが使い勝手が良いかも。
Arduino IDE 由来の AVR toolchain ではなく、本家 [http://distribute.atmel.no/tools/opensource/Atmel-AVR-GNU-Toolchain/3.5.0/ Atmel AVR 8-bit GNU Toolchain] を cygwin で build しても動くかもとおもったが、Arduino IDE に絡んでいることが発覚。Arduino.cc の AVR toolchain の 3.5.0 版でチャレンジ。
:Arduino.cc 版 AVR toolchain 3.5.0 ポーティング自体が、はまだ作業途中なので、動く状態だけど、様子見
[https://github.com/nxhack/toolchain-avr/tree/cygwin_only-3.5.0 avr toolchain 3.5.0 cygwin only]
[https://github.com/arduino/toolchain-avr/compare/trecinquezero...nxhack:cygwin_only-3.5.0 本家との差分]
<syntaxhighlight lang="bash">
git clone -b cygwin_only-3.5.0 https://github.com/nxhack/toolchain-avr.git
cd toolchain-avr
./arch.cygwin.build.bash
</syntaxhighlight>


= Develop =
= Develop =
(レガシープログラマーのリハビリ大作戦)
== Arduino 系の雑多なメモ ==
== Arduino 系の雑多なメモ ==
=== Library 作成 ===
=== Library 作成 ===
Line 383: Line 396:
AVR のニーモニックで HALT (Z80 や x86 でおなじみ) がみつからない。SLEEP てーのがそれかな?
AVR のニーモニックで HALT (Z80 や x86 でおなじみ) がみつからない。SLEEP てーのがそれかな?
  [http://playground.arduino.cc/Learning/ArduinoSleepCode How to let your Arduino go to sleep and wake up on an external event.]
  [http://playground.arduino.cc/Learning/ArduinoSleepCode How to let your Arduino go to sleep and wake up on an external event.]
<syntaxhighlight lang="text" enclose="div">
SLEEP_MODE_IDLE
SLEEP_MODE_ADC
SLEEP_MODE_PWR_SAVE
SLEEP_MODE_EXT_STANDBY
SLEEP_MODE_STANDBY
SLEEP_MODE_PWR_DOWN
</syntaxhighlight>


==== sleep_mode() ====
==== sleep_mode() ====
Line 407: Line 429:
もちろん、power_*_disable() もある。いったん power_all_disable() してから個別に設定も可。
もちろん、power_*_disable() もある。いったん power_all_disable() してから個別に設定も可。


==== float の printf ====
==== Power Save いろいろ ====
arduino は float と double の差はない。[https://www.arduino.cc/en/Reference/Double Double]
これから試してみる...
<syntaxhighlight lang="c" enclose="div">
  set_sleep_mode (SLEEP_MODE_PWR_DOWN); 
 
  byte old_ADCSRA = ADCSRA;
  ADCSRA = 0; 
  sleep_mode();
  ADCSRA = old_ADCSRA;
</syntaxhighlight>
 
<syntaxhighlight lang="c" enclose="div">
  noInterrupts();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_bod_disable();
  interrupts();
  sleep_cpu();
  sleep_disable();
</syntaxhighlight>
 
avr/sleep.h のコメント読む...
<syntaxhighlight lang="c" enclose="div">
    #include <avr/sleep.h>
 
    ...
      set_sleep_mode(<mode>);
      sleep_mode();
</syntaxhighlight>
<syntaxhighlight lang="c" enclose="div">
    #include <avr/interrupt.h>
    #include <avr/sleep.h>
 
    ...
      set_sleep_mode(<mode>);
      cli();
      if (some_condition)
      {
        sleep_enable();
        sei();
        sleep_cpu();
        sleep_disable();
      }
      sei();
</syntaxhighlight>
<syntaxhighlight lang="c" enclose="div">
    #include <avr/interrupt.h>
    #include <avr/sleep.h>
 
    ...
      set_sleep_mode(<mode>);
      cli();
      if (some_condition)
      {
        sleep_enable();
        sleep_bod_disable();
        sei();
        sleep_cpu();
        sleep_disable();
      }
      sei();
</syntaxhighlight>
 
実測の結果:
<syntaxhighlight lang="c" enclose="div">
#include <avr/sleep.h>
#include <avr/power.h>
 
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
 
...
 
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  cbi(ADCSRA, ADEN);
  power_all_disable();
  //noInterrupts();
  sleep_enable();
  sleep_bod_disable();
  //interrupts();
  sleep_cpu();
  sleep_disable();
  power_all_enable();
  sbi(ADCSRA, ADEN);
</syntaxhighlight>
 
==== disable BOD ====
ブラウンアウト機能 要調査
uno.bootloader.extended_fuses=0x07
 
[http://jasper.sikken.nl/arduinobootloader/index.html Arduino Uno brown-out detection]
 
=== float の printf ===
Arduino UNO (atmega328p) は float と double の差はない。[https://www.arduino.cc/en/Reference/Double Double]


printf 系は float は使えない。'?' に変換される。(小一時間ハマった)
printf 系は float は使えない。'?' に変換される。(小一時間ハマった)
:avr-ld のオプションに -lprintf_flt -lm を追加したら動く。コードが大きくなるので、標準ではミニマム機能のみにしている。
avr libs の (stdlib.h) で代用できる。[http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga6c140bdd3b9bd740a1490137317caa44 dtostre, dtostrf]


avr libs の dtostrf (stdlibs.h) で代用できる。[http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga6c140bdd3b9bd740a1490137317caa44 dtostre]
:そもそも FPU の無いマイコンで浮動小数使うのはダメね。無理やり整数か、美しくするなら固定小数点で書き換える。
 
:そもそも FPU の無いマイコンで浮動小数使うのはダメね。固定小数点で書き換える。


  [https://ucexperiment.wordpress.com/2015/03/31/avr-gcc-fixed-point-vs-floating-point-comparison/ AVR GCC Fixed-Point vs. Floating Point Comparison | µC eXperiment]
  [https://ucexperiment.wordpress.com/2015/03/31/avr-gcc-fixed-point-vs-floating-point-comparison/ AVR GCC Fixed-Point vs. Floating Point Comparison | µC eXperiment]
  [https://gcc.gnu.org/wiki/avr-gcc#Fixed-Point_Support avr-gcc - GCC Wiki]
  [https://gcc.gnu.org/wiki/avr-gcc#Fixed-Point_Support avr-gcc - GCC Wiki]
  [https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html Fixed-Point - Using the GNU Compiler Collection (GCC)]
  [https://gcc.gnu.org/onlinedocs/gcc/Fixed-Point.html Fixed-Point - Using the GNU Compiler Collection (GCC)]
[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf ISO/IEC TR 18037:2006(E)]
:固定小数点 fixed point の情報すくないね。もともと FPU の無い マイコン系の拡張なのでネットでも情報出て無い。
:gcc の拡張は動くけど、g++ は未対応なので、arduino の場合は pure C では使えるが、いろいろ大変そう。


==== メモメモ ====
=== Tips ===
[http://embeddedgurus.com/stack-overflow/category/efficient-cc/ Efficient C/C++ « Stack Overflow]
:(富豪的プログラミングになれてしまてっていたので)マイコンのプログラムテクニックは新鮮。
:除算は工夫して少なく。できたら浮動小数は使わない。
:むかしむかし、HP-PA のコンパイラの出すコードに関心したように、RISC の場合は、あるていどコンパイラー任せにできるような(わかりやすい)コードを心がけるのも忘れないように。(むかしついでに... HP-PA の 浮動小数点プロセッサーが Intel 系のそれと精度がちがっていて数日ハマってたな)
 
==== 除算 ====
[http://embeddedgurus.com/stack-overflow/2009/06/division-of-integers-by-constants/ Division of integers by constants « Stack Overflow]
 
:Using the values generated by Jones, together with some of the values I have computed, here’s a summary of some common divisors for unsigned 16 bit integers.
<syntaxhighlight lang="c" enclose="div">
Divide by 3: (((uint32_t)A * (uint32_t)0xAAAB) >> 16) >> 1
Divide by 5: (((uint32_t)A * (uint32_t)0xCCCD) >> 16) >> 2
Divide by 6: (((uint32_t)A * (uint32_t)0xAAAB) >> 16) >> 2
Divide by 7: ((((uint32_t)A * (uint32_t)0x2493) >> 16) + A) >> 1) >> 2
Divide by 9: (((uint32_t)A * (uint32_t)0xE38F) >> 16) >> 3
Divide by 10: (((uint32_t)A * (uint32_t)0xCCCD) >> 16) >> 3
Divide by 11: (((uint32_t)A * (uint32_t)0xBA2F) >> 16) >> 3
Divide by 12: (((uint32_t)A * (uint32_t)0xAAAB) >> 16) >> 3
Divide by 13: (((uint32_t)A * (uint32_t)0x9D8A) >> 16) >> 3
Divide by 14: ((((uint32_t)A * (uint32_t)0x2493) >> 16) + A) >> 1) >> 3
Divide by 15: (((uint32_t)A * (uint32_t)0x8889) >> 16) >> 3
Divide by 30: (((uint32_t)A * (uint32_t)0x8889) >> 16) >> 4
Divide by 60: (((uint32_t)A * (uint32_t)0x8889) >> 16) >> 5
Divide by 100: (((((uint32_t)A * (uint32_t)0x47AF) >> 16U) + A) >> 1) >> 6
Divide by PI: ((((uint32_t)A * (uint32_t)0x45F3) >> 16) + A) >> 1) >> 1
Divide by √2: (((uint32_t)A * (uint32_t)0xB505) >> 16) >> 0
</syntaxhighlight>
 
==== modulus (%) ====
[http://embeddedgurus.com/stack-overflow/2011/02/efficient-c-tip-13-use-the-modulus-operator-with-caution/ Efficient C Tip #13 – use the modulus (%) operator with caution « Stack Overflow]
 
<syntaxhighlight lang="c" enclose="div">
void display_value(uint16_t value)
{
uint8_t    msd, nsd, lsd;
 
if (value > 999)
{
value = 999;
}
 
lsd = value % 10;
value /= 10;
nsd = value % 10;
value /= 10;
msd = value;
 
/* Now display the digits */
}
</syntaxhighlight>
 
<syntaxhighlight lang="c" enclose="div">
void display_value(uint16_t value)
{
uint8_t    msd, nsd, lsd;
 
if (value > 999U)
{
  value = 999U;
}
 
msd = value / 100U;
value -= msd * 100U;
 
nsd = value / 10U;
value -= nsd * 10U;
 
lsd = value;
 
/* Now display the digits */
}
</syntaxhighlight>
 
==== Fast loops ====
[http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-7-fast-loops/ Efficient C Tips #7 – Fast loops « Stack Overflow]
 
<syntaxhighlight lang="text" enclose="div">
So how does this efficiency arise? Well in the count up case, the assembly language generated by the compiler typically looks something like this:
 
INC lpc      ; Increment loop counter
SUB lpc, #10 ; Compare loop counter to 10
BNZ loop    ; Branch if loop counter not equal to 10
 
By contrast, in the count down case the assembly language looks something like this
 
DEC lpc  ; Decrement loop counter
BNZ loop  ; Branch if non zero
 
Evidently, because of the ‘specialness’ of zero, more efficient code can be generated.
</syntaxhighlight>
 
=== メモメモ===
  [http://www.gammon.com.au/power Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors]
  [http://www.gammon.com.au/power Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors]
:ここがまとまってる
:sleep 系はここがまとまってる


  [http://programresource.net/2015/04/23/2547.html ArduinoでDS3231 RTCモジュールを使った定期処理 | Program Resource]
  [http://programresource.net/2015/04/23/2547.html ArduinoでDS3231 RTCモジュールを使った定期処理 | Program Resource]
Line 466: Line 679:
基本の7セグ
基本の7セグ
  [http://www.thecoderscorner.com/electronics/microcontrollers/driving-displays/45-arduino-example-for-driving-7-segment-led-s Arduino 7 segment LED display tutorial]
  [http://www.thecoderscorner.com/electronics/microcontrollers/driving-displays/45-arduino-example-for-driving-7-segment-led-s Arduino 7 segment LED display tutorial]
:[http://garretlab.web.fc2.com/arduino_reference/language/structure/bitwise_operators/port_manipulation.html ポートレジスタ]使わない形の一番シンプルなもの
:[http://garretlab.web.fc2.com/arduino_reference/language/structure/bitwise_operators/port_manipulation.html ポート・レジスタ]使わない形の一番シンプルなもの


=== AVR libc ===
=== AVR libc ===
  [http://cega.jp/avr-libc-jp/ AVR Libc 日本語]
  [http://cega.jp/avr-libc-jp/ AVR Libc 日本語]
[http://www.akamoz.jp/you/junkbox/avr/avr-libc.htm AVR libcを使ってみる you/junkbox]


=== IoT するなら ===
=== IoT するなら ===
Line 477: Line 692:
  [http://recruit.gmo.jp/engineer/jisedai/blog/%E3%80%90%E9%9B%BB%E5%AD%90%E5%B7%A5%E4%BD%9C%E3%80%91arduino-yun%E3%82%92google-docs%E3%82%84twitter%E3%81%A8%E9%80%A3%E6%90%BA%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F/ 【電子工作】Arduino YúnをGoogle DocsやTwitterと連携してみた | GMOインターネット 次世代システム研究室]
  [http://recruit.gmo.jp/engineer/jisedai/blog/%E3%80%90%E9%9B%BB%E5%AD%90%E5%B7%A5%E4%BD%9C%E3%80%91arduino-yun%E3%82%92google-docs%E3%82%84twitter%E3%81%A8%E9%80%A3%E6%90%BA%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F/ 【電子工作】Arduino YúnをGoogle DocsやTwitterと連携してみた | GMOインターネット 次世代システム研究室]
  [http://blog.parse.com/learn/using-parse-for-iot-to-create-an-order-button/ Using Parse for IoT to Create an Order Button]
  [http://blog.parse.com/learn/using-parse-for-iot-to-create-an-order-button/ Using Parse for IoT to Create an Order Button]
  [http://qiita.com/ToshiakiEnami/items/7b4b3090f3687979d21a awsIoT - 太陽光パネルの発電量をAWS IoTとAmazon Elasticsearch Serviceを使って可視化してみる - Qiita]
  [http://qiita.com/ToshiakiEnami/items/7b4b3090f3687979d21a awsIoT - 太陽光パネルの発電量をAWS IoTとAmazon Elasticsearch Serviceを使って可視化してみる]


  [https://github.com/aws/aws-iot-device-sdk-arduino-yun/ SDK for connecting to AWS IoT from an Arduino Yún]
  [https://github.com/aws/aws-iot-device-sdk-arduino-yun/ SDK for connecting to AWS IoT from an Arduino Yún]
Line 489: Line 704:
  [http://ser2net.sourceforge.net/ Serial to Network Proxy (ser2net)]
  [http://ser2net.sourceforge.net/ Serial to Network Proxy (ser2net)]


  [http://qiita.com/syoyo/items/d31e9db6851dfee3ef82 tunneling - Reverse ssh tunnel を安定運用する - Qiita]
  [http://qiita.com/syoyo/items/d31e9db6851dfee3ef82 tunneling - Reverse ssh tunnel を安定運用する]
  [http://mm011106.github.io/blog/2015/02/23/init-dot-d-script/ init.dスクリプトを書いてみる - MQTT and …]
  [http://mm011106.github.io/blog/2015/02/23/init-dot-d-script/ init.dスクリプトを書いてみる - MQTT and …]
== Arduino の歴史 ==
[http://arduinohistory.github.io/ja.html  Arduinoの語られざる歴史]
:こえーーー
[http://wiring.org.co/ Wiring]