• 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 ) { } }

  • 相关阅读:
    【华磊随笔】一个企业的内部讨论(含高层)“30岁IT人,产出代码还是产出思想”之我见。
    dotNetGrid 演示站点(new)争取做ASP.NET中最好的Ajax开源表格控件
    【华磊随笔】进一步的分析:开发者版本;你属于哪个版本的程序员?
    新增格式化单元格数据功能dotNetFlexGrid1.24,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。(更新FieldFormatorHandle参数)
    【华磊随笔】技术到底值不值钱,到底值多少钱有感于公司新产品的上线
    【华磊原创】某企业前两年的开发人员技术能力评估表(不分语言)相对比较全面也比较细
    【华磊原创】安装程序自动检测安装.Net Framework运行环境(使用InnoSetup)
    mainline.js主线
    JS中的“!!”
    读“周大师代码评审”有感
  • 原文地址:https://www.cnblogs.com/shangdawei/p/4796612.html
Copyright © 2020-2023  润新知