AnsweredAssumed Answered

Control Phase Shift with Master and Slave timers

Question asked by zarks.con on Nov 29, 2015
Hi, I'm trying to control the phase shift of TIM1 and TIM8 (they are on toggle mode and I need them on toggle mode).

I want to change their phase shift live, inside while loop. I tryied changing the TIMx->CNT value but it doesn't work all the time. So I thought if it's possible to set them as slaves of TIM3 and TIM4 respectevely, and those two slaves to TIM2 so they could start together.

In my code bellow PB4 changes duty cycle but PE9 doesn't. What's more, if you comment everything inside while loop and rebuild the code, then you'll see that PB4 and PE9 have a delay which I can't figure out why since TIM1 is set on Trigger Slave Mode.

Any help or idea would be appreciated !

001./* Includes ------------------------------------------------------------------*/
002.#include "stm32f4xx.h"
003.#include "stm32f4xx_rcc.h"
004.#include "stm32f4xx_gpio.h"
005.#include "stm32f4xx_tim.h"
006.#include "misc.h"
007.#include "math.h"
008. 
009./* Private typedef -----------------------------------------------------------*/
010.GPIO_InitTypeDef  GPIO_InitStructure;
011.USART_InitTypeDef USART_InitStructure;
012.TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
013.TIM_OCInitTypeDef  TIM_OCInitStructure;
014.TIM_BDTRInitTypeDef  TIM_BDTRInitStructure;
015./* Private define ------------------------------------------------------------*/
016./* Private macro -------------------------------------------------------------*/
017./* Private variables ---------------------------------------------------------*/
018.float F1 = 45.0;
019.float F2 = 45.0;
020.float F= 0.0;
021.float Dead_time = 0.000000250;
022./* Private function prototypes -----------------------------------------------*/
023.void TIM4_Configuration(double period);
024.void TIM3_Configuration(double period);
025.void TIM2_Configuration(double period);
026.void TIM1_Config(void);
027.void TIM8_Config(void);
028.void TIM3_Config(void);
029.void TIM4_Config(void);
030.void Toggle_TIM1_Config(double period);
031.void Toggle_TIM8_Config(double period);
032.void PWM_TIM3_SetDC(uint16_t channel,uint16_t dutycycle);
033.void PWM_TIM4_SetDC(uint16_t channel,uint16_t dutycycle);
034.void Delay(__IO uint32_t nCount);
035. 
036./* Private functions ---------------------------------------------------------*/
037. 
038.int main(void)
039.{
040.    /* 42KHz */
041.    double period = ((SystemCoreClock /2.0) / 42000) - 1;
042.    /* starting value for the controlable DC*/
043.    double pulse_width = period*0.1;
044.    /* TIM Configuration */
045.    TIM3_Config();
046.    TIM4_Config();
047.    TIM1_Config();
048.    TIM8_Config();
049. 
050.    /* Toggle Configuration */
051.    Toggle_TIM1_Config(period);
052.    Toggle_TIM8_Config(period);
053.    TIM4_Configuration(period);
054.    TIM3_Configuration(period);
055.    TIM2_Configuration(period);
056.//  /* TIM2 enable counter */
057.//  TIM_Cmd(TIM2, ENABLE);
058.//  /* TIM3 enable counter */
059.//  TIM_Cmd(TIM3, ENABLE);
060.//      /* TIM4 enable counter */
061.//  TIM_Cmd(TIM4, ENABLE);
062.    /* TIM1 enable counter */
063.    TIM_Cmd(TIM1, ENABLE);
064.    /* Main Output Enable */
065.    TIM_CtrlPWMOutputs(TIM1, ENABLE);
066.    /* TIM8 enable counter */
067.    TIM_Cmd(TIM8, ENABLE);
068.    /* Main Output Enable */
069.    TIM_CtrlPWMOutputs(TIM8, ENABLE);
070. 
071.  while (1)
072.  {
073.        TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Reset);
074.    PWM_TIM3_SetDC(1,pulse_width++);
075.    if (pulse_width > (period*0.9))
076.    {
077.      pulse_width = period*0.1;
078.    }
079.        TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Trigger);
080. 
081.    Delay(1000000);
082.  }
083.}
084.void TIM1_Config(void)
085.{
086.  GPIO_InitTypeDef GPIO_InitStructure;
087. 
088.  /* TIM3 clock enable */
089.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
090. 
091.  /* GPIOB clock enable */
092.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
093.   
094.  /* GPIOC Configuration: TIM3 CH1 (PB4) */
095.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
096.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
097.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
098.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
099.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
100.  GPIO_Init(GPIOE, &GPIO_InitStructure);
101.   
102.  /* Connect TIM1 pins */ 
103.  GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1); //channel 1
104.  GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_TIM1);//channel 2
105.  GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_TIM1);//channel 3
106.  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);//channel 4
107. 
108.}
109.void TIM8_Config(void)
110.{
111.  GPIO_InitTypeDef GPIO_InitStructure;
112. 
113.  /* TIM3 clock enable */
114.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
115. 
116.  /* GPIOB clock enable */
117.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC , ENABLE);
118.   
119.  /* GPIOA Configuration:  */
120.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
121.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
122.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
123.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
124.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
125.  GPIO_Init(GPIOA, &GPIO_InitStructure);
126.     
127.    /* GPIOB Configuration: */
128.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
129.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
130.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
131.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
132.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
133.  GPIO_Init(GPIOB, &GPIO_InitStructure);
134.   
135.    /* GPIOC Configuration: */
136.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
137.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
138.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
139.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
140.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
141.  GPIO_Init(GPIOC, &GPIO_InitStructure);
142.     
143.  /* Connect TIM3 pins */ 
144.  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);//channel 1
145.  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM8);//channel 2
146.  GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM8);//channel 3
147.  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8);//channel 4
148.}
149.void TIM3_Config(void)
150.{
151.  GPIO_InitTypeDef GPIO_InitStructure;
152. 
153.  /* TIM3 clock enable */
154.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
155. 
156.  /* GPIOB clock enable */
157.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
158.   
159.  /* GPIOC Configuration: TIM3 CH1 (PB4) */
160.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
161.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
162.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
163.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
164.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
165.  GPIO_Init(GPIOB, &GPIO_InitStructure);
166.   
167.  /* Connect TIM3 pins to AF2 */ 
168.  GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3);
169.}
170.void TIM4_Config(void)
171.{
172.  GPIO_InitTypeDef GPIO_InitStructure;
173. 
174.  /* TIM3 clock enable */
175.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
176. 
177.  /* GPIOC and GPIOB clock enable */
178.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
179.   
180.  /* GPIOC Configuration: TIM4 CH2 (PB7) */
181.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7 ;
182.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
183.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
184.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
185.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
186.  GPIO_Init(GPIOB, &GPIO_InitStructure);
187.   
188.  /* Connect TIM3 pins to AF2 */ 
189.  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
190.}
191.void Toggle_TIM1_Config(double period)
192.{
193. 
194.  uint16_t PrescalerValue = 0;
195. 
196.  /* TIM1 Peripheral Configuration ----------------------------------------*/
197.  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
198.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
199.  TIM_TimeBaseStructure.TIM_Period = period;
200.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
201.  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
202.  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
203. 
204.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
205.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
206.  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
207.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
208.  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
209.  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
210.  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
211.  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
212.  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
213. 
214.  if (F1>=0 && F1 <180)
215.    {
216.        TIM_OCInitStructure.TIM_Pulse = 0.0;
217.        TIM_OC1Init(TIM1, &TIM_OCInitStructure);
218.        TIM_OC1PreloadConfig(TIM1, ENABLE);
219. 
220.        TIM_OCInitStructure.TIM_Pulse = 0.0+(fabs(F1)/180.0)*period ;
221.        TIM_OC2Init(TIM1, &TIM_OCInitStructure);
222.        TIM_OC2PreloadConfig(TIM1, ENABLE);
223.    }
224. 
225.  if (F1<0 && F1 >-180)
226.    {
227.        TIM_OCInitStructure.TIM_Pulse = 0.0+(fabs(F1)/180.0)*period ;
228.        TIM_OC1Init(TIM1, &TIM_OCInitStructure);
229.        TIM_OC1PreloadConfig(TIM1, ENABLE);
230. 
231.        TIM_OCInitStructure.TIM_Pulse = 0.0;
232.        TIM_OC2Init(TIM1, &TIM_OCInitStructure);
233.        TIM_OC2PreloadConfig(TIM1, ENABLE);
234.    }
235. 
236.  if (F1==180)
237.  {
238.        TIM_OCInitStructure.TIM_Pulse = 0.0;
239.        TIM_OC1Init(TIM1, &TIM_OCInitStructure);
240.        TIM_OC1PreloadConfig(TIM1, ENABLE);
241. 
242.        TIM_OCInitStructure.TIM_Pulse = period ;
243.        TIM_OC2Init(TIM1, &TIM_OCInitStructure);
244.        TIM_OC2PreloadConfig(TIM1, ENABLE);
245.  }
246.  if (F1==-180)
247.  {
248.        TIM_OCInitStructure.TIM_Pulse = period ;
249.        TIM_OC1Init(TIM1, &TIM_OCInitStructure);
250.        TIM_OC1PreloadConfig(TIM1, ENABLE);
251. 
252.        TIM_OCInitStructure.TIM_Pulse = 0.0;
253.        TIM_OC2Init(TIM1, &TIM_OCInitStructure);
254.        TIM_OC2PreloadConfig(TIM1, ENABLE);
255.  }
256. 
257.  /* Automatic Output enable, Break, dead time and lock configuration*/
258.  double DeadTime = Dead_time * (SystemCoreClock);
259.  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
260.  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
261.  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
262.  TIM_BDTRInitStructure.TIM_DeadTime = DeadTime;
263.  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
264.  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
265.  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
266.  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
267. 
268.  /* Slave Mode selection: TIM1 */
269.  TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Trigger); // Phase align on initial trigger
270.  TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2); // ITR2 on TIM1 is TIM3_TRGO
271. 
272.//  if (F>0 && F<=180)
273.//  {
274.//          TIM_SetCounter(TIM1, period / (fabs(180.0f/F)));
275.//  }
276.//  if (F<0 && F>=-180)
277.//  {
278.//          TIM_SetCounter(TIM1, period);
279.//  }
280.}
281.void Toggle_TIM8_Config(double period)
282.{
283.    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
284.    TIM_OCInitTypeDef  TIM_OCInitStructure;
285.    TIM_BDTRInitTypeDef  TIM_BDTRInitStructure;
286.     
287.  uint16_t PrescalerValue = 0;
288. 
289.  /* TIM8 Peripheral Configuration ----------------------------------------*/
290.  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
291.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
292.  TIM_TimeBaseStructure.TIM_Period = period;
293.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
294.  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
295.  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
296. 
297.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
298.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
299.  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
300.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
301.  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
302.  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
303.  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
304.  TIM_OC1Init(TIM8, &TIM_OCInitStructure);
305.  TIM_OC2Init(TIM8, &TIM_OCInitStructure);
306. 
307.  if (F2>=0 && F2 <180)
308.    {
309.        TIM_OCInitStructure.TIM_Pulse = 0.0;
310.        TIM_OC1Init(TIM8, &TIM_OCInitStructure);
311.        TIM_OC1PreloadConfig(TIM8, ENABLE);
312. 
313.        TIM_OCInitStructure.TIM_Pulse = 0.0 +(fabs(F2)/180.0)*period;
314.        TIM_OC2Init(TIM8, &TIM_OCInitStructure);
315.        TIM_OC2PreloadConfig(TIM8, ENABLE);
316.    }
317. 
318.  if (F2<0 && F2 >-180)
319.    {
320.        TIM_OCInitStructure.TIM_Pulse = 0.0 +(fabs(F2)/180.0)*period;
321.        TIM_OC1Init(TIM8, &TIM_OCInitStructure);
322.        TIM_OC1PreloadConfig(TIM8, ENABLE);
323. 
324.        TIM_OCInitStructure.TIM_Pulse = 0.0;
325.        TIM_OC2Init(TIM8, &TIM_OCInitStructure);
326.        TIM_OC2PreloadConfig(TIM8, ENABLE);
327.    }
328. 
329.  if (F2==180)
330.  {
331.        TIM_OCInitStructure.TIM_Pulse = 0.0;
332.        TIM_OC1Init(TIM8, &TIM_OCInitStructure);
333.        TIM_OC1PreloadConfig(TIM8, ENABLE);
334. 
335.        TIM_OCInitStructure.TIM_Pulse = period ;
336.        TIM_OC2Init(TIM8, &TIM_OCInitStructure);
337.        TIM_OC2PreloadConfig(TIM8, ENABLE);
338.  }
339.  if (F2==-180)
340.  {
341.        TIM_OCInitStructure.TIM_Pulse = period ;
342.        TIM_OC1Init(TIM8, &TIM_OCInitStructure);
343.        TIM_OC1PreloadConfig(TIM8, ENABLE);
344. 
345.        TIM_OCInitStructure.TIM_Pulse = 0.0;
346.        TIM_OC2Init(TIM8, &TIM_OCInitStructure);
347.        TIM_OC2PreloadConfig(TIM8, ENABLE);
348.  }
349. 
350.  /* Automatic Output enable, Break, dead time and lock configuration*/
351.  double DeadTime = Dead_time * (SystemCoreClock);
352.  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
353.  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
354.  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
355.  TIM_BDTRInitStructure.TIM_DeadTime = DeadTime;
356.  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
357.  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
358.  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
359.  TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);
360. 
361.  /* Slave Mode selection: TIM8 */
362.  TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Trigger); // Phase align on initial trigger
363.  TIM_SelectInputTrigger(TIM8, TIM_TS_ITR2); // ITR2 on TIM8 is TIM4_TRGO
364. 
365.//  if (F>0 && F<=180)
366.//  {
367.//      TIM_SetCounter(TIM8, period);
368.//  }
369.//  if (F<0 && F>=-180)
370.//  {
371.//      TIM_SetCounter(TIM8, period / (fabs(180.0f/F)));
372.//  }
373.}
374.void PWM_TIM3_SetDC(uint16_t channel,uint16_t dutycycle)
375.{
376.  if (channel == 1)
377.  {
378.    TIM3->CCR1 = dutycycle;
379.  }
380.  else if (channel == 2)
381.  {
382.    TIM3->CCR2 = dutycycle;
383.  }
384.  else if (channel == 3)
385.  {
386.    TIM3->CCR3 = dutycycle;
387.  }
388.  else
389.  {
390.    TIM3->CCR4 = dutycycle;
391.  }
392.}
393.void PWM_TIM4_SetDC(uint16_t channel,uint16_t dutycycle)
394.{
395.  if (channel == 1)
396.  {
397.    TIM4->CCR1 = dutycycle;
398.  }
399.  else if (channel == 2)
400.  {
401.    TIM4->CCR2 = dutycycle;
402.  }
403.  else if (channel == 3)
404.  {
405.    TIM4->CCR3 = dutycycle;
406.  }
407.  else
408.  {
409.    TIM4->CCR4 = dutycycle;
410.  }
411.}
412. 
413.void Delay(__IO uint32_t nCount)
414.{
415.    while(nCount--)
416.  {
417.  }
418.}
419.void TIM4_Configuration(double period)
420.{
421.  /* TIM4 clock enable */
422.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
423. 
424.  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
425.  TIM_OCInitTypeDef       TIM_OCInitStructure;
426. 
427.  /* TIM4 Peripheral Configuration ----------------------------------------*/
428.  /* Time Base configuration */
429.  TIM_TimeBaseStructure.TIM_Prescaler = 0000;
430.  TIM_TimeBaseStructure.TIM_Period = period;
431.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
432.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
433.  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
434. 
435.  /* Channel 2 Configuration in PWM mode */
436.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
437.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
438.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
439.  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
440.  TIM_OCInitStructure.TIM_Pulse = period /2.0;
441.  TIM_OC2Init(TIM4, &TIM_OCInitStructure);
442.     
443.      /* Slave Mode selection: TIM4 */
444.  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger); // Phase align on initial trigger
445.  TIM_SelectInputTrigger(TIM4, TIM_TS_ITR1); // ITR1 on TIM4 is TIM2_TRGO
446.     
447.  /* Master Mode selection */
448.  TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_Enable); // Trigger when enabled
449. 
450.  /* Select the Master Slave Mode */
451.  TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);
452.}
453.void TIM3_Configuration(double period)
454.{
455.  /* TIM3 clock enable */
456.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
457. 
458.  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
459.  TIM_OCInitTypeDef       TIM_OCInitStructure;
460. 
461.  /* TIM3 Peripheral Configuration ----------------------------------------*/
462.  /* Time Base configuration */
463.  TIM_TimeBaseStructure.TIM_Prescaler = 0000;
464.  TIM_TimeBaseStructure.TIM_Period = period;
465.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
466.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
467.  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
468. 
469.  /* Channel 1 Configuration in PWM mode */
470.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
471.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
472.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
473.  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
474.  TIM_OCInitStructure.TIM_Pulse = period /2.0;
475.  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
476.     
477.          /* Slave Mode selection: TIM3 */
478.  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger); // Phase align on initial trigger
479.  TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1); // ITR1 on TIM2 is TIM2_TRGO
480.     
481.  /* Master Mode selection */
482.  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Enable); // Trigger when enabled
483. 
484.  /* Select the Master Slave Mode */
485.  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
486.}
487.void TIM2_Configuration(double period)
488.{
489.  /* TIM2 clock enable */
490.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
491. 
492.  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
493.   
494.  //double Period = ((SystemCoreClock / 2) /50 )-1; // On APB1, slow clock, 32-bit
495. 
496.  /* TIM2 Peripheral Configuration ----------------------------------------*/
497.  /* Time Base configuration */
498.  TIM_TimeBaseStructure.TIM_Prescaler = 0000;
499.  TIM_TimeBaseStructure.TIM_Period = period;
500.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
501.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
502.  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
503. 
504.  /* Channel 1 Configuration in PWM mode */
505.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
506.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
507.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
508.  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
509.  TIM_OCInitStructure.TIM_Pulse = period/2.0;
510. 
511.  /* Master Mode selection */
512.  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Enable); // Trigger when enabled
513. 
514.  /* Select the Master Slave Mode */
515.  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
516.}

Outcomes