cancel
Showing results for 
Search instead for 
Did you mean: 

Use timer to trigger ADC sampling using STM32CubeMX generated code

patrickbrataas9
Associate II
Posted on October 29, 2014 at 14:09

I have previously used a timer for triggering ADC for sampling using the standard peripheral library. I am now trying out the STM32CubeMX GUI and generating code from it to be used with Keil. I am working on the STM32F429DISCOVERY board.

I get the timer IRQ/ISR running, but the ADC is not triggered. I have read in the ref. manual that timer4 ch4 can be used to trigger a regular group conversion on the ADC. This is what I am trying to do.

What kind of mode is needed to trigger ADC? I can not find any information about this in the reference manual... I used a STM32F103VC earlier and it had to be put in PWM mode (also not explained in the reference manual).

Looking on page 577 in the STM32F429 ref. manual there is a figure ''Figure 134. General-purpose timer block diagram'' and as far as I can see there is no interrupt from the capture compare that can trigger the TRGO output that is connected to the ADC.

But the ADC says that it can be trigger on a capture compare event.

I have tried comparing with the STMF4 Standard Peripheral Library examples, but no luck.

I have tried output compare mode, PWM and time base without luck.

Anyone been able to use the STM32CubeMX to generate a working example? I would appreciate a .ioc file with a working setup or some more information if possible. I see this question has been asked before when using the standard peripheral library, but not using STM32CubeMX.
4 REPLIES 4
Amel NASRI
ST Employee
Posted on October 31, 2014 at 13:51

Anyone been able to use the STM32CubeMX to generate a working example?

The CubeMX generates the initialization code, not the whole project. This has to be customized depending on your expected application case.

Did you checked the examples provided in the STM32CubeF4 package?

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

patrickbrataas9
Associate II
Posted on November 03, 2014 at 17:46

I know the CubeMX only does the initilaization code and that I have to e.g. do HAL_TIM_Start() and implement the callback functions etc, but this has nothing to do with the fact that it is not explained anywhere what mode the ADC/TIM has to be set to in order to use the timer to trigger ADC conversions...

I checked the examples. As far as I can see there is no example doing this, except using the TRGO.

Posted on February 27, 2015 at 14:14

Hi Phataas,

The configuration from CubeMX of ADC triggered by timer is working correctly.

You need to set:

- ADC:

- external trigger conversion edge: rising edge (or falling edge)

- external trigger conversion source: TIM2 TRGO

- Timer:

- Trigger output update

Please find attached an example running on STM32F4 discovery:

- Files: CubeMx file (.ioc) and project files generated with IDE: IAR.

Generated with CubeMX v4.4.0, FWSTM32Cube_FW_F4_V1.3.0

- Configuration: CubeMX is configuring the ADC and timer, I have added into user sections the execution functions: ADC start, timer start, leds toggle (led orange toggling on TIM2 time base set to 1s, led green is toggling every 100ms in main loop).

I have also added ADC transfer by DMA: each conversion is done every 1 second, transferred by DMA into an array. DMa is circular: when transfer is complete, it starts to the beginning of buffer again.

Best regards,

Heisenberg.

________________

Attachments :

Example_CubeMX_STM32F4Discovery_ADC_trig_timer_DMA.7z : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0aG&d=%2Fa%2F0X0000000baq%2FhF5DLi0U7_YklgEO7eFK8Kk4j.xCnRY0XoREtbOLdGI&asPdf=false
kristaps
Associate
Posted on May 26, 2015 at 07:51

Thank you for sharing this!

Does the same apply if I want to read multiple ADC channels?

If I have 8 channels to read then I could just define the buffer length as 8?

I'm designing a BLDC motor driver and I should sense the back-emf voltages as well as user speed input, current measurements e.t.c.

Also, how exactly does one configure the smart XOR'd hall-sensor reading using the timers? (for now Im' just waiting for an EXTI interrupt but it might be wiser to use that one instead, am I right?)

Thank you very much, Heisenberg!

Peace!