documentation about measure

This commit is contained in:
Rémi Heredero 2023-03-18 16:05:24 +01:00
parent 2773896fb5
commit 86e8ae4228
6 changed files with 41 additions and 48 deletions

View File

@ -13,58 +13,58 @@
// Number of samples to do the averaging during measures // Number of samples to do the averaging during measures
#define AVERAGE_SAMPLES 8 #define AVERAGE_SAMPLES 8
void adc_init(void) void adc_init(void) {
{
// TODO -> complete adc initialisation // TODO -> complete adc initialisation
//offsetCurrent = measure_current(0); //offsetCurrent = measure_current(0);
} }
/** /**
* Read one ADC channel. This function is only * Read one ADC channel. This function is only
* local to this file. * local to this file.
* This function make the average on samples
* *
* @param channel : the channel to be measured * @param channel : the channel to be measured
* @return the ADC read value * @return the ADC read value with an average
*/ */
static uint16_t measure_adc(uint8_t channel) { static uint16_t measure_adc(uint8_t channel) {
return (uint16_t) (ADC_GetConversion(channel)); uint32_t value = 0;
// Make an average
for(int i = 0; i < AVERAGE_SAMPLES; i++) {
value += (uint16_t) (ADC_GetConversion(channel));
}
value /= AVERAGE_SAMPLES;
return (uint16_t) (value);
} }
/** /**
* * Measure voltage
* @return * @return
*/ */
uint16_t measure_voltage() { uint16_t measure_voltage() {
uint32_t sum = 0; uint32_t value = measure_adc(VOLTAGE_CHANNEL);
// Make an average
for(int i = 0; i < AVERAGE_SAMPLES; i++) {
sum += measure_adc(VOLTAGE_CHANNEL);
}
sum /= AVERAGE_SAMPLES;
// Convert sum from bits to mV // Convert sum from bits to mV
sum = (sum * ADC_REFH) / ADC_RESOLUTION; value = (value * ADC_REFH) / ADC_RESOLUTION;
return (uint16_t)(sum); return (uint16_t)(value);
} }
/**
*
* @param offset
* @return
*/
uint16_t measure_current(uint16_t offset) { uint16_t measure_current(uint16_t offset) {
uint32_t sum = 0; uint32_t value = measure_adc(CURRENT_CHANNEL);
for(int i = 0; i< AVERAGE_SAMPLES; i++){
sum += measure_adc(CURRENT_CHANNEL);
}
uint32_t m = (sum / AVERAGE_SAMPLES); // m is bits
m = (m * ADC_REFH) / ADC_RESOLUTION; // m is mV
m *= 1000; // m is uV
m /= GAIN;
m /= RESISTOR; // m is uA
if(m <= offset){
m = 0;
} else {
m -= offset;
}
return (uint16_t)m; // Convert from bits to uA
value = (value * ADC_REFH) / ADC_RESOLUTION; // [mV]
value *= 1000; // [uV]
value /= GAIN; // [uV]
value /= RESISTOR; // [uA]
// Return value without offset or null if it's too low
if(value > offset) return (uint16_t)(value-offset);
return 0;
} }

View File

@ -1,14 +1,8 @@
# #
#Tue Mar 14 13:52:25 CET 2023 #Sat Mar 18 16:03:57 CET 2023
default.languagetoolchain.version=2.40 proj.dir=C\:\\Users\\remi\\MPLABXProjects\\Solar-Panel
default.Pack.dfplocation=C\:\\Program Files\\Microchip\\MPLABX\\v6.00\\packs\\Microchip\\PIC18F-J_DFP\\1.5.44 host.id=aq84-7qg1-w
conf.ids=default
default.languagetoolchain.dir=C\:\\Program Files\\Microchip\\xc8\\v2.40\\bin
host.id=3awj-afwq-rl
configurations-xml=4dc45b219db50423420a0eb2e1f688ad configurations-xml=4dc45b219db50423420a0eb2e1f688ad
default.com-microchip-mplab-mdbcore-snap-SnapToolImpl.md5=eaa336cefb7fc46db8b50b7b2b6e54ca com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=bcd6e5453a11ce86aaffd5305e838602
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=6e02ca5e9f5042ffd365b42ab82d3a9b
user-defined-mime-resolver-xml=none
default.com-microchip-mplab-nbide-toolchain-xc8-XC8LanguageToolchain.md5=ab1e0737b447a24f7366e9fd8fe5a2f0
proj.dir=C\:\\Users\\remi\\MPLABXProjects\\solar_panel
host.platform=windows host.platform=windows
conf.ids=

View File

@ -24,7 +24,7 @@ CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}}
# Project Name # Project Name
PROJECTNAME=solar_panel PROJECTNAME=Solar-Panel
# Active Configuration # Active Configuration
DEFAULTCONF=default DEFAULTCONF=default

View File

@ -6,5 +6,5 @@
CND_BASEDIR=`pwd` CND_BASEDIR=`pwd`
# default configuration # default configuration
CND_ARTIFACT_DIR_default=dist/default/production CND_ARTIFACT_DIR_default=dist/default/production
CND_ARTIFACT_NAME_default=solar_panel.production.hex CND_ARTIFACT_NAME_default=Solar-Panel.production.hex
CND_ARTIFACT_PATH_default=dist/default/production/solar_panel.production.hex CND_ARTIFACT_PATH_default=dist/default/production/Solar-Panel.production.hex

View File

@ -3,8 +3,7 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/> <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group> <group>
<file>file:/C:/Users/remi/MPLABXProjects/solar_panel/measure.h</file> <file>file:/C:/Users/remi/MPLABXProjects/Solar-Panel/measure.c</file>
<file>file:/C:/Users/remi/MPLABXProjects/solar_panel/measure.c</file>
</group> </group>
</open-files> </open-files>
</project-private> </project-private>

View File

@ -352,7 +352,7 @@
</entry> </entry>
<entry> <entry>
<key class="com.microchip.mcc.core.tokenManager.RegisterKey" moduleName="ADC" registerAlias="ADCON2"/> <key class="com.microchip.mcc.core.tokenManager.RegisterKey" moduleName="ADC" registerAlias="ADCON2"/>
<value>128</value> <value>0</value>
</entry> </entry>
<entry> <entry>
<key class="com.microchip.mcc.core.tokenManager.RegisterKey" moduleName="ADC" registerAlias="ADRESH"/> <key class="com.microchip.mcc.core.tokenManager.RegisterKey" moduleName="ADC" registerAlias="ADRESH"/>
@ -396,7 +396,7 @@
</entry> </entry>
<entry> <entry>
<key class="com.microchip.mcc.core.tokenManager.SettingKey" moduleName="ADC" registerAlias="ADCON2" settingAlias="ADFM"/> <key class="com.microchip.mcc.core.tokenManager.SettingKey" moduleName="ADC" registerAlias="ADCON2" settingAlias="ADFM"/>
<value>right</value> <value>left</value>
</entry> </entry>
<entry> <entry>
<key class="com.microchip.mcc.core.tokenManager.SettingKey" moduleName="ADC" registerAlias="ADI" settingAlias="enable"/> <key class="com.microchip.mcc.core.tokenManager.SettingKey" moduleName="ADC" registerAlias="ADI" settingAlias="enable"/>