I am developing an application to control a 3-phase motor by means of center-aligned PWM. In order to obtain the best measurements, I need to sample the ADC channels when all 3 PWM outputs are ON and/or OFF at the same time. This should happen at the peak of the counter (ARR) and at 0. Nevertheless I am having problems trying to synchronize the ADC to be triggered at the proper times. I have taken bits and examples from many websites and the documentation, since it seems difficult to find comprehensive examples with the latest versions of Chibi.
What is working right now:
- TIM1 is used for center-aligned PWM @ 20 kHz (oscilloscope says 21 kHz tho), 3 complementary channels with tiny dead-times.
- ADC triggered by TIM2 @ 20 kHz, asynchronous conversion, 6 channels, 12-bit, 15 cycles each.
What I have tried without success:
- Set TIM2 to 40 kHz to trigger ADC twice per period, but same scenario.
- Set TIM2 to start at the same time as TIM1 so the counters are synchronized. Following the indications of the reference manual. I call pwmStart() and gptStart with the configuration first and then I manipulate the registers. I trigger manually TIF1 by code in the main thread. Same as before, sometimes I get the ADC to sample at the right time, but not always.
- Use PWM end-of-period callback and also use the remaining channel with a duty cycle of 99% to trigger a callback. Same results, can't get the right values all the time.
- Use injected channels triggering with TIM1, although I think this does not fit my case since I have 6 channels. It did not work either.
Are these approaches wrong? Is there an easier method of achieving what I am trying to do? I have tried to find examples of this on the internet but I did not find anything.
Thanks.