cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 ADC1 regular + ADC2 and ADC3 in injected mode

canta76
Associate II
Posted on August 24, 2015 at 22:57

Hi All,

I've some problem configuring the ADCs of a STM32F407.

I have 2 motors connected to my board that I drive with 2 different pwm.

For the 1st motor I use a pwm from PE9 (TIM1 CH1)

For the 2nd motor I use a pwm from PE13 (TIM1_CH3)

I need to sample the bemf in the middle of the PWM Toff;

also I need to sample the current in the middle of the PWM Ton and in the middle of the Toff.

MOTOR1 bemf analog pin is connected to PC2 = ADC123_IN12;

MOTOR2 bemf analog pin is connected to PA0 = ADC123_IN0;

MOTOR1 current sensor analog pin is connected to PA5 = ADC12_IN5;

MOTOR2 current sensor analog pin is connected to PA3 = ADC123_IN3.

I also need to sample other analog pins connected to:

PB0 = ADC12_IN8

PB1 = ADC12_IN9

and also Vrefint, Temp, Vbat

I'd like to achieve that in the following way:

ADC1 in regular mode for PB0, PB1, PC2, PA0, PA5, PA3, Vrefint, Temp and Vbat

(in this way I'll use the bemf and current values from this ADC when the PWMs are 0% and 100%)

ADC2 in injected mode for PC2 (MOTOR1 bemf) and PA5 (MOTOR1 current)

ADC3 in injected mode for PA0 (MOTOR2 bemf) and PA3 (MOTOR2 current)

At the moment I'm ok with the ADC1 configuration.

How I can configure ADC2 and ADC3 to do this?

Also how I know when the sample is taken in the middle of the Ton or in the middle of the Toff?

Someone have some sample code?

Thanks
3 REPLIES 3
Posted on August 24, 2015 at 23:24

What's the tolerance on taking the measurements? You could use TIM1 CH2 and/or CH4 to generate CC interrupts anywhere in the cycle. If not injected, you could look also at triggering the ADC's with this or another timer in lock-step.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
canta76
Associate II
Posted on August 25, 2015 at 01:03

the pwm is working at 12kHz

I could limit the duty cycle in the range of 10%..90%.

I think it's not so important if I sample exactly in the middle of the Ton or the Toff.

Maybe it could be ok also if I sample just after the edge. In this case, could I avoid to setup another timer?

How I can trigger the adc when the pwm toggles the pin?

Posted on August 25, 2015 at 01:36

TIM1->CNT represents the phase angle, it cycles through the period.

The channel compare registers trigger at specific phase angles.

You'd have to check the Reference Manual for the specific TIMx_CHx Triggers available to a specific Regular/Injected function on a given ADC. But as timers share a synchronous clock you could use a second timer to shadow the TIM1 if you need additional flexibility.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..