Custom project for driving a STC3100 sensor

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
tommienator
Date:
Sun Oct 29 00:11:17 2017 +0000
Commit message:
Custom project

Changed in this revision

STC3100Sensor.cpp Show annotated file Show diff for this revision Revisions of this file
STC3100Sensor.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 014d4be7a437 STC3100Sensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STC3100Sensor.cpp	Sun Oct 29 00:11:17 2017 +0000
@@ -0,0 +1,98 @@
+/**
+ * Driver for the STC3100
+ */
+#include "mbed.h"
+#include "STC3100Sensor.h"
+
+/**
+ * Configuring the STC3100
+ * ------------------------
+ * This is done for the user automatically. See datasheet STC3100 page 15 for the
+ * configuration that is applied.
+ */
+void stc3100Configure(void) {
+
+    //The user need to provide the first address of the register, an increment
+    //will be done automatically.
+    char writeData[3];
+    //REG_MODE address
+    writeData[0] = 0x00;
+    //Configuration REG_MODE
+    writeData[1] = 0x1c;
+    //Configuration REG_CTRL
+    writeData[2] = 0x03;
+
+    i2c.write(STC3100_ADRESS_WRITE, writeData, 3);
+}
+
+/**
+ * Reading the whole memory block (that is been declared) and save it to the byteArray
+ * of the ustc3100Data union.
+ */
+void stc3100ReadChip(void) {
+
+    i2c.read(STC3100_ADDRESS_READ, &(stc3100Data.byteArray[0]), 10);
+}
+
+/**
+ * Calculating of the data, they will be stored in the stc3100ActualData struct.
+ */
+void updateData(void) {
+
+    unsigned int high_byte = 0;
+    int value = 0;
+
+    stc3100ReadChip();
+
+    // Converting to voltage
+    high_byte = (unsigned int) stc3100Data.VoltageHigh;
+    high_byte <<= 8;
+    value = (high_byte & 0xFF00) | stc3100Data.VoltageLow;
+    stc3100ActualData.voltage = (float) value * 2.44;
+
+    // Converting to current
+    high_byte = (unsigned int) stc3100Data.CurrentHigh;
+    high_byte <<= 8;
+    value = (high_byte & 0xFF00) | stc3100Data.CurrentLow;
+    value <<= 2;
+    stc3100ActualData.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;
+
+    // Converting to charge
+    high_byte = (unsigned int) stc3100Data.ChargeHigh;
+    high_byte <<= 8;
+    value = (high_byte & 0xFF00) | stc3100Data.ChargeLow;
+    stc3100ActualData.charge = ((float) value * 6.70) / 10.0;
+
+    //  // Converting to temperature
+    //  high_byte = (unsigned int) stc3100Data.TemperatureHigh;
+    //  high_byte <<= 8;
+    //  value = (high_byte & 0xFF00) | stc3100Data.TemperatureLow;
+    //  value <<= 4;
+    //  stc3100ActualData.temperature = ((float) value * 0.125) / 16.0;
+
+}
+
+/**
+ * Method for returning the voltage
+ */
+float getVoltage(void) {
+    return stc3100ActualData.voltage;
+}
+
+/**
+ * Method for returning the current
+ */
+float getCurrent(void) {
+    return stc3100ActualData.current;
+}
+
+/**
+ * Method for returning the charge
+ */
+float getCharge(void) {
+    return stc3100ActualData.charge;
+}
diff -r 000000000000 -r 014d4be7a437 STC3100Sensor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/STC3100Sensor.h	Sun Oct 29 00:11:17 2017 +0000
@@ -0,0 +1,96 @@
+#ifndef STC3100Sensor_H_
+#define STC3100Sensor_H_
+
+/**
+ * I2C Device address
+ * ------------------
+ * The mbed API uses 8 bit addresses, so make sure to thake the 7 bit address
+ * and left shift it by 1 before passing it.
+ * The eight bit presents a R/W bit, see datasheet STC3100 page 12.
+ */
+#define STC3100_ADDRESS_READ    0b11100000
+#define STC3100_ADRESS_WRITE    0b11100001
+
+/**
+ * INTERNAL REGISTER MAP
+ * ---------------------
+ * For this application the RAM registers are not used
+ */
+#define REG_MODE                0   // Mode register                            (R/W)
+#define REG_CTRL                1   // Control and status register              (R/W)
+#define REG_CHARGE_LOW          2   // Gas gauge charge data,       bits 0-7    (R)
+#define REG_CHARGE_HIGH         3   // Gas gauge charge data,       bits 8-15   (R)
+#define REG_COUNTER_LOW         4   // Number of conversions,       bits 0-7    (R)
+#define REG_COUNTER_HIGH        5   // Number of conversions,       bits 8-15   (R)
+#define REG_CURRENT_LOW         6   // Battery current value,       bits 0-7    (R)
+#define REG_CURRENT_HIGH        7   // Battery current value,       bits 8-15   (R)
+#define REG_VOLTAGE_LOW         8   // Battery voltage value,       bits 0-7    (R)
+#define REG_VOLTAGE_HIGH        9   // Battery voltage value,       bits 8-15   (R)
+//#define REG_TEMPERATURE_LOW     10    // Temperature value,           bits 0-7    (R)
+//#define REG_TEMPERATURE_HIGH    11    // Temperature value,           bits 8-15   (R)
+
+//The following registers are the Device ID registers (R)
+//#define REG_ID0                 24
+//#define REG_ID1                 25
+//#define REG_ID2                 26
+//#define REG_ID3                 27
+//#define REG_ID4                 28
+//#define REG_ID5                 29
+//#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;
+        unsigned char ChargeHigh;
+        unsigned char CounterLow;
+        unsigned char CounterHigh;
+        unsigned char CurrentLow;
+        unsigned char CurrentHigh;
+        unsigned char VoltageLow;
+        unsigned char VoltageHigh;
+//      unsigned char TemperatureLow;
+//      unsigned char TemperatureHigh;
+//      unsigned char Reserved[13];
+//      unsigned char ID0;
+//      unsigned char ID1;
+//      unsigned char ID2;
+//      unsigned char ID3;
+//      unsigned char ID4;
+//      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
+    float current;
+    // Current charge of the battery mAh
+    float charge;
+//  // Temperature in °C
+//  float temperature;
+};
+
+STC3100ActualData stc3100ActualData;
+STC3100Data stc3100Data;
+
+void stc3100Configure(void);
+void stc3100ReadChip(void);
+void updateData(void);
+float getVoltage(void);
+float getCurrent(void);
+float getCharge(void);
+//float getTemperature(void);
+
+#endif /* STC3100Sensor_H_ */
diff -r 000000000000 -r 014d4be7a437 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Oct 29 00:11:17 2017 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+
+int main() {
+    while(1) {
+        myled = 1; // LED is ON
+        wait(0.2); // 200 ms
+        myled = 0; // LED is OFF
+        wait(1.0); // 1 sec
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 014d4be7a437 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Oct 29 00:11:17 2017 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/fb8e0ae1cceb
\ No newline at end of file