做无刷电机BLDC控制,因为公司选的是F0的芯片,然而F0资料又比较少,只是参考原子哥F1的资料,对着F0的资料研究了两天多,今天终于做出来了,英文渣没办法,感谢原子哥的中文文档和大量教程。好了进入正题:[mw_shl_code=c,true]/**********************************************************************
* Description : None
* Input : None
* Output : None
* Return : None
* Attention : None
**********************************************************************/
void BLDC_GPIOConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10; //TIM1输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;//TIM1输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //TIM3的霍尔输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //TIM3的霍尔输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**********************************************************************
* Description : None
* Input : None
* Output : None
* Return : None
* Attention : None
**********************************************************************/
void BLDC_TIM1Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //基本结构体变量定义
TIM_OCInitTypeDef TIM_OCInitStructure; //输出结构体变量定义
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //死区刹车结构体变量定义
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = 2; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;//中央对齐计数模式
TIM_TimeBaseStructure.TIM_Period = 1499; //PWM 16K
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道初始化
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse =1200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =1200;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse =1200;
TIM_OC3Init(TIM1,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道4初始化,用来触发AD注入采样
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse =1495;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM1,&TIM_OCInitStructure);
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //死区刹车初始化
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 100;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //如打开,开机无输出且状态紊乱????
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道1)
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道2)
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable); //使能捕获比较寄存器预装载(通道3)
TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2); //输入触发源选择TIM3
//TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Trigger);//从模式选择 触发
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}[/mw_shl_code]
我的代码参考了网上一个基于stm32f103Rbt6的BLDC程序 上传到了下面附件
这段代码选择霍尔接口之后,异或进入TIM3 CH1 捕获通道 任何霍尔跳变都将被捕获 同时 TIM3 OC4 作为一个计数 ,触发CC1捕获 可以计算OC4的溢出次数和当前计数值得到每个霍尔跳变的时间间隔 从而得到霍尔频率 根据极对数 算出电机反馈转速 同时巧妙利用 OC2 产生一个PWM 去出发TIM1内部触发中断 在这里面进行换向函数执行 很巧妙的设计 最大限度的减少了定时器的使用 个人理解 有误请指出
然后就在这个基础上 写了下面这段f030的代码
[mw_shl_code=c,true]void TIM3_Init(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;//将TIM3的1通道配置成输入捕获模式
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //TIM3时钟使能
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period =65535;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_TRC;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_SelectHallSensor(TIM3,ENABLE);
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1F_ED);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_OC2Ref);
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
TIM_Cmd(TIM3, ENABLE);
/* Enable the TIM1 Trigger and commutation interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}[/mw_shl_code]
这样设置好之后 CC1触发始终没和电机转动同步起来 总是乱跳, 后来发现一个关键点 (PS:也不知道算不算,因为030资料少 也是刚开始用),就是虽然是做输入捕获 f1的对应IO口直接设置为上拉输入,然后使能重映射就行了 然而f0没这个RCC使能 所以 这里的IO口要设置为复用
[mw_shl_code=c,true]void Hall_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_1);
}[/mw_shl_code]
一周热门 更多>