Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:b8fa872b60e6, committed 2016-06-10
- Comitter:
- jk1lot
- Date:
- Fri Jun 10 14:27:27 2016 +0000
- Commit message:
- first published version
Changed in this revision
| TC77.cpp | Show annotated file Show diff for this revision Revisions of this file |
| TC77.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r b8fa872b60e6 TC77.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TC77.cpp Fri Jun 10 14:27:27 2016 +0000
@@ -0,0 +1,55 @@
+#include "TC77.h"
+
+TC77::TC77(SPI *s, PinName pin):spi(s),cs(pin)
+{
+ cs=1;
+ spi->format(16);
+ spi->frequency(100*1000);
+ start();
+}
+
+void TC77::start(void)
+{
+ write(0x0000);
+ //shutdownモードからの復帰には最大400ms掛かる(データシートより)
+ wait_ms(400);
+}
+
+uint16_t TC77::id(void)
+{
+ shutdown();
+ int16_t v=write(0x0000);
+ wait_ms(400);
+ return v;
+}
+
+float TC77::temperature(void)
+{
+ int16_t t0=read();
+ //bit2が0のときは測定が終わっていないので値は不正
+ while((t0&0x0004)==0) {
+ wait_ms(400);
+ t0=read();
+ }
+ return ((signed int)t0>>3)*0.0625f;
+}
+
+//TC77は読み取りと書込みが同時には出来ない
+//16bit読み込むと次の16bitが書込みになる
+int16_t TC77::write(int16_t command)
+{
+ cs=0;
+ int16_t v=spi->write(0x0000);
+ spi->write(command);
+ cs=1;
+ return v;
+}
+
+//読むだけならば16bitのやり取りで良い
+int16_t TC77::read(void)
+{
+ cs=0;
+ int16_t v=spi->write(0x0000);
+ cs=1;
+ return v;
+}
diff -r 000000000000 -r b8fa872b60e6 TC77.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TC77.h Fri Jun 10 14:27:27 2016 +0000
@@ -0,0 +1,73 @@
+/*
+ * TC77 SPI Thermal Sensor
+ *
+ * mbed TC77
+ * MOSI ---R 10K---+
+ * |
+ * MISO -----------+--- pin1 SI/O.
+ * SCK --------------- pin2 SCK.
+ * CS --------------- pin7 CS(active low).
+ *
+ */
+
+#ifndef TC77_H
+#define TC77_H
+
+#include "mbed.h"
+
+/** TC77 SPI Thermal Sensor chip
+ *
+ * @note pin connection
+ * @code
+ * mbed TC77device
+ * MOSI ---R 10K---+
+ * |
+ * MISO -----------+--- pin1 SI/O
+ * SCK --------------- pin2 SCK
+ * CS --------------- pin7 CS(active low)
+ * @endcode
+ *
+@note sample program
+@code
+#include "mbed.h"
+#include "TC77.h"
+
+SPI spi1(p5,p6,p7);
+TC77 tc77(&spi1, p8);
+
+int main() {
+ while(true) {
+ printf("%.1f \r\n",tc77.temperature());
+ wait(1.0);
+ }
+}
+@endcode
+*/
+class TC77
+{
+ SPI *spi;
+ DigitalOut cs;
+public:
+ //! @param s pointer to SPI object
+ //! @param pin PinName of CS
+ TC77(SPI *s, PinName pin);
+
+ //! go to shutdown mode(power save)
+ inline void shutdown(void) {write(0xffff);}
+ //! start from shutdown mode
+ void start(void);
+ //! get device ID
+ uint16_t id(void);
+ //! get temperature @return Temperature value, expressed in degrees Celsius
+ float temperature(void);
+protected:
+ //! read data and send command.
+ //! It is impossible in the TC77 to perform the writing and reading at the same time.
+ //! After reading 16bit, TC77 is ready to write. So total transferred bit is 32.
+ int16_t write(int16_t command);
+ //! read data.
+ //! transferred 16bit only
+ int16_t read(void);
+};
+
+#endif
\ No newline at end of file