STM32F4 Timer External Clock TI2 Both Edges Demo

#define CLK_FREQ    ( 10000 )
#define CORE_FREQ   ( 168000000 )

static void TIM_GPIO_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStructure;

  // Enable GPIOA clock
  __HAL_RCC_GPIOA_CLK_ENABLE( );

  // Configure PA8 pin as CLK output -- to CK Input
  GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStructure.Pull = GPIO_NOPULL;
  GPIO_InitStructure.Pin = GPIO_PIN_8;
  GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
  GPIO_InitStructure.Alternate = GPIO_AF1_TIM1;

  HAL_GPIO_Init( GPIOA, &GPIO_InitStructure );

  // Enable GPIOC clock
  __HAL_RCC_GPIOC_CLK_ENABLE( );

  // Configure PC7 pin as CK input -- from CLK Output
// Can not uses GPIO_MODE_INPUT : AF = 0
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; // AF = 3 GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Pin = GPIO_PIN_7; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = GPIO_AF3_TIM8; HAL_GPIO_Init( GPIOC, &GPIO_InitStructure ); // Configure PC6 pin as CK output -- to Oscilloscope Display GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Pin = GPIO_PIN_6; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Alternate = GPIO_AF3_TIM8; HAL_GPIO_Init( GPIOC, &GPIO_InitStructure ); } TIM_HandleTypeDef TimHandle; TIM_MasterConfigTypeDef sMasterConfig; TIM_SlaveConfigTypeDef sSlaveConfig; TIM_OC_InitTypeDef sConfig; static void Error_Handler( void ) { while ( 1 ) { } } void TIM_CLK_Output( void ) { __HAL_RCC_TIM1_CLK_ENABLE( ) ; TimHandle.Instance = TIM1; TimHandle.Init.Period = 1; TimHandle.Init.Prescaler = CORE_FREQ / CLK_FREQ - 1; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if ( HAL_TIM_PWM_Init( &TimHandle ) != HAL_OK ) Error_Handler( ); sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.OCFastMode = TIM_OCFAST_DISABLE; sConfig.Pulse = 1; if ( HAL_TIM_PWM_ConfigChannel( &TimHandle, &sConfig, TIM_CHANNEL_1 ) != HAL_OK ) Error_Handler( ); if ( HAL_TIM_PWM_Start( &TimHandle, TIM_CHANNEL_1 ) != HAL_OK ) Error_Handler( ); } void TIM_CK_Input( void ) { __HAL_RCC_TIM8_CLK_ENABLE( ); TimHandle.Instance = TIM8; TIM_IC_InitTypeDef IC_InitTypeDef; IC_InitTypeDef.ICFilter = 0; IC_InitTypeDef.ICPolarity = TIM_ICPOLARITY_BOTHEDGE; IC_InitTypeDef.ICPrescaler = TIM_ICPSC_DIV1; IC_InitTypeDef.ICSelection = TIM_ICSELECTION_DIRECTTI; // TIM8_CH2 : PC7 HAL_TIM_IC_ConfigChannel( &TimHandle, &IC_InitTypeDef, TIM_CHANNEL_2 ); TIM_ClockConfigTypeDef ClockConfig; ClockConfig.ClockFilter = 0; ClockConfig.ClockPolarity = TIM_CLOCKPOLARITY_BOTHEDGE; ClockConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1; ClockConfig.ClockSource = TIM_CLOCKSOURCE_TI2; // TIM8_CH2 : PC7 HAL_TIM_ConfigClockSource( &TimHandle, &ClockConfig ); TimHandle.Init.Period = 1; TimHandle.Init.Prescaler = 0; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if ( HAL_TIM_Base_Init( &TimHandle ) != HAL_OK ) Error_Handler( ); if ( HAL_TIM_Base_Start( &TimHandle ) != HAL_OK ) Error_Handler( ); } void TIM_CK_Output( void ) { TimHandle.Instance = TIM8; sConfig.OCMode = TIM_OCMODE_PWM1; sConfig.OCPolarity = TIM_OCPOLARITY_LOW; // Inverted CK_Input sConfig.OCFastMode = TIM_OCFAST_DISABLE; sConfig.Pulse = 1; if ( HAL_TIM_PWM_ConfigChannel( &TimHandle, &sConfig, TIM_CHANNEL_1 ) != HAL_OK ) Error_Handler( ); if ( HAL_TIM_PWM_Start( &TimHandle, TIM_CHANNEL_1 ) != HAL_OK ) Error_Handler( ); } void Timer_Demo( void ) { TIM_GPIO_Config( ); TIM_CLK_Output( ); // To CK_Input 5 KHz TIM_CK_Input( ); // From CLK_Output 5 KHz TIM_CK_Output( ); // PWM Output 5 KHz -- Inverted CL_Output with Delay while ( 1 ) { } }

 

posted @ 2015-09-10 01:54  IAmAProgrammer  阅读(870)  评论(0编辑  收藏  举报