请教定时器输入捕获问题

2019-03-23 19:52发布

要测一个输入方波的频率,但是时钟频率好像没有经过分频,测出的数据都是72M时钟的。 而且计数上限跟TIM_BaseInitStructure.TIM_Prescaler 设置的值有关。 具体程序如下,望高手帮忙看看。 /*******************************************************************************
* File Name          : main.c
* Author             : Wuhan R&D Center, Embest
* Date First Issued  : 08/08/2008
* Description        : Main program body
********************************************************************************/ /* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define ADC1_DR_Address    ((u32)0x4001244C) unsigned short int ADC_ConvertedValue;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef   ADC_InitStructure;
DMA_InitTypeDef   DMA_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
ErrorStatus HSEStartUpStatus;
extern vu32 TimingDelay;
int aa=0;
u16 IC2Value[10] = {0};
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void tim2_config(void);   //定义TIM初始化函数
void GPIO_Configuration(void);
void Delay(vu32 nTime);
void SysTick_Configuration(void);
void SetupLED (void) ; 
void TIM2_IRQHandler(void);
extern void SetupADC    (void);
 
/* Private functions ---------------------------------------------------------*/ /*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
  debug();
#endif
  
  /* Configure the system clocks */
  RCC_Configuration();
  SysTick_Configuration();
   
  /* NVIC Configuration */
  tim2_config();   //定时器2配置
  NVIC_Configuration();   /* Configure the GPIO ports */
  GPIO_Configuration();
 
  /* Connect EXTI Line9 to PB.9 */
  //GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8);   /* Configure EXTI Line9 to generate an interrupt on falling edge */ 
  //EXTI_InitStructure.EXTI_Line = EXTI_Line8;
  //EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  //EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  //EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  //EXTI_Init(&EXTI_InitStructure);    for(;;)
   {
 //GPIOB->ODR = 0xfffffc4f;
 GPIOB->BSRR = GPIO_Pin_9;
 Delay(100);
 GPIOB->BRR = GPIO_Pin_9;
 Delay(100);
 //aa++;
 //GPIOB->ODR = 0xfffffc8f;
 //Delay(800);
 //GPIOB->ODR = 0xfffffd0f;
 //Delay(800);
 //GPIOB->ODR = 0xfffffe0f;
 //Delay(800);
 }   } /*******************************************************************************
* Function Name  : SysTick_Configuration
* Description    : Configures the SysTick to generate an interrupt each 1 millisecond.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SysTick_Configuration(void)
{
  /* Select AHB clock(HCLK) as SysTick clock source */
  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);   /* Set SysTick Priority to 3 */
  NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
  
  /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
  SysTick_SetReload(72000);   /* Enable the SysTick Interrupt */
  SysTick_ITConfig(ENABLE);
}   /*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();   /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);   /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();   if(HSEStartUpStatus == SUCCESS)
  {
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);     /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);     /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);     /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);     /* Enable PLL */
    RCC_PLLCmd(ENABLE);     /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }     /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);     /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
  
  /* Enable GPIOB, GPIOC and AFIO clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
                         RCC_APB2Periph_AFIO, ENABLE);
}
void tim2_config(void) {    
   TIM_TimeBaseInitTypeDef   TIM_BaseInitStructure;//定时器初始化结构
   TIM_ICInitTypeDef TIM_ICInitStructure;
   TIM_DeInit(TIM2);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
   TIM_BaseInitStructure.TIM_Prescaler = 999;      //分频系数
   TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_BaseInitStructure.TIM_Period = 0xFFFF;           //计数频率 
   TIM_BaseInitStructure.TIM_ClockDivision = 0; 
  
   TIM_TimeBaseInit(TIM2, &TIM_BaseInitStructure);   //时钟基本初始化
      
   //TIM_PrescalerConfig(TIM2, 4, TIM_PSCReloadMode_Immediate);
  
      //TIM_ICInitStructure.TIM_ICMode = 2;  //配置为输入捕获模式
   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_ICInit(TIM2, &TIM_ICInitStructure);     //捕获设置初始化    TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);        /* Select the TIM2 Input Trigger: TI2FP2 */
   TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);                       //复位模式为从模式
   TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);          //使能主从模式
   TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);          //使能CC2中断请求
   TIM_Cmd(TIM2, ENABLE);                           //使能TIM2计数器
  
}   /*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void) {    NVIC_InitTypeDef NVIC_InitStructure;
   #ifdef   VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */    NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);    #else   /* VECT_TAB_FLASH   */ /* Set the Vector Table base location at 0x08000000 */    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);    #endif      /* Configure the NVIC Preemption Priority Bits */     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);      /* Enable the USART1 Interrupt */    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    NVIC_Init(&NVIC_InitStructure); }
/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* Configure PC. as Output push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
   
  /* Configure PB9 as input floating (EXTI Line9) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOB, &GPIO_InitStructure);   /* Configure PA3 as input floating (EXTI Line9) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure); }
/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nTime: specifies the delay time length, in milliseconds.
* Output         : None
* Return         : None
*******************************************************************************/
void Delay(u32 nTime)
{
  /* Enable the SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Enable);
 
  TimingDelay = nTime;   while(TimingDelay != 0);   /* Disable the SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Disable);
  /* Clear the SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Clear);
} /*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nCount: specifies the delay time length.
* Output         : None
* Return         : None
*******************************************************************************/
/*
void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
} */ void TIM2_IRQHandler(void) {
    /* Clear TIM2 Capture compare interrupt pending bit */
  if(TIM_GetITStatus(TIM2,TIM_IT_CC2)!=RESET)
  {
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
  /* Get the Input Capture value */
  if(aa!=10)
  {
   IC2Value[aa] = TIM_GetCapture2(TIM2); 
   //IC2Value[aa] = TIM2->CCR2;
 aa++;
 }
  else
  {
   aa=0;
 }
  }
}     #ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 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 ", file, line) */   /* Infinite loop */
  while (1)
  {
  }
}
#endif /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/ 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。