STM32 Timer-based ADC sampling at a specific rate, with debug output

preview_player
Показать описание
How to set up an STM32 to sample an ADC input at a specific rate, with a timer generating "events" that automatically perform the next ADC acquisition. You'll see in the code that there's no timer ISR used to kick off each conversion - it's all automatic until the DMA buffer is full.
Рекомендации по теме
Комментарии
Автор

Thanks for making this video. I have to go back to it every time I migrate my project to a different chip and set everything up all over again. It would be nice if STM32cubeIDE would just let you select a different chip, and highlight issues that need to be solved manually.

icollided
Автор

*Summary*

*Goal:* (Starts at 0:00)

* Sample an ADC input at a specific rate (40kHz in the example) using an STM32F303K8 microcontroller.
* Use timer events to automatically trigger ADC conversions, eliminating the need for software-triggered conversions.
* Provide a debug output signal synchronized with the ADC sampling rate.

*ADC Setup:* (Starts at 1:16)

* *Disable Scan Conversion Mode:* Ensures that only a single ADC channel is sampled.
* *Disable Continuous Conversion Mode:* Prevents continuous ADC conversions; conversions will be triggered by the timer.
* *Set External Trigger Conversion Source:* Select the timer output event that will trigger the ADC conversions (Timer 1 Trigger Out Event in this case).
* *Configure Trigger Polarity:* Choose whether the ADC conversion should be triggered on the rising edge, falling edge, or both edges of the timer output.

*Timer Setup:* (Starts at 3:05)

* *Select Internal Clock Source:* Use the internal clock to drive the timer.
* *Configure Timer Period (ARR Register):* Set the timer period to achieve the desired ADC sampling rate. The example uses a period of 1000 clock cycles for a 40kHz sampling rate with a 40MHz clock.
* *Enable Update Event Generation:* This event is triggered when the timer counter resets, which will be used to trigger the ADC conversions.
* *Configure Output Compare Channel (Optional):* This can be used to generate a debug output signal synchronized with the ADC sampling rate. The example configures Output Compare Channel 1 to toggle on every timer counter reset, generating a square wave at the sampling frequency.

*Software Implementation:* (Starts at ~5:22 - not explicitly stated but code walkthrough begins)

* *Initialize Timer and ADC:* Set up the timer and ADC peripherals with the desired configurations.
* *Start Timer and Output Compare (if used):* Enable the timer and output compare channel to begin generating trigger events and debug output.
* *Start ADC DMA:* Configure and enable DMA to transfer ADC conversion data to a memory buffer.
* *Handle DMA Conversion Complete Callback:* This callback function is called when the DMA buffer is full. It can be used to process the acquired ADC data and restart the DMA transfer for the next buffer.

*Key Points:*

* The code leverages hardware features (timers and DMA) to automate the ADC sampling process, minimizing software overhead.
* No interrupt service routines are required for ADC conversions.
* A debug output signal allows for visual verification of the sampling rate.

i used gemini 1.5 pro to summarize the transcript.

wolpumba
Автор

Didn't go through the DMA configuration.

tshepomaredi_
Автор

Nice presentation! Your code only works on an 32F407VG if I enable DMA Continuous, which in the manual says "Bit 9 DDS: DMA disable selection (for single ADC mode) 1: DMA requests are issued as long as data are converted and DMA=1". Without that it just does one set of samples. I can see the timer3 OC toggle at the right frequency, so not sure why it doesn´t work without.

robc
Автор

Hi Rick, thank you for the useful video. Can I clarify one thing? I’m fairly new to programming STM32s.
The yellow trace on the oscilloscope (channel 1) is generated by the GPIO toggle in the HAL_ADC_ConvCpltCallback(…), right?

NickAlexeev
Автор

I want to read 1 khz analog waveform after 50 microsecond of rising edge on adc which method of adc should i use for this

ishuvarshney
Автор

Hi your video is really good, but I need to write these results by using usart but I don't know in which function can I write, I an writing in convcallback function, I could not see any output, I write in while it has no output also, what can I do to make it write by usart?

merveozdas
Автор

Thank you, it worked for me, the only issue is the adc is giving wrong reading in the buffer for indexes 1, when i use this method and the error is fixed where i get 2920 instead of 3520 for all of them ( im using a fixed dc signal just to test which corresponds to 3520) , but when i use the normal case without timers i dont get that error , so for indexes 0, 2, 4.... i get correct values 3520 . any idea why this is happening ?

brahimmazighi
Автор

Hi I'm quite new at this and I got stuck and need some help.
Creating the tx buffer gives me a compiler failure, did I forget to include something?

johansvensson
Автор

In your case whether configuring dma is not required?

gundaharishkumar
Автор

I could not understnd exactly, I need to sample my signal at 1000 Hz, what can I change from your code?

merveozdas
Автор

Can you take a video about the outputs, I could not get an output

merveozdas
Автор

I'm facing a bug if i follow the video exactly only the first element of the array is getting updated and the other 9 are still at 0
Update: it got fixed after using HAL_TIM_PWM...
instead of HAL_TIM_Base..

joshuachettiar
Автор

Please.Can you help me explain how to activate a XOR mode

hsenaed
Автор

If you set dma to circular mode then you don't have to start dma again in interrupt

mlorand
Автор

how can i change the samplig rate in the while?

Alan
welcome to shbcf.ru