mnynt121 发表于 2023-4-25 21:00

HK32F030MF4P6 定时器TIMER输出翻转



/********************************************************************************* @file         : main.c* @brief          : Main program body******************************************************************************* @attention**//* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "hk32f030m.h"
#include "hk32f030m_gpio.h"
#include <stdio.h>
#include "stdarg.h"uint16_t TimerPeriod = 0;// 自动重载值
uint16_t Channel1Pulse = 0;// 脉冲宽度void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Config(void);int main(void)/* Infinite loop */
{RCC_Configuration();GPIO_Configuration();TIM_Config();while (1){}
}
/*配置时钟*/
void RCC_Configuration(void)
{RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOD, ENABLE );RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;/*PD4 = TIM2 Channel 1*///GPIOD Configuration: Channel 1GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_4);
}
/*配置TIMER*/
void TIM_Config(void)
{TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;TIM_OCInitTypeDefTIM_OCInitStructure;// 频率配置为100KHZSystemCoreClock = 32Mhz(内部高速时钟)    自动重载值 = 32Mhz/100 000 -1 = 3199TimerPeriod = (SystemCoreClock / 100000 ) - 1;/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */// 脉宽值 // 5*(3199-1)/10 = 1599// duty cycle 占空比为 1/2 = 50%Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10); /* Time Base configuration */TIM_TimeBaseStructure.TIM_Prescaler = 0;//预分频系数TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数TIM_TimeBaseStructure.TIM_Period = TimerPeriod;//自动重载值3200TIM_TimeBaseStructure.TIM_ClockDivision = 0;TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);/* Channel 1 Configuration in PWM mode */TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//翻转TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;TIM_OC1Init(TIM2, &TIM_OCInitStructure);//TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);/* TIM2 counter enable */TIM_Cmd(TIM2, ENABLE);
}#ifdefUSE_FULL_ASSERT
/*** @briefReports the name of the source file and the source line number*         where the assert_param error has occurred.* @paramfile: pointer to the source file name* @paramline: assert_param error line source number* @retval None*/
void assert_failed(uint8_t* file, uint32_t line)
{/* User can add his own implementation to report the file name and line number,tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */

hudi008 发表于 2023-5-5 21:53

利用定时器输出比较模式的翻转功能来输出PWM。

mattlincoln 发表于 2023-5-5 22:34

当定时器计数达到设定的计数值时,会触发定时器中断。在中断服务程序中,可以通过GPIO口驱动输出引脚并实现翻转效果。

ingramward 发表于 2023-5-5 23:30

怎样用定时器做两个输出循环交替            

mattlincoln 发表于 2023-5-6 00:22

在定时器中断服务程序中反转输出引脚。

tpgf 发表于 2023-6-2 14:52

ingramward 发表于 2023-5-5 23:30
怎样用定时器做两个输出循环交替

用一个定时器是不是就可以实现这个循环交替的功能啊

nawu 发表于 2023-6-2 15:29

只要功能代码不复杂 在中断函数中还是可以做一些事情的

aoyi 发表于 2023-6-2 15:50

如果精度要求不高的话 当然可以这么做了

zljiu 发表于 2023-6-2 16:09

在中断中处理问题 会影响定时器的计数吗

gwsan 发表于 2023-6-2 17:02

zljiu 发表于 2023-6-2 16:09
在中断中处理问题 会影响定时器的计数吗

我猜 如果优先级不一样 最后的结论也是不一样的

tfqi 发表于 2023-6-2 17:18

一般来说io口能承受的反转的频率是多少呢
页: [1]
查看完整版本: HK32F030MF4P6 定时器TIMER输出翻转