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