void Freq_Meter_Init(void)
{      
  /* TIM Configuration */
  TIM_Config();

  /* TIM4 configuration: PWM Input mode ------------------------
     The external signal is connected to TIM4 CH2 pin (PB.07), 
     The Rising edge is used as active edge,
     The TIM4 CCR2 is used to compute the frequency value 
     The TIM4 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(TIM4, &TIM_ICInitStructure);

  /* Select the TIM4 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM4, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);
}
Example #2
0
void config_tim3( void )
{
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_ICInitTypeDef  TIM_ICInitStructure;

	/* Enable the TIM3 global Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	TIM_ICStructInit( &TIM_ICInitStructure );
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
	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(TIM3, &TIM_ICInitStructure);

	/* Select the TIM3 Input Trigger: TI2FP1 */
	TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);

	/* Select the slave Mode: Reset Mode */
	TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

	/* Enable the Master/Slave Mode */
	TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

	/* TIM enable counter */
	TIM_Cmd(TIM3, ENABLE);

	/* Enable the CC1 Interrupt Request */
	TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
}
Example #3
0
// 计时器设置为PWM捕获器,此时只能捕获1路
// eg: TIM5, CH2, 捕获PA1
void Timer::mode_pwm_input(PinTypedef p) {
  Pin pin(p); // 函数调用后变量就消失了
  uint8_t tmp = this->GPIO_AF_TIM(this->TIM);
  // 看看tmp有没有问题
  pin.mode_pwm_input(tmp);

  NVIC_InitTypeDef NVIC_InitStructure;  // 中断初始化器
  NVIC_InitStructure.NVIC_IRQChannel = this->IRQn;
  // 低频高优先级原则,20mS的捕获中断设为最高优先级
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ 通道使能
  NVIC_Init(&NVIC_InitStructure);

  // 时钟预分频
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructrue; // 计时器分频初始化器
  TIM_TimeBaseStructrue.TIM_Prescaler = PRESCALER;
  TIM_TimeBaseStructrue.TIM_Prescaler = PRESCALER;
  switch (this->TIM_No) {
    case (1):
    case (8):
    case (9):
    case (10):
    case (11): {
      TIM_TimeBaseStructrue.TIM_Prescaler = PRESCALER_APB2;
      break;
    }
  }
  TIM_TimeBaseStructrue.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructrue.TIM_Period = PERIOD;
  TIM_TimeBaseStructrue.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseInit(this->TIM, &TIM_TimeBaseStructrue);

  // 设置TIM5 CH2 pin为外部信号输入
  // CCR2测频率
  // CCR1测占空比
  TIM_ICInitTypeDef TIM_ICInitStructure; // 计时器模块初始化器
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; // input channel 2
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; // 上升沿中断
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // map IC1 to TI1
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // 输入分频,不分频
  TIM_ICInitStructure.TIM_ICFilter = 0x00; //滤波设置,经历几个周期跳变认定波形稳定0x0~0xF

  TIM_PWMIConfig(this->TIM, &TIM_ICInitStructure);

  /* Select the TIM5 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(this->TIM, TIM_TS_TI2FP2);

  TIM_SelectSlaveMode(this->TIM,TIM_SlaveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
  TIM_SelectMasterSlaveMode(this->TIM,TIM_MasterSlaveMode_Enable); //启动定时器的被动触发

  TIM_Cmd(this->TIM, ENABLE); // run TIM5
  // 设置中断ISR为PWM Input模式中断
  // this->set_IRQHandler(Timer::PWM_Input_Handler_Dispatch);
  if ((TIM_No==1)||(TIM_No==8))
    TIM_ITConfig(this->TIM, TIM_IT_Update, ENABLE); // 打开中断,不要
  else
    TIM_ITConfig(this->TIM, TIM_IT_CC2, ENABLE); // 打开中断,TIM_IT_Update不要
}
Example #4
0
void TIM4_Init(void)
{
	 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef  				TIM_ICInitStructure;
  GPIO_InitTypeDef					GPIO_InitStructure;
  NVIC_InitTypeDef 					NVIC_InitStructure;

  /* TIM4 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  /* GPIOB clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  
  /* TIM4 chennel2 configuration : PB.07 */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* Connect TIM pin to AF2 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
	
	TIM_TimeBaseStructure.TIM_Period = 0xffff; //设置自动重装载寄存期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =83; //设置用来作为TIMx时钟频率除数的预分频值  1Mhz的计数频率  
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
	
  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_DIV4;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_PWMIConfig(TIM4, &TIM_ICInitStructure);

  /* Select the TIM4 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM4, ENABLE);

  /* Enable the CC2 Interrupt Request */

    /* Enable the TIM4 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

}
//--------------------------------------------------------------
// interne Funktion
// Init vom Timer
//--------------------------------------------------------------
void P_ICPWM_InitTIM(void)
{
  TIM_ICInitTypeDef  TIM_ICInitStructure;

  // Clock enable
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 

  // Vorteiler einstellen
  TIM_PrescalerConfig(TIM2, ICPWM_TIM2_PRESCALE, TIM_PSCReloadMode_Immediate);
  
  if(ICPWM_TIM2.CHANNEL==1) {
    // Channel 1
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    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);

    // input Trigger
    TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);      
  }
  if(ICPWM_TIM2.CHANNEL==2) {
    // Channel 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_PWMIConfig(TIM2, &TIM_ICInitStructure);

    // input Trigger
    TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
  }

  // Slave-Mode (Reset)
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);

  // Timer enable
  TIM_Cmd(TIM2, ENABLE);
}
Example #6
0
//timer 1
void pwmInit(void) 
{
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_ICInitTypeDef  TIM_ICInitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    pwmSetConstants();

    // TIM1 channel 1 pin (PA.08) configuration
    GPIO_InitStructure.GPIO_Pin = PWM_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(PWM_PORT, &GPIO_InitStructure);

    // Enable the TIM1 global Interrupt
    NVIC_InitStructure.NVIC_IRQChannel = PWM_IRQ;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);



    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Prescaler = (PWM_CLK_DIVISOR-1);
    TIM_TimeBaseStructure.TIM_Period = 0xffff;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(PWM_TIM, &TIM_TimeBaseStructure);

    TIM_ICInitStructure.TIM_Channel = PWM_CHANNEL;
    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(PWM_TIM, &TIM_ICInitStructure);


    // Select the TIM Input Trigger: TI1FP1
	// 滤波后的定时器输入1(TI1FP1) 
    TIM_SelectInputTrigger(PWM_TIM, TIM_TS_TI1FP1);

    // Select the slave Mode: Reset Mode
    TIM_SelectSlaveMode(PWM_TIM, TIM_SlaveMode_Reset);//复位模式

    // Enable the Master/Slave Mode
    TIM_SelectMasterSlaveMode(PWM_TIM, TIM_MasterSlaveMode_Enable);

    // TIM enable counter
    TIM_Cmd(PWM_TIM, ENABLE);

    pwmIsrAllOn();
}
Example #7
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main. 
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */
       
  /* TIM Configuration */
  TIM_Config();

  
  /* --------------------------------------------------------------------------- 
    TIM4 configuration: PWM Input mode

    In this example TIM4 input clock (TIM4CLK) is set to 2 * APB1 clock (PCLK1), 
    since APB1 prescaler is different from 1.   
      TIM4CLK = 2 * PCLK1  
      PCLK1 = HCLK / 4 
      => TIM4CLK = HCLK / 2 = SystemCoreClock /2

    External Signal Frequency = TIM4 counter clock / TIM4_CCR2 in Hz. 

    External Signal DutyCycle = (TIM4_CCR1*100)/(TIM4_CCR2) in %.

  --------------------------------------------------------------------------- */
  
  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(TIM4, &TIM_ICInitStructure);

  /* Select the TIM4 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM4, ENABLE);

  /* Enable the CC2 Interrupt Request */
   TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);

  while (1);
}
/*采用PA.0 作为外部脉冲计数*/
void pulse_init( void )
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	NVIC_InitTypeDef	NVIC_InitStructure;
	TIM_ICInitTypeDef	TIM_ICInitStructure;

	/* TIM5 clock enable */
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM5, ENABLE );

	/* GPIOA clock enable */
	RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE );

	/* TIM5 chennel1 configuration : PA.0 */
	GPIO_InitStructure.GPIO_Pin		= GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType	= GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd	= GPIO_PuPd_UP;
	GPIO_Init( GPIOA, &GPIO_InitStructure );

	/* Connect TIM pin to AF0 */
	GPIO_PinAFConfig( GPIOA, GPIO_PinSource0, GPIO_AF_TIM5 );

	/* Enable the TIM5 global Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel						= TIM5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority	= 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority			= 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd					= ENABLE;
	NVIC_Init( &NVIC_InitStructure );

	TIM_ICInitStructure.TIM_Channel		= TIM_Channel_1;
	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( TIM5, &TIM_ICInitStructure );

	/* Select the TIM5 Input Trigger: TI1FP1 */
	TIM_SelectInputTrigger( TIM5, TIM_TS_TI1FP1 );

	/* Select the slave Mode: Reset Mode */
	TIM_SelectSlaveMode( TIM5, TIM_SlaveMode_Reset );
	TIM_SelectMasterSlaveMode( TIM5, TIM_MasterSlaveMode_Enable );

	/* TIM enable counter */
	TIM_Cmd( TIM5, ENABLE );

	/* Enable the CC2 Interrupt Request */
	TIM_ITConfig( TIM5, TIM_IT_CC2, ENABLE );
}
Example #9
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System Clocks Configuration */
  RCC_Configuration();

  /* NVIC configuration */
  NVIC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* TIM3 configuration: PWM Input mode ------------------------
     The external signal is connected to TIM3 CH2 pin (PA.01), 
     The Rising edge is used as active edge,
     The TIM3 CCR2 is used to compute the frequency value 
     The TIM3 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(TIM3, &TIM_ICInitStructure);

  /* Select the TIM3 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM3, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);

  while (1);
}
Example #10
0
void TIM2_PWMINPUT_INIT(u16 arr,u16 psc)
{
  
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;        //TIM???????
        NVIC_InitTypeDef NVIC_InitStructure;                        //????
        TIM_ICInitTypeDef  TIM2_ICInitStructure;                 //TIM2  PWM?????
        GPIO_InitTypeDef GPIO_InitStructure;                         //IO??????
 
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);     //Open TIM2 clock
 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //open gpioB clock
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | RCC_APB2Periph_AFIO, ENABLE);  //??GPIO???AFIO????????
 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);          //??JTAG
         GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer2?????  TIM2_CH2->PB3

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;             //GPIO 3
        GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IPU;          //???? ????
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
 
        TIM_TimeBaseStructure.TIM_Period = arr; //???????????????????????????  
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //??????TIMx???????????  
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //??????:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM??????
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //??TIM_TimeBaseInitStruct?????????TIMx???????
 
        
        /*???????*/
        NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;                     
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  TIM2_ICInitStructure.TIM_Channel = TIM_Channel_2;                   
  TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;       
  TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;   
  TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 
  TIM2_ICInitStructure.TIM_ICFilter = 0x3;   //Filter:??
 
  TIM_PWMIConfig(TIM2, &TIM2_ICInitStructure);     //PWM????           
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);     //???????        
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);  //?????????
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);//??????????                                       
  TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_Update, ENABLE);          //????
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC2|TIM_IT_Update); //???????
  TIM_Cmd(TIM2, ENABLE);    
}
Example #11
0
void TIM2_Init(void)
{
	TIM_ICInitTypeDef TIM_ICInitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
	TIM_ICInitStructure.TIM_ICFilter = 0x00;
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM_PWMIConfig(TIM2,&TIM_ICInitStructure);
	
	TIM_SelectInputTrigger(TIM2,TIM_TS_TI2FP2);
	
	TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
	
	TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
	TIM_Cmd(TIM2, ENABLE);
}
Example #12
0
/********************************************************************************
 * FunctionName: MyTIM1CaptureInit
 *
 * Description :
 *
 * Parameters  :
 *
 * Returns     :
 *******************************************************************************/
