本帖最后由 中华有为 于 2019-7-2 10:24 编辑
- <p>#include "l298n_control.h"
- //L298N初始化 PA4--IN1, PA5--IN2, PA6--IN3, PA7--IN4, PA2--ENA, PA3--ENB,
- void L298N_Init(u16 period, u16 prescaler)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- </p><p>//使能时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
-
- //PA4,PA5,PA6,PA7为推挽输出,L,R电机正反转控制引脚
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //L:IN1,IN2 R:IN3,IN4
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
-
- //PA0,PA1,PA2,PA3为复用推挽输出,PWM输出引脚
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //ENA,ENB
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- //初始化TIM2
- TIM_TimeBaseStructure.TIM_Period = period; //设置自动装载周期值
- TIM_TimeBaseStructure.TIM_Prescaler = prescaler; //设置预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //初始化
-
- //初始化TIM2 CH3-CH4 PWM模式
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择PWM2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性高
- TIM_OC3Init(TIM2, &TIM_OCInitStructure); //初始化CH3
- TIM_OC4Init(TIM2, &TIM_OCInitStructure); //初始化CH4
-
- TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能CH3预装载寄存器
- TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能CH4预装载寄存器
-
- TIM_Cmd(TIM2, ENABLE); //使能TIM2
- }</p>
复制代码把
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);改为TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);则能输出PWM,这个是哪里出错了呢,望大神斧正。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
我用你的代码也试了试,还是初始化TIM3才有效。我现在一头雾水,我再试试其他的。谢谢
一周热门 更多>