[技术问答] 定时器计数/PWM输出/输出翻转/输入捕获

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


  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 CCR1_Val = 5000;
  7. uint16_t CCR2_Val = 2500;
  8. uint16_t CCR3_Val = 1250;
  9. uint16_t CCR4_Val = 625;
  10. uint16_t PrescalerValue = 0;void RCC_Configuration(void);
  11. void GPIO_Configuration(void);
  12. void TIM_Config(void);
  13. void GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
  14. void TIM6_IRQHandler(void);int main(void)/* Infinite loop */
  15. {RCC_Configuration();GPIO_Configuration();TIM_Config();while (1){}
  16. }
  17. /*配置时钟*/
  18. void RCC_Configuration(void)
  19. {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_TIM6 , ENABLE);
  20. }
  21. /*配置GPIO*/
  22. void GPIO_Configuration(void)
  23. {GPIO_InitTypeDef GPIO_InitStructure;//GPIOA,PA1,PA2,PA3GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;GPIO_Init(GPIOA, &GPIO_InitStructure);        //GPIOD,PD4GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_Init(GPIOD, &GPIO_InitStructure);
  24. }
  25. /*配置TIMER*/
  26. void TIM_Config(void)
  27. {TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;NVIC_InitTypeDef NVIC_InitStructure;// 预分频器值 // SystemCoreClock = 48M,计数器时钟counter clock = 1 MHz
  28. //  PrescalerValue = (uint16_t) (SystemCoreClock  / 1000000) - 1;// 32MHzPrescalerValue = (uint16_t) (SystemCoreClock  / 10000) - 1;// 3200/* Enable the TIM1 gloabal Interrupt */NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;NVIC_InitStructure.NVIC_IRQChannelPriority = 0; // 中断优先级NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/* Time Base configuration */// 公式:Tout= ((arr+1)*(psc+1))/Tclk// 1s = ((10000+1)*(3200+1))/32000000// 0.5s = ((5000+1)*(3200+1))/32000000TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;// 预分频系数 3200TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;// 计数模式:向上计数TIM_TimeBaseStructure.TIM_Period = 5000; // 自动重载值 10000,计数模式为向上计数时,定时器从0开始计数,计数超过到arr时触发定时中断服务函数TIM_TimeBaseStructure.TIM_ClockDivision = 0;TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);/* TIM Interrupts enable */TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);/* TIM1 counter enable */TIM_Cmd(TIM6, ENABLE);
  29. }
  30. /*配置IO翻转*/
  31. void GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  32. {/* Check the parameters */assert_param(IS_GPIO_PIN(GPIO_Pin));GPIOx->ODR ^= GPIO_Pin; // 输出翻转
  33. }uint16_t capture = 0;void TIM6_IRQHandler(void)
  34. {if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET){TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //每次进入中断都要清空中断标志,否则主函数将无法正常执行/* PA1 toggling with frequency = 50 Hz */GPIO_TogglePin(GPIOD ,GPIO_Pin_4);// PD4 翻转 //                capture = TIM_GetCapture1(TIM1);
  35. //               
  36. //    TIM_SetCompare1(TIM1, capture);}
  37. }#ifdef  USE_FULL_ASSERT
  38. /*** @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*/
  39. void assert_failed(uint8_t* file, uint32_t line)
  40. {/* 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) */
  41. }
  42. #endif /* USE_FULL_ASSERT */
  1. /********************************************************************************* @file           : main.c* @brief          : 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, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 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_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
  15. }
  16. /*配置GPIO*/
  17. void GPIO_Configuration(void)
  18. {//GPIOA Configuration: Channel 1N, 2N, 3NGPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ;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 ;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_3);        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_3);        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_3);        //GPIOC Configuration: Channel 1, 2GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_3);        GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_3);        /*PD3 = TIMER Channel 3*///GPIOD Configuration: Channel 3GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOD,GPIO_PinSource3,GPIO_AF_3);        /*PD4 = TIMER Channel 4*///GPIOD Configuration: Channel 4GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_3);//        //GPIOB Configuration: BreakGPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;GPIO_Init(GPIOB, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_3);        //        /*PC3 = TIMER Channel 3*/
  19. //        GPIO_IOMUX_PinAFConfig(GPIOC,GPIO_PinSource3,IOMUX_PC3_TIM1CH3);
  20. //        //GPIOC Configuration: Channel 3 PC3
  21. //  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  22. //  GPIO_Init(GPIOC, &GPIO_InitStructure);
  23. //        GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_3);       
  24. }
  25. /*配置TIMER*/
  26. void TIM_Config(void)
  27. {TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef  TIM_OCInitStructure;//TIM_BDTRInitTypeDef TIM_BDTRInitStructure;TimerPeriod = (SystemCoreClock / 3200 ) - 1;//SystemCoreClock=32MHZ  // 自动重载值是10000 /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);/* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);/* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);/* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);/* Time Base configuration */TIM_TimeBaseStructure.TIM_Prescaler = 0;// 预分频系数TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数TIM_TimeBaseStructure.TIM_Period = TimerPeriod;// 自动重载值TIM_TimeBaseStructure.TIM_ClockDivision = 0;//一般不使用,默认TIM_CKD_DIV1TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);/* Channel 1, 2,3 and 4 Configuration in PWM mode */TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//设置待装入捕获比较寄存器的脉冲值TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;TIM_OC1Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;TIM_OC2Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;TIM_OC3Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;TIM_OC4Init(TIM1, &TIM_OCInitStructure);
  28. //  /* Automatic Output enable, Break, dead time and lock configuration*/
  29. //  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  30. //  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  31. //  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
  32. //  TIM_BDTRInitStructure.TIM_DeadTime = 50;
  33. //  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
  34. //  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  35. //  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);/* TIM1 counter enable */TIM_Cmd(TIM1, ENABLE);/* TIM1 Main Output Enable */TIM_CtrlPWMOutputs(TIM1, ENABLE);}#ifdef  USE_FULL_ASSERT
  36. /*** @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*/
  37. void assert_failed(uint8_t* file, uint32_t line)
  38. {/* 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) */
  39. }
  40. #endif /* USE_FULL_ASSERT */


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

本版积分规则

30

主题

3322

帖子

2

粉丝

30

主题

3322

帖子

2

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