Hi everyone!
I´m quite new but have to solve a measurement task to read analog inputs with 12bit resolution and a sampling rate of faster then 4kHz ( <250µsec).
Does anybody have a "simple" solution hint for me?
Using WEB Editor at the moment.
THX in advance
Christian
1 Like
I suspect the answer is no. For 12-bit answers you need to have at least 12-bit questions.
In seriousness if 12-bit resolution are your requirements you will likely need spent some effort. There are lots of information available on these subjects. Google for Application notes from the major semiconductor companies to get started.
Hi Christian,
The following code sets up the ADC to run continously in free run mode. The input is on A0 and the results are output to the console. The converstion rate should be around 80us:
// Use the SAMD21's ISR to transfer ADC results from A0 to the console
// Conversion time (free running) = ((resolution / 2) + gain delay) / clk(ADC) + sample time length
// Conversion time (free running) = ((12 / 2) + 1) / 93750) + 5.333us = 80us
volatile uint16_t adcResult; // ADC result
volatile bool resultsReady = false; // Results ready flag
void setup() {
SerialUSB.begin(115200); // Start the native USB port
while(!SerialUSB); // Wait for the console to open
ADC->INPUTCTRL.bit.MUXPOS = 0x0; // Set the analog input to A0
while(ADC->STATUS.bit.SYNCBUSY); // Wait for synchronization
ADC->SAMPCTRL.bit.SAMPLEN = 0x00; // Set sampling time length to half divided ADC clock pulse (5.333us * (SAMPLEN + 1))
ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV512 | // Divide Clock ADC GCLK by 512 (48MHz/512 = 93.75kHz)
ADC_CTRLB_RESSEL_12BIT | // Set the ADC resolution to 12-bits
ADC_CTRLB_FREERUN; // Set the ADC to free run
while(ADC->STATUS.bit.SYNCBUSY); // Wait for synchronization
NVIC_SetPriority(ADC_IRQn, 0); // Set the Nested Vector Interrupt Controller (NVIC) priority for the ADC to 0 (highest)
NVIC_EnableIRQ(ADC_IRQn); // Connect the ADC to Nested Vector Interrupt Controller (NVIC)
ADC->INTENSET.reg = ADC_INTENSET_RESRDY; // Generate interrupt on result ready (RESRDY)
ADC->CTRLA.bit.ENABLE = 1; // Enable the ADC
while(ADC->STATUS.bit.SYNCBUSY); // Wait for synchronization
ADC->SWTRIG.bit.START = 1; // Initiate a software trigger to start an ADC conversion
while(ADC->STATUS.bit.SYNCBUSY); // Wait for synchronization
}
void loop()
{
if (resultsReady) // Check if the ADC conversion is ready?
{
SerialUSB.println(adcResult); // Output the result
resultsReady = false; // Clear the resultsReady flag
}
}
void ADC_Handler()
{
if (ADC->INTFLAG.bit.RESRDY) // Check if the result ready (RESRDY) flag has been set
{
ADC->INTFLAG.bit.RESRDY = 1; // Clear the RESRDY flag
while(ADC->STATUS.bit.SYNCBUSY); // Wait for read synchronization
adcResult = ADC->RESULT.reg; // Read the result;
resultsReady = true; // Set the resultsReady flag
}
}
1 Like
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.