code to access the AT30TSE75x temperature and E-prom device
Dependents: AT30TSE752TST AT30TSE752TST2
AT30TSE75x.cpp@0:7cb648bc5c2a, 2017-01-13 (annotated)
- Committer:
- wbeaumont
- Date:
- Fri Jan 13 12:11:55 2017 +0000
- Revision:
- 0:7cb648bc5c2a
- Child:
- 1:c0db18a0c56b
read temperature / r/w eeprom
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 0:7cb648bc5c2a | 1 | #include "mbed.h" |
wbeaumont | 0:7cb648bc5c2a | 2 | #include "AT30TSE75x.h" |
wbeaumont | 0:7cb648bc5c2a | 3 | |
wbeaumont | 0:7cb648bc5c2a | 4 | /* |
wbeaumont | 0:7cb648bc5c2a | 5 | * info see AT30SE75x.h |
wbeaumont | 0:7cb648bc5c2a | 6 | * (C) Wim Beaumont Universiteit Antwerpen 2017 |
wbeaumont | 0:7cb648bc5c2a | 7 | |
wbeaumont | 0:7cb648bc5c2a | 8 | * some parts of the code are copied |
wbeaumont | 0:7cb648bc5c2a | 9 | * from https://developer.mbed.org/users/akhilpanayam/code/AT30TSE75X/file/0e430cef393b/AT30TSE75X.h |
wbeaumont | 0:7cb648bc5c2a | 10 | */ |
wbeaumont | 0:7cb648bc5c2a | 11 | |
wbeaumont | 0:7cb648bc5c2a | 12 | |
wbeaumont | 0:7cb648bc5c2a | 13 | #define VERSION_AT30TSE75x_SRC "0.80" |
wbeaumont | 0:7cb648bc5c2a | 14 | |
wbeaumont | 0:7cb648bc5c2a | 15 | #define AT30TSE75X_ADD_TEMP 0x48 /*Temperature Sensor: 0b1001xxx */ |
wbeaumont | 0:7cb648bc5c2a | 16 | #define AT30TSE75X_ADD_EEPROM 0x50 /*EEPROM: 0b1010xxx */ |
wbeaumont | 0:7cb648bc5c2a | 17 | #define AT30TSE752 1 |
wbeaumont | 0:7cb648bc5c2a | 18 | #define AT30TSE754 2 |
wbeaumont | 0:7cb648bc5c2a | 19 | #define AT30TSE758 3 |
wbeaumont | 0:7cb648bc5c2a | 20 | |
wbeaumont | 0:7cb648bc5c2a | 21 | #define AT30TSE_CONFIG_RES_9_bit 0 |
wbeaumont | 0:7cb648bc5c2a | 22 | #define AT30TSE_CONFIG_RES_10_bit 1 |
wbeaumont | 0:7cb648bc5c2a | 23 | #define AT30TSE_CONFIG_RES_11_bit 2 |
wbeaumont | 0:7cb648bc5c2a | 24 | #define AT30TSE_CONFIG_RES_12_bit 3 |
wbeaumont | 0:7cb648bc5c2a | 25 | |
wbeaumont | 0:7cb648bc5c2a | 26 | |
wbeaumont | 0:7cb648bc5c2a | 27 | #define AT30TSE_TEMPERATURE_REG_SIZE 2 |
wbeaumont | 0:7cb648bc5c2a | 28 | |
wbeaumont | 0:7cb648bc5c2a | 29 | |
wbeaumont | 0:7cb648bc5c2a | 30 | #define TReg 0x0 // 16 bits |
wbeaumont | 0:7cb648bc5c2a | 31 | #define ConfigReg 0x1 // 16 bits |
wbeaumont | 0:7cb648bc5c2a | 32 | #define TLowReg 0x2 // 16 bits |
wbeaumont | 0:7cb648bc5c2a | 33 | #define THighReg 0x3 // 16 bits |
wbeaumont | 0:7cb648bc5c2a | 34 | #define ConfigRegNV 0x11 // 16 bits |
wbeaumont | 0:7cb648bc5c2a | 35 | #define TLowRegNV 0x12 // 16 bits |
wbeaumont | 0:7cb648bc5c2a | 36 | #define THighRegNV 0x13 // 16 bits |
wbeaumont | 0:7cb648bc5c2a | 37 | |
wbeaumont | 0:7cb648bc5c2a | 38 | AT30TSE75x::AT30TSE75x (I2CInterface* i2cinterface, int device_address_bits,int eepromsize): |
wbeaumont | 0:7cb648bc5c2a | 39 | getVersion( VERSION_AT30TSE75x_HDR,VERSION_AT30TSE75x_SRC, __TIME__, __DATE__),_i2c(i2cinterface) { |
wbeaumont | 0:7cb648bc5c2a | 40 | Taddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_TEMP ) & 0x7F; Taddr=Taddr<<1; |
wbeaumont | 0:7cb648bc5c2a | 41 | Eaddr= ((device_address_bits & 0x7) | AT30TSE75X_ADD_EEPROM ) & 0x7F; Eaddr=Eaddr<<1; |
wbeaumont | 0:7cb648bc5c2a | 42 | |
wbeaumont | 0:7cb648bc5c2a | 43 | Esize=1; |
wbeaumont | 0:7cb648bc5c2a | 44 | if( eepromsize ==4 ) Esize=2; if( eepromsize ==8) Esize=3; |
wbeaumont | 0:7cb648bc5c2a | 45 | resolution=AT30TSE_CONFIG_RES_12_bit; |
wbeaumont | 0:7cb648bc5c2a | 46 | } |
wbeaumont | 0:7cb648bc5c2a | 47 | |
wbeaumont | 0:7cb648bc5c2a | 48 | |
wbeaumont | 0:7cb648bc5c2a | 49 | |
wbeaumont | 0:7cb648bc5c2a | 50 | |
wbeaumont | 0:7cb648bc5c2a | 51 | float AT30TSE75x::get_temperature(int *error) { |
wbeaumont | 0:7cb648bc5c2a | 52 | |
wbeaumont | 0:7cb648bc5c2a | 53 | |
wbeaumont | 0:7cb648bc5c2a | 54 | float temperature; |
wbeaumont | 0:7cb648bc5c2a | 55 | uint16_t data; |
wbeaumont | 0:7cb648bc5c2a | 56 | int locerr=-200; |
wbeaumont | 0:7cb648bc5c2a | 57 | buffer[0] = 0; |
wbeaumont | 0:7cb648bc5c2a | 58 | buffer[1] = 0; |
wbeaumont | 0:7cb648bc5c2a | 59 | |
wbeaumont | 0:7cb648bc5c2a | 60 | locerr=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); |
wbeaumont | 0:7cb648bc5c2a | 61 | if ( error ) *error=locerr; // pointer !=0 |
wbeaumont | 0:7cb648bc5c2a | 62 | |
wbeaumont | 0:7cb648bc5c2a | 63 | data = (buffer[0] << 8) | buffer[1]; |
wbeaumont | 0:7cb648bc5c2a | 64 | int8_t sign = 1; |
wbeaumont | 0:7cb648bc5c2a | 65 | |
wbeaumont | 0:7cb648bc5c2a | 66 | /* Check if negative and clear sign bit. */ |
wbeaumont | 0:7cb648bc5c2a | 67 | if (data & (1 << 15)) { |
wbeaumont | 0:7cb648bc5c2a | 68 | sign *= -1; |
wbeaumont | 0:7cb648bc5c2a | 69 | data &= ~(1 << 15); |
wbeaumont | 0:7cb648bc5c2a | 70 | } |
wbeaumont | 0:7cb648bc5c2a | 71 | |
wbeaumont | 0:7cb648bc5c2a | 72 | /* Convert to temperature. */ |
wbeaumont | 0:7cb648bc5c2a | 73 | switch (resolution) { |
wbeaumont | 0:7cb648bc5c2a | 74 | case AT30TSE_CONFIG_RES_9_bit: |
wbeaumont | 0:7cb648bc5c2a | 75 | data = (data >> 7); |
wbeaumont | 0:7cb648bc5c2a | 76 | (temperature) = data * sign * 0.5; |
wbeaumont | 0:7cb648bc5c2a | 77 | break; |
wbeaumont | 0:7cb648bc5c2a | 78 | |
wbeaumont | 0:7cb648bc5c2a | 79 | case AT30TSE_CONFIG_RES_10_bit: |
wbeaumont | 0:7cb648bc5c2a | 80 | data = (data >> 6); |
wbeaumont | 0:7cb648bc5c2a | 81 | (temperature) = data * sign * 0.25; |
wbeaumont | 0:7cb648bc5c2a | 82 | break; |
wbeaumont | 0:7cb648bc5c2a | 83 | |
wbeaumont | 0:7cb648bc5c2a | 84 | case AT30TSE_CONFIG_RES_11_bit: |
wbeaumont | 0:7cb648bc5c2a | 85 | data = (data >> 5); |
wbeaumont | 0:7cb648bc5c2a | 86 | (temperature) = data * sign * 0.125; |
wbeaumont | 0:7cb648bc5c2a | 87 | break; |
wbeaumont | 0:7cb648bc5c2a | 88 | |
wbeaumont | 0:7cb648bc5c2a | 89 | case AT30TSE_CONFIG_RES_12_bit: |
wbeaumont | 0:7cb648bc5c2a | 90 | data = (data >> 4); |
wbeaumont | 0:7cb648bc5c2a | 91 | (temperature) = data * sign * 0.0625; |
wbeaumont | 0:7cb648bc5c2a | 92 | break; |
wbeaumont | 0:7cb648bc5c2a | 93 | |
wbeaumont | 0:7cb648bc5c2a | 94 | default: |
wbeaumont | 0:7cb648bc5c2a | 95 | break; |
wbeaumont | 0:7cb648bc5c2a | 96 | } |
wbeaumont | 0:7cb648bc5c2a | 97 | return temperature; |
wbeaumont | 0:7cb648bc5c2a | 98 | } |
wbeaumont | 0:7cb648bc5c2a | 99 | |
wbeaumont | 0:7cb648bc5c2a | 100 | |
wbeaumont | 0:7cb648bc5c2a | 101 | int AT30TSE75x::read_config( int Nonvolatille,int *error ) { |
wbeaumont | 0:7cb648bc5c2a | 102 | uint16_t data; |
wbeaumont | 0:7cb648bc5c2a | 103 | int locerr; |
wbeaumont | 0:7cb648bc5c2a | 104 | buffer[0]= ConfigReg; // set pointer to config reg nv |
wbeaumont | 0:7cb648bc5c2a | 105 | locerr=_i2c->write(Taddr,(char *)buffer,1,false); |
wbeaumont | 0:7cb648bc5c2a | 106 | locerr= locerr<<2; |
wbeaumont | 0:7cb648bc5c2a | 107 | locerr|=_i2c->read(Taddr,(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false); |
wbeaumont | 0:7cb648bc5c2a | 108 | locerr= locerr<<2; |
wbeaumont | 0:7cb648bc5c2a | 109 | data = (buffer[0] << 8) | buffer[1]; |
wbeaumont | 0:7cb648bc5c2a | 110 | buffer[0]= TReg; |
wbeaumont | 0:7cb648bc5c2a | 111 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); |
wbeaumont | 0:7cb648bc5c2a | 112 | if ( error ) *error=locerr; // pointer !=0 |
wbeaumont | 0:7cb648bc5c2a | 113 | return (int)data; |
wbeaumont | 0:7cb648bc5c2a | 114 | } |
wbeaumont | 0:7cb648bc5c2a | 115 | |
wbeaumont | 0:7cb648bc5c2a | 116 | |
wbeaumont | 0:7cb648bc5c2a | 117 | int AT30TSE75x::set_config( int Nonvolatille ) { |
wbeaumont | 0:7cb648bc5c2a | 118 | int locerr; |
wbeaumont | 0:7cb648bc5c2a | 119 | buffer[0]= ConfigReg; // set pointer to config reg nv |
wbeaumont | 0:7cb648bc5c2a | 120 | |
wbeaumont | 0:7cb648bc5c2a | 121 | |
wbeaumont | 0:7cb648bc5c2a | 122 | |
wbeaumont | 0:7cb648bc5c2a | 123 | /* for no all fixed values |
wbeaumont | 0:7cb648bc5c2a | 124 | 15 normal : 1 0b1xxx xxxx xxxx xxxx |
wbeaumont | 0:7cb648bc5c2a | 125 | 14:13 12 bit :11 0b111x xxxx xxxx xxxx |
wbeaumont | 0:7cb648bc5c2a | 126 | 12:11 1 fault:00 0b1110 0xxx xxxx xxxx |
wbeaumont | 0:7cb648bc5c2a | 127 | 10 0 act low 0b1110 00xx xxxx xxxx |
wbeaumont | 0:7cb648bc5c2a | 128 | 9 0 cmp mode 0b1110 000x xxxx xxxx |
wbeaumont | 0:7cb648bc5c2a | 129 | 8 0 temp act 0b1110 0000 xxxx xxxx |
wbeaumont | 0:7cb648bc5c2a | 130 | 7:1 dont care 0b1110 0000 0000 000x |
wbeaumont | 0:7cb648bc5c2a | 131 | 0 read only 0b1110 0000 0000 0000 |
wbeaumont | 0:7cb648bc5c2a | 132 | => 0xE0 |
wbeaumont | 0:7cb648bc5c2a | 133 | */ |
wbeaumont | 0:7cb648bc5c2a | 134 | buffer[1]=0xE0; buffer[2]=0x00; |
wbeaumont | 0:7cb648bc5c2a | 135 | locerr=_i2c->write(Taddr,(char *)buffer,3,false); |
wbeaumont | 0:7cb648bc5c2a | 136 | buffer[0]= TReg; |
wbeaumont | 0:7cb648bc5c2a | 137 | locerr= locerr<<2; |
wbeaumont | 0:7cb648bc5c2a | 138 | locerr|=_i2c->write(Taddr,(char *)buffer,1,false); // make sure temp read reg is active again |
wbeaumont | 0:7cb648bc5c2a | 139 | return locerr; |
wbeaumont | 0:7cb648bc5c2a | 140 | } |
wbeaumont | 0:7cb648bc5c2a | 141 | |
wbeaumont | 0:7cb648bc5c2a | 142 | |
wbeaumont | 0:7cb648bc5c2a | 143 | |
wbeaumont | 0:7cb648bc5c2a | 144 | |
wbeaumont | 0:7cb648bc5c2a | 145 | int AT30TSE75x::read_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) { |
wbeaumont | 0:7cb648bc5c2a | 146 | char buff[20]; |
wbeaumont | 0:7cb648bc5c2a | 147 | buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4); |
wbeaumont | 0:7cb648bc5c2a | 148 | |
wbeaumont | 0:7cb648bc5c2a | 149 | _i2c->write(Eaddr,buff,1,false); |
wbeaumont | 0:7cb648bc5c2a | 150 | return _i2c->read(Eaddr,data,length,false); |
wbeaumont | 0:7cb648bc5c2a | 151 | } |
wbeaumont | 0:7cb648bc5c2a | 152 | |
wbeaumont | 0:7cb648bc5c2a | 153 | int AT30TSE75x::write_eeprompage(char *data, uint8_t length, uint8_t word_addr, uint8_t page) |
wbeaumont | 0:7cb648bc5c2a | 154 | { |
wbeaumont | 0:7cb648bc5c2a | 155 | char buff[length+1]; |
wbeaumont | 0:7cb648bc5c2a | 156 | int buffsizep1=length+1;//buffersize+1 |
wbeaumont | 0:7cb648bc5c2a | 157 | buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4); |
wbeaumont | 0:7cb648bc5c2a | 158 | for(int i = 1; i < buffsizep1; i++) { |
wbeaumont | 0:7cb648bc5c2a | 159 | buff[i] = *(data++); |
wbeaumont | 0:7cb648bc5c2a | 160 | } |
wbeaumont | 0:7cb648bc5c2a | 161 | return _i2c->write(Eaddr,buff,(length + 1),false); |
wbeaumont | 0:7cb648bc5c2a | 162 | } |
wbeaumont | 0:7cb648bc5c2a | 163 | |
wbeaumont | 0:7cb648bc5c2a | 164 | |
wbeaumont | 0:7cb648bc5c2a | 165 | int AT30TSE75x::read_eeprombyte(char &data, uint8_t word_addr, uint8_t page){ |
wbeaumont | 0:7cb648bc5c2a | 166 | char rbuf[2]; |
wbeaumont | 0:7cb648bc5c2a | 167 | int i2cresult=read_eeprompage(rbuf,1,word_addr,page); |
wbeaumont | 0:7cb648bc5c2a | 168 | data=rbuf[0]; |
wbeaumont | 0:7cb648bc5c2a | 169 | return i2cresult; |
wbeaumont | 0:7cb648bc5c2a | 170 | } |
wbeaumont | 0:7cb648bc5c2a | 171 | |
wbeaumont | 0:7cb648bc5c2a | 172 | |
wbeaumont | 0:7cb648bc5c2a | 173 | int AT30TSE75x::write_eeprombyte(char data, uint8_t word_addr, uint8_t page){ |
wbeaumont | 0:7cb648bc5c2a | 174 | char wbuf[2]; wbuf[0]=data; |
wbeaumont | 0:7cb648bc5c2a | 175 | return write_eeprompage(wbuf,1,word_addr,page); |
wbeaumont | 0:7cb648bc5c2a | 176 | } |