mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 #include "AT30TSE75X.h"
elessair 0:f269e3021894 2
elessair 0:f269e3021894 3 AT30TSE75X::AT30TSE75X(PinName sda, PinName scl) : at30tse75x_i2c(sda, scl), resolution(AT30TSE_CONFIG_RES_9_bit) {
elessair 0:f269e3021894 4 at30tse75x_i2c.frequency(10000);
elessair 0:f269e3021894 5 }
elessair 0:f269e3021894 6 AT30TSE75X::~AT30TSE75X() { }
elessair 0:f269e3021894 7
elessair 0:f269e3021894 8 float AT30TSE75X::read_temp() {
elessair 0:f269e3021894 9 uint8_t bufferwrite[1];
elessair 0:f269e3021894 10 uint8_t buffer[2];
elessair 0:f269e3021894 11 float temperature;
elessair 0:f269e3021894 12 uint16_t data;
elessair 0:f269e3021894 13 buffer[0] = 0;
elessair 0:f269e3021894 14 buffer[1] = 0;
elessair 0:f269e3021894 15
elessair 0:f269e3021894 16 at30tse75x_i2c.read((AT30TSE_TEMPERATURE_TWI_ADDR << 1 ),(char *)buffer,AT30TSE_TEMPERATURE_REG_SIZE,false);
elessair 0:f269e3021894 17
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 data = (buffer[0] << 8) | buffer[1];
elessair 0:f269e3021894 20 int8_t sign = 1;
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 /* Check if negative and clear sign bit. */
elessair 0:f269e3021894 23 if (data & (1 << 15)) {
elessair 0:f269e3021894 24 sign *= -1;
elessair 0:f269e3021894 25 data &= ~(1 << 15);
elessair 0:f269e3021894 26 }
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 /* Convert to temperature. */
elessair 0:f269e3021894 29 switch (this->resolution) {
elessair 0:f269e3021894 30 case AT30TSE_CONFIG_RES_9_bit:
elessair 0:f269e3021894 31 data = (data >> 7);
elessair 0:f269e3021894 32 (temperature) = data * sign * 0.5;
elessair 0:f269e3021894 33 break;
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 case AT30TSE_CONFIG_RES_10_bit:
elessair 0:f269e3021894 36 data = (data >> 6);
elessair 0:f269e3021894 37 (temperature) = data * sign * 0.25;
elessair 0:f269e3021894 38 break;
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 case AT30TSE_CONFIG_RES_11_bit:
elessair 0:f269e3021894 41 data = (data >> 5);
elessair 0:f269e3021894 42 (temperature) = data * sign * 0.125;
elessair 0:f269e3021894 43 break;
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 case AT30TSE_CONFIG_RES_12_bit:
elessair 0:f269e3021894 46 data = (data >> 4);
elessair 0:f269e3021894 47 (temperature) = data * sign * 0.0625;
elessair 0:f269e3021894 48 break;
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 default:
elessair 0:f269e3021894 51 break;
elessair 0:f269e3021894 52 }
elessair 0:f269e3021894 53 return temperature;
elessair 0:f269e3021894 54 }
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 uint8_t AT30TSE75X:: read_eeprom(uint8_t *data, uint8_t length, uint8_t word_addr, uint8_t page) {
elessair 0:f269e3021894 57 uint8_t buff[20];
elessair 0:f269e3021894 58 buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4);
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60 at30tse75x_i2c.write((int)(AT30TSE_EEPROM_TWI_ADDR << 1),(char *)buff,1,false);
elessair 0:f269e3021894 61 return at30tse75x_i2c.read((int)(AT30TSE_EEPROM_TWI_ADDR << 1),(char *)data,length,false);
elessair 0:f269e3021894 62 }
elessair 0:f269e3021894 63
elessair 0:f269e3021894 64 uint8_t AT30TSE75X:: write_eeprom(uint8_t *data, uint8_t length, uint8_t word_addr, uint8_t page)
elessair 0:f269e3021894 65 {
elessair 0:f269e3021894 66 uint8_t buff[length+1];;
elessair 0:f269e3021894 67 buff[0] = (word_addr & 0x0F) | ((0x0F & page) << 4);
elessair 0:f269e3021894 68 for(int i = 0; i < length; i++)
elessair 0:f269e3021894 69 {
elessair 0:f269e3021894 70 buff[i+1] = *data++;
elessair 0:f269e3021894 71 }
elessair 0:f269e3021894 72 return at30tse75x_i2c.write((int)(AT30TSE_EEPROM_TWI_ADDR << 1),(char *)buff,(length + 1),false);
elessair 0:f269e3021894 73 }