AnsweredAssumed Answered

problems with input capture timer1 for stm32f103rc

Question asked by mi.se on Dec 27, 2012
                                   
           
Input capture timer 1 
           
Hi All, 
           
            I want to capture a signal with 600Hz->3MHz frequency by a timer input capture. Frequency of the input pulse is generated by Timer4 pin PB8. Input capture on PIN PA11  of Timer1. Frequency is displayed up teminal through the UART1 interface. But I do not get the frequency displayed on the teminal. Please help me
           
           
            #include "stm32f10x.h"
            #include <stdio.h>
            #include "stm32f10x_it.h"
           
           
            __IO uint32_t IC2ReadValue1 = 0, IC2ReadValue2 = 0;
            __IO uint16_t CaptureNumber = 0;
            __IO uint32_t Capture = 0;
            __IO uint32_t TIM1Freq = 0;
           
           
            #ifdef __GNUC__
             
              #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
            #else
              #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
            #endif /* __GNUC__ */
           
           
            GPIO_InitTypeDef GPIO_InitStructure;
            USART_InitTypeDef USART_InitStructure;
            TIM_ICInitTypeDef  TIM_ICInitStructure;
            TIM_OCInitTypeDef  TIM_OCInitStructure;
            TIM_TimeBaseInitTypeDef      TIM_TimeBaseStructure;
            NVIC_InitTypeDef       NVIC_InitStructure;
           
            void RCC_Configuration(void);
            void GPIO_Configuration(void);
            void NVIC_Configuration(void);
            void USART_Configuration(void);
            void TIMER4_Configuation(void);
            void TIM1_IRQHandler(void);
            void Delay(__IO uint32_t num)
            {
                 __IO uint32_t index = 0;
           
           
                 for(index = (720000 * num); index != 0; index--)
                 {
                 }
            }
           
            int main(void)
            {
              RCC_Configuration();
              NVIC_Configuration();
              GPIO_Configuration();
                 USART_Configuration();
              
                 TIM_TimeBaseStructure.TIM_Prescaler = 0;
                 TIM_TimeBaseStructure.TIM_Period = 65535;
                 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
                 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
                 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
                 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
           
                    TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
                    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_ICInit(TIM1, &TIM_ICInitStructure);
                             
                 TIM_CCxCmd(TIM1,TIM_Channel_4,ENABLE);
                    TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
                 TIM_Cmd(TIM1, ENABLE);
                 
                 TIMER4_Configuation();
                 TIM1_IRQHandler();
           
              while (1)
              {
              
              }
            }
           
           
            PUTCHAR_PROTOTYPE
            {
           
                 USART_SendData(USART1, (uint8_t) ch);
                 while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
                 {}
                 return ch;
            }     
           
            void RCC_Configuration(void)
            {
                 RCC->APB2ENR |= RCC_APB2Periph_AFIO; 
                 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE)
                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
                 
                 RCC_PCLK1Config(RCC_HCLK_Div4);
                 RCC_PCLK2Config(RCC_HCLK_Div2);
            }
           
            void GPIO_Configuration(void)
            {
             
                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                 GPIO_Init(GPIOA, &GPIO_InitStructure);
                 
                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                 GPIO_Init(GPIOB, &GPIO_InitStructure);
                 GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
                 GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
            }
           
            void NVIC_Configuration(void)
            {
              NVIC_InitTypeDef NVIC_InitStructure;
              /* Enable the TIM3 global Interrupt */
              NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
                 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
                 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                 NVIC_Init(&NVIC_InitStructure);
            }
           
            void USART_Configuration(void)
            {
                 /* Configure PA9 for USART Tx as alternate function push-pull */
                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                 GPIO_Init(GPIOA, &GPIO_InitStructure);
                 /* Configure PA10 for USART Rx as input floating */
                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                 GPIO_Init(GPIOA, &GPIO_InitStructure);
                 /* usart configuration */
                 USART_InitStructure.USART_BaudRate = 115200;
                 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
                 USART_InitStructure.USART_StopBits = USART_StopBits_1;
                 USART_InitStructure.USART_Parity = USART_Parity_No;
                 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
                 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;     
                 /* USART configuration */
                 USART_Init(USART1, &USART_InitStructure);
                 /* Enable USART */
                 USART_Cmd(USART1, ENABLE);
            }
           
            void TIMER4_Configuation(void)
            {
                 TIM_TimeBaseStructure.TIM_Prescaler = 0;
                 TIM_TimeBaseStructure.TIM_Period = 20;
                 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
                 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
                 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
           
                 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
                 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
                 TIM_OCInitStructure.TIM_Pulse = 10;
                 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
           
                 TIM_OC3Init(TIM4, &TIM_OCInitStructure);
                 TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);          
           
                 TIM_ARRPreloadConfig(TIM4, ENABLE);
                 TIM_Cmd(TIM4, ENABLE);
            }
           
            void TIM1_IRQHandler(void)
            { 
              if(TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET) 
              {
                /* Clear TIM3 Capture compare interrupt pending bit */
                      TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);
                if(CaptureNumber == 0)
                {
                           
                  /* Get the Input Capture value */
                  IC2ReadValue1 = TIM_GetCapture2(TIM1);
                  CaptureNumber = 1;
                }
                else if(CaptureNumber == 1)
                {
                  /* Get the Input Capture value */
                  IC2ReadValue2 = TIM_GetCapture2(TIM1); 
                  
                  /* Capture computation */
                  if (IC2ReadValue2 > IC2ReadValue1)
                  {
                    Capture = ((uint32_t)IC2ReadValue2 - (uint32_t)IC2ReadValue1);                     
                  }
                  else
                  {
                    Capture = ((uint32_t)(0x10000 - IC2ReadValue1) + (uint32_t)IC2ReadValue2);
                                
                  }
                  /* Frequency computation */ 
                  TIM1Freq = (uint32_t) 72000000 /(Capture-1);
                           CaptureNumber = 0;
                           printf("Capture:%d-Tan so:%d\n\r ", Capture,TIM1Freq) ;
                           Delay(5);
                                 
                }
                      
                      TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);
              }
            }
           
            #ifdef  USE_FULL_ASSERT
           
           
            /**
              * @brief  Reports the name of the source file and the source line number
              *         where the assert_param error has occurred.
              * @param  file: pointer to the source file name
              * @param  line: assert_param error line source number
              * @retval None
              */
            void assert_failed(uint8_t* file, uint32_t line)
            {
              /* User can add his own implementation to report the file name and line number,
                 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
           
              while (1)
              {}
            }
           
            Link project http://www.mediafire.com/?9pgprxiums8lcg1
           
           

Outcomes