diff --git a/measure.c b/measure.c index b7b502f..350fe2b 100644 --- a/measure.c +++ b/measure.c @@ -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; } \ No newline at end of file diff --git a/nbproject/Makefile-genesis.properties b/nbproject/Makefile-genesis.properties index e0e3ccb..06cdaf9 100644 --- a/nbproject/Makefile-genesis.properties +++ b/nbproject/Makefile-genesis.properties @@ -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= diff --git a/nbproject/Makefile-impl.mk b/nbproject/Makefile-impl.mk index a5b9960..93da44b 100644 --- a/nbproject/Makefile-impl.mk +++ b/nbproject/Makefile-impl.mk @@ -24,7 +24,7 @@ CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}} # Project Name -PROJECTNAME=solar_panel +PROJECTNAME=Solar-Panel # Active Configuration DEFAULTCONF=default diff --git a/nbproject/Makefile-variables.mk b/nbproject/Makefile-variables.mk index 3b32a6c..dc7da78 100644 --- a/nbproject/Makefile-variables.mk +++ b/nbproject/Makefile-variables.mk @@ -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 diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 2511ffb..ae3b013 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -3,8 +3,7 @@ - file:/C:/Users/remi/MPLABXProjects/solar_panel/measure.h - file:/C:/Users/remi/MPLABXProjects/solar_panel/measure.c + file:/C:/Users/remi/MPLABXProjects/Solar-Panel/measure.c diff --git a/solar_panel.mc3 b/solar_panel.mc3 index c17b2d9..0ff43f9 100644 --- a/solar_panel.mc3 +++ b/solar_panel.mc3 @@ -352,7 +352,7 @@ - 128 + 0 @@ -396,7 +396,7 @@ - right + left