TC77 --- SPI Thermal Sensor
TC77 SPI thermal(temperature) sensor chip
ライブラリを公開する手順の練習を兼ねて、サンプルプログラムとして公開していた物からライブラリ部分を分離してみました。
温度センサの TC77 を使うためのライブラリです。 SPIなのにIOが共通になっているという変わったチップです。
MOSIとMISOを抵抗で繋いでしまうというのがポイントで、それさえ解れば難しいトコはありません。
pin connection mbed TC77 MOSI ---R 10K---+ | MISO -----------+--- SI/O SCK --------------- SCK CS --------------- CS(active low)
TC77.h@0:b8fa872b60e6, 2016-06-10 (annotated)
- Committer:
- jk1lot
- Date:
- Fri Jun 10 14:27:27 2016 +0000
- Revision:
- 0:b8fa872b60e6
first published version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jk1lot | 0:b8fa872b60e6 | 1 | /* |
jk1lot | 0:b8fa872b60e6 | 2 | * TC77 SPI Thermal Sensor |
jk1lot | 0:b8fa872b60e6 | 3 | * |
jk1lot | 0:b8fa872b60e6 | 4 | * mbed TC77 |
jk1lot | 0:b8fa872b60e6 | 5 | * MOSI ---R 10K---+ |
jk1lot | 0:b8fa872b60e6 | 6 | * | |
jk1lot | 0:b8fa872b60e6 | 7 | * MISO -----------+--- pin1 SI/O. |
jk1lot | 0:b8fa872b60e6 | 8 | * SCK --------------- pin2 SCK. |
jk1lot | 0:b8fa872b60e6 | 9 | * CS --------------- pin7 CS(active low). |
jk1lot | 0:b8fa872b60e6 | 10 | * |
jk1lot | 0:b8fa872b60e6 | 11 | */ |
jk1lot | 0:b8fa872b60e6 | 12 | |
jk1lot | 0:b8fa872b60e6 | 13 | #ifndef TC77_H |
jk1lot | 0:b8fa872b60e6 | 14 | #define TC77_H |
jk1lot | 0:b8fa872b60e6 | 15 | |
jk1lot | 0:b8fa872b60e6 | 16 | #include "mbed.h" |
jk1lot | 0:b8fa872b60e6 | 17 | |
jk1lot | 0:b8fa872b60e6 | 18 | /** TC77 SPI Thermal Sensor chip |
jk1lot | 0:b8fa872b60e6 | 19 | * |
jk1lot | 0:b8fa872b60e6 | 20 | * @note pin connection |
jk1lot | 0:b8fa872b60e6 | 21 | * @code |
jk1lot | 0:b8fa872b60e6 | 22 | * mbed TC77device |
jk1lot | 0:b8fa872b60e6 | 23 | * MOSI ---R 10K---+ |
jk1lot | 0:b8fa872b60e6 | 24 | * | |
jk1lot | 0:b8fa872b60e6 | 25 | * MISO -----------+--- pin1 SI/O |
jk1lot | 0:b8fa872b60e6 | 26 | * SCK --------------- pin2 SCK |
jk1lot | 0:b8fa872b60e6 | 27 | * CS --------------- pin7 CS(active low) |
jk1lot | 0:b8fa872b60e6 | 28 | * @endcode |
jk1lot | 0:b8fa872b60e6 | 29 | * |
jk1lot | 0:b8fa872b60e6 | 30 | @note sample program |
jk1lot | 0:b8fa872b60e6 | 31 | @code |
jk1lot | 0:b8fa872b60e6 | 32 | #include "mbed.h" |
jk1lot | 0:b8fa872b60e6 | 33 | #include "TC77.h" |
jk1lot | 0:b8fa872b60e6 | 34 | |
jk1lot | 0:b8fa872b60e6 | 35 | SPI spi1(p5,p6,p7); |
jk1lot | 0:b8fa872b60e6 | 36 | TC77 tc77(&spi1, p8); |
jk1lot | 0:b8fa872b60e6 | 37 | |
jk1lot | 0:b8fa872b60e6 | 38 | int main() { |
jk1lot | 0:b8fa872b60e6 | 39 | while(true) { |
jk1lot | 0:b8fa872b60e6 | 40 | printf("%.1f \r\n",tc77.temperature()); |
jk1lot | 0:b8fa872b60e6 | 41 | wait(1.0); |
jk1lot | 0:b8fa872b60e6 | 42 | } |
jk1lot | 0:b8fa872b60e6 | 43 | } |
jk1lot | 0:b8fa872b60e6 | 44 | @endcode |
jk1lot | 0:b8fa872b60e6 | 45 | */ |
jk1lot | 0:b8fa872b60e6 | 46 | class TC77 |
jk1lot | 0:b8fa872b60e6 | 47 | { |
jk1lot | 0:b8fa872b60e6 | 48 | SPI *spi; |
jk1lot | 0:b8fa872b60e6 | 49 | DigitalOut cs; |
jk1lot | 0:b8fa872b60e6 | 50 | public: |
jk1lot | 0:b8fa872b60e6 | 51 | //! @param s pointer to SPI object |
jk1lot | 0:b8fa872b60e6 | 52 | //! @param pin PinName of CS |
jk1lot | 0:b8fa872b60e6 | 53 | TC77(SPI *s, PinName pin); |
jk1lot | 0:b8fa872b60e6 | 54 | |
jk1lot | 0:b8fa872b60e6 | 55 | //! go to shutdown mode(power save) |
jk1lot | 0:b8fa872b60e6 | 56 | inline void shutdown(void) {write(0xffff);} |
jk1lot | 0:b8fa872b60e6 | 57 | //! start from shutdown mode |
jk1lot | 0:b8fa872b60e6 | 58 | void start(void); |
jk1lot | 0:b8fa872b60e6 | 59 | //! get device ID |
jk1lot | 0:b8fa872b60e6 | 60 | uint16_t id(void); |
jk1lot | 0:b8fa872b60e6 | 61 | //! get temperature @return Temperature value, expressed in degrees Celsius |
jk1lot | 0:b8fa872b60e6 | 62 | float temperature(void); |
jk1lot | 0:b8fa872b60e6 | 63 | protected: |
jk1lot | 0:b8fa872b60e6 | 64 | //! read data and send command. |
jk1lot | 0:b8fa872b60e6 | 65 | //! It is impossible in the TC77 to perform the writing and reading at the same time. |
jk1lot | 0:b8fa872b60e6 | 66 | //! After reading 16bit, TC77 is ready to write. So total transferred bit is 32. |
jk1lot | 0:b8fa872b60e6 | 67 | int16_t write(int16_t command); |
jk1lot | 0:b8fa872b60e6 | 68 | //! read data. |
jk1lot | 0:b8fa872b60e6 | 69 | //! transferred 16bit only |
jk1lot | 0:b8fa872b60e6 | 70 | int16_t read(void); |
jk1lot | 0:b8fa872b60e6 | 71 | }; |
jk1lot | 0:b8fa872b60e6 | 72 | |
jk1lot | 0:b8fa872b60e6 | 73 | #endif |