LM75B Digital temperature sensor and thermal watchdog

Dependents:   testLM75B testSensor TCPSocket_Client

Revision:
0:1cf64e89f29d
Child:
1:61987c319606
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75B.cpp	Wed Jul 06 07:02:52 2016 +0000
@@ -0,0 +1,83 @@
+#include "mbed.h"
+#include "LM75B.h"
+
+/* Register list */
+#define PTR_CONF    0x01
+#define PTR_TEMP    0x00
+#define PTR_TOS     0x03
+#define PTR_THYST   0x02
+
+/* Configuration register */
+/* B[7:5] : Reserved */
+/* B[4:3] : OS_F_QUE[1:0] OS fault queue value */
+#define CONFIG_QUE_1    0x00
+#define CONFIG_QUE_2    (0x01 << 3)
+#define CONFIG_QUE_4    (0x10 << 3)
+#define CONFIG_QUE_6    (0x11 << 3) 
+/* B[2] : OS_POL  0 = OS active LOW,  1 = OS active HIGH */
+#define CONFIG_OS_POL_L 0x00
+#define CONFIG_OS_POL_H (0x01 << 2)
+/* B[1] : OS_COMP_INT 0 = OS comparator, 1 = OS interrupt */
+#define CONFIG_OS_COMP  0x00
+#define CONFIG_OS_INT   (0x01 << 1)
+/* B[0] : SHUTDOWN    0 = normal, 1 = shutdown */
+#define CONFIG_NORMARL  0x00
+#define CONFIG_SHUTDOWN 0x01
+
+/* Temperature register */
+/* D[15:5] = 11 bit data 0.125 * temp data */
+/* D[4:0] : reserved */
+
+
+
+LM75B::LM75B(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) {
+    // activate the peripheral
+}
+
+LM75B::~LM75B() { }
+
+int8_t LM75B::temp(void)
+{
+    char t[1] = { 0x00 } ;
+    int8_t temp ;
+    m_i2c.write(m_addr, t, 1, true) ;
+    m_i2c.read(m_addr, t, 1) ;
+    temp = (int8_t)t[0] ;
+    return( temp ) ;
+}
+
+void LM75B::getTemp(float *temp)
+{
+    char t[2] ;
+    int16_t iTemp = 0 ;
+    m_i2c.read(m_addr, t, 2) ; /* read MSB, LSB */
+    iTemp = (t[0] << 8) | t[1] ;
+    iTemp >>= 5 ;
+    *temp = 0.125 * iTemp ;
+}
+
+uint8_t LM75B::getConfig(uint8_t ptr_byte) 
+{
+    char config ;
+    m_i2c.write(m_addr, (char*)(&ptr_byte), 1, true) ;
+    m_i2c.read(m_addr, &config, 1) ;
+    return( config ) ;
+}
+
+void LM75B::setConfig(uint8_t ptr_byte, uint8_t config_data) 
+{
+    char t[2] ;
+    t[0] = ptr_byte ;
+    t[1] = config_data ;
+    m_i2c.write(m_addr, t, 2, true) ;
+}
+
+void LM75B::readRegs(int addr, uint8_t * data, int len) {
+    char t[1] = {addr};
+    m_i2c.write(m_addr, t, 1, true);
+    m_i2c.read(m_addr, (char *)data, len);
+}
+
+void LM75B::writeRegs(uint8_t * data, int len) {
+    m_i2c.write(m_addr, (char *)data, len);
+}
\ No newline at end of file