void MyTIM1CaptureInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_ICInitTypeDef TIM_ICInitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
    TIM_InternalClockConfig(TIM1);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  //推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /**
     * 计数0-0xFFFF,分频71。则周期为1S,溢出一次时间为1uS*0xFFFF=65ms
     */
    TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF;                  // 计数周期
    TIM_TimeBaseInitStructure.TIM_Prescaler = 71;                   // 72MHz / (71+1) = 1000KHz = 1M -->1uS
    TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // 上升计数
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
    TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);                     // 禁止溢出中断
    TIM_ClearFlag(TIM1, TIM_FLAG_Update);

    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;                // 通道1输入捕获
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;     // 下降沿触发
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;           // 每次检测到都触发一次捕获
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; // TI1被连接到IC1
    TIM_ICInitStructure.TIM_ICFilter = 0x0;
    TIM_PWMIConfig(TIM1, &TIM_ICInitStructure);

    TIM_SelectInputTrigger(TIM1, TIM_TS_TI1FP1);                    // 选择触发信号
    TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);                 // 选中触发信号的下降沿重新初始化计数器并触发寄存器更新
    TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);    // 使能主从模式
    TIM_ITConfig(TIM1, TIM_IT_CC1, DISABLE);
    TIM_ClearFlag(TIM1, TIM_FLAG_CC1);
    TIM_Cmd(TIM1, ENABLE);
    TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
}
Example #13
0
void Tim1_cfg1()
{
   TIM_ICInitTypeDef  TIM_ICInitStructure;
   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

   /* TIM3 configuration: PWM Input mode ------------------------
      The external signal is connected to TIM4 CH2 pin (PB.07), 
      The Rising edge is used as active edge,
      The TIM4 CCR2 is used to compute the frequency value 
      The TIM4 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(TIM1, &TIM_ICInitStructure);
   
   TIM_TimeBaseStructure.TIM_Period = 0xFFFF;     //周期0~FFFF
   TIM_TimeBaseStructure.TIM_Prescaler = 32-1;    //时钟分频,分频数为4分频(尽可以测0~2.7ms的脉宽,故仅适用于舵机信号解码)
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;   //时钟分割
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//模式
   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);//基本初始化
 
   /* Select the TIM3 Input Trigger: TI2FP2 */
   TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);//设置IC2为中断触发源,这个导致一个定时器只能捕捉1个PWM
 
   /* Select the slave Mode: Reset Mode */
   TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);
 
   /* Enable the Master/Slave Mode */
   TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
 
   /* TIM enable counter */
   TIM_Cmd(TIM1, ENABLE);
 
   /* Enable the CC2 Interrupt Request */
   TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
}
Example #14
0
/**
  * @brief  Initialize the RC5 decoder module ( Time range)
  * @param  None
  * @retval None
  */
