mbed-os
Fork of mbed-os by
features/unsupported/tests/peripherals/TMP102/TMP102.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | #include "TMP102.h" |
elessair | 0:f269e3021894 | 2 | |
elessair | 0:f269e3021894 | 3 | #define TEMP_REG_ADDR 0x00 |
elessair | 0:f269e3021894 | 4 | |
elessair | 0:f269e3021894 | 5 | TMP102::TMP102(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
elessair | 0:f269e3021894 | 6 | m_i2c.frequency(400000); |
elessair | 0:f269e3021894 | 7 | } |
elessair | 0:f269e3021894 | 8 | TMP102::~TMP102() { } |
elessair | 0:f269e3021894 | 9 | |
elessair | 0:f269e3021894 | 10 | float TMP102::read() { |
elessair | 0:f269e3021894 | 11 | const char tempRegAddr = TEMP_REG_ADDR; |
elessair | 0:f269e3021894 | 12 | |
elessair | 0:f269e3021894 | 13 | m_i2c.write(m_addr, &tempRegAddr, 1); |
elessair | 0:f269e3021894 | 14 | |
elessair | 0:f269e3021894 | 15 | char reg[2] = {0,0}; |
elessair | 0:f269e3021894 | 16 | m_i2c.read(m_addr, reg, 2); |
elessair | 0:f269e3021894 | 17 | |
elessair | 0:f269e3021894 | 18 | unsigned short res = (reg[0] << 4) | (reg[1] >> 4); |
elessair | 0:f269e3021894 | 19 | |
elessair | 0:f269e3021894 | 20 | float temp = (float) ((float)res * 0.0625); |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | return temp; |
elessair | 0:f269e3021894 | 23 | } |