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
#define AVERAGE_SAMPLES 8
void adc_init(void)
{
void adc_init(void) {
// TODO -> complete adc initialisation
//offsetCurrent = measure_current(0);
}
/**
* Read one ADC channel. This function is only
* local to this file.
* This function make the average on samples
*
* @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) {
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
*/
uint16_t measure_voltage() {
uint32_t sum = 0;
// Make an average
for(int i = 0; i < AVERAGE_SAMPLES; i++) {
sum += measure_adc(VOLTAGE_CHANNEL);
}
sum /= AVERAGE_SAMPLES;
uint32_t value = measure_adc(VOLTAGE_CHANNEL);
// Convert sum from bits to mV
sum = (sum * ADC_REFH) / ADC_RESOLUTION;
return (uint16_t)(sum);
value = (value * ADC_REFH) / ADC_RESOLUTION;
return (uint16_t)(value);
}
/**
*
* @param offset
* @return
*/
uint16_t measure_current(uint16_t offset) {
uint32_t sum = 0;
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;
}
uint32_t value = measure_adc(CURRENT_CHANNEL);
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
default.languagetoolchain.version=2.40
default.Pack.dfplocation=C\:\\Program Files\\Microchip\\MPLABX\\v6.00\\packs\\Microchip\\PIC18F-J_DFP\\1.5.44
conf.ids=default
default.languagetoolchain.dir=C\:\\Program Files\\Microchip\\xc8\\v2.40\\bin
host.id=3awj-afwq-rl
#Sat Mar 18 16:03:57 CET 2023
proj.dir=C\:\\Users\\remi\\MPLABXProjects\\Solar-Panel
host.id=aq84-7qg1-w
configurations-xml=4dc45b219db50423420a0eb2e1f688ad
default.com-microchip-mplab-mdbcore-snap-SnapToolImpl.md5=eaa336cefb7fc46db8b50b7b2b6e54ca
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
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=bcd6e5453a11ce86aaffd5305e838602
host.platform=windows
conf.ids=

View File

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

View File

@ -6,5 +6,5 @@
CND_BASEDIR=`pwd`
# default configuration
CND_ARTIFACT_DIR_default=dist/default/production
CND_ARTIFACT_NAME_default=solar_panel.production.hex
CND_ARTIFACT_PATH_default=dist/default/production/solar_panel.production.hex
CND_ARTIFACT_NAME_default=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"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<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>
</open-files>
</project-private>

View File

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