Levi Mariën
/
STC3100ppp
Custom project for driving a STC3100 sensor
Fork of STC3100 by
Revision 1:dbc1e56be2cc, committed 2017-11-04
- Comitter:
- tommienator
- Date:
- Sat Nov 04 13:07:17 2017 +0000
- Parent:
- 0:014d4be7a437
- Commit message:
- stc3100
Changed in this revision
--- a/STC3100Sensor.cpp Sun Oct 29 00:11:17 2017 +0000 +++ b/STC3100Sensor.cpp Sat Nov 04 13:07:17 2017 +0000 @@ -4,6 +4,8 @@ #include "mbed.h" #include "STC3100Sensor.h" +I2C i2c(D14, D15); + /** * Configuring the STC3100 * ------------------------ @@ -31,7 +33,7 @@ */ void stc3100ReadChip(void) { - i2c.read(STC3100_ADDRESS_READ, &(stc3100Data.byteArray[0]), 10); + i2c.read(STC3100_ADDRESS_READ, &(byteArray[0]), 10); } /** @@ -45,27 +47,27 @@ stc3100ReadChip(); // Converting to voltage - high_byte = (unsigned int) stc3100Data.VoltageHigh; + high_byte = (unsigned int) VoltageHigh; high_byte <<= 8; - value = (high_byte & 0xFF00) | stc3100Data.VoltageLow; - stc3100ActualData.voltage = (float) value * 2.44; + value = (high_byte & 0xFF00) | VoltageLow; + voltage = (float) value * 2.44; // Converting to current - high_byte = (unsigned int) stc3100Data.CurrentHigh; + high_byte = (unsigned int) CurrentHigh; high_byte <<= 8; - value = (high_byte & 0xFF00) | stc3100Data.CurrentLow; + value = (high_byte & 0xFF00) | CurrentLow; value <<= 2; - stc3100ActualData.current = + current = ((((float) value * 11.77) / 10.0) / 4.0) > 0.0 ? ((((float) value * 11.77) / 10.0) / 4.0) : -((((float) value * 11.77) / 10.0) / 4.0); - stc3100ActualData.current -= 54.0; + current -= 54.0; // Converting to charge - high_byte = (unsigned int) stc3100Data.ChargeHigh; + high_byte = (unsigned int) ChargeHigh; high_byte <<= 8; - value = (high_byte & 0xFF00) | stc3100Data.ChargeLow; - stc3100ActualData.charge = ((float) value * 6.70) / 10.0; + value = (high_byte & 0xFF00) | ChargeLow; + charge = ((float) value * 6.70) / 10.0; // // Converting to temperature // high_byte = (unsigned int) stc3100Data.TemperatureHigh; @@ -80,19 +82,19 @@ * Method for returning the voltage */ float getVoltage(void) { - return stc3100ActualData.voltage; + return voltage; } /** * Method for returning the current */ float getCurrent(void) { - return stc3100ActualData.current; + return current; } /** * Method for returning the charge */ float getCharge(void) { - return stc3100ActualData.charge; + return charge; }
--- a/STC3100Sensor.h Sun Oct 29 00:11:17 2017 +0000 +++ b/STC3100Sensor.h Sat Nov 04 13:07:17 2017 +0000 @@ -39,12 +39,6 @@ //#define REG_ID6 30 //#define REG_ID7 31 -typedef union STC3100Data { - - // Reading of the chip will be stored in the byteArray - unsigned char byteArray[10]; - - struct { unsigned char Mode; unsigned char ControlStatus; unsigned char ChargeLow; @@ -66,12 +60,10 @@ // unsigned char ID5; // unsigned char ID6; // unsigned char ID7; - }; -}; + /** * Struct that stores the updated values. */ -typedef struct STC3100ActualData { // Voltage in mV float voltage; // Current in mA @@ -80,10 +72,11 @@ float charge; // // Temperature in °C // float temperature; -}; -STC3100ActualData stc3100ActualData; -STC3100Data stc3100Data; +//STC3100ActualData stc3100ActualData; +//STC3100Data stc3100Data; +// Reading of the chip will be stored in the byteArray +char byteArray[10]; void stc3100Configure(void); void stc3100ReadChip(void);
--- a/main.cpp Sun Oct 29 00:11:17 2017 +0000 +++ b/main.cpp Sat Nov 04 13:07:17 2017 +0000 @@ -1,12 +1,13 @@ #include "mbed.h" - -DigitalOut myled(LED1); +#include "STC3100Sensor.h" int main() { + + stc3100Configure(); + while(1) { - myled = 1; // LED is ON - wait(0.2); // 200 ms - myled = 0; // LED is OFF - wait(1.0); // 1 sec + + printf("%f\n", getVoltage()); + } } \ No newline at end of file