Levi Mariën / Mbed 2 deprecated STC3100

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STC3100Sensor.cpp Source File

STC3100Sensor.cpp

00001 /**
00002  * Driver for the STC3100
00003  */
00004 #include "mbed.h"
00005 #include "STC3100Sensor.h"
00006 
00007 /**
00008  * Configuring the STC3100
00009  * ------------------------
00010  * This is done for the user automatically. See datasheet STC3100 page 15 for the
00011  * configuration that is applied.
00012  */
00013 void stc3100Configure(void) {
00014 
00015     //The user need to provide the first address of the register, an increment
00016     //will be done automatically.
00017     char writeData[3];
00018     //REG_MODE address
00019     writeData[0] = 0x00;
00020     //Configuration REG_MODE
00021     writeData[1] = 0x1c;
00022     //Configuration REG_CTRL
00023     writeData[2] = 0x03;
00024 
00025     i2c.write(STC3100_ADRESS_WRITE, writeData, 3);
00026 }
00027 
00028 /**
00029  * Reading the whole memory block (that is been declared) and save it to the byteArray
00030  * of the ustc3100Data union.
00031  */
00032 void stc3100ReadChip(void) {
00033 
00034     i2c.read(STC3100_ADDRESS_READ, &(stc3100Data.byteArray[0]), 10);
00035 }
00036 
00037 /**
00038  * Calculating of the data, they will be stored in the stc3100ActualData struct.
00039  */
00040 void updateData(void) {
00041 
00042     unsigned int high_byte = 0;
00043     int value = 0;
00044 
00045     stc3100ReadChip();
00046 
00047     // Converting to voltage
00048     high_byte = (unsigned int) stc3100Data.VoltageHigh;
00049     high_byte <<= 8;
00050     value = (high_byte & 0xFF00) | stc3100Data.VoltageLow;
00051     stc3100ActualData.voltage = (float) value * 2.44;
00052 
00053     // Converting to current
00054     high_byte = (unsigned int) stc3100Data.CurrentHigh;
00055     high_byte <<= 8;
00056     value = (high_byte & 0xFF00) | stc3100Data.CurrentLow;
00057     value <<= 2;
00058     stc3100ActualData.current =
00059             ((((float) value * 11.77) / 10.0) / 4.0) > 0.0 ?
00060                     ((((float) value * 11.77) / 10.0) / 4.0) :
00061                     -((((float) value * 11.77) / 10.0) / 4.0);
00062     stc3100ActualData.current -= 54.0;
00063 
00064     // Converting to charge
00065     high_byte = (unsigned int) stc3100Data.ChargeHigh;
00066     high_byte <<= 8;
00067     value = (high_byte & 0xFF00) | stc3100Data.ChargeLow;
00068     stc3100ActualData.charge = ((float) value * 6.70) / 10.0;
00069 
00070     //  // Converting to temperature
00071     //  high_byte = (unsigned int) stc3100Data.TemperatureHigh;
00072     //  high_byte <<= 8;
00073     //  value = (high_byte & 0xFF00) | stc3100Data.TemperatureLow;
00074     //  value <<= 4;
00075     //  stc3100ActualData.temperature = ((float) value * 0.125) / 16.0;
00076 
00077 }
00078 
00079 /**
00080  * Method for returning the voltage
00081  */
00082 float getVoltage(void) {
00083     return stc3100ActualData.voltage;
00084 }
00085 
00086 /**
00087  * Method for returning the current
00088  */
00089 float getCurrent(void) {
00090     return stc3100ActualData.current;
00091 }
00092 
00093 /**
00094  * Method for returning the charge
00095  */
00096 float getCharge(void) {
00097     return stc3100ActualData.charge;
00098 }