void initUltimateTimer()
{
noInterrupts();
TCCR2A = (1 << WGM21); //CTC-Mode Activation
TCCR2B = (1 << CS21); //Prescaler 8
OCR2A = 250; //125 µs periodicity using 2 MHz counter clock
TIMSK2 = (1 << OCIE2A); //Compare Match A interrupt is enabled
interrupts();
}
ISR(TIMER2_COMPA_vect)
{
ADCSRA |= (1 << ADSC); //An AD-Conversion is started
}
void initADC()
{
noInterrupts();
ADCSRA = 0; // Setting the ADC Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
ADMUX = 0;
// Set voltage reference
// REFS1 REFS0 Voltage Reference Selection
// 0 0 AREF, Internal Vref turned off
// 0 1 AVCC with external capacitor at AREF pin
// 1 0 Reserved
// 1 1 Internal 1.1V Voltage Reference with external capacitor at AREF pin
ADMUX |= (1 << REFS0); // Enable ADC and ADC interrupt
ADCSRA |= (1 << ADEN) | (1 << ADIE); // Start first conversion which takes 25 cycles
ADCSRA |= (1 << ADSC); // Start conversion
while(!(ADCSRA & ADIF)); // Wait for the first conversion to complete
interrupts();
}
//Low Pass Filter ****************************
double lp_filter_value = 0.0;
double dT = 125e-6;
double Tau = 2; //Sekunden, Zeitkonstante Tau
double alpha = dT / Tau;
//*********************************************
double adc_input = 0;
double filtered_adc_input = 0;
ISR(ADC_vect)
{
adc_input = ADC;
filtered_adc_input = alpha * adc_input + (1 - alpha) * filtered_adc_input;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
initADC();
initUltimateTimer();
}
void loop()
{
delay(100);
//Serial.print(alpha);
Serial.print(adc_input); Serial.print(",");
Serial.println(filtered_adc_input);
}