Using Arduino Due to Convert Measurements with a Constant Sampling Frequency

Hi, all,

I’m working in a project with Arduino Due that I need to convert measurements with a constant determined sampling frequency.

This convertion must begins, synchronously, when the phase of the reference sine wave is zero (for that matter i’m using a zero-crossing with a pulse generator and digital reading it). From that signal, I must measure a sine wave from 8 to 10 times with a specific sampling frequency (the more I can set, the better) when the pulse is high. Then, I switch to other channel and repeat the process. I was able to measure with 250kHz the wave and no more than that with the following code.

volatile uint32_t lastConversion;
volatile boolean Flag;

void setup()
{

  adc_setup();
  tc_setup();
}

void loop()
{
if (Flag== true)
{
  Flag = false;
  // A new conversion is available
// Do not Serial print each ADC conversion. A Serial Print takes ~100 us
}
}

/*************  Configure ADC function  *******************/
void adc_setup() {

  PMC->PMC_PCER1 |= PMC_PCER1_PID37;                    // ADC power on
  ADC->ADC_CR = ADC_CR_SWRST;                           // Reset ADC
  ADC->ADC_MR |=  ADC_MR_TRGEN_EN                       // Hardware trigger select
                  | ADC_MR_TRGSEL_ADC_TRIG3             // Trigger by TIOA2
                  | ADC_MR_PRESCAL(1);
  ADC->ADC_ACR = ADC_ACR_IBCTL(0b01);                   // For frequencies > 500 KHz

  ADC->ADC_CHER = ADC_CHER_CH7;                        // Enable ADC CH7 = A0
  ADC->ADC_IER = ADC_IER_EOC7;                         // Interrupt on End of conversion
  NVIC_EnableIRQ(ADC_IRQn);                            // Enable ADC interrupt

}
void ADC_Handler()
{
  lastConversion = ADC->ADC_CDR[7];
  Flag = true;
}
/*************  Timer Counter 0 Channel 2 to generate PWM pulses thru TIOA2  ************/
void tc_setup() {

  PMC->PMC_PCER0 |= PMC_PCER0_PID29;                      // TC2 power ON : Timer Counter 0 channel 2 IS TC2
  TC0->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK2  // MCK/8, clk on rising edge
                              | TC_CMR_WAVE               // Waveform mode
                              | TC_CMR_WAVSEL_UP_RC        // UP mode with automatic trigger on RC Compare
                              | TC_CMR_ACPA_CLEAR          // Clear TIOA2 on RA compare match
                              | TC_CMR_ACPC_SET;           // Set TIOA2 on RC compare match


  TC0->TC_CHANNEL[2].TC_RC = 238;  //<*********************  Frequency = (Mck/8)/TC_RC  Hz = 44.117 Hz
  TC0->TC_CHANNEL[2].TC_RA = 40;  //<********************   Any Duty cycle in between 1 and 874

  TC0->TC_CHANNEL[2].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC2 counter and enable

}

Even if I could get the measurements, when I increased/decreased the sampling frequency, the output seems to be not ok, It always seems to be measuring 2 periods of a 50k frequency sine wave regardeless of the sampling frequency with 10 samples. I couldn’t obtain more than 250k sampling rate either.

I wonder what I can do to obtain good measurements. Some ideas are appreciatted.

Thanks in advance.