Solar panel
Loading...
Searching...
No Matches
measure.c
Go to the documentation of this file.
1#include <xc.h>
2
3#include "measure.h"
5
6#define VOLTAGE_CHANNEL 0x5
7#define CURRENT_CHANNEL 0x6
8#define ADC_RESOLUTION (1024 - 1)
9#define ADC_REFH 3300
10#define GAIN 66
11#define RESISTOR 3
12
13// Number of samples to do the averaging during measures
14#define AVERAGE_SAMPLES 8
15
16void adc_init(void)
17{
18 // TODO -> complete adc initialisation
19 //offsetCurrent = measure_current(0);
20
21}
22
30static uint16_t measure_adc(uint8_t channel)
31{
32 return (uint16_t) (ADC_GetConversion(channel));
33}
34
35
36
38{
39 uint32_t sum = 0;
40
41 for(int i = 0; i < AVERAGE_SAMPLES; i++) {
42 sum += measure_adc(VOLTAGE_CHANNEL);
43 }
44 sum /= AVERAGE_SAMPLES;
45 sum = (sum * ADC_REFH) / ADC_RESOLUTION;
46 return (uint16_t)(sum);
47}
48
49uint16_t measure_current(uint16_t offset)
50{
51 uint32_t sum = 0;
52 for(int i = 0; i< AVERAGE_SAMPLES; i++){
53 sum += measure_adc(CURRENT_CHANNEL);
54 }
55 uint32_t m = (sum / AVERAGE_SAMPLES); // m is bits
56 m = (m * ADC_REFH) / ADC_RESOLUTION; // m is mV
57 m *= 1000; // m is uV
58 m /= GAIN;
59 m /= RESISTOR; // m is uA
60 if(m <= offset){
61 m = 0;
62 } else {
63 m -= offset;
64 }
65
66 return (uint16_t)m;
67}
adc_result_t ADC_GetConversion(adc_channel_t channel)
Definition: adc.c:109
#define ADC_RESOLUTION
Definition: measure.c:8
#define RESISTOR
Definition: measure.c:11
#define AVERAGE_SAMPLES
Definition: measure.c:14
#define ADC_REFH
Definition: measure.c:9
uint16_t measure_voltage()
Definition: measure.c:37
void adc_init(void)
Definition: measure.c:16
#define VOLTAGE_CHANNEL
Definition: measure.c:6
#define CURRENT_CHANNEL
Definition: measure.c:7
#define GAIN
Definition: measure.c:10
uint16_t measure_current(uint16_t offset)
Definition: measure.c:49
Measuring current and voltage from the solar panel.