I have setup a little blue pill STM32F103 as an A/D converter. Using STM32CubeMX, I have set the ADC clock to be 12 MHz. I am sampling with a 7.5 cycle set-up time. The A/D conversion should thus be about 12.5 + 7.5 cycles and should thus take about 1.75 microS. My program uses the software polled, tight loop, A/D conversion as shown in the code here (I'm using TrueSTUDIO 9.1);
Code: Select all
ptr = 0;
ADC1->CR2 |= ADC_CR2_ADON; // turn on the ADC_SR
while (ptr < total_sample_number)
ADC1->CR2 |= ADC_CR2_ADON; // set the ADON bit in the ADC_SR
while ((ADC1->SR & ADC_SR_JEOC) != 0); // wait till the EOC bit is set in the ADC_CR2 register
data[ptr++] = (uint16_t) ADC1->DR;
I have connected a waveform generator producing a 2350 Hz triangular waveform to the channel 0 input. The resulting data, plotted with gnuplot, shows that a 1000 samples of the data corresponds to one cycle of the 2350 Hz waveform. I adjusted the frequency of the waveform generator to get one complete cycle of the waveform in the 1000 samples. That means that a single sample takes about (1 / (1000*2350) ) of a second; about 0.425 microS. This is about 4 times faster than I would expect.
I conclude from this that STM32CubeMX is not really setting the ADC clock properly ... is there a better explanation? I've looked at the waveform with an oscilloscope and it is, indeed, 2350 Hz as close as one can tell looking at it.