Input Capture does not work
Hello,
I have a problem with the Input Capturing on Timer 8.I use these advanced Timer becuase if the timer has count up, I set the “overflow” variable to one and the rotary frequency is defined as 0,otherwise if a rising edge occures I want the TIM->CCR to be written via input capture and calculate it by my algorithm. My rotary encoder produces a nearly perfect edge form 0V to 5V. I tried neary everything. I switched the input channel an so on. Do you maybe see any mistakes in my Timer and Input declaration below? I would be very thankful for any advice. Do I have to program the transfer of TIM8->CNT to TIM-CCRx in my Interrupt or has anything else to be done?
void start_timer8(uint16_t period, uint16_t prescaler/*, uint16_t ccr1, uint16_t ccr2, uint16_t ccr3, uint16_t ccr4*/)
{
NVIC_InitTypeDef NVIC_InitStructure;
//Clock für den Timer
/*TIM8 clock enable 1)*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
//GPIOC clock enable
// RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//2)
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// unsigned short PrescalerValue = (unsigned short) (SYSTEM_TIMERCLOCK / TIMER_TAKT) - 1;
//3)
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t PrescalerValue = (uint16_t) prescaler-1;
TIM_TimeBaseStructure.TIM_Period= (period-1); //
TIM_TimeBaseStructure.TIM_Prescaler= (prescaler-1); //15381;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM8, PrescalerValue, TIM_PSCReloadMode_Immediate);
TIM_ICInitTypeDef TIM_ICInitStructure;
//4)
TIM_ICInitStructure.TIM_Channel = 1; //TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Which Edge capture occures
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // Div:1, every edge detected
TIM_ICInitStructure.TIM_ICFilter = 0x1;
//5)
TIM_ICInit(TIM8, &TIM_ICInitStructure);
//6) Enable the TIM8 gloabal Interrupt //
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIM8 CC Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//7)
/* Enable the CCx Interrupt Request */
TIM_ClearITPendingBit(TIM8, 1); //TIM_IT_CCx);
TIM_ITConfig(TIM8, TIM_IT_Update | TIM_IT_CC1, ENABLE); //TIM_IT_UPDATE for Timer UP Interrupt; TIM_IT_CC1 für Captureinterrupt
//8)
/* TIM4 enable counter */
TIM_Cmd(TIM8, ENABLE);
}
Kind Regards Jan