2013-12-18 08:59 AM
Hi,
I'm trying to read a 5kHz square wave dc=50%. I'm using thw pwm input capture.The problem is that the program gives me a dc=100% and strange frequencies, but i cannot figure out why it happens.. can someone help me? /******************main****************//* Init TIMER 1 */ CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE); // configure TIM1 channel 1 to capture a PWM signal TIM1_PWMIConfig(TIM1_Channel_2, TIM1_ICPolarity_Rising, TIM1_ICSelection_DirectTI, TIM1_ICPSC_DIV1, TIM1_ICFilter); // Select the TIM1 Input Trigger: TI1FP1 TIM1_SelectInputTrigger(TIM1_TRGSelection_TI1FP1); TIM1_SelectSlaveMode(TIM1_SlaveMode_Reset); // Enable CC1 interrupt request TIM1_ITConfig(TIM1_IT_CC2, ENABLE); // Enable TIM1 TIM1_Cmd(ENABLE); enableInterrupts();/**********interruptINTERRUPT_HANDLER(TIM1_CAP_IRQHandler, 24){ disableInterrupts(); /* Get the Input Capture value by reading CCR1 register */ /* CCR1 regsiter contains signal frequency value */ IC1Value = TIM1_GetCapture2(); if (IC1Value != 0) { /* Get the Input Capture value by reading CCR2 register */ /* CCR2 regsiter contains how much time the signal remained at high level */ IC2Value = TIM1_GetCapture2(); clock_frequency = CLK_GetClockFreq(); /* Duty cycle computation */ SignalDutyCycle = ((uint32_t) IC2Value * 100) / IC1Value; /* Frequency computation */ SignalFrequency = (uint32_t) (CLK_GetClockFreq() / IC1Value); if(SignalFrequency>4999){ GPIO_ToggleBits(GPIOE,GPIO_Pin_7);} } else { SignalDutyCycle = 0; SignalFrequency = 0; } /* Clear TIM1 Capture compare interrupt pending bit */ TIM1_ClearITPendingBit(TIM1_IT_CC2); TIM1_ClearFlag(TIM1_FLAG_CC1); TIM1->CNTRH = 0; TIM1->CNTRL = 0; enableInterrupts();}One last question: what should be the correct value in the IC filter?Thanks,Gilberto #stm8-microcontrollers