Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)
Dependencies: UniGraphic mbed vt100
LM75B.cpp
00001 #include "mbed.h" 00002 #include "LM75B.h" 00003 #include "af_mgr.h" 00004 00005 /* Register list */ 00006 #define PTR_CONF 0x01 00007 #define PTR_TEMP 0x00 00008 #define PTR_TOS 0x03 00009 #define PTR_THYST 0x02 00010 00011 /* Configuration register */ 00012 /* B[7:5] : Reserved */ 00013 /* B[4:3] : OS_F_QUE[1:0] OS fault queue value */ 00014 #define CONFIG_QUE_1 0x00 00015 #define CONFIG_QUE_2 (0x01 << 3) 00016 #define CONFIG_QUE_4 (0x10 << 3) 00017 #define CONFIG_QUE_6 (0x11 << 3) 00018 /* B[2] : OS_POL 0 = OS active LOW, 1 = OS active HIGH */ 00019 #define CONFIG_OS_POL_L 0x00 00020 #define CONFIG_OS_POL_H (0x01 << 2) 00021 /* B[1] : OS_COMP_INT 0 = OS comparator, 1 = OS interrupt */ 00022 #define CONFIG_OS_COMP 0x00 00023 #define CONFIG_OS_INT (0x01 << 1) 00024 /* B[0] : SHUTDOWN 0 = normal, 1 = shutdown */ 00025 #define CONFIG_NORMARL 0x00 00026 #define CONFIG_SHUTDOWN 0x01 00027 00028 /* Temperature register */ 00029 /* D[15:5] = 11 bit data 0.125 * temp data */ 00030 /* D[4:0] : reserved */ 00031 00032 /* Tos register */ 00033 /* D[15:7] = 9 bit data */ 00034 /* D[6:0] : reserved */ 00035 00036 /* Thyst register */ 00037 /* D[15:7] = 9 ibt data */ 00038 /* D[6:0] : reserved */ 00039 00040 LM75B::LM75B(I2C *i2c, int addr) : m_addr(addr<<1) { 00041 p_i2c = i2c ; 00042 p_i2c->frequency(100000); /* 100kHz */ 00043 // activate the peripheral 00044 } 00045 00046 LM75B::~LM75B() { } 00047 00048 int LM75B::temp(int8_t *temp) 00049 { 00050 int result ; 00051 char t[1] = { 0x00 } ; 00052 result = p_i2c->write(m_addr, t, 1, true) ; 00053 if (result == 0) { 00054 result = p_i2c->read(m_addr, t, 1) ; 00055 } 00056 if (result == 0) { 00057 *temp = (int8_t)t[0] ; 00058 } 00059 return( result ) ; 00060 } 00061 00062 int LM75B::getTemp(float *temp) 00063 { 00064 int result ; 00065 char t[2] = { 0, 0 } ; 00066 int16_t iTemp = 0 ; 00067 result = p_i2c->write(m_addr, t, 1) ; /* write pointer byte 0x00 */ 00068 if (result == 0) { 00069 result = p_i2c->read(m_addr, t, 2) ; /* read MSB, LSB */ 00070 } 00071 if (result == 0) { 00072 iTemp = (t[0] << 8) | t[1] ; 00073 iTemp >>= 5 ; 00074 *temp = 0.125 * iTemp ; 00075 } 00076 return( result ) ; 00077 } 00078 00079 int LM75B::getConfig(uint8_t ptr_byte, uint8_t *config_data) 00080 { 00081 int result ; 00082 char config = 0x00 ; /* default value */ 00083 result = p_i2c->write(m_addr, (char*)(&ptr_byte), 1, true) ; 00084 if (result == 0) { 00085 result = p_i2c->read(m_addr, &config, 1) ; 00086 } 00087 if (result == 0) { 00088 *config_data = config ; 00089 } 00090 return( result ) ; 00091 } 00092 00093 int LM75B::setConfig(uint8_t ptr_byte, uint8_t config_data) 00094 { 00095 int result ; 00096 char t[2] ; 00097 t[0] = ptr_byte ; 00098 t[1] = config_data ; 00099 result = p_i2c->write(m_addr, t, 2, true) ; 00100 return( result ) ; 00101 } 00102 00103 int LM75B::readRegs(int addr, uint8_t * data, int len) 00104 { 00105 int result ; 00106 char t[1] = {addr}; 00107 __disable_irq() ; // Disable Interrupts 00108 result = p_i2c->write(m_addr, t, 1, true); 00109 if (result == 0) { 00110 result = p_i2c->read(m_addr, (char *)data, len); 00111 } 00112 __enable_irq() ; // Enable Interrupts 00113 return( result ) ; 00114 } 00115 00116 int LM75B::writeRegs(uint8_t * data, int len) { 00117 int result ; 00118 __disable_irq() ; // Disable Interrupts 00119 result = p_i2c->write(m_addr, (char *)data, len); 00120 __enable_irq() ; // Enable Interrupts 00121 return( result ) ; 00122 }
Generated on Wed Jul 13 2022 12:25:10 by 1.7.2