void RC5_Init(void)
{ 
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  TIM_ICInitTypeDef TIM_ICInitStructure;
  
  /*  Clock Configuration for TIMER */
  RCC_APB1PeriphClockCmd(IR_TIM_CLK , ENABLE);

  /* Enable Button GPIO clock */
  RCC_AHBPeriphClockCmd(IR_GPIO_PORT_CLK , ENABLE);
 
  /* Pin configuration: input floating */
  GPIO_InitStructure.GPIO_Pin = IR_GPIO_PIN;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(IR_GPIO_PORT, &GPIO_InitStructure);
  
  GPIO_PinAFConfig( IR_GPIO_PORT,IR_GPIO_SOURCE,GPIO_AF_2);
  
  /* Enable the TIM global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = IR_TIM_IRQn ;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
    
  /* TIMER frequency input */
  TIM_PrescalerConfig(IR_TIM, TIM_PRESCALER, TIM_PSCReloadMode_Immediate);
  
  TIM_ICStructInit(&TIM_ICInitStructure);
  
  /* TIM configuration */
  TIM_ICInitStructure.TIM_Channel = IR_TIM_Channel;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;
  
  TIM_PWMIConfig(IR_TIM, &TIM_ICInitStructure); 

  /* Timer Clock */
  TIMCLKValueKHz = TIM_GetCounterCLKValue()/1000; 

  /* Select the TIM Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(IR_TIM, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(IR_TIM, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(IR_TIM, TIM_MasterSlaveMode_Enable);

  /* Configures the TIM Update Request Interrupt source: counter overflow */
  TIM_UpdateRequestConfig(IR_TIM,  TIM_UpdateSource_Regular);
   
  RC5TimeOut = TIMCLKValueKHz * RC5_TIME_OUT_US/1000;

  /* Set the TIM auto-reload register for each IR protocol */
  IR_TIM->ARR = RC5TimeOut;
  
  /* Clear update flag */
  TIM_ClearFlag(IR_TIM, TIM_FLAG_Update);

  /* Enable TIM Update Event Interrupt Request */
  TIM_ITConfig(IR_TIM, TIM_IT_Update, ENABLE);
    
  /* Enable the CC2/CC1 Interrupt Request */
  TIM_ITConfig(IR_TIM, TIM_IT_CC2, ENABLE);
    /* Enable the CC2/CC1 Interrupt Request */
  TIM_ITConfig(IR_TIM, TIM_IT_CC1, ENABLE);

  /* Enable the timer */
  TIM_Cmd(IR_TIM, ENABLE);  
  
  if (CECDemoStatus == 0)
  {
    /* Set the LCD Back Color */
    LCD_SetBackColor(LCD_COLOR_RED);
    
    /* Set the LCD Text Color */
    LCD_SetTextColor(LCD_COLOR_GREEN);    
    LCD_DisplayStringLine(LCD_LINE_0, "   STM320518-EVAL   ");
    LCD_DisplayStringLine(LCD_LINE_1, " RC5 InfraRed Demo  ");
    LCD_SetBackColor(LCD_COLOR_BLUE);
    
    /* Set the LCD Text Color */
    LCD_SetTextColor(LCD_COLOR_WHITE);  
  }
  
  /* Bit time range */
  RC5MinT = (RC5_T_US - RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000;
  RC5MaxT = (RC5_T_US + RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000;
  RC5Min2T = (2 * RC5_T_US - RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000;
  RC5Max2T = (2 * RC5_T_US + RC5_T_TOLERANCE_US) * TIMCLKValueKHz / 1000;
  
  /* Default state */
  RC5_ResetPacket();
}
Example #15
0
/**
  * @brief  Configure the TIM IRQ Handler.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  TIM_ICInitTypeDef  TIM_ICInitStructure;  
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* GPIOB clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  
  /* TIM2 chennel2 configuration : PB.03 */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* Connect TIM pin to AF2 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_2);

  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
   /* --------------------------------------------------------------------------- 
    TIM2 configuration: PWM Input mode
     The external signal is connected to TIM2 CH2 pin (PB.03)
     TIM2 CCR2 is used to compute the frequency value 
     TIM2 CCR1 is used to compute the duty cycle value

    In this example TIM2 input clock (TIM2CLK) is set to APB1 clock (PCLK1), since
    APB1 prescaler is set to 1.
      TIM2CLK = PCLK1 = HCLK = SystemCoreClock

    External Signal Frequency = SystemCoreClock / TIM2_CCR2 in Hz.
    External Signal DutyCycle = (TIM2_CCR1*100)/(TIM2_CCR2) in %.
  Note: 
  SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
  Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
  function to update SystemCoreClock variable value. Otherwise, any configuration
  based on this variable will be incorrect.
  --------------------------------------------------------------------------- */
  
  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_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM2, ENABLE);

  /* Enable the CC2 Interrupt Request */
   TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
}
Example #16
0
void CONF_TIMERS(void)
{
	  GPIO_InitTypeDef GPIO_InitStructure;
	   NVIC_InitTypeDef NVIC_InitStructure;

	 RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB| RCC_AHBPeriph_GPIOE, ENABLE);

	  /* GPIOA Configuration: Channel 1, 2, 3 and 4 as alternate function push-pull */
	  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14;
	  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
	  GPIO_Init(GPIOE, &GPIO_InitStructure);

	  GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_2);
	  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_2);
	  GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_2);
	  GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_2);


	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
	  TIM_TimeBaseStructure.TIM_Prescaler = 10;
	  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
	  TIM_TimeBaseStructure.TIM_Period = 60000;
	  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
	  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

	  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
	  TIM_OCInitStructure.TIM_Pulse = 3400;
	  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
	  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
	  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
	  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
	  TIM_OCInitStructure.TIM_Pulse = 3400;

	  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
	  TIM_OCInitStructure.TIM_Pulse = 3400;
	  TIM_OC3Init(TIM1, &TIM_OCInitStructure);
	  TIM_OCInitStructure.TIM_Pulse = 3400;
	  TIM_OC4Init(TIM1, &TIM_OCInitStructure);

	  TIM_Cmd(TIM1, ENABLE);
	  TIM_CtrlPWMOutputs(TIM1, ENABLE);
	  ////////////////////////////////////////////////////////////////////////////////////


	   /* TIM2 clock enable */
	   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

	   /* GPIOB clock enable */
	   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

	   /* TIM2 chennel2 configuration : PA.01 */
	   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;
	   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
	   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
	   GPIO_Init(GPIOA, &GPIO_InitStructure);

	   /* Connect TIM pin to AF1 */
	   GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1);

	   TIM_TimeBaseStructure.TIM_Prescaler = 360;
	   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	   TIM_TimeBaseStructure.TIM_Period = 65535;
	   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
	   /* Enable the TIM2 global Interrupt */
	   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	   NVIC_Init(&NVIC_InitStructure);
	   //////
	   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_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);

	   /* TIM enable counter */
	   TIM_Cmd(TIM2, ENABLE);

	   /* Enable the CC2 Interrupt Request */
	    TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);


}
Example #17
0
void CONF_PWMIN(void)
{

	  GPIO_InitTypeDef GPIO_InitStructure;
	   NVIC_InitTypeDef NVIC_InitStructure;

	   /* TIM2 clock enable */
	   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

	   /* GPIOB clock enable */
	   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

	   /* TIM2 chennel2 configuration : PA.01 */
	   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7;
	   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
	   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
	   GPIO_Init(GPIOC, &GPIO_InitStructure);

	   /* Connect TIM pin to AF1 */
	   GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_2);

	   TIM_TimeBaseStructure.TIM_Prescaler = 360;
	   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	   TIM_TimeBaseStructure.TIM_Period = 65535;
	   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	   /* Enable the TIM2 global Interrupt */
	   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	   NVIC_Init(&NVIC_InitStructure);
	   //////
	   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(TIM3, &TIM_ICInitStructure);

	   /* Select the TIM2 Input Trigger: TI2FP2 */
	   TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

	   /* Select the slave Mode: Reset Mode */
	   TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
	   TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);

	   /* TIM enable counter */
	   TIM_Cmd(TIM3, ENABLE);

	   /* Enable the CC2 Interrupt Request */
	    TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);


	    ////TIM4
		   /* TIM2 clock enable */
		   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

		   /* GPIOB clock enable */
		   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);

		   /* TIM2 chennel2 configuration : PA.01 */
		   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13;
		   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
		   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
		   GPIO_Init(GPIOD, &GPIO_InitStructure);

		   /* Connect TIM pin to AF1 */
		   GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_2);

		   TIM_TimeBaseStructure.TIM_Prescaler = 360;
		   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
		   TIM_TimeBaseStructure.TIM_Period = 65535;
		   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
		   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
		   /* Enable the TIM2 global Interrupt */
		   NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
		   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
		   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		   NVIC_Init(&NVIC_InitStructure);
		   //////
		   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(TIM4, &TIM_ICInitStructure);

		   /* Select the TIM2 Input Trigger: TI2FP2 */
		   TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

		   /* Select the slave Mode: Reset Mode */
		   TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
		   TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

		   /* TIM enable counter */
		   TIM_Cmd(TIM4, ENABLE);

		   /* Enable the CC2 Interrupt Request */
		    TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE);

		    ////TIM8
			   /* TIM2 clock enable */
			   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);

			   /* GPIOB clock enable */
			   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

			   /* TIM2 chennel2 configuration : PA.01 */
			   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;
			   GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
			   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
			   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
			   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
			   GPIO_Init(GPIOC, &GPIO_InitStructure);

			   /* Connect TIM pin to AF1 */
			   GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_4);

			   TIM_TimeBaseStructure.TIM_Prescaler = 360;
			   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
			   TIM_TimeBaseStructure.TIM_Period = 65535;
			   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
			   TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
			   /* Enable the TIM2 global Interrupt */
			   NVIC_InitStructure.NVIC_IRQChannel = TIM8_BRK_IRQn;
			   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
			   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
			   NVIC_Init(&NVIC_InitStructure);
			   //////
			   TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
			   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(TIM8, &TIM_ICInitStructure);

			   /* Select the TIM2 Input Trigger: TI2FP2 */
			   TIM_SelectInputTrigger(TIM8, TIM_TS_TI1FP1);

			   /* Select the slave Mode: Reset Mode */
			   TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Reset);
			   TIM_SelectMasterSlaveMode(TIM8,TIM_MasterSlaveMode_Enable);

			   /* TIM enable counter */
			   TIM_Cmd(TIM8, ENABLE);

			   /* Enable the CC2 Interrupt Request */
			    TIM_ITConfig(TIM8, TIM_IT_CC2, ENABLE);
}
Example #18
0
void initPWMInput()
{

	GPIO_InitTypeDef GPIO_InitStructure;

	//TIM3 as PWM input
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	//PA7 as DIR pin
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	//PA5 as ENA pin
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	EXTI_InitTypeDef EXTI_initStructure;
	EXTI_initStructure.EXTI_Line = EXTI_Line5;
	EXTI_initStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_initStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
	EXTI_initStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_initStructure);

	NVIC_InitTypeDef nvicStructure;
	nvicStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
	nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
	nvicStructure.NVIC_IRQChannelSubPriority = 2;
	nvicStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&nvicStructure);


	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
	TIM_TimeBaseStructure.TIM_Prescaler = 1;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period = 65535;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);



	TIM_ICInitTypeDef TIM_ICInit;
