[技术文档] HK32F030M PWM

[复制链接]
1143|0
 楼主| mnynt121 发表于 2023-4-24 15:33 | 显示全部楼层 |阅读模式
航顺的库。。

目前是 HK32F030Mxx_ExampleV1.0.5

PWM部分的demo还没做好,这里可以参照STM32F030的搞进去



这里输出 125K 定时器 就是 8us了
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  4.   * [url=home.php?mod=space&uid=247401]@brief[/url]          : Main program body
  5.   ******************************************************************************
  6.   * @attention
  7.   *
  8.   */

  9. /* Includes ------------------------------------------------------------------*/
  10. #include "main.h"
  11. #include "hk32f030m.h"
  12. #include "hk32f030m_gpio.h"
  13. #include <stdio.h>
  14. #include "stdarg.h"

  15. uint16_t PrescalerValue = 0;

  16. void RCC_Configuration(void);
  17. void GPIO_Configuration(void);
  18. void TIM_Config(void);
  19. int main(void)
  20.   /* Infinite loop */
  21. {
  22.     RCC_Configuration();
  23.     GPIO_Configuration();
  24.     TIM_Config();   
  25.   while (1)
  26.   {
  27.   }
  28. }
  29. /*时钟配置*/
  30. void RCC_Configuration(void)
  31. {
  32.     RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );
  33.     RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );
  34.     RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );
  35.     RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOD, ENABLE );
  36.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  37. }
  38. /*GPIO配置*/
  39. void GPIO_Configuration(void)
  40. {
  41.     GPIO_InitTypeDef GPIO_InitStructure;

  42.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  43.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  44.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  45.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  46.     /*PD4 = TIM2 Channel 1*/
  47.     //GPIOD Configuration: Channel 1
  48.     /*
  49.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  50.   GPIO_Init(GPIOD, &GPIO_InitStructure);
  51.     GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_4);
  52.     */
  53.     /*PD3 = TIM2 Channel 2*/
  54.     //GPIOD Configuration: Channel 2
  55.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  56.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  57.     GPIO_PinAFConfig(GPIOD,GPIO_PinSource3,GPIO_AF_4);

  58.        /*PC3 = TIM2 Channel 1*/
  59.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  60.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  61.     GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_4);
  62.    
  63.         //PA3,IO翻转输出
  64.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  65.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  66.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  67.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  68.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  69. }
  70. /*TIMER配置*/
  71. void TIM_Config(void)
  72. {
  73.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  74.     NVIC_InitTypeDef NVIC_InitStructure;
  75.       /* -----------------------------------------------------------------------
  76.     TIM2 Configuration: Encoder mode1:
  77.    
  78.     In this example TIM2 input clock (TIM2CLK) is set to APB1 clock (PCLK1).
  79.       TIM2CLK = PCLK1  
  80.       PCLK1 = HCLK
  81.       => TIM2CLK = HCLK = SystemCoreClock
  82.          
  83.     To get TIM2 counter clock at 32 MHz, the prescaler is computed as follows:
  84.        Prescaler = (TIM2CLK / TIM2 counter clock) - 1
  85.        Prescaler = ((SystemCoreClock) /32 MHz) - 1

  86.    TIM2 is configured to interface with an encoder:
  87.    - The encoder mode is encoder mode1: Counter counts up/down on TI2 rising edge
  88.    depending on TI1 level

  89.    - The Autoreload value is set to 10, so the encoder round is 10 TIM counter clock.  
  90.                                              

  91.     Note:
  92.      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
  93.      Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
  94.      function to update SystemCoreClock variable value. Otherwise, any configuration
  95.      based on this variable will be incorrect.   
  96.   ----------------------------------------------------------------------- */
  97.   /* Compute the prescaler value */
  98. // PrescalerValue = (uint16_t) ((SystemCoreClock ) / 32000000) - 1;//配置频率为32M
  99.   PrescalerValue = 32-1; /* 和下面设置预分频一样 */
  100.   /* Time base configuration */
  101.   TIM_TimeBaseStructure.TIM_Period = 8-1  ;    /* 自动重装 数值*/
  102.   TIM_TimeBaseStructure.TIM_Prescaler = 0;  /* 预分频 */
  103.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;  /* 不分频 */
  104.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  105.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  106.   /* Prescaler configuration */
  107.   /* 先配好ARR ,再设置预分频,PSC有预重装功能只能等更新事件来弄,不像ARR CRR立即更新重载 */
  108.   TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);//每次UPdate重新初始化计数器
  109.     /*选择编码器模式1,根据TIFP1的电平,计数器在TI2FP2的边沿上下计数*/
  110. //  TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  111.    
  112.   /* TIM Interrupts enable */
  113.   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

  114.   /* TIM3 enable counter */
  115.   TIM_Cmd(TIM2, ENABLE);
  116.   
  117.   /* Enable the TIM2 gloabal Interrupt */
  118.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  119.   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  120.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  121.   NVIC_Init(&NVIC_InitStructure);
  122.   
  123.   
  124.   
  125.   
  126.   
  127.     TIM_OCInitTypeDef TIM_OCInitStructure;
  128.   
  129.     /* 频道1,2,3,4的PWM 模式设置 */
  130.    
  131.     /* PWM1 Mode configuration: Channel1 */  
  132.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                           //PWM模式

  133.    
  134.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  135.     TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  136.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  137.     TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  138.     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  139.     TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  140.    // TIM_OCInitStructure.TIM_Pulse = 500;//使能频道1配置
  141.    // TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  142.    
  143.     /* 通道1 */
  144.     TIM_OCInitStructure.TIM_Pulse = 4;//使能频道2配置
  145.     TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  146.    
  147.     /* 通道2 */
  148.     TIM_OCInitStructure.TIM_Pulse = 4;//使能频道2配置
  149.     TIM_OC2Init(TIM2, &TIM_OCInitStructure);

  150.    
  151.     /* TIM1 主输出使能 */
  152.     TIM_CtrlPWMOutputs(TIM2, ENABLE);
  153.   
  154.   
  155.   
  156. }
  157. /*配置IO翻转*/
  158. void GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  159. {
  160.   /* Check the parameters */
  161.   assert_param(IS_GPIO_PIN(GPIO_Pin));

  162.   GPIOx->ODR ^= GPIO_Pin;
  163. }
  164. #ifdef  USE_FULL_ASSERT
  165. /**
  166.   * @brief  Reports the name of the source file and the source line number
  167.   *         where the assert_param error has occurred.
  168.   * @param  file: pointer to the source file name
  169.   * @param  line: assert_param error line source number
  170.   * @retval None
  171.   */
  172. void assert_failed(uint8_t* file, uint32_t line)
  173. {
  174.   /* User can add his own implementation to report the file name and line number,
  175.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  176. }
  177. #endif /* USE_FULL_ASSERT */
转自https://www.cnblogs.com/XZHDJH/articles/13523787.html

您需要登录后才可以回帖 登录 | 注册

本版积分规则

30

主题

3414

帖子

2

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