Question
Timer
Posted on October 18, 2013 at 04:13
Hi, I am makin a encoder interface on the et-arm stamp stm32, my problem is that maximun frequency I can get from the encoder is 30Hz and I just can measure at less 1190Hz (based in PWM_IN example) I am measuring the time betwen falling and rising voltage and this is my code. The code doesn't work could anyone please help me?
void RCC_ExIT_Configuration(void){ /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);}void GPIO_ExIT_Configuration(void){ //GPIO_InitTypeDef GPIO_InitStructure; /* TIM2 channel 2 pin (PA.01) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);}void EXTI_Exp(void){ // System Clocks Configuration // RCC_ExIT_Configuration(); // // Configure the GPIO ports // GPIO_ExIT_Configuration(); /* TIM2 configuration: PWM Input mode ------------------------ The external signal is connected to TIM2 CH2 pin (PA.01), The Rising edge is used as active edge, The TIM2 CCR2 is used to compute the frequency value The TIM2 CCR1 is used to compute the duty cycle value ------------------------------------------------------------ */ TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM2, &TIM_ICInitStructure); /* Select the TIM2 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset); /* Enable the Master/Slave Mode */ TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM2, ENABLE); /* Enable the CC2 Interrupt Request */ //TIM_ITConfig(TIM2, TIM_IT_CC2 | TIM_IT_Trigger, ENABLE);}void TIM2_IRQHandler(void){ int IC2Value=0; // Clear TIM2 Capture compare interrupt pending bit //TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); if (TIM_GetITStatus(TIM2, TIM_IT_CC2)== SET){ if(count!=0){ // Get the Input Capture value IC2Value = TIM_GetCapture2(TIM2); if ((IC2Value != 0)) { // Frequency computation Frequency = SystemCoreClock / IC2Value; count+=(1/Frequency); //Frequency2 = SystemCoreClock / IC2Value2; } else { //DutyCycle = 0; Frequency = 0; //Frequency2 = 0; } TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); count=0; } } else if(TIM_GetITStatus(TIM2, TIM_IT_Trigger)== SET){ count+=1/1190; }}