ADC Sampling in Teensy 4.0

Hi,

I have some frequency detection code that works on Arduino Nano (AVR) using autocorrelation. I know that the ADC Sampling lines in the code below are for AVR chips (doesn't work on esp32), but I've read that it should work on Teensy 4.0. But it doesn't currently. I'm wondering if I need to include a library or something similar like that:

These lines error:
Code:
  //set up continuous sampling of analog pin 0
  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0 ;
  ADCSRB = 0 ;

  ADMUX |= (1 << REFS0) ; //set reference voltage
  ADMUX |= (1 << ADLAR) ; //left align the ADC value- so we can read highest 8 bits from ADCH register only

  ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // ADC clock 128 prescaler- 16mHz/128=125kHz->9615 sps
  ADCSRA |= (1 << ADATE); //enable auto trigger
  ADCSRA |= (1 << ADIE) ; //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN) ; //enable ADC
  ADCSRA |= (1 << ADSC) ; //start ADC measurements
}

ISR(ADC_vect) {     // When ADC sample ready, put in buffer if not full
  if (sampleCnt < bufferSize)
  {
    rawData[sampleCnt] = ADCH ;
    sampleCnt++ ;
  }
}
 
ADMUX, ADCSRA, etc. are AVR-specific registers. T4.x is not ARM, not AVR, so this code will definitely not work or even compile. You can use the standard Arduino API for analog inputs, i.e. analogRead(), etc., or you can look at the examples for Teensy's ADC library, which is installed with TeensyDuino.
 
Mistyped there, you mean "is ARM, not AVR" - even ARM processors differ in register assigments as they can have a variety of different hardware peripherals assigned.
 
Back
Top