[mw_shl_code=c,true]#include "hard_pwm.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#define MOTOR_PERIOD 15000
// pwm管脚分配
// TIM2_CH1 PWM1 PA0
// TIM2_CH2 PWM2 PA1
// TIM2_CH3 PWM3 PB10
// TIM2_CH4 PWM4 PB11
// TIM3_CH1 PWM5 PA6
// TIM3_CH2 PWM6 PA7
// TIM4_CH1 PWM7 PB6
// TIM4_CH2 PWM8 PB7
char is_pwm_output_enable = 0;
void pwm_output(char m_ch, short m_value)
{
switch (m_ch)
{
case 1:
TIM2->CCR1 = m_value;
break;
case 2:
TIM2->CCR2 = m_value;
break;
case 3:
TIM2->CCR3 = m_value;
break;
case 4:
TIM2->CCR4 = m_value;
break;
case 5:
TIM3->CCR1 = m_value;
break;
case 6:
TIM3->CCR2 = m_value;
break;
case 7:
TIM4->CCR1 = m_value;
break;
case 8:
TIM4->CCR1 = m_value;
break;
default:
break;
}
}
static void pwm_gpio_init(void)
{
GPIO_InitTypeDef gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_AFIO,
ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
RCC_APB1Periph_TIM4,
ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2, ENABLE);
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOA, &gpio);
gpio.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_Init(GPIOB, &gpio);
}
static void pwm_tim_init(void)
{
TIM_TimeBaseInitTypeDef tim_timebase_structure;
TIM_OCInitTypeDef tim_ocinit_stucture;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
RCC_APB1Periph_TIM4,
ENABLE);
// TIM时钟分频,输出PWM周期50Hz
tim_timebase_structure.TIM_Period = MOTOR_PERIOD;
tim_timebase_structure.TIM_Prescaler = 72 - 1;
tim_timebase_structure.TIM_ClockDivision = TIM_CKD_DIV1;
tim_timebase_structure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &tim_timebase_structure);
TIM_TimeBaseInit(TIM3, &tim_timebase_structure);
TIM_TimeBaseInit(TIM4, &tim_timebase_structure);
tim_ocinit_stucture.TIM_OCMode = TIM_OCMode_PWM1;
tim_ocinit_stucture.TIM_OutputNState = TIM_OutputState_Enable;
tim_ocinit_stucture.TIM_Pulse = 1000;
tim_ocinit_stucture.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &tim_ocinit_stucture);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2Init(TIM2, &tim_ocinit_stucture);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3Init(TIM2, &tim_ocinit_stucture);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4Init(TIM2, &tim_ocinit_stucture);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC1Init(TIM3, &tim_ocinit_stucture);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2Init(TIM3, &tim_ocinit_stucture);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1Init(TIM4, &tim_ocinit_stucture);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2Init(TIM4, &tim_ocinit_stucture);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
static void pwm_gpio_deinit(void)
{
GPIO_DeInit(GPIOA);
GPIO_DeInit(GPIOB);
}
static void pwm_tim_deinit(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
RCC_APB1Periph_TIM4,
ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
RCC_APB1Periph_TIM4,
DISABLE);
}
void pwm_output_init(void)
{
if (is_pwm_output_enable == 0)
{
pwm_gpio_init();
pwm_tim_init();
for (int i = 1; i < 9; ++i)
pwm_output(i, 1000);
is_pwm_output_enable = 1;
}
}
void pwm_output_deinit(void)
{
if (is_pwm_output_enable == 1)
{
pwm_tim_deinit();
// pwm_gpio_deinit();
is_pwm_output_enable = 0;
}
}
[/mw_shl_code]
我这样配置的8路pwm输出,但是不能输出波形,请教各位是哪配置错了吗?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
是一个地方写错了
tim_ocinit_stucture.TIM_OutputNState 应该是 tim_ocinit_stucture.TIM_OutputState
自动补全手一滑点偏了,也没注意看寄存器
一周热门 更多>