Creating a project for TT_Mxx

Committer:
ThunderSoft
Date:
Fri Apr 26 09:48:41 2019 +0000
Revision:
2:2186c624272f
Parent:
0:1159c687a20f
"Update the mbed-os code to support TT_M4G9"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThunderSoft 0:1159c687a20f 1 #include "HTU21D.h"
ThunderSoft 0:1159c687a20f 2
ThunderSoft 0:1159c687a20f 3
ThunderSoft 0:1159c687a20f 4
ThunderSoft 0:1159c687a20f 5
ThunderSoft 0:1159c687a20f 6
ThunderSoft 0:1159c687a20f 7 #if defined(__TT_M4G9__) | defined(__TT_M3HQ__)
ThunderSoft 0:1159c687a20f 8 #define DEFAULT_SDA_PIN I2C_SDA
ThunderSoft 0:1159c687a20f 9 #define DEFAULT_SCL_PIN I2C_SCL
ThunderSoft 0:1159c687a20f 10 #endif
ThunderSoft 0:1159c687a20f 11
ThunderSoft 0:1159c687a20f 12
ThunderSoft 0:1159c687a20f 13
ThunderSoft 0:1159c687a20f 14 HTU21D::HTU21D()
ThunderSoft 0:1159c687a20f 15 {
ThunderSoft 0:1159c687a20f 16 i2c_ = new I2C(DEFAULT_SDA_PIN, DEFAULT_SCL_PIN);
ThunderSoft 0:1159c687a20f 17 i2c_->frequency(400000);
ThunderSoft 0:1159c687a20f 18 }
ThunderSoft 0:1159c687a20f 19
ThunderSoft 0:1159c687a20f 20
ThunderSoft 0:1159c687a20f 21 HTU21D::HTU21D(PinName sda, PinName scl) {
ThunderSoft 0:1159c687a20f 22
ThunderSoft 0:1159c687a20f 23 i2c_ = new I2C(sda, scl);
ThunderSoft 0:1159c687a20f 24 //400KHz, as specified by the datasheet.
ThunderSoft 0:1159c687a20f 25 i2c_->frequency(400000);
ThunderSoft 0:1159c687a20f 26
ThunderSoft 0:1159c687a20f 27
ThunderSoft 0:1159c687a20f 28
ThunderSoft 0:1159c687a20f 29 }
ThunderSoft 0:1159c687a20f 30
ThunderSoft 0:1159c687a20f 31 int HTU21D::sample_ctemp(void) {
ThunderSoft 0:1159c687a20f 32
ThunderSoft 0:1159c687a20f 33 char tx[1];
ThunderSoft 0:1159c687a20f 34 char rx[2];
ThunderSoft 0:1159c687a20f 35 int status ;
ThunderSoft 0:1159c687a20f 36 tx[0] = TRIGGER_TEMP_MEASURE; // Triggers a temperature measure by feeding correct opcode.
ThunderSoft 0:1159c687a20f 37 i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
ThunderSoft 0:1159c687a20f 38 wait_ms(50); // Per datasheet, wait long enough for device to sample temperature
ThunderSoft 0:1159c687a20f 39
ThunderSoft 0:1159c687a20f 40 // Reads triggered measure
ThunderSoft 0:1159c687a20f 41 i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
ThunderSoft 0:1159c687a20f 42 wait_ms(1);
ThunderSoft 0:1159c687a20f 43
ThunderSoft 0:1159c687a20f 44 // Algorithm from datasheet to compute temperature.
ThunderSoft 0:1159c687a20f 45 unsigned int rawTemperature = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
ThunderSoft 0:1159c687a20f 46 rawTemperature &= 0xFFFC;
ThunderSoft 0:1159c687a20f 47
ThunderSoft 0:1159c687a20f 48 float tempTemperature = rawTemperature / (float)65536; //2^16 = 65536
ThunderSoft 0:1159c687a20f 49 float realTemperature = -46.85 + (175.72 * tempTemperature); //From page 14
ThunderSoft 0:1159c687a20f 50
ThunderSoft 0:1159c687a20f 51 return (int)realTemperature;
ThunderSoft 0:1159c687a20f 52
ThunderSoft 0:1159c687a20f 53 }
ThunderSoft 0:1159c687a20f 54
ThunderSoft 0:1159c687a20f 55 int HTU21D::sample_ftemp(void){
ThunderSoft 0:1159c687a20f 56 int temptemp = sample_ctemp();
ThunderSoft 0:1159c687a20f 57 int ftemp = temptemp * 1.8 + 32;
ThunderSoft 0:1159c687a20f 58
ThunderSoft 0:1159c687a20f 59 return ftemp;
ThunderSoft 0:1159c687a20f 60 }
ThunderSoft 0:1159c687a20f 61
ThunderSoft 0:1159c687a20f 62 int HTU21D::sample_ktemp(void){
ThunderSoft 0:1159c687a20f 63 int temptemp = sample_ctemp();
ThunderSoft 0:1159c687a20f 64 int ktemp = temptemp + 274;
ThunderSoft 0:1159c687a20f 65
ThunderSoft 0:1159c687a20f 66 return ktemp;
ThunderSoft 0:1159c687a20f 67
ThunderSoft 0:1159c687a20f 68
ThunderSoft 0:1159c687a20f 69 }
ThunderSoft 0:1159c687a20f 70
ThunderSoft 0:1159c687a20f 71 int HTU21D::sample_humid(void) {
ThunderSoft 0:1159c687a20f 72
ThunderSoft 0:1159c687a20f 73 char tx[1];
ThunderSoft 0:1159c687a20f 74 char rx[2];
ThunderSoft 0:1159c687a20f 75
ThunderSoft 0:1159c687a20f 76
ThunderSoft 0:1159c687a20f 77 tx[0] = TRIGGER_HUMD_MEASURE; // Triggers a humidity measure by feeding correct opcode.
ThunderSoft 0:1159c687a20f 78 i2c_->write((HTU21D_I2C_ADDRESS << 1) & 0xFE, tx, 1);
ThunderSoft 0:1159c687a20f 79 wait_ms(16); // Per datasheet, wait long enough for device to sample humidity
ThunderSoft 0:1159c687a20f 80
ThunderSoft 0:1159c687a20f 81 // Reads triggered measure
ThunderSoft 0:1159c687a20f 82 i2c_->read((HTU21D_I2C_ADDRESS << 1) | 0x01, rx, 2);
ThunderSoft 0:1159c687a20f 83 wait_ms(1);
ThunderSoft 0:1159c687a20f 84
ThunderSoft 0:1159c687a20f 85 //Algorithm from datasheet.
ThunderSoft 0:1159c687a20f 86 unsigned int rawHumidity = ((unsigned int) rx[0] << 8) | (unsigned int) rx[1];
ThunderSoft 0:1159c687a20f 87
ThunderSoft 0:1159c687a20f 88 rawHumidity &= 0xFFFC; //Zero out the status bits but keep them in place
ThunderSoft 0:1159c687a20f 89
ThunderSoft 0:1159c687a20f 90 //Given the raw humidity data, calculate the actual relative humidity
ThunderSoft 0:1159c687a20f 91 float tempRH = rawHumidity / (float)65536; //2^16 = 65536
ThunderSoft 0:1159c687a20f 92 float rh = -6 + (125 * tempRH); //From page 14
ThunderSoft 0:1159c687a20f 93
ThunderSoft 0:1159c687a20f 94
ThunderSoft 0:1159c687a20f 95 return (int)rh;
ThunderSoft 0:1159c687a20f 96
ThunderSoft 0:1159c687a20f 97 }