code to access the AT30TSE75x temperature and E-prom device
Dependents: AT30TSE752TST AT30TSE752TST2
Diff: AT30TSE75x.cpp
- Revision:
- 0:7cb648bc5c2a
- Child:
- 1:c0db18a0c56b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AT30TSE75x.cpp Fri Jan 13 12:11:55 2017 +0000 @@ -0,0 +1,176 @@ +#include "mbed.h" +#include "AT30TSE75x.h" + +/* + * info see AT30SE75x.h + * (C) Wim Beaumont Universiteit Antwerpen 2017 + + * some parts of the code are copied + * from https://developer.mbed.org/users/akhilpanayam/code/AT30TSE75X/file/0e430cef393b/AT30TSE75X.h +*/ + + +#define VERSION_AT30TSE75x_SRC "0.80" + +#define AT30TSE75X_ADD_TEMP 0x48 /*Temperature Sensor: 0b1001xxx */ +#define AT30TSE75X_ADD_EEPROM 0x50 /*EEPROM: 0b1010xxx */ +#define AT30TSE752 1 +#define AT30TSE754 2 +#define AT30TSE758 3 + +#define AT30TSE_CONFIG_RES_9_bit 0 +#define AT30TSE_CONFIG_RES_10_bit 1 +#define AT30TSE_CONFIG_RES_11_bit 2 +#define AT30TSE_CONFIG_RES_12_bit 3 + + +#define AT30TSE_TEMPERATURE_REG_SIZE 2 + + +#define TReg 0x0 // 16 bits +#define ConfigReg 0x1 // 16 bits +#define TLowReg 0x2 // 16 bits +#define THighReg 0x3 // 16 bits +#define ConfigRegNV 0x11 // 16 bits +#define TLowRegNV 0x12 // 16 bits +#define THighRegNV 0x13 // 16 bits + +AT30TSE75x::AT30TSE75x (I2CInterface* i2cinterface, int device_address_bits,int eepromsize): + getVersion( VERSION_AT30TSE75x_HDR,VERSION_AT30TSE75x_SRC, __TIME__, __DATE__),_i2c(i2cinterface) { + Taddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_TEMP ) & 0x7F; Taddr=Taddr<<1; + Eaddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_EEPROM ) & 0x7F; Eaddr=Eaddr<<1; + + Esize=1; + if( eepromsize ==4 ) Esize=2; if( eepromsize ==8) Esize=3; + resolution=AT30TSE_CONFIG_RES_12_bit; +} + + + + +float AT30TSE75x::get_temperature(int *error) { + + + float temperature; + uint16_t data; + int locerr=-200; + buffer[0] = 0; + buffer[1] = 0; + + locerr=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); + if ( error ) *error=locerr; // pointer !=0 + + data = (buffer[0] << 8) | buffer[1]; + int8_t sign = 1; + + /* Check if negative and clear sign bit. */ + if (data & (1 << 15)) { + sign *= -1; + data &= ~(1 << 15); + } + + /* Convert to temperature. */ + switch (resolution) { + case AT30TSE_CONFIG_RES_9_bit: + data = (data >> 7); + (temperature) = data * sign * 0.5; + break; + + case AT30TSE_CONFIG_RES_10_bit: + data = (data >> 6); + (temperature) = data * sign * 0.25; + break; + + case AT30TSE_CONFIG_RES_11_bit: + data = (data >> 5); + (temperature) = data * sign * 0.125; + break; + + case AT30TSE_CONFIG_RES_12_bit: + data = (data >> 4); + (temperature) = data * sign * 0.0625; + break; + + default: + break; + } + return temperature; +} + + +int AT30TSE75x::read_config( int Nonvolatille,int *error ) { + uint16_t data; + int locerr; + buffer[0]= ConfigReg; // set pointer to config reg nv + locerr=_i2c->write(Taddr,(char *)buffer,1,false); + locerr= locerr<<2; + locerr|=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); + locerr= locerr<<2; + data = (buffer[0] << 8) | buffer[1]; + buffer[0]= TReg; + locerr|=_i2c->write(Taddr,(char *)buffer,1,false); + if ( error ) *error=locerr; // pointer !=0 + return (int)data; +} + + +int AT30TSE75x::set_config( int Nonvolatille ) { + int locerr; + buffer[0]= ConfigReg; // set pointer to config reg nv + + + + /* for no all fixed values + 15 normal : 1 0b1xxx xxxx xxxx xxxx + 14:13 12 bit :11 0b111x xxxx xxxx xxxx + 12:11 1 fault:00 0b1110 0xxx xxxx xxxx + 10 0 act low 0b1110 00xx xxxx xxxx + 9 0 cmp mode 0b1110 000x xxxx xxxx + 8 0 temp act 0b1110 0000 xxxx xxxx + 7:1 dont care 0b1110 0000 0000 000x + 0 read only 0b1110 0000 0000 0000 + => 0xE0 + */ + buffer[1]=0xE0; buffer[2]=0x00; + locerr=_i2c->write(Taddr,(char *)buffer,3,false); + buffer[0]= TReg; + locerr= locerr<<2; + locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again + return locerr; +} + + + + +int AT30TSE75x::read_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) { + char buff[20]; + buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4); + + _i2c->write(Eaddr,buff,1,false); + return _i2c->read(Eaddr,data,length,false); +} + +int AT30TSE75x::write_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) +{ + char buff[length+1]; + int buffsizep1=length+1;//buffersize+1 + buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4); + for(int i = 1; i < buffsizep1; i++) { + buff[i] = *(data++); + } + return _i2c->write(Eaddr,buff,(length + 1),false); +} + + +int AT30TSE75x::read_eeprombyte(char &data, uint8_t word_addr, uint8_t page){ + char rbuf[2]; + int i2cresult=read_eeprompage(rbuf,1,word_addr,page); + data=rbuf[0]; + return i2cresult; +} + + +int AT30TSE75x::write_eeprombyte(char data, uint8_t word_addr, uint8_t page){ + char wbuf[2]; wbuf[0]=data; + return write_eeprompage(wbuf,1,word_addr,page); + } \ No newline at end of file