AnsweredAssumed Answered

Count Pulses

Question asked by salfern on Jan 10, 2013
Latest reply on Jan 10, 2013 by salfern
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 far I'm having trouble settings up the basic count up and interrupt on rollover. Any hints, sample code 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);
}



Outcomes