请教大神,STM32F030不用TIM1的刹车功能能不能实现有刷直流电机的正反转切换?如果可以,应该怎么切换?在从正转到反转的切换过程中是否需要刹车?
[mw_shl_code=c,true]#include "time.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t TimerPeriod = 0;
uint16_t Channel1Pulse = 0, Channel4Pulse = 0;
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIO时钟 */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
/* 配置GPIO管脚复用*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_8|GPIO_Pin_11);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
//SD拉高
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_12);
}
void TIM_PWM_Config(void)
{
/* TIM1 的配置 ---------------------------------------------------
TIM1 输入时钟(TIM1CLK) 设置为 APB2 时钟 (PCLK2)
=> TIM1CLK = PCLK2 = SystemCoreClock
TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
SystemCoreClock 为48 MHz
我们的目标产生 4 路PWM 信号在17.57 KHz:
- TIM1_Period = (SystemCoreClock / 17570) - 1
信道1设置的占空比为 50%
信道2设置的占空比为 37.5%
信道3设置的占空比为 25%
信道4设置的占空比为 12.5%
定时器脉冲的计算方式如下:
- ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
*/
/*计算预定表的值,也就是多少个时钟计数为一个周期*/
TimerPeriod = (SystemCoreClock / 17570 ) - 1;
/*计算CCR1 跳转值 在占空比为50%时*/
Channel1Pulse = (uint16_t) (((uint32_t) 50 * (TimerPeriod - 1)) / 100);
/*计算CCR2 跳转值 在占空比为37.5%时*/
// Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
/*计算CCR3 跳转值 在占空比为25%时*/
// Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
/*计算CCR4跳转值 在占空比为12.5%时*/
Channel4Pulse = (uint16_t) (((uint32_t) 50 * (TimerPeriod- 1)) / 100);
/* TIM1 时钟使能 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
/* Time 定时基础设置*/
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /* Time 定时设置为上升沿计算模式*/
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* 频道1,2,3,4的PWM 模式设置 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//使能频道1配置
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;//使能频道2配置
// TIM_OC2Init(TIM1, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;//使能频道3配置
// TIM_OC3Init(TIM1, &TIM_OCInitStructure);
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
// TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;//使能频道4配置
// TIM_OC4Init(TIM1, &TIM_OCInitStructure);
/* TIM1 计算器使能*/
TIM_Cmd(TIM1, ENABLE);
/* TIM1 主输出使能 */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
void Motor_Forward(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_PinLockConfig(GPIOA,GPIO_Pin_8);
}
void Motor_Reverse(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_11);
GPIO_PinLockConfig(GPIOA,GPIO_Pin_11);
}
[/mw_shl_code]
|