cancel
Showing results for 
Search instead for 
Did you mean: 

CCR2 won't update

bsimone
Associate II
Posted on April 26, 2013 at 22:54

Hello,

I'm trying to count pulse on PB7 using timer TIM4 ch2.

Everything is ok, but I'd like to reset CCR2 so I can start another measure.

I tried with

TIM_SetCompare2(TIM4, 0);

but CCR2 isn't set to 0 after execution.

Here's how I setup TIM4:

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

/* TIM4 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

/* GPIOB clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

/* TIM4 chennel2 configuration : PB.07 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;

GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Connect TIM pin to AF2 */

GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);

/* Enable the TIM4 global Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

TIM_TimeBaseStructure.TIM_Period = 65535;

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

TIM_TIxExternalClockConfig(TIM4,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0);

//TI2FP2

/* TIM enable counter */

TIM_Cmd(TIM4, ENABLE);

/* Enable the CC2 Interrupt Request */

TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);

Thanks,

Simone

2 REPLIES 2
Posted on April 26, 2013 at 23:32

The write goes to a shadow register, and this doesn't forward until the counter rolls (ie at update), the counter also needs to be clocked, as I recall.

If channel CC2 is configured as output: CCR2 is the value to be loaded in the actual capture/compare 2 register (preload value).

 

It is loaded permanently if the preload feature is not selected in the TIMx_CCMR2 register (bit OC2PE). Else the preload value is copied in the active capture/compare 2 register when an update event occurs.

 

 

CNT holds the count, CCR2 is a latch and doesn't count.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bsimone
Associate II
Posted on April 26, 2013 at 23:53

Thank you very much for answer!

Problem solved by setting

TIM_SetCounter(TIM4, 0);

and waiting for the next pulse.