打印

为何配置了3个定时器12个通道输出PWM,但是只有前4个GPIO有输出呢??

[复制链接]
452|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
电员师|  楼主 | 2020-4-26 21:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
【新手求助】
想用3个定时器12个通道来分别控制6个驱动(每两个通道控制一个驱动),但是接在示波器上只有一个定时器(TIM8)的四个通道好用,其他都没有波形,求问这是怎么一回事呢,应该怎样改?


/********************motor.c**************************/
#include "motor.h"
// ʹÓà TIM1
static void ADVANCE_TIM1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(ADVANCE_TIM1_CH1_GPIO_CLK|\
             ADVANCE_TIM1_CH2_GPIO_CLK|\
             ADVANCE_TIM1_CH3_GPIO_CLK|\
             ADVANCE_TIM1_CH4_GPIO_CLK, ENABLE);

GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM1_CH1_PIN|\
                ADVANCE_TIM1_CH2_PIN|\
                ADVANCE_TIM1_CH3_PIN|\
                ADVANCE_TIM1_CH4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(ADVANCE_TIM1_CH1_PORT, &GPIO_InitStructure);
GPIO_Init(ADVANCE_TIM1_CH2_PORT, &GPIO_InitStructure);
GPIO_Init(ADVANCE_TIM1_CH3_PORT, &GPIO_InitStructure);
GPIO_Init(ADVANCE_TIM1_CH4_PORT, &GPIO_InitStructure);
}
static void ADVANCE_TIM1_Mode_Config(void)
{
ADVANCE_TIM1_APBxClock_FUN(ADVANCE_TIM1_CLK,ENABLE);

/* Time base Configuration */
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Period=TIM_PERIOD;
TIM_TimeBaseStructure.TIM_Prescaler = TIM_PSC;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(ADVANCE_TIM1, &TIM_TimeBaseStructure);

/* PWM1 Mode Configuration */
TIM_OCInitTypeDef  TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

/* Channel 1 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC1Init(ADVANCE_TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);

/* Channel 2 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC2Init(ADVANCE_TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);

/* Channel 3 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC3Init(ADVANCE_TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);
/* Channel 4 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC4Init(ADVANCE_TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(ADVANCE_TIM1, TIM_OCPreload_Enable);


TIM_Cmd(ADVANCE_TIM1, ENABLE);
TIM_CtrlPWMOutputs(ADVANCE_TIM1, ENABLE);
}
static void ADVANCE_TIM1_Init(void)
{
ADVANCE_TIM1_GPIO_Config();
ADVANCE_TIM1_Mode_Config();
}
// ʹÓà TIM8
static void ADVANCE_TIM8_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(ADVANCE_TIM8_CH1_GPIO_CLK|\
             ADVANCE_TIM8_CH2_GPIO_CLK|\
             ADVANCE_TIM8_CH3_GPIO_CLK|\
             ADVANCE_TIM8_CH4_GPIO_CLK, ENABLE);

GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM8_CH1_PIN|\
                ADVANCE_TIM8_CH2_PIN|\
                ADVANCE_TIM8_CH3_PIN|\
                ADVANCE_TIM8_CH4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(ADVANCE_TIM8_CH1_PORT, &GPIO_InitStructure);
GPIO_Init(ADVANCE_TIM8_CH2_PORT, &GPIO_InitStructure);
GPIO_Init(ADVANCE_TIM8_CH3_PORT, &GPIO_InitStructure);
GPIO_Init(ADVANCE_TIM8_CH4_PORT, &GPIO_InitStructure);
}
static void ADVANCE_TIM8_Mode_Config(void)
{
ADVANCE_TIM8_APBxClock_FUN(ADVANCE_TIM8_CLK,ENABLE);

/* Time base Configuration */
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Period=TIM_PERIOD;
TIM_TimeBaseStructure.TIM_Prescaler = TIM_PSC;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(ADVANCE_TIM8, &TIM_TimeBaseStructure);

/* PWM1 Mode Configuration */
TIM_OCInitTypeDef  TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

/* Channel 1 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC1Init(ADVANCE_TIM8, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);

/* Channel 2 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC2Init(ADVANCE_TIM8, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);

/* Channel 3 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC3Init(ADVANCE_TIM8, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);
/* Channel 4 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC4Init(ADVANCE_TIM8, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(ADVANCE_TIM8, TIM_OCPreload_Enable);


TIM_Cmd(ADVANCE_TIM8, ENABLE);
TIM_CtrlPWMOutputs(ADVANCE_TIM8, ENABLE);
}
static void ADVANCE_TIM8_Init(void)
{
ADVANCE_TIM8_GPIO_Config();
ADVANCE_TIM8_Mode_Config();
}
// ʹÓà TIM3
static void GENERAL_TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(GENERAL_TIM3_CH1_GPIO_CLK|\
             GENERAL_TIM3_CH2_GPIO_CLK|\
             GENERAL_TIM3_CH3_GPIO_CLK|\
             GENERAL_TIM3_CH4_GPIO_CLK, ENABLE);

GPIO_InitStructure.GPIO_Pin = GENERAL_TIM3_CH3_PIN|\
                GENERAL_TIM3_CH3_PIN|\
                GENERAL_TIM3_CH3_PIN|\
                GENERAL_TIM3_CH3_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GENERAL_TIM3_CH1_PORT, &GPIO_InitStructure);
GPIO_Init(GENERAL_TIM3_CH2_PORT, &GPIO_InitStructure);
GPIO_Init(GENERAL_TIM3_CH3_PORT, &GPIO_InitStructure);
GPIO_Init(GENERAL_TIM3_CH4_PORT, &GPIO_InitStructure);
}
static void GENERAL_TIM3_Mode_Config(void)
{
GENERAL_TIM3_APBxClock_FUN(GENERAL_TIM3_CLK,ENABLE);

/* Time base Configuration */
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Period=TIM_PERIOD;
TIM_TimeBaseStructure.TIM_Prescaler=TIM_PSC;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(GENERAL_TIM3, &TIM_TimeBaseStructure);

/* PWM1 Mode Configuration */
TIM_OCInitTypeDef  TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

/* Channel 1 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC1Init(GENERAL_TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);

/* Channel 2 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC2Init(GENERAL_TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);

/* Channel 3 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC3Init(GENERAL_TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);

/* Channel 4 */
TIM_OCInitStructure.TIM_Pulse = TIM_PULSE;
TIM_OC4Init(GENERAL_TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(GENERAL_TIM3, TIM_OCPreload_Enable);

TIM_Cmd(GENERAL_TIM3, ENABLE);
}
void ADVANCE_TIM_Init(void)
{
ADVANCE_TIM1_Init();
ADVANCE_TIM8_Init();
}
void GENERAL_TIM_Init(void)
{
GENERAL_TIM3_GPIO_Config();
GENERAL_TIM3_Mode_Config();
}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

471

主题

480

帖子

0

粉丝