I2C 接続の温度センサ ADT7410 用のライブラリ. Library for temperature sensor ADT7410 connected using I2C interface.
Dependents: Mbed2_ConnectTestAll Demo_ADT7410
ADT7410.cpp@7:990e3c54ac51, 2020-04-16 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Apr 16 04:28:59 2020 +0000
- Revision:
- 7:990e3c54ac51
- Parent:
- 6:438204ab1793
8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:b6dc7c54b451 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 7:990e3c54ac51 | 2 | // ADT7410 クラスのメンバ関数の定義 |
MikamiUitOpen | 5:bbcd91ba7c6b | 3 | // |
MikamiUitOpen | 7:990e3c54ac51 | 4 | // 2020/04/16, Copyright (c) 2020 MIKAMI, Naoki |
MikamiUitOpen | 0:b6dc7c54b451 | 5 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:b6dc7c54b451 | 6 | |
MikamiUitOpen | 0:b6dc7c54b451 | 7 | #include "ADT7410.hpp" |
MikamiUitOpen | 0:b6dc7c54b451 | 8 | |
MikamiUitOpen | 0:b6dc7c54b451 | 9 | namespace Mikami |
MikamiUitOpen | 0:b6dc7c54b451 | 10 | { |
MikamiUitOpen | 7:990e3c54ac51 | 11 | // コンストラクタ |
MikamiUitOpen | 0:b6dc7c54b451 | 12 | ADT7410::ADT7410(PinName sda, PinName scl, uint8_t addr) |
MikamiUitOpen | 7:990e3c54ac51 | 13 | : ADDR_(addr), i2cPtr_(new I2C(sda, scl)), i2c_(*i2cPtr_) |
MikamiUitOpen | 7:990e3c54ac51 | 14 | { Reset(); } |
MikamiUitOpen | 7:990e3c54ac51 | 15 | |
MikamiUitOpen | 7:990e3c54ac51 | 16 | // 外部の I2C オブジェクトを与えるコンストラクタ |
MikamiUitOpen | 7:990e3c54ac51 | 17 | ADT7410::ADT7410(I2C &i2c, uint8_t addr) |
MikamiUitOpen | 7:990e3c54ac51 | 18 | : ADDR_(addr), i2cPtr_(NULL), i2c_(i2c) |
MikamiUitOpen | 5:bbcd91ba7c6b | 19 | { Reset(); } |
MikamiUitOpen | 0:b6dc7c54b451 | 20 | |
MikamiUitOpen | 7:990e3c54ac51 | 21 | // コンフィギュレーション・レジスタの設定 |
MikamiUitOpen | 7:990e3c54ac51 | 22 | // ADT7410 のデータシートの Figure 15 参照 |
MikamiUitOpen | 7:990e3c54ac51 | 23 | void ADT7410::SetConfig(char val) const |
MikamiUitOpen | 0:b6dc7c54b451 | 24 | { |
MikamiUitOpen | 7:990e3c54ac51 | 25 | TxRegAddr(CONFIG_, true); |
MikamiUitOpen | 7:990e3c54ac51 | 26 | i2c_.write(val); |
MikamiUitOpen | 7:990e3c54ac51 | 27 | i2c_.stop(); |
MikamiUitOpen | 0:b6dc7c54b451 | 28 | } |
MikamiUitOpen | 5:bbcd91ba7c6b | 29 | |
MikamiUitOpen | 7:990e3c54ac51 | 30 | // 温度の読み込み |
MikamiUitOpen | 7:990e3c54ac51 | 31 | // ADT7410 のデータシートの Figure 18 参照 |
MikamiUitOpen | 7:990e3c54ac51 | 32 | float ADT7410::Read() const |
MikamiUitOpen | 0:b6dc7c54b451 | 33 | { |
MikamiUitOpen | 7:990e3c54ac51 | 34 | TxRegAddr(TEMPER_, true); |
MikamiUitOpen | 7:990e3c54ac51 | 35 | i2c_.start(); |
MikamiUitOpen | 7:990e3c54ac51 | 36 | i2c_.write(ADDR_ | 0x01); |
MikamiUitOpen | 7:990e3c54ac51 | 37 | uint8_t valH = i2c_.read(1); |
MikamiUitOpen | 7:990e3c54ac51 | 38 | int16_t value = (valH << 8) | i2c_.read(0); |
MikamiUitOpen | 7:990e3c54ac51 | 39 | i2c_.stop(); |
MikamiUitOpen | 5:bbcd91ba7c6b | 40 | |
MikamiUitOpen | 2:c134a43c7875 | 41 | if ( (GetConfig() & 0x80) == 0x00 ) |
MikamiUitOpen | 7:990e3c54ac51 | 42 | value &= 0xfff8; // 分解能: 13 ビットの場合 |
MikamiUitOpen | 0:b6dc7c54b451 | 43 | return value/128.0f; |
MikamiUitOpen | 0:b6dc7c54b451 | 44 | } |
MikamiUitOpen | 0:b6dc7c54b451 | 45 | |
MikamiUitOpen | 7:990e3c54ac51 | 46 | // リセット |
MikamiUitOpen | 7:990e3c54ac51 | 47 | // ADT7410 のデータシート Rev.A の p.19 参照 |
MikamiUitOpen | 7:990e3c54ac51 | 48 | void ADT7410::Reset() const |
MikamiUitOpen | 0:b6dc7c54b451 | 49 | { |
MikamiUitOpen | 7:990e3c54ac51 | 50 | TxRegAddr(RESET_); |
MikamiUitOpen | 0:b6dc7c54b451 | 51 | wait_ms(250); |
MikamiUitOpen | 7:990e3c54ac51 | 52 | // 分解能: 13 ビット,continuous 変換モード に設定 |
MikamiUitOpen | 2:c134a43c7875 | 53 | SetConfig(0x00); |
MikamiUitOpen | 0:b6dc7c54b451 | 54 | } |
MikamiUitOpen | 7:990e3c54ac51 | 55 | |
MikamiUitOpen | 7:990e3c54ac51 | 56 | // ADT7410 内部レジスタのアドレスの送信 |
MikamiUitOpen | 7:990e3c54ac51 | 57 | void ADT7410::TxRegAddr(uint8_t reg, bool repeated) const |
MikamiUitOpen | 7:990e3c54ac51 | 58 | { |
MikamiUitOpen | 7:990e3c54ac51 | 59 | i2c_.start(); |
MikamiUitOpen | 7:990e3c54ac51 | 60 | i2c_.write(ADDR_); |
MikamiUitOpen | 7:990e3c54ac51 | 61 | i2c_.write(reg); |
MikamiUitOpen | 7:990e3c54ac51 | 62 | if (!repeated) i2c_.stop(); |
MikamiUitOpen | 7:990e3c54ac51 | 63 | } |
MikamiUitOpen | 7:990e3c54ac51 | 64 | |
MikamiUitOpen | 7:990e3c54ac51 | 65 | // ADT7410 の内部レジスタの 1 バイトの読み込み |
MikamiUitOpen | 7:990e3c54ac51 | 66 | uint8_t ADT7410::GetReg(Reg addr) const |
MikamiUitOpen | 7:990e3c54ac51 | 67 | { |
MikamiUitOpen | 7:990e3c54ac51 | 68 | TxRegAddr(addr, true); |
MikamiUitOpen | 7:990e3c54ac51 | 69 | i2c_.start(); |
MikamiUitOpen | 7:990e3c54ac51 | 70 | i2c_.write(ADDR_ | 0x01); |
MikamiUitOpen | 7:990e3c54ac51 | 71 | uint8_t val = i2c_.read(0); |
MikamiUitOpen | 7:990e3c54ac51 | 72 | i2c_.stop(); |
MikamiUitOpen | 7:990e3c54ac51 | 73 | return val; |
MikamiUitOpen | 7:990e3c54ac51 | 74 | } |
MikamiUitOpen | 6:438204ab1793 | 75 | } |