AnsweredAssumed Answered

Co-work TIM2 and TIM3

Question asked by Ivan Zuev on Sep 13, 2012
Hello, friends.

I have a task: I need to generate short buzz,  when I pressed a button ("User" button on STM32L-Discovery).
What I did?
1. Connect TIM2_CH2 with BUZZER (connect to PA1)
2. Configured TIM3_CH2 as Master, One Pulse Mode.
3. Configured TIM2_CH2 as Slave, PWM.
4. Configured Interrupt for "User" button. When interrupt is occured, TIM3 is started by software.

Everything works OK,  but TIM2_CH2 output sometime is finished in HIGH state. It is inadmissible. In the most cases TIM2_CH2 output is finished in LOW state.

And I don't why? In which direction should I look for? I'd like that TIM2_CH2 finished always in LOW state!

Code:
01.void vInitTIM(void)
02.{
03.  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
04.  GPIO_InitTypeDef GPIO_InitStructure; 
05.  TIM_OCInitTypeDef  TIM_OCInitStructure;
06.   
07.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 |RCC_APB1Periph_TIM3, ENABLE);
08.   
09.  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
10.   
11.  /*--------------------------------- GPIO Configuration -------------------------*/
12.  /* GPIOA Configuration: Pins  1 */
13.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1;
14.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
15.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
16.  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
17.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
18.   
19.  GPIO_Init(GPIOA, &GPIO_InitStructure); 
20.   
21.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5;
22. 
23.  GPIO_Init(GPIOB, &GPIO_InitStructure);
24.     
25.    /* Remap pins */
26.  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
27.  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3);
28.   
29.   
30.   
31.  /* Time base configuration */
32.  TIM_TimeBaseStructure.TIM_Period = TIM2_Period;
33.  TIM_TimeBaseStructure.TIM_Prescaler = 0x1000;
34.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
35.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
36.   
37.  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
38.   
39.   
40.  /* Output Compare Timing Mode configuration: Channel1 */
41.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
42.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
43.  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
44.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
45.   
46.  TIM_OC2Init(TIM2, &TIM_OCInitStructure);
47.   
48.   
49.   
50.  /* Time base configuration */
51.  TIM_TimeBaseStructure.TIM_Period = TIM3_Period;
52.  TIM_TimeBaseStructure.TIM_Prescaler = 0x1000;
53.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
54.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
55.  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 
56.   
57.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
58.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
59.  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
60.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
61.   
62.  TIM_OC2Init(TIM3, &TIM_OCInitStructure); 
63.   
64.  TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
65.   
66.   
67.  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
68.  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Enable);
69.   
70.  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);
71.  TIM_SelectInputTrigger(TIM2, TIM_TS_ITR2);
72.   
73.  /* TIM2 enable counter */
74.  TIM_Cmd(TIM2, ENABLE);
75.  /* TIM3 enable counter */
76.  TIM_Cmd(TIM3, ENABLE); 
77.   
78.   
79.}

Thanks for helping!

Best regards,
Ivan Zuev

Attachments

Outcomes