Skip to content

Instantly share code, notes, and snippets.

@weldtype
Created April 2, 2017 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weldtype/d1993613381f2a3873c3461a8ac231ca to your computer and use it in GitHub Desktop.
Save weldtype/d1993613381f2a3873c3461a8ac231ca to your computer and use it in GitHub Desktop.
// MLX90614の実験
// 2017/01/28 edy
// 2017/01/29 8x2行LCDを追加
//http://mag.switch-science.com/2014/11/05/i2c_lcd_breakout_int/
//
//2017/03/19 ラジオペンチさんのdelayWDTを使う
//http://radiopench.blog96.fc2.com/blog-entry-486.html
/***************************************************
This is a library example for the MLX90614 Temp Sensor
Designed specifically to work with the MLX90614 sensors in the
adafruit shop
----> https://www.adafruit.com/products/1748
----> https://www.adafruit.com/products/1749
These sensors use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include<Wire.h>
#include <Adafruit_MLX90614.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
#define LCDAdr 0x3e
byte contrast = 30; // コントラスト(0~63)
void setup()
{
Wire.begin();
mlx.begin();
Serial.begin(9600);
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00111001); // function set
lcd_cmd(0b00000100); // EntryModeSet
lcd_cmd(0b00010100); // interval osc
lcd_cmd(0b01110000 | (contrast & 0xF)); // contrast Low
lcd_cmd(0b01011100 | ((contrast >> 4) & 0x3)); // contast High/icon/power
lcd_cmd(0b01101100); // follower control
delay(200);
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00001100); // Display On
lcd_cmd(0b00000001); // Clear Display
delay(100);
lcd_clear();
lcd_setCursor(0, 0);
lcd_printStr("MLX90614");
Serial.println("MLX90614 test");
delay(1000);
}
void loop()
{
float objTemp = mlx.readObjectTempC();
float dieTemp = mlx.readAmbientTempC();
Serial.print(objTemp);
Serial.print(",");
Serial.println(dieTemp);
lcd_clear();
lcd_setCursor(0, 0);
lcd_printFloat(objTemp, 5, 1);
lcd_setCursor(0, 1);
lcd_printFloat(dieTemp, 5, 1);
delayWDT(5);
}
void lcd_cmd(byte x) {
Wire.beginTransmission(LCDAdr);
Wire.write(0b00000000); // CO = 0,RS = 0
Wire.write(x);
Wire.endTransmission();
}
void lcd_clear()
{
lcd_cmd(0b00000001);
}
void lcd_contdata(byte x) {
Wire.write(0b11000000); // CO = 1, RS = 1
Wire.write(x);
}
void lcd_lastdata(byte x) {
Wire.write(0b01000000); // CO = 0, RS = 1
Wire.write(x);
}
// 文字の表示
void lcd_printStr(const char *s) {
Wire.beginTransmission(LCDAdr);
while (*s) {
if (*(s + 1)) {
lcd_contdata(*s);
} else {
lcd_lastdata(*s);
}
s++;
}
Wire.endTransmission();
}
// 表示位置の指定
void lcd_setCursor(byte x, byte y) {
lcd_cmd(0x80 | (y * 0x40 + x));
}
void lcd_printInt(int num)
{
char int2str[10];
sprintf(int2str, "%d", num);
lcd_printStr(int2str);
}
//浮動小数点を文字列に変換して表示
//http://garretlab.web.fc2.com/arduino/reverse_lookup/index.html#string-float_to_string
void lcd_printFloat(float num, int width, int prec)
{
char float2str[10];
dtostrf(num, width, prec, float2str);
lcd_printStr(float2str);
}
// ここから下を全て使う
void delayWDT(unsigned long t) { // パワーダウンモードでdelayを実行
delayWDT_setup(t); // ウォッチドッグタイマー割り込み条件設定
ADCSRA &= ~(1 << ADEN); // ADENビットをクリアしてADCを停止(120μA節約)
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // パワーダウンモード
sleep_enable();
sleep_mode(); // ここでスリープに入る
sleep_disable(); // WDTがタイムアップでここから動作再開
ADCSRA |= (1 << ADEN); // ADCの電源をON (|=が!=になっていたバグを修正2014/11/17)
}
void delayWDT_setup(unsigned int ii) { // ウォッチドッグタイマーをセット。
// 引数はWDTCSRにセットするWDP0-WDP3の値。設定値と動作時間は概略下記
// 0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms
// 6=1sec, 7=2sec, 8=4sec, 9=8sec
byte bb;
if (ii > 9 ){ // 変な値を排除
ii = 9;
}
bb =ii & 7; // 下位3ビットをbbに
if (ii > 7){ // 7以上(7.8,9)なら
bb |= (1 << 5); // bbの5ビット目(WDP3)を1にする
}
bb |= ( 1 << WDCE );
MCUSR &= ~(1 << WDRF); // MCU Status Reg. Watchdog Reset Flag ->0
// start timed sequence
WDTCSR |= (1 << WDCE) | (1<<WDE); // ウォッチドッグ変更許可(WDCEは4サイクルで自動リセット)
// set new watchdog timeout value
WDTCSR = bb; // 制御レジスタを設定
WDTCSR |= _BV(WDIE);
}
ISR(WDT_vect) { // WDTがタイムアップした時に実行される処理
// wdt_cycle++; // 必要ならコメントアウトを外す
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment