cancel
Showing results for 
Search instead for 
Did you mean: 

Count Pulses

gsaldanha
Associate
Posted on January 10, 2013 at 00:58

I

just got my hands on a STM32 and I'm quite new to

MCU programming. I'm

trying

to implement a

(seemingly) simple counter.

I want to count up the pulses without being interrupted and be interrupted only when the counter rolls over

.

I would also like to

be able to arbitrarily

reset the counter to 0.

So fa

r I'm having trouble

settings u

p the basic co

unt up and interrupt on rollover.

Any hi

nts, sample co

de is very welcome. I'm very new to MCU programming so go easy on me!

void
Counter_Config(
void
)
{
/* PE.06 [TIM9_CH2] (INPUT) */
/* RCC Configuration */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
/* GPIO Configuration */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_TIM9);
/* Timer configuration */
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);
/* External Trigger set to External Clock, Mode 1 */
TIM_ETRClockMode1Config (TIM9,
TIM_ExtTRGPSC_OFF,
TIM_ExtTRGPolarity_NonInverted,
0);
TIM_Cmd(TIM9, ENABLE);
}
void
TIM1_BRK_TIM9_IRQHandler(
void
) 
{
if
(TIM_GetITStatus(TIM9, TIM_IT_Update) != RESET)
{
//TODO: Interrupt Implement Me!!
//TIM_GetCounter(TIM9)
}
/* Clear the interrupt */
TIM_ClearITPendingBit(TIM9, TIM_IT_Update);
}

#stm32-timer-counter
2 REPLIES 2
Posted on January 10, 2013 at 01:20

Yeah,

You're going to want a non-zero value for the period. You'll need to enable the interrupt in the NVIC and enable the update interrupt in the timer. Presumably an STM32F4? Honestly not sure PE6 TIM9_CH2 routes to the ETR, I'd have to read the manual (RM0090). Pretty sure you need CH1 via TI1FD_ED or TI1_FP1, or CH2 via TI2_FP2

NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM9 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
..
/* TIM Interrupts enable */
TIM_ITConfig(TIM9, TIM_IT_Update, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gsaldanha
Associate
Posted on January 10, 2013 at 21:38

Yes this is a STM32F4. Thanks for the help. I'll look at the reference manual.