STM32F303K8 - Sampling a 40KHz sine-wave
- May 26, 2023
- 3 replies
- 4213 views
The problem
I am to compare the phase shift between two sinewaves from an external source (I used a function generator in this case). I am running the STM32F303K8 at 64MHz, and this clock goes directly to the ADC and Timers 1 and 2 as well with a prescalar of 1.
Now according to Nyquist theorem, i have to sample at atleast x2 the frequency that is available, of interest. I start off with both my sinewaves having exactly 40KHz.
snapshot of my function generator
I am using timer triggerd ADCs 1 and 2 with DMA + continous mode, each taking in a sinewave because i need them to be sampled simultaneously with very minimal distortion.
According to the datasheet on P.323, the Tconv = (Sampling time + 12.5 (12bit)) / 4095. I went for 601.5 cycles which puts me at Tconv to be 9.9us approx 100KHz, puting me well beyong the Nyquist frequency to sample the 40KHz waves.
I also configured separate timers for each ADC, 1 and 2 to be triggered by TIM 1 and 2 update event respectively.
When i convert the DMA'd values back to voltages, i can't recreate the sinewaves again, with the STM Studio.
snapshot from STM Studio.
I use the formula to convert my adc values back to voltages.
uint32_t adc1 = 0, adc2 = 0;
float voltage0 = 0.0, voltage1 = 0.0;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *adc) {
if (adc->Instance == ADC1)
voltage0 = (adc1 * 3.3) / 4095;
if (adc->Instance == ADC2)
voltage1 = (adc2 * 3.3) / 4095;
}In order not to overwhelm the question, i have attached my main.c file on this topic.
I think based on the sampling frequency, i can digitalize my sinewaves effortlessly, but still i don't know what is missing. I cant seem realize the theory.