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 ) { } }
分类:
STM32
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2013-09-10 Digital Current-Mode Control Challenges Analog Counterparts
2013-09-10 利用DC/DC开关调节器延长DSP系统的电池寿命 - 动态电压调节
2013-09-10 PWM DAC Low Pass Filtering
2013-09-10 [转]如何在Windows Server 2012中安装.Net Framework 3.5?
2012-09-10 CM3 支持 64 位整数, LDRD STRD
2012-09-10 Double linked list structure
2012-09-10 双向链接表 linux