[技术问答] HK32F030MF4P6 定时器TIMER输出翻转

[复制链接]
1567|10
 楼主| mnynt121 发表于 2023-4-25 21:00 | 显示全部楼层 |阅读模式


  1. /********************************************************************************* [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 ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "hk32f030m.h"
  4. #include "hk32f030m_gpio.h"
  5. #include <stdio.h>
  6. #include "stdarg.h"uint16_t TimerPeriod = 0;  // 自动重载值
  7. uint16_t Channel1Pulse = 0;// 脉冲宽度void RCC_Configuration(void);
  8. void GPIO_Configuration(void);
  9. void TIM_Config(void);int main(void)/* Infinite loop */
  10. {RCC_Configuration();GPIO_Configuration();TIM_Config();while (1){}
  11. }
  12. /*配置时钟*/
  13. void RCC_Configuration(void)
  14. {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);
  15. }
  16. /*配置GPIO*/
  17. void GPIO_Configuration(void)
  18. {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);
  19. }
  20. /*配置TIMER*/
  21. void TIM_Config(void)
  22. {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);
  23. }#ifdef  USE_FULL_ASSERT
  24. /*** @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*/
  25. void assert_failed(uint8_t* file, uint32_t line)
  26. {/* 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) */
  27. }
  28. #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口能承受的反转的频率是多少呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

30

主题

3382

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部