| 1 | = MAX6675 K型熱電対温度センサのスケッチ = |
| 2 | |
| 3 | * [wiki:MAX31855 概要についてはこちら。] |
| 4 | * [wiki:MAX31855Instruction 作り方の説明はこちら。] |
| 5 | * [http://www.switch-science.com/products/detail.php?product_id=864 キットとして販売しています。] |
| 6 | |
| 7 | どんなスケッチ(ソフトウェア)で使うのかを説明します。 |
| 8 | |
| 9 | すでにモジュールが完成していて、Arduino Uno (Arduino Duemilanove) のデジタル8番~13番の位置に差し込んであるものとします。 |
| 10 | |
| 11 | [attachment:max31855_example.zip このページの添付ファイル「max31855_example.zip」]をダウンロードして、展開します。[[BR]] |
| 12 | 展開してできたフォルダを、Arduinoのスケッチブックのフォルダに移動します。[[BR]] |
| 13 | スケッチブックのフォルダは、ウィンドウズでは標準は「ドキュメント」(Vista/7の場合)または「マイドキュメント」(XPの場合)の中にある、「Arduino」というフォルダです。 |
| 14 | |
| 15 | Arduino統合環境を起動して、「ファイル」メニューの「スケッチブック」から「max31855_example」を探して開いてください。 |
| 16 | |
| 17 | あとは、普通にArduinoに書き込んで実行します。[[BR]] |
| 18 | 「シリアルモニタ」を有効にすると、定期的に温度が表示されます。[[BR]] |
| 19 | K型熱電対の温度 // 基準接点の温度(max31855の温度)[[BR]] |
| 20 | 「ERROR: 」と表示された場合は、熱電対がきちんとつながっていません。[[BR]] |
| 21 | 続いて「Short to Vcc, 」と表示された場合は、Vcc側とショートしています。[[BR]] |
| 22 | 「Short to GND, 」と表示された場合は、GND側とショートしています。[[BR]] |
| 23 | 「Open Circuit, 」と表示された場合は、K型熱電対が接続されていないか、途中で切れています。 |
| 24 | |
| 25 | スケッチの先頭に、以下の定義があります。 |
| 26 | {{{ |
| 27 | #!c |
| 28 | /* |
| 29 | // Arduinoボードの電源電圧が3.3Vの場合、 |
| 30 | // 以下のマクロを有効にしてモジュールを直挿しできます。 |
| 31 | #define VCC 8 |
| 32 | #define GND 9 |
| 33 | */ |
| 34 | }}} |
| 35 | モジュールをデジタルの8番~13番に直結したい場合は、これらの定義を有効にしてください。[[BR]] |
| 36 | VCCおよびGNDの定義は、デジタルの8番および9番からモジュールに電源供給を行うためです。 |
| 37 | この方法が使えるのはArduinoの電源電圧が3.3Vの場合に限ります。Arduino UNOでは出来ません。[[BR]] |
| 38 | |
| 39 | {{{ |
| 40 | #!c |
| 41 | #define SLAVE 10 |
| 42 | }}} |
| 43 | SLAVEの定義は、SPIのスレーブセレクトにデジタルの何番を使うかを決めています。 |
| 44 | |
| 45 | ※このスケッチはArduinoIDE付属のSPIライブラリを使用しています。 |
| 46 | |
| 47 | === max31855_example.pde (Arduino 1.0付属SPIライブラリ対応版) === |
| 48 | {{{ |
| 49 | #!c |
| 50 | // K型熱電対温度センサモジュールキット(SPI接続)MAX31855使用(3.3V版)サンプルスケッチ |
| 51 | // switch-science 2012.2.8 |
| 52 | |
| 53 | #include "SPI.h" |
| 54 | |
| 55 | /* |
| 56 | // Arduinoボードの電源電圧が3.3Vの場合、 |
| 57 | // 以下のマクロを有効にしてモジュールを直挿しできます。 |
| 58 | #define VCC 8 |
| 59 | #define GND 9 |
| 60 | */ |
| 61 | #define SLAVE 10 |
| 62 | |
| 63 | byte enabled = 255; |
| 64 | |
| 65 | void SPI_disable() { |
| 66 | if(enabled != 255) { |
| 67 | digitalWrite(enabled,HIGH); |
| 68 | enabled = 255; |
| 69 | } |
| 70 | } |
| 71 | void SPI_enable(byte slaveselecter) { |
| 72 | SPI_disable(); |
| 73 | digitalWrite(slaveselecter,LOW); |
| 74 | enabled = slaveselecter; |
| 75 | } |
| 76 | |
| 77 | byte SPI_read() { |
| 78 | return SPI.transfer(0x00); |
| 79 | } |
| 80 | |
| 81 | void setup() { |
| 82 | #ifdef GND |
| 83 | pinMode(GND, OUTPUT); |
| 84 | digitalWrite(GND, LOW); |
| 85 | #endif |
| 86 | #ifdef VCC |
| 87 | pinMode(VCC, OUTPUT); |
| 88 | digitalWrite(VCC, HIGH); |
| 89 | #endif |
| 90 | pinMode(SLAVE,OUTPUT); |
| 91 | digitalWrite(SLAVE,HIGH); |
| 92 | |
| 93 | Serial.begin(9600); |
| 94 | SPI.begin(); |
| 95 | SPI.setBitOrder(MSBFIRST); |
| 96 | SPI.setClockDivider(SPI_CLOCK_DIV4); |
| 97 | SPI.setDataMode(SPI_MODE0); |
| 98 | } |
| 99 | |
| 100 | void loop() { |
| 101 | unsigned int thermocouple; // 14-Bit Thermocouple Temperature Data + 2-Bit |
| 102 | unsigned int internal; // 12-Bit Internal Temperature Data + 4-Bit |
| 103 | float disp; // display value |
| 104 | |
| 105 | delay(500); |
| 106 | SPI_enable(SLAVE); |
| 107 | thermocouple = (unsigned int)SPI_read() << 8; |
| 108 | thermocouple |= (unsigned int)SPI_read() ; |
| 109 | internal = (unsigned int)SPI_read() << 8; |
| 110 | internal |= (unsigned int)SPI_read(); |
| 111 | SPI_disable(); |
| 112 | |
| 113 | if((thermocouple & 0x0001) != 0) { |
| 114 | Serial.print("ERROR: "); |
| 115 | if ((internal & 0x0004) !=0) { |
| 116 | Serial.print("Short to Vcc, "); |
| 117 | } |
| 118 | if ((internal & 0x0002) !=0) { |
| 119 | Serial.print("Short to GND, "); |
| 120 | } |
| 121 | if ((internal & 0x0001) !=0) { |
| 122 | Serial.print("Open Circuit, "); |
| 123 | } |
| 124 | Serial.println(""); |
| 125 | } else { |
| 126 | if((thermocouple & 0x8000) == 0){ // 0℃以上 |
| 127 | disp = (thermocouple >> 2) * 0.25; |
| 128 | } else { // 0℃未満 |
| 129 | disp = (0x3fff - (thermocouple >> 2) + 1) * -0.25; |
| 130 | } |
| 131 | Serial.print(thermocouple,HEX); |
| 132 | Serial.print(" : "); |
| 133 | Serial.print(disp); |
| 134 | |
| 135 | Serial.print(" // "); |
| 136 | |
| 137 | if((internal & 0x8000) == 0){ // 0℃以上 |
| 138 | disp = (internal >> 4) * 0.0625; |
| 139 | } else { // 0℃未満 |
| 140 | disp = (((0xffff - internal) >> 4) + 1) * -0.0625; |
| 141 | } |
| 142 | Serial.print(internal,HEX); |
| 143 | Serial.print(" : "); |
| 144 | Serial.print(disp); |
| 145 | |
| 146 | Serial.println(""); |
| 147 | } |
| 148 | } |
| 149 | }}} |