New program with continuous sampling:
/*
Copyright 2024 Picuino
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
const int PIN_VDD_OPAMP = 6;
const int PIN_PWM_OUT = 3;
const int PIN_ANALOG = A0;
const int SAMPLES_PER_EDGE = 80;
const int EDGES_PER_MEASURE = 5000 / SAMPLES_PER_EDGE;
volatile long adc_sum;
volatile long adc_result;
volatile unsigned int adc_value;
volatile unsigned int adc_samples;
volatile unsigned char timer0_state;
volatile unsigned char output_edge;
volatile unsigned char adc_measure_end;
void setup() {
Serial.begin(9600);
// Power up instrumentation amplifier
pinMode(PIN_VDD_OPAMP, OUTPUT);
digitalWrite(PIN_VDD_OPAMP, HIGH);
// Set up output reference signal pin
pinMode(PIN_PWM_OUT, OUTPUT);
setup_timer0();
// Set up ADC
setup_adc();
// Main Loop
float resistance;
adc_sum = 0;
adc_samples = 0;
timer0_state = 0;
for (;;) {
if (adc_measure_end == 1) {
adc_measure_end = 0;
resistance = adc_result;
resistance = resistance * (1.0 / (SAMPLES_PER_EDGE * EDGES_PER_MEASURE));
Serial.println(resistance);
}
}
}
void loop() {}
void setup_adc(void) {
analogRead(PIN_ANALOG);
cli(); //stop interrupts
ADMUX = (1 << 6) | (0 << ADLAR);
ADCSRA = (1 << ADEN) | (0 << ADSC) | (0 << ADATE) | (0 << ADIE) | (0b111);
ADCSRB = 0x00;
sei(); //allow interrupts
}
void setup_timer0(void) {
cli(); //stop interrupts
//set timer0 interrupt at 2kHz
TCCR0A = 0; // set entire TCCR2A register to 0
TCCR0B = 0; // same for TCCR2B
TCNT0 = 0; //initialize counter value to 0
// set compare match register
OCR0A = 26; // f = 16000000 / ((OCR0A + 1) * 64) = 9259 Hz
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS01 and CS00 bits for 64 prescaler
TCCR0B |= (1 << CS01) | (1 << CS00);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
sei(); //allow interrupts
}
ISR(TIMER0_COMPA_vect) {
// ADC manage
adc_value = (ADCL | (ADCH << 8));
if (output_edge == 1) {
adc_sum -= adc_value;
}
if (output_edge == 0) {
adc_sum += adc_value;
}
ADCSRA |= (1 << ADSC); // ADC Start Conversion
adc_samples++;
delayMicroseconds(12); // Wait for Sample and Hold
// Update next state
if (++timer0_state >= (SAMPLES_PER_EDGE * 2)) {
timer0_state = 0;
}
// Update output reference signal
if (timer0_state < SAMPLES_PER_EDGE) {
PORTD |= (1 << PIN_PWM_OUT);
output_edge = 1;
}
else {
PORTD &= ~(1 << PIN_PWM_OUT);
output_edge = 0;
}
// Take one measure after several samples
if (adc_samples > SAMPLES_PER_EDGE * EDGES_PER_MEASURE * 2) {
adc_measure_end = 1;
adc_result = adc_sum;
adc_sum = 0;
adc_samples = 0;
}
}
Same error in the measurements:
112.91
113.61
112.85
112.79
112.99
113.02
112.92
112.65
112.78
112.89
113.14
112.81
112.90
112.59
112.80
112.54
112.44
112.39
112.34
112.19
112.12
111.70
111.89
111.71
112.10
111.82
111.44
111.78
111.46
111.78
112.02
112.03
111.91
112.08
112.20
111.95
112.28
112.41
112.53
112.73
112.37
112.36
112.54
112.75
112.59
112.38
112.94
113.09
112.98
112.76
113.23
112.77
113.00
112.72
112.96
113.23
112.65
112.76
112.94
112.90
113.11
113.01
112.76
112.87
112.77
112.61
112.98
112.77
112.94
113.08
112.88
113.07
113.01
113.13
112.89
112.93
112.65
113.11
113.20
112.97
113.52
113.30
113.30
113.43
113.32
113.28
113.13
113.18
113.41
113.34
113.38
113.09
113.43
113.25
113.19
112.89
113.00
113.11
113.27
113.19
113.12
113.22
113.20
112.81
113.31
112.92
113.22