Custom project for driving a STC3100 sensor

Dependencies:   mbed

Fork of STC3100 by Levi Mariën

Committer:
tommienator
Date:
Sat Nov 04 13:07:17 2017 +0000
Revision:
1:dbc1e56be2cc
Parent:
0:014d4be7a437
stc3100

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tommienator 0:014d4be7a437 1 /**
tommienator 0:014d4be7a437 2 * Driver for the STC3100
tommienator 0:014d4be7a437 3 */
tommienator 0:014d4be7a437 4 #include "mbed.h"
tommienator 0:014d4be7a437 5 #include "STC3100Sensor.h"
tommienator 0:014d4be7a437 6
tommienator 1:dbc1e56be2cc 7 I2C i2c(D14, D15);
tommienator 1:dbc1e56be2cc 8
tommienator 0:014d4be7a437 9 /**
tommienator 0:014d4be7a437 10 * Configuring the STC3100
tommienator 0:014d4be7a437 11 * ------------------------
tommienator 0:014d4be7a437 12 * This is done for the user automatically. See datasheet STC3100 page 15 for the
tommienator 0:014d4be7a437 13 * configuration that is applied.
tommienator 0:014d4be7a437 14 */
tommienator 0:014d4be7a437 15 void stc3100Configure(void) {
tommienator 0:014d4be7a437 16
tommienator 0:014d4be7a437 17 //The user need to provide the first address of the register, an increment
tommienator 0:014d4be7a437 18 //will be done automatically.
tommienator 0:014d4be7a437 19 char writeData[3];
tommienator 0:014d4be7a437 20 //REG_MODE address
tommienator 0:014d4be7a437 21 writeData[0] = 0x00;
tommienator 0:014d4be7a437 22 //Configuration REG_MODE
tommienator 0:014d4be7a437 23 writeData[1] = 0x1c;
tommienator 0:014d4be7a437 24 //Configuration REG_CTRL
tommienator 0:014d4be7a437 25 writeData[2] = 0x03;
tommienator 0:014d4be7a437 26
tommienator 0:014d4be7a437 27 i2c.write(STC3100_ADRESS_WRITE, writeData, 3);
tommienator 0:014d4be7a437 28 }
tommienator 0:014d4be7a437 29
tommienator 0:014d4be7a437 30 /**
tommienator 0:014d4be7a437 31 * Reading the whole memory block (that is been declared) and save it to the byteArray
tommienator 0:014d4be7a437 32 * of the ustc3100Data union.
tommienator 0:014d4be7a437 33 */
tommienator 0:014d4be7a437 34 void stc3100ReadChip(void) {
tommienator 0:014d4be7a437 35
tommienator 1:dbc1e56be2cc 36 i2c.read(STC3100_ADDRESS_READ, &(byteArray[0]), 10);
tommienator 0:014d4be7a437 37 }
tommienator 0:014d4be7a437 38
tommienator 0:014d4be7a437 39 /**
tommienator 0:014d4be7a437 40 * Calculating of the data, they will be stored in the stc3100ActualData struct.
tommienator 0:014d4be7a437 41 */
tommienator 0:014d4be7a437 42 void updateData(void) {
tommienator 0:014d4be7a437 43
tommienator 0:014d4be7a437 44 unsigned int high_byte = 0;
tommienator 0:014d4be7a437 45 int value = 0;
tommienator 0:014d4be7a437 46
tommienator 0:014d4be7a437 47 stc3100ReadChip();
tommienator 0:014d4be7a437 48
tommienator 0:014d4be7a437 49 // Converting to voltage
tommienator 1:dbc1e56be2cc 50 high_byte = (unsigned int) VoltageHigh;
tommienator 0:014d4be7a437 51 high_byte <<= 8;
tommienator 1:dbc1e56be2cc 52 value = (high_byte & 0xFF00) | VoltageLow;
tommienator 1:dbc1e56be2cc 53 voltage = (float) value * 2.44;
tommienator 0:014d4be7a437 54
tommienator 0:014d4be7a437 55 // Converting to current
tommienator 1:dbc1e56be2cc 56 high_byte = (unsigned int) CurrentHigh;
tommienator 0:014d4be7a437 57 high_byte <<= 8;
tommienator 1:dbc1e56be2cc 58 value = (high_byte & 0xFF00) | CurrentLow;
tommienator 0:014d4be7a437 59 value <<= 2;
tommienator 1:dbc1e56be2cc 60 current =
tommienator 0:014d4be7a437 61 ((((float) value * 11.77) / 10.0) / 4.0) > 0.0 ?
tommienator 0:014d4be7a437 62 ((((float) value * 11.77) / 10.0) / 4.0) :
tommienator 0:014d4be7a437 63 -((((float) value * 11.77) / 10.0) / 4.0);
tommienator 1:dbc1e56be2cc 64 current -= 54.0;
tommienator 0:014d4be7a437 65
tommienator 0:014d4be7a437 66 // Converting to charge
tommienator 1:dbc1e56be2cc 67 high_byte = (unsigned int) ChargeHigh;
tommienator 0:014d4be7a437 68 high_byte <<= 8;
tommienator 1:dbc1e56be2cc 69 value = (high_byte & 0xFF00) | ChargeLow;
tommienator 1:dbc1e56be2cc 70 charge = ((float) value * 6.70) / 10.0;
tommienator 0:014d4be7a437 71
tommienator 0:014d4be7a437 72 // // Converting to temperature
tommienator 0:014d4be7a437 73 // high_byte = (unsigned int) stc3100Data.TemperatureHigh;
tommienator 0:014d4be7a437 74 // high_byte <<= 8;
tommienator 0:014d4be7a437 75 // value = (high_byte & 0xFF00) | stc3100Data.TemperatureLow;
tommienator 0:014d4be7a437 76 // value <<= 4;
tommienator 0:014d4be7a437 77 // stc3100ActualData.temperature = ((float) value * 0.125) / 16.0;
tommienator 0:014d4be7a437 78
tommienator 0:014d4be7a437 79 }
tommienator 0:014d4be7a437 80
tommienator 0:014d4be7a437 81 /**
tommienator 0:014d4be7a437 82 * Method for returning the voltage
tommienator 0:014d4be7a437 83 */
tommienator 0:014d4be7a437 84 float getVoltage(void) {
tommienator 1:dbc1e56be2cc 85 return voltage;
tommienator 0:014d4be7a437 86 }
tommienator 0:014d4be7a437 87
tommienator 0:014d4be7a437 88 /**
tommienator 0:014d4be7a437 89 * Method for returning the current
tommienator 0:014d4be7a437 90 */
tommienator 0:014d4be7a437 91 float getCurrent(void) {
tommienator 1:dbc1e56be2cc 92 return current;
tommienator 0:014d4be7a437 93 }
tommienator 0:014d4be7a437 94
tommienator 0:014d4be7a437 95 /**
tommienator 0:014d4be7a437 96 * Method for returning the charge
tommienator 0:014d4be7a437 97 */
tommienator 0:014d4be7a437 98 float getCharge(void) {
tommienator 1:dbc1e56be2cc 99 return charge;
tommienator 0:014d4be7a437 100 }