cancel
Showing results for 
Search instead for 
Did you mean: 

Co-work TIM2 and TIM3

zuew
Associate II
Posted on September 13, 2012 at 15:12

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:


void
vInitTIM(
void
)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

GPIO_InitTypeDef GPIO_InitStructure; 

TIM_OCInitTypeDef TIM_OCInitStructure;


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 |RCC_APB1Periph_TIM3, ENABLE);


RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);


/*--------------------------------- GPIO Configuration -------------------------*/

/* GPIOA Configuration: Pins 1 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;


GPIO_Init(GPIOA, &GPIO_InitStructure); 


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;


GPIO_Init(GPIOB, &GPIO_InitStructure);


/* Remap pins */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3);




/* Time base configuration */

TIM_TimeBaseStructure.TIM_Period = TIM2_Period;

TIM_TimeBaseStructure.TIM_Prescaler = 0x1000;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;


TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);



/* Output Compare Timing Mode configuration: Channel1 */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = CCR1_Val;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;


TIM_OC2Init(TIM2, &TIM_OCInitStructure);




/* Time base configuration */

TIM_TimeBaseStructure.TIM_Period = TIM3_Period;

TIM_TimeBaseStructure.TIM_Prescaler = 0x1000;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;


TIM_OC2Init(TIM3, &TIM_OCInitStructure); 


TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);



TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Enable);


TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);

TIM_SelectInputTrigger(TIM2, TIM_TS_ITR2);


/* TIM2 enable counter */

TIM_Cmd(TIM2, ENABLE);

/* TIM3 enable counter */

TIM_Cmd(TIM3, ENABLE); 



}

Thanks for helping! Best regards, Ivan Zuev #pwm #tim2 #tim3
0 REPLIES 0