hal_pwm.c 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "hal_pwm.h"
  2. #include "stm32f4xx_tim.h"
  3. void pwm_init(pwm_content_t *pwm_content)
  4. {
  5. RCC_APB1PeriphClockCmd(pwm_content->pwm_timer_rcu, ENABLE);
  6. RCC_AHB1PeriphClockCmd(pwm_content->pwm_gpio_rcu, ENABLE);
  7. // GPIO初始化
  8. GPIO_PinAFConfig(pwm_content->pwm_gpio_port, pwm_content->pwm_gpio_af, pwm_content->pwm_time_af);
  9. GPIO_InitTypeDef GPIO_InitStructure;
  10. GPIO_InitStructure.GPIO_Pin = pwm_content->pwm_gpio_pin;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  13. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  14. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  15. GPIO_Init(pwm_content->pwm_gpio_port, &GPIO_InitStructure);
  16. // 定时器初始化
  17. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  18. TIM_TimeBaseStructure.TIM_Period = pwm_content->pwm_period; // 定时时间的配置,也就是配置重载值,而重载值会传递给计数值
  19. TIM_TimeBaseStructure.TIM_Prescaler = pwm_content->pwm_prescaler; // 配置分频值,确定定时器的时钟频率
  20. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  21. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  22. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数,0->TIM_Period就会触发中断请求
  23. TIM_TimeBaseInit(pwm_content->pwm_timer, &TIM_TimeBaseStructure);
  24. // PWM初始化
  25. TIM_OCInitTypeDef TIM_OCInitStructure;
  26. TIM_OCInitStructure.TIM_OCMode = pwm_content->pwm_timer_ch;
  27. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 输出状态使能
  28. TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
  29. TIM_OCInitStructure.TIM_Pulse = pwm_content->pwm_pulse;
  30. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 有效状态为高电平
  31. TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  32. TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
  33. TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  34. TIM_OC1Init(pwm_content->pwm_timer, &TIM_OCInitStructure);
  35. // PWM设置
  36. TIM_OC1PreloadConfig(pwm_content->pwm_timer, TIM_OCPreload_Enable);
  37. // 自动重装
  38. TIM_ARRPreloadConfig(pwm_content->pwm_timer, ENABLE);
  39. // 使能定时器
  40. TIM_Cmd(pwm_content->pwm_timer, ENABLE);
  41. }
  42. void pwm_mode(pwm_content_t *pwm_content, uint8_t status)
  43. {
  44. if (1 == status)
  45. {
  46. if (pwm_content->pwm_status == 0)
  47. {
  48. TIM_Cmd(pwm_content->pwm_timer, ENABLE);
  49. pwm_content->pwm_status = 1;
  50. }
  51. }
  52. if (0 == status)
  53. {
  54. if (pwm_content->pwm_status == 1)
  55. {
  56. TIM_Cmd(pwm_content->pwm_timer, DISABLE);
  57. GPIO_InitTypeDef GPIO_InitStructure;
  58. GPIO_InitStructure.GPIO_Pin = pwm_content->pwm_gpio_pin;
  59. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  60. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  61. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  62. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  63. GPIO_Init(pwm_content->pwm_gpio_port, &GPIO_InitStructure);
  64. // GPIO_ResetBits(pwm_content->pwm_gpio_port, pwm_content->pwm_gpio_pin);
  65. pwm_content->pwm_status = 0;
  66. }
  67. }
  68. }
  69. pwm_content_t pwm_timer3_content = {
  70. .pwm_gpio_port = GPIOF,
  71. .pwm_gpio_pin = GPIO_Pin_9,
  72. .pwm_gpio_rcu = RCC_AHB1Periph_GPIOF,
  73. .pwm_gpio_af = GPIO_PinSource9,
  74. .pwm_time_af = GPIO_AF_TIM14,
  75. .pwm_timer_rcu = RCC_APB1Periph_TIM14,
  76. .pwm_timer = TIM14,
  77. .pwm_timer_ch = TIM_OCMode_PWM1,
  78. .pwm_prescaler = 84 - 1,
  79. .pwm_period = 500 - 1,
  80. .pwm_pulse = 50,
  81. .pwm_status = 0,
  82. };