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.
Dependents: Frequency_Counter_w_GPS_1PPS MQTToverCC3000 Frequency_Cntr_1PPS_F746ZG
Revision 1:4a1eb0f32025, committed 2014-11-30
- Comitter:
- kenjiArai
- Date:
- Sun Nov 30 09:05:07 2014 +0000
- Parent:
- 0:6ec4df1fa459
- Child:
- 2:231bddd40e29
- Commit message:
- fixed bug and added functions
Changed in this revision
| ADT7410.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ADT7410.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ADT7410.cpp Fri Nov 28 10:32:19 2014 +0000
+++ b/ADT7410.cpp Sun Nov 30 09:05:07 2014 +0000
@@ -7,7 +7,7 @@
* http://www.page.sannet.ne.jp/kenjia/index.html
* http://mbed.org/users/kenjiArai/
* Created: November 26th, 2014
- * Revised: November 28th, 2014
+ * Revised: November 30th, 2014
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -31,20 +31,6 @@
#define PRINTF(...) {;}
#endif
-////////////// REGISTER DEFINITION //////////////
-#define ADT7410_A_T_MSB 0x00
-#define ADT7410_A_T_LSB 0x01
-#define ADT7410_A_STATUS 0x02
-#define ADT7410_A_CONFIG 0x03
-#define ADT7410_A_T_H_MSB 0x04
-#define ADT7410_A_T_H_LSB 0x05
-#define ADT7410_A_T_L_MSB 0x06
-#define ADT7410_A_T_L_LSB 0x07
-#define ADT7410_A_T_C_MSB 0x08
-#define ADT7410_A_T_C_LSB 0x09
-#define ADT7410_A_T_HYS 0x0a
-#define ADT7410_A_ID 0x0b
-#define ADT7410_A_SW_RESET 0x2f
ADT7410::ADT7410 (PinName p_sda, PinName p_scl, uint8_t addr) : _i2c(p_sda, p_scl)
{
@@ -62,63 +48,108 @@
void ADT7410::read_all()
{
dt[0] = ADT7410_A_T_MSB;
- _i2c.write((int)ADT7410_addr, (char *)dt, 1, false);
- _i2c.read((int)ADT7410_addr, (char *)dt, 12, true);
+ _i2c.write((int)ADT7410_addr, (char *)dt, 1);
+ _i2c.read((int)ADT7410_addr, (char *)dt, 4);
}
/////////////// Read Temperature //////////////////////////
float ADT7410::read_temp()
{
- dt[0] = ADT7410_A_T_MSB;
- _i2c.write((int)ADT7410_addr, (char *)dt, 1, false);
- _i2c.read((int)ADT7410_addr, (char *)dt, 2, true);
- uint32_t d = (dt[0] << 8) + dt[1];
- PRINTF("d= 0x%04x , dt0= 0x%02x, dt1= 0x%02x\r\n", d, dt[0], dt[1]);
- return (float)d;
+ uint16_t data;
+
+ read_all();
+ PRINTF("Th=0x%02x, Tl=0x%02x, S=0x%02x, C=0x%02x // ", dt[0], dt[1], dt[2], dt[3]);
+ if (dt[3] & 0x80){ // 16bit
+ data = (dt[0] << 8) + dt[1];
+ } else { // 13bit
+ data = (dt[0] << 8) + (dt[1] & 0xf8);
+ }
+#if 1
+ if (dt[0] & 0x80){ // -
+ return (float)(data - 32768) / 128;
+ } else { // +
+ return (float)data / 128;
+ }
+#else
+ return -12.34f; // check - temp for debug
+#endif
}
-
/////////////// Check Status //////////////////////////////
uint8_t ADT7410::read_status()
{
- read_all();
- PRINTF("status= 0x%02x\r\n", dt[2]);
- return dt[2];
+ int8_t dt = read_reg_byte(ADT7410_A_STATUS);
+ PRINTF("status= 0x%02x\r\n", dt);
+ return dt;
}
/////////////// Check Configration //////////////////////////
uint8_t ADT7410::read_config()
{
- read_all();
- PRINTF("config= 0x%02x\r\n", dt[3]);
- return dt[3];
+ int8_t dt = read_reg_byte(ADT7410_A_CONFIG);
+ PRINTF("config= 0x%02x\r\n", dt);
+ return dt;
}
/////////////// Set Configration //////////////////////////
uint8_t ADT7410::set_config(uint8_t cfg)
{
- uint8_t dx[2];
+ return write_reg_byte(ADT7410_A_CONFIG, cfg);
+}
+
+/////////////// Read ID & REV //////////////////////////
+void ADT7410::read_id_rev()
+{
+ uint8_t dt;
- dx[0] = ADT7410_A_CONFIG;
- dx[1] = cfg;
- _i2c.write((int)ADT7410_addr, (char *)dx, 2);
- return read_config();
+ dt = read_reg_byte(ADT7410_A_ID);
+ PRINTF("ID & Rev: 0x%02x\r\n", dt);
+ id_number = GET_ID(dt);
+ rev_number = GET_REV(dt);
+}
+
+uint8_t ADT7410::read_ID()
+{
+ return id_number;
+}
+
+uint8_t ADT7410::read_REV()
+{
+ return rev_number;
}
-/////////////// Read ID ///////////////////////////////////
-uint8_t ADT7410::read_id()
+uint8_t ADT7410::who_am_i()
{
- read_all();
- PRINTF("ID reg= 0x%02x\r\n", dt[11]);
- return (dt[11 ]>> 3);
+ if (id_number == I_AM_ADT7410){
+ return 1;
+ } else {
+ return 0;
+ }
}
-/////////////// Read Revision /////////////////////////////
-uint8_t ADT7410::read_revision()
+/////////////// Read/Write specific register //////////////
+uint8_t ADT7410::read_reg_byte(uint8_t reg)
{
- read_all();
- PRINTF("ID reg= 0x%02x\r\n", dt[11]);
- return (dt[11] & 0x7);
+ uint8_t dx[2];
+
+ dx[0] = reg;
+ _i2c.write((int)ADT7410_addr, (char *)dx, 1, true);
+ _i2c.read((int)ADT7410_addr, (char *)dx, 1);
+ _i2c.stop();
+ PRINTF("reg:0x%02x, data=0x%02x\r\n", reg, dx[0]);
+ return dx[0];
+}
+
+uint8_t ADT7410::write_reg_byte(uint8_t reg, uint8_t dt)
+{
+ uint8_t dx[2];
+
+ dx[0] = reg;
+ _i2c.write((int)ADT7410_addr, (char *)dx, 2);
+ _i2c.stop();
+ dx[1] = read_reg_byte(reg);
+ PRINTF("reg:0x%02x, write/data=0x%02x, read/data=0x%02x\r\n", reg, dx[0], dx[1]);
+ return dx[1];
}
/////////////// Initialize ////////////////////////////////
@@ -127,6 +158,7 @@
uint8_t dx[2];
dx[0] = ADT7410_A_CONFIG;
- dx[1] = RESOLUTION_16BIT + OPERATION_MODE_1SPS;
+ dx[1] = RESOLUTION_13BIT + OPERATION_MODE_CONT;
_i2c.write((int)ADT7410_addr, (char *)dx, 2);
+ read_id_rev();
}
--- a/ADT7410.h Fri Nov 28 10:32:19 2014 +0000
+++ b/ADT7410.h Sun Nov 30 09:05:07 2014 +0000
@@ -7,7 +7,7 @@
* http://www.page.sannet.ne.jp/kenjia/index.html
* http://mbed.org/users/kenjiArai/
* Created: November 26th, 2014
- * Revised: November 28th, 2014
+ * Revised: November 30th, 2014
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -19,7 +19,7 @@
#ifndef MBED_ADT7410
#define MBED_ADT7410
-// ADT7410 Temperature Sensor
+////////////// ADDRESS //////////////////////////
// 7bit address = 0b01100000(0x48) -> 8bit = 0b11000000(0x90) -> 0x91(Read) or 0x90(Write)
// ADDR_01 = (A1=0=J4)+(A0=1=J3), ADDR_1N = (A1=1)+(A0=No Connection =0)
// -> Please make sure your H/W configuration
@@ -31,17 +31,32 @@
#define ADT7410ADDR_10 0x96
#define ADT7410ADDR_11 0x98
-// ID
+////////////// REGISTER DEFINITION //////////////
+#define ADT7410_A_T_MSB 0x00
+#define ADT7410_A_T_LSB 0x01
+#define ADT7410_A_STATUS 0x02
+#define ADT7410_A_CONFIG 0x03
+#define ADT7410_A_T_H_MSB 0x04
+#define ADT7410_A_T_H_LSB 0x05
+#define ADT7410_A_T_L_MSB 0x06
+#define ADT7410_A_T_L_LSB 0x07
+#define ADT7410_A_T_C_MSB 0x08
+#define ADT7410_A_T_C_LSB 0x09
+#define ADT7410_A_T_HYS 0x0a
+#define ADT7410_A_ID 0x0b
+#define ADT7410_A_SW_RESET 0x2f
+
+////////////// ID ///////////////////////////////
#define I_AM_ADT7410 0x19
#define GET_ID(x) ((x >> 3) & 0x1f)
#define GET_REV(x) (x & 0x7)
-// Configration
+////////////// Configration /////////////////////
#define OPERATION_MODE_CONT 0x00
#define OPERATION_MODE_ONESHOT 0x20
#define OPERATION_MODE_1SPS 0x40
#define OPERATION_MODE_SHTDWN 0x60
-#define RESOLUTION_15BIT 0x00
+#define RESOLUTION_13BIT 0x00
#define RESOLUTION_16BIT 0x80
/** ADT7410 class
@@ -57,11 +72,12 @@
* // If you connected I2C line not only this device but also other devices,
* // you need to declare following method.
* I2C i2c(PinName p_sda, PinName p_scl);
- * ADT7410 t(I2C& p_i2c, addr);
+ * ADT7410 t(I2C& p_i2c, addr); // default: 13bit resolution & continuous conv.
*
* int main() {
+ * t.set_config(OPERATION_MODE_1SPS + RESOLUTION_16BIT); // you can change the config.
* while(1){
- * printf("T=%5.2f degC\r\n", t.read_temp());
+ * printf("T=%+6.3f degC\r\n", t.read_temp());
* wait(1.0):
* }
* }
@@ -107,27 +123,48 @@
*/
uint8_t set_config(uint8_t cfg);
- /** Read ADT7410 chip ID
+ /** Read ID
* @param none
- * @return ID number
+ * @return ID
*/
- uint8_t read_id(void);
+ uint8_t read_ID();
+
+ /** Read Revision
+ * @param none
+ * @return Revision
+ */
+ uint8_t read_REV();
- /** Read ADT7410 chip revision
+ /** Read one byte from specific register
+ * @param register address
+ * @return register data
+ */
+ uint8_t read_reg_byte(uint8_t reg);
+
+ /** Write one byte into specific register
+ * @param register address, data
+ * @return register saved data
+ */
+ uint8_t write_reg_byte(uint8_t reg, uint8_t dt);
+
+ /** check ID
* @param none
- * @return revision data
+ * @return ADT7410 = 1, others 0
*/
- uint8_t read_revision(void);
+ uint8_t who_am_i();
protected:
- I2C _i2c;
+ I2C _i2c;
void read_all();
+ void read_id_rev();
void init();
private:
- uint8_t dt[16];
+ uint8_t dt[4];
char ADT7410_addr;
+ uint8_t id_number;
+ uint8_t rev_number;
};
#endif // MBED_ADT7410