Arduino cheat sheet: Difference between revisions

 
(33 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 406: Line 428:
</syntaxhighlight>
</syntaxhighlight>
もちろん、power_*_disable() もある。いったん power_all_disable() してから個別に設定も可。
もちろん、power_*_disable() もある。いったん power_all_disable() してから個別に設定も可。
==== Power Save いろいろ ====
これから試してみる...
<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 ===
=== float の printf ===
Line 412: Line 530:
printf 系は float は使えない。'?' に変換される。(小一時間ハマった)
printf 系は float は使えない。'?' に変換される。(小一時間ハマった)
:avr-ld のオプションに -lprintf_flt -lm を追加したら動く。コードが大きくなるので、標準ではミニマム機能のみにしている。
:avr-ld のオプションに -lprintf_flt -lm を追加したら動く。コードが大きくなるので、標準ではミニマム機能のみにしている。
avr libs の dtostrf (stdlibs.h) で代用できる。[http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga6c140bdd3b9bd740a1490137317caa44 dtostre]
avr libs の (stdlib.h) で代用できる。[http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga6c140bdd3b9bd740a1490137317caa44 dtostre, dtostrf]


:そもそも FPU の無いマイコンで浮動小数使うのはダメね。無理やり整数か、美しくするなら固定小数点で書き換える。
:そもそも FPU の無いマイコンで浮動小数使うのはダメね。無理やり整数か、美しくするなら固定小数点で書き換える。
Line 426: Line 544:
  [http://embeddedgurus.com/stack-overflow/category/efficient-cc/ Efficient C/C++ « Stack Overflow]
  [http://embeddedgurus.com/stack-overflow/category/efficient-cc/ Efficient C/C++ « Stack Overflow]
:(富豪的プログラミングになれてしまてっていたので)マイコンのプログラムテクニックは新鮮。
:(富豪的プログラミングになれてしまてっていたので)マイコンのプログラムテクニックは新鮮。
:除算は少なく。浮動小数は使うな。
:除算は工夫して少なく。できたら浮動小数は使わない。
:むかしむかし、HP-PA のコンパイラの出すコードに関心したように、RISC の場合は、あるていどコンパイラー任せにできるような(わかりやすい)コードを心がけるのも忘れないように。(むかしついでに... HP-PA の 浮動小数点プロセッサーが Intel 系のそれと精度がちがっていて数日ハマってたな)
:むかしむかし、HP-PA のコンパイラの出すコードに関心したように、RISC の場合は、あるていどコンパイラー任せにできるような(わかりやすい)コードを心がけるのも忘れないように。(むかしついでに... HP-PA の 浮動小数点プロセッサーが Intel 系のそれと精度がちがっていて数日ハマってたな)


Line 517: Line 635:
=== メモメモ===
=== メモメモ===
  [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 561: 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 572: 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 584: 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]