#include "pwm.h" void pwm_init(pwm_content_t *pwm_content) { RCC_APB1PeriphClockCmd(pwm_content->pwm_timer_rcu, ENABLE); RCC_AHB1PeriphClockCmd(pwm_content->pwm_gpio_rcu, ENABLE); // GPIO初始化 GPIO_PinAFConfig(pwm_content->pwm_gpio_port, pwm_content->pwm_gpio_af, pwm_content->pwm_time_af); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = pwm_content->pwm_gpio_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(pwm_content->pwm_gpio_port, &GPIO_InitStructure); // 定时器初始化 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = pwm_content->pwm_period; // 定时时间的配置,也就是配置重载值,而重载值会传递给计数值 TIM_TimeBaseStructure.TIM_Prescaler = pwm_content->pwm_prescaler; // 配置分频值,确定定时器的时钟频率 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数,0->TIM_Period就会触发中断请求 TIM_TimeBaseInit(pwm_content->pwm_timer, &TIM_TimeBaseStructure); // PWM初始化 TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = pwm_content->pwm_timer_ch; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 输出状态使能 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = pwm_content->pwm_pulse; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 有效状态为高电平 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; TIM_OC1Init(pwm_content->pwm_timer, &TIM_OCInitStructure); // PWM设置 TIM_OC1PreloadConfig(pwm_content->pwm_timer, TIM_OCPreload_Enable); // 自动重装 TIM_ARRPreloadConfig(pwm_content->pwm_timer, ENABLE); // 使能定时器 TIM_Cmd(pwm_content->pwm_timer, ENABLE); } void pwm_mode(pwm_content_t *pwm_content, uint8_t status) { if (1 == status) { if (pwm_content->pwm_status == 0) { TIM_Cmd(pwm_content->pwm_timer, ENABLE); pwm_content->pwm_status = 1; } } if (0 == status) { if (pwm_content->pwm_status == 1) { TIM_Cmd(pwm_content->pwm_timer, DISABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = pwm_content->pwm_gpio_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(pwm_content->pwm_gpio_port, &GPIO_InitStructure); // GPIO_ResetBits(pwm_content->pwm_gpio_port, pwm_content->pwm_gpio_pin); pwm_content->pwm_status = 0; } } } pwm_content_t pwm_timer3_content = { .pwm_gpio_port = GPIOF, .pwm_gpio_pin = GPIO_Pin_9, .pwm_gpio_rcu = RCC_AHB1Periph_GPIOF, .pwm_gpio_af = GPIO_PinSource9, .pwm_time_af = GPIO_AF_TIM14, .pwm_timer_rcu = RCC_APB1Periph_TIM14, .pwm_timer = TIM14, .pwm_timer_ch = TIM_OCMode_PWM1, .pwm_prescaler = 84 - 1, .pwm_period = 500 - 1, .pwm_pulse = 50, .pwm_status = 0, };