#if STEP_POLARITY == 1
	TIM_ICInit.TIM_ICPolarity = TIM_ICPolarity_Rising;
#else
	TIM_ICInit.TIM_ICPolarity = TIM_ICPolarity_Falling;
#endif
	TIM_ICInit.TIM_ICFilter = 5;
	TIM_ICInit.TIM_Channel = TIM_Channel_1;
	TIM_ICInit.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM_ICInit.TIM_ICSelection = TIM_ICSelection_DirectTI;

	TIM_PWMIConfig(TIM3, &TIM_ICInit);
	TIM_CCxCmd(TIM3, TIM_Channel_1, ENABLE);
	TIM_CCxCmd(TIM3, TIM_Channel_2, ENABLE);

	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
	TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
	TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_Update, ENABLE);
	TIM_Cmd(TIM3,ENABLE);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}
/**
  * @brief  Calibration of External crystal oscillator auto(through Timer
  *   
  * @param  None
  * @retval : None
  */
void AutoClockCalibration(void)
{
  RCC_ClocksTypeDef ClockValue;
  uint16_t TimerPrescalerValue=0x0003;
  uint16_t CountWait;
  uint16_t DeviationInteger;
  uint32_t CalibrationTimer;
  float f32_Deviation;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
  
  TIM_DeInit(TIM2);
  BKP_TamperPinCmd(DISABLE);
  BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
  
  /* 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_PrescalerConfig(TIM2,TimerPrescalerValue,TIM_PSCReloadMode_Immediate);
  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 = 0x00;
  TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
  TIM_ICInit(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);
  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
  /* TIM enable Counter */
  TIM_Cmd(TIM2, ENABLE);
  LCD_Clear(Blue2);
  LCD_DisplayString(Line4,Column1,"Please Wait.....");
  /* Wait for 2 seconds */
  CalibrationTimer = RTC_GetCounter();
 
  while((RTC_GetCounter() - CalibrationTimer) < 2)
  {
  }
  
  RCC_GetClocksFreq(&ClockValue);
  TimerFrequency=(ClockValue.PCLK1_Frequency * 2)/(TimerPrescalerValue+1);
   /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
   /* Wait for 2 seconds */
  CalibrationTimer = RTC_GetCounter();
  
  while((RTC_GetCounter() - CalibrationTimer) < 2)
  {
  }

  if(!(TIM_GetFlagStatus(TIM2, TIM_FLAG_CC1)))
   /* There is no signal at the timer TIM2 peripheral input */
  {
    LCD_Clear(Blue2);
    LCD_DisplayString(Line3,Column0,"Please connect wire");
    LCD_DisplayString(Line4,Column0,"link between PC13");
    LCD_DisplayString(Line5,Column0,"and PA1");
    LCD_DisplayString(Line7,Column0,"No calibration done");
  }
  else
  {
    /* Calulate Deviation in ppm  using the formula :
    Deviation in ppm = (Deviation from 511.968/511.968)*1 million*/
    if(f32_Frequency > 511.968)
    {
      f32_Deviation=((f32_Frequency-511.968)/511.968)*1000000;
    }
    else
    {
      f32_Deviation=((511.968-f32_Frequency)/511.968)*1000000;
    }
     DeviationInteger = (uint16_t)f32_Deviation;
    
    if(f32_Deviation >= (DeviationInteger + 0.5))
    {
      DeviationInteger = ((uint16_t)f32_Deviation)+1;
    }
    
   CountWait=0;

   /* Frequency deviation in ppm should be les than equal to 121 ppm*/
   if(DeviationInteger <= 121)
   {
     while(CountWait<128)
     {
       if(CalibrationPpm[CountWait] == DeviationInteger)
       break;
       CountWait++;
     }

     BKP_SetRTCCalibrationValue(CountWait);
     LCD_Clear(Blue2);
     LCD_DisplayString(Line4,Column1,"Calibration Value");
     LCD_DisplayChar(Line5,Column10,(CountWait%10)+0x30);
     CountWait=CountWait/10;
     LCD_DisplayChar(Line5,Column9,(CountWait%10)+0x30);
     CountWait=CountWait/10;
   
     if(CountWait>0)
     {
       LCD_DisplayChar(Line5,Column8,(CountWait%10)+0x30);
     }
   }
   else /* Frequency deviation in ppm is more than 121 ppm, hence calibration
           can not be done */
   {
     LCD_Clear(Blue2);
     LCD_DisplayString(Line3,Column1,"Out Of Calibration");
     LCD_DisplayString(Line4,Column4,"Range");
   }
  }
  
  BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
  TIM_ITConfig(TIM2, TIM_IT_CC2, DISABLE);
  TIM_Cmd(TIM2, DISABLE);
  TIM_DeInit(TIM2);
  CalibrationTimer=RTC_GetCounter();
  
  /*  Wait for 2 seconds  */
  while((RTC_GetCounter() - CalibrationTimer) < 5)
  {
  }
  
  MenuInit();
}
Example #20
0
/*******************************************************************************
* Function Name  : AutoClockCalibration
* Description    : Calibration of External crystal oscillator auto(through Timer
                   peripheral)
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void AutoClockCalibration(void)
{
  RCC_ClocksTypeDef ClockValue;
  u16 u16_TimerPrescalerValue=0x0003;
  u16 u16_CountWait;
  u16 u16_DeviationInteger;
  u32 u32_CalibrationTimer;
  float f32_Deviation;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
  
  TIM_DeInit(TIM2);
  BKP_TamperPinCmd(DISABLE);
  BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
  
  /* 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_PrescalerConfig(TIM2,u16_TimerPrescalerValue,TIM_PSCReloadMode_Immediate);
  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 = 0x00;
  TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
  TIM_ICInit(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);
  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
  /* TIM enable u16_Counter */
  TIM_Cmd(TIM2, ENABLE);

  /* Wait for 2 seconds */
  u32_CalibrationTimer = RTC_GetCounter();
 
  while((RTC_GetCounter() - u32_CalibrationTimer) < 2)
  {
  }
  
  RCC_GetClocksFreq(&ClockValue);
  u32_TimerFrequency=(ClockValue.PCLK1_Frequency * 2)/(u16_TimerPrescalerValue+1);
   /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
   /* Wait for 2 seconds */
  u32_CalibrationTimer = RTC_GetCounter();
  
  while((RTC_GetCounter() - u32_CalibrationTimer) < 2)
  {
  }

  if(!(TIM_GetFlagStatus(TIM2, TIM_FLAG_CC1)))
   /* There is no signal at the timer TIM2 peripheral input */
  {

  }
  else
  {
    /* Calulate Deviation in ppm  using the formula :
    Deviation in ppm = (Deviation from 511.968/511.968)*1 million*/
    if(f32_Frequency > 511.968)
    {
      f32_Deviation=((f32_Frequency-511.968)/511.968)*1000000;
    }
    else
    {
      f32_Deviation=((511.968-f32_Frequency)/511.968)*1000000;
    }
     u16_DeviationInteger = (u16)f32_Deviation;
    
    if(f32_Deviation >= (u16_DeviationInteger + 0.5))
    {
      u16_DeviationInteger = ((u16)f32_Deviation)+1;
    }
    
   u16_CountWait=0;

   /* Frequency deviation in ppm should be les than equal to 121 ppm*/
   if(u16_DeviationInteger <= 121)
   {
     while(u16_CountWait<128)
     {
       if(u8_CalibrationPpm[u16_CountWait] == u16_DeviationInteger)
       break;
       u16_CountWait++;
     }

     BKP_SetRTCCalibrationValue(u16_CountWait);

     u16_CountWait=u16_CountWait/10;

     u16_CountWait=u16_CountWait/10;
   
     if(u16_CountWait>0)
     {

     }
   }
   else /* Frequency deviation in ppm is more than 121 ppm, hence calibration
           can not be done */
   {

   }
  }
  
  BKP_RTCOutputConfig(BKP_RTCOutputSource_None);
  TIM_ITConfig(TIM2, TIM_IT_CC2, DISABLE);
  TIM_Cmd(TIM2, DISABLE);
  TIM_DeInit(TIM2);
  u32_CalibrationTimer=RTC_GetCounter();
  
  /*  Wait for 2 seconds  */
  while((RTC_GetCounter() - u32_CalibrationTimer) < 5)
  {
  }
  
  //MenuInit();
}
void
maxbotix12_init(void)
{
  sonar_meas_raw = 0;
  sonar_data_available = FALSE;

  //GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);


  /* TIM3 channel 4 pin (PB1) configuration */
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = SONAR_MAXBOTIX12_GPIO_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(SONAR_MAXBOTIX12_GPIO, &GPIO_InitStructure);

  //GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, DISABLE);
  //GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);

  /* TIM5 clock enable */
  //RCC_APB1PeriphClockCmd(SONAR_MAXBOTIX12_TIM_PERIPH, ENABLE);
  RCC_APB2PeriphClockCmd(SONAR_MAXBOTIX12_TIM_PERIPH, ENABLE);

  /* GPIOB clock enable */
  RCC_APB2PeriphClockCmd(SONAR_MAXBOTIX12_GPIO_PERIPH, ENABLE);

  /* Time Base configuration */

  /* The sensor has a pulse duration of 58us per cm.
   * It can measure a range from about 20 cm to 1068 cm, that is a pulsewidth between 1.160 ms and 61.944 ms.
   *
   * Calculation example:
   * Our clock is running at 72 MHz
   * At 16 bits resolution, the maximum pulse we could measure would be
   * (0xffff / 72 000 000) * 1000 = 0.910 ms
   *
   * So our prescaler has to be:
   *  (58 * (10^(-6)) * 1068) / (0xffff / 72 000 000) = 68,054
   *
   *  We don't want it to overflow when it's almost at it's maximum, as that would cause the system to think
   *  the A/C is on the ground.
   *+
   *  So we want prescaler 69. We have to se 68 as value, as the clock is divided by ( 1 + prescaler ).
   *
   *  @TODO make this dependant of the APB_CLK value
   */

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Period = 0xffff;
  TIM_TimeBaseStructure.TIM_Prescaler = SONAR_MAXBOTIX12_TIM_PRESCALER;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_InternalClockConfig(SONAR_MAXBOTIX12_TIM);
  TIM_TimeBaseInit(SONAR_MAXBOTIX12_TIM, &TIM_TimeBaseStructure);

  /* TIM3 configuration
   * Input signal is connected to TIM3 CH4 pin (PB1)
   * The Rising edge is used as active edge.
   */
  TIM_ICInitTypeDef TIM_ICInitStructure;
  TIM_ICInitStructure.TIM_Channel = SONAR_MAXBOTIX12_TIM_CHANNEL;
  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 = 0x00;

  /* Initialize PWM Input measurement
   * See page 301 of the STM32F103 reference manual. */
  TIM_PWMIConfig(SONAR_MAXBOTIX12_TIM, &TIM_ICInitStructure);

  /* Enable the TIM3 global Interrupt */
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = SONAR_MAXBOTIX12_IRQ_CHANNEL;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* we can use TI2FP2 because it is bound to TIM channel 2 , which we are using */
  TIM_SelectInputTrigger(SONAR_MAXBOTIX12_TIM, SONAR_MAXBOTIX12_TIM_TS);

  TIM_SelectSlaveMode(SONAR_MAXBOTIX12_TIM, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(SONAR_MAXBOTIX12_TIM, TIM_MasterSlaveMode_Enable);

  /* TIM3 enable counter */
  TIM_Cmd(SONAR_MAXBOTIX12_TIM, ENABLE);

  /* Enable the IRQ
   * We need CC1 only. CC2 would contain the PWM period (that is, 10Hz)
   */
  TIM_ITConfig(SONAR_MAXBOTIX12_TIM, TIM_IT_CC1, ENABLE);
}
void configurar_timers_PWM_I(void)
{
	
  /* - Configuração do Timer 5 -> Input do controle para YAW - */

  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

	TIM_ICInitTypeDef  TIM_ICInitStructure;
  	
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);							//Liga o timer 2 ao barramento.
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);							//Liga o timer 3 ao barramento.
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);							//Liga o timer 5 ao barramento.
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);							//Liga o timer 5 ao barramento.

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);							//PInos de input dos PWMS.

  uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 1000000)- 1;		//1000000 Contagens por segundo    //SystemcoreCLock/2 -> 84 Mhz

  /*Configuração de temporização básica do timer*/

  //Configurações dos timers de 32 bits - TIM5 e TIM2 - Clock do barramento de 84 MHz
  TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;									
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  	
  TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
  	
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  //Configurações do timer de 16 bits - TIM3 - Clock do barramento de 84 MHz
	TIM_TimeBaseStructure.TIM_Period = 0xFFFF;	
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  //Configurações do timer de 16bits - TIM9 - Clock do barramento de 168 MHz
	PrescalerValue = (uint16_t) ((SystemCoreClock) / 1000000)- 1;
	TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
	TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);  	

  /*Configuração do pino utilizado como entrada para a leitura do PWM.*/

 	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_0 | GPIO_Pin_7;				//PA1 ->Tim5_Ch2, PA0 -> Tim2_Ch2, PA7-> Tim3_Ch2, PA2 -> Tim9_Ch1
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;        //Pinos no modo de Função alternativa.
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;   //Pinos com velocidade de 100 MHz.
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;       //Configuração de saída -> Push Pull
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;       //Pino com pull Up.
	GPIO_Init(GPIOA, &GPIO_InitStructure);
  	
	/*ATiva a função alternativa do pino*/
  	
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5);

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM9);

	GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
  
  //Configurações dos pinos 2 e 6  	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //Pinos no modo de entrada.
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /*Configuração do interrupção.*/
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  	
  NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
  NVIC_Init(&NVIC_InitStructure);

  /*Configuração dos registradores de "capture" do timer 5*/
  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;

 	/*Configura a função de PWM Input */ 
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
  TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
  	
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
  TIM_PWMIConfig(TIM5, &TIM_ICInitStructure);
  TIM_PWMIConfig(TIM9, &TIM_ICInitStructure);
  	
  /* Select the TIM5 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);
	TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
  TIM_SelectInputTrigger(TIM5, TIM_TS_TI2FP2);
  TIM_SelectInputTrigger(TIM9, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
 	TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);

 	TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);

  TIM_SelectSlaveMode(TIM5, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM5,TIM_MasterSlaveMode_Enable);

  TIM_SelectSlaveMode(TIM9, TIM_SlaveMode_Reset);
  TIM_SelectMasterSlaveMode(TIM9,TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM2, ENABLE);
  TIM_Cmd(TIM3, ENABLE);
  TIM_Cmd(TIM5, ENABLE);
  TIM_Cmd(TIM9, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
  TIM_ITConfig(TIM5, TIM_IT_CC2, ENABLE);
  TIM_ITConfig(TIM9, TIM_IT_CC2, ENABLE);
  	
}