I2C 接続の温度センサ ADT7410 用のライブラリ. Library for temperature sensor ADT7410 connected using I2C interface.
Dependents: Mbed2_ConnectTestAll Demo_ADT7410
ADT7410.hpp@6:438204ab1793, 2020-04-02 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Apr 02 02:38:16 2020 +0000
- Revision:
- 6:438204ab1793
- Parent:
- 5:bbcd91ba7c6b
- Child:
- 7:990e3c54ac51
7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:b6dc7c54b451 | 1 | //-------------------------------------------------------------- |
MikamiUitOpen | 5:bbcd91ba7c6b | 2 | // Class for ADT7410 (Header) |
MikamiUitOpen | 2:c134a43c7875 | 3 | // Default: 13-bit resolution |
MikamiUitOpen | 5:bbcd91ba7c6b | 4 | // |
MikamiUitOpen | 6:438204ab1793 | 5 | // このプログラムを作った際の mbed のリビジョン:Rev.172 |
MikamiUitOpen | 6:438204ab1793 | 6 | // 2020/04/02, Copyright (c) 2020 MIKAMI, Naoki |
MikamiUitOpen | 0:b6dc7c54b451 | 7 | //-------------------------------------------------------------- |
MikamiUitOpen | 0:b6dc7c54b451 | 8 | |
MikamiUitOpen | 0:b6dc7c54b451 | 9 | #ifndef ADT7410_HPP |
MikamiUitOpen | 0:b6dc7c54b451 | 10 | #define ADT7410_HPP |
MikamiUitOpen | 0:b6dc7c54b451 | 11 | |
MikamiUitOpen | 0:b6dc7c54b451 | 12 | #include "mbed.h" |
MikamiUitOpen | 0:b6dc7c54b451 | 13 | |
MikamiUitOpen | 0:b6dc7c54b451 | 14 | namespace Mikami |
MikamiUitOpen | 0:b6dc7c54b451 | 15 | { |
MikamiUitOpen | 0:b6dc7c54b451 | 16 | class ADT7410 |
MikamiUitOpen | 0:b6dc7c54b451 | 17 | { |
MikamiUitOpen | 0:b6dc7c54b451 | 18 | public: |
MikamiUitOpen | 0:b6dc7c54b451 | 19 | // Constructor |
MikamiUitOpen | 3:e0717c58a396 | 20 | #if defined(STM32F4) || defined(STM32L0) || defined(__STM32F3xx_H) |
MikamiUitOpen | 0:b6dc7c54b451 | 21 | ADT7410(PinName sda = D14, // SDA |
MikamiUitOpen | 0:b6dc7c54b451 | 22 | PinName scl = D15, // SCL |
MikamiUitOpen | 0:b6dc7c54b451 | 23 | uint8_t addr = 0x90); // I2C bus address |
MikamiUitOpen | 3:e0717c58a396 | 24 | // Default constructor is defined for only Nucleo |
MikamiUitOpen | 3:e0717c58a396 | 25 | #else |
MikamiUitOpen | 3:e0717c58a396 | 26 | ADT7410(PinName sda, // SDA |
MikamiUitOpen | 3:e0717c58a396 | 27 | PinName scl, // SCL |
MikamiUitOpen | 3:e0717c58a396 | 28 | uint8_t addr = 0x90); // I2C bus address |
MikamiUitOpen | 5:bbcd91ba7c6b | 29 | #endif |
MikamiUitOpen | 0:b6dc7c54b451 | 30 | // Set configuration register |
MikamiUitOpen | 5:bbcd91ba7c6b | 31 | // See Figure 15 in data sheet of ADT7410 |
MikamiUitOpen | 5:bbcd91ba7c6b | 32 | void SetConfig(char val) |
MikamiUitOpen | 5:bbcd91ba7c6b | 33 | { i2c_.write(ADDR_, (char[]){CONFIG_, val}, 2); } |
MikamiUitOpen | 5:bbcd91ba7c6b | 34 | |
MikamiUitOpen | 0:b6dc7c54b451 | 35 | // Get value in configuration register |
MikamiUitOpen | 0:b6dc7c54b451 | 36 | uint8_t GetConfig(); |
MikamiUitOpen | 0:b6dc7c54b451 | 37 | |
MikamiUitOpen | 1:ad3046485a3e | 38 | // Read temperature |
MikamiUitOpen | 1:ad3046485a3e | 39 | float Read(); |
MikamiUitOpen | 1:ad3046485a3e | 40 | |
MikamiUitOpen | 1:ad3046485a3e | 41 | // Operator shorthand for Read() |
MikamiUitOpen | 1:ad3046485a3e | 42 | operator float() { return Read(); } |
MikamiUitOpen | 1:ad3046485a3e | 43 | |
MikamiUitOpen | 1:ad3046485a3e | 44 | // Reset |
MikamiUitOpen | 1:ad3046485a3e | 45 | void Reset(); |
MikamiUitOpen | 0:b6dc7c54b451 | 46 | |
MikamiUitOpen | 0:b6dc7c54b451 | 47 | protected: |
MikamiUitOpen | 5:bbcd91ba7c6b | 48 | // Register addresses |
MikamiUitOpen | 5:bbcd91ba7c6b | 49 | enum Reg { TEMPER_ = 0x00, // Temperature value MSByte |
MikamiUitOpen | 5:bbcd91ba7c6b | 50 | CONFIG_ = 0x03, // Configuration |
MikamiUitOpen | 5:bbcd91ba7c6b | 51 | RESET_ = 0x2F}; // Software reset |
MikamiUitOpen | 5:bbcd91ba7c6b | 52 | |
MikamiUitOpen | 0:b6dc7c54b451 | 53 | // Write single byte |
MikamiUitOpen | 0:b6dc7c54b451 | 54 | // "STOP" を送らずにふたたび "START" を送る,つまりリスタート・コンディションに |
MikamiUitOpen | 0:b6dc7c54b451 | 55 | // する場合は,"repeated" を "true" にする |
MikamiUitOpen | 5:bbcd91ba7c6b | 56 | void WriteSingleByte(Reg reg, bool repeated = false) |
MikamiUitOpen | 6:438204ab1793 | 57 | { i2c_.write(ADDR_, (char []){reg}, 1, repeated); } |
MikamiUitOpen | 5:bbcd91ba7c6b | 58 | |
MikamiUitOpen | 0:b6dc7c54b451 | 59 | private: |
MikamiUitOpen | 4:3769397d3803 | 60 | const uint8_t ADDR_; // left-justified 7-bit slave address of ADT7410 |
MikamiUitOpen | 0:b6dc7c54b451 | 61 | I2C i2c_; // Object of I2C |
MikamiUitOpen | 0:b6dc7c54b451 | 62 | }; |
MikamiUitOpen | 0:b6dc7c54b451 | 63 | } |
MikamiUitOpen | 6:438204ab1793 | 64 | #endif // ADT7410_HPP |