打印
[技术问答]

HK32F030MF4P6 定时器TIMER输出翻转

[复制链接]
662|10
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mnynt121|  楼主 | 2023-4-25 21:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


/********************************************************************************* [url=home.php?mod=space&uid=288409]@file[/url]           : main.c* [url=home.php?mod=space&uid=247401]@brief[/url]          : 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_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef  TIM_OCInitStructure;// 频率配置为100KHZ  SystemCoreClock = 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);
}#ifdef  USE_FULL_ASSERT
/*** @brief  Reports the name of the source file and the source line number*         where the assert_param error has occurred.* @param  file: pointer to the source file name* @param  line: 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 | 只看该作者
怎样用定时器做两个输出循环交替              

使用特权

评论回复
5
mattlincoln| | 2023-5-6 00:22 | 只看该作者
在定时器中断服务程序中反转输出引脚。

使用特权

评论回复
6
tpgf| | 2023-6-2 14:52 | 只看该作者
ingramward 发表于 2023-5-5 23:30
怎样用定时器做两个输出循环交替

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

使用特权

评论回复
7
nawu| | 2023-6-2 15:29 | 只看该作者
只要功能代码不复杂 在中断函数中还是可以做一些事情的

使用特权

评论回复
8
aoyi| | 2023-6-2 15:50 | 只看该作者
如果精度要求不高的话 当然可以这么做了

使用特权

评论回复
9
zljiu| | 2023-6-2 16:09 | 只看该作者
在中断中处理问题 会影响定时器的计数吗

使用特权

评论回复
10
gwsan| | 2023-6-2 17:02 | 只看该作者
zljiu 发表于 2023-6-2 16:09
在中断中处理问题 会影响定时器的计数吗

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

使用特权

评论回复
11
tfqi| | 2023-6-2 17:18 | 只看该作者
一般来说io口能承受的反转的频率是多少呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

17

主题

2841

帖子

2

粉丝