pwm.c 3.6 KB

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