直流无刷电机开环控制程序-6步换向法
STM32单片机源程序如下:
- #include "stm32f10x.h"
- #include <stdio.h>
- unsigned short int CCR1_Val = 0;
- unsigned short int CCR2_Val = 0;
- unsigned short int CCR3_Val = 0;
- volatile unsigned char HALL_Value = 0;
- unsigned char HALL_flag =1;
- /*******************************************************************************
- * 函数名称: Timer1_Init();
- * 功能描述: 定时器1初始化
- * 输入参数: void
- * 返回参数: 无
- ********************************************************************************/
- void PWM_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //时钟配置
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); // 使能GPIOA/GPIOB端口
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10; //PWM???PA8,9,10
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //??????
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure); //???GPIO
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //PWM???PA7
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //??????
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure); //???GPIO
- }
- /*******************************************************************************
- * 函数名称: HALL_GPIO_Init();
- * 功能描述: 霍尔端口初始化
- * 输入参数: void
- * 返回参数: 无
- ********************************************************************************/
- void HALL_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); // 使能GPIOC端口
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2; //PWM???PA7
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //??????
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- /*******************************************************************************
- * 函数名称: Timer1_Init();
- * 功能描述: 定时器1初始化
- * 输入参数: void
- * 返回参数: 无
- ********************************************************************************/
- void Timer1_Init(void)
- {
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
-
- PWM_GPIO_Init(); //端口初始化
- HALL_GPIO_Init();
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE); // 打开定时器1的时钟
-
-
- // 默认20KHZ
- //设定3对PWM占空比
- CCR1_Val = 2799;
- CCR2_Val = 2799;
- CCR3_Val = 2799;
-
- //设置定时器相关参数
- TIM_TimeBaseInitStructure.TIM_Prescaler = 0; // 72MHZ
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
- TIM_TimeBaseInitStructure.TIM_Period = 3599; // 自动重装载计数器周期的值
- TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; // 时钟分频系数--不分频
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
-
- //PWM模式设置-CH1
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //PWM模式
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出使能
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//互补端使能输出
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val ; //设置占空比
- 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_OCNIdleState_Set; //死区后互补端输出状态
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
-
- TIM_OCInitStructure.TIM_Pulse = CCR2_Val ;
- TIM_OC2Init(TIM1, &TIM_OCInitStructure);
-
- TIM_OCInitStructure.TIM_Pulse = CCR3_Val ;
- TIM_OC3Init(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 = 0xc8; //死区时间--2us
- TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //刹车功能使能
- TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;//刹车输入极性
- TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;//自动输出使能
- TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
-
-
- TIM_CCPreloadControl(TIM1, ENABLE); //使能预装值
-
- TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE); //开启通信事件
- TIM_Cmd(TIM1, ENABLE);
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
-
- }
- /*******************************************************************************
- * 函数名称: BLDC_Hall_Convet();
- * 功能描述: 根据霍尔状态进行换向--T1 T3 T5/T4 T6 T2排列
- * 输入参数: 无
- * 返回参数: 无
- ********************************************************************************/
- void BLDC_Hall_Convet(void)
- {
- // char PB0_Value,PB1_Value,PB2_Value;
- // PB0_Value = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0);
- // PB1_Value = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)*2;
- // PB2_Value = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)*4;
- //
- // HALL_Value = PB0_Value+PB1_Value+PB2_Value;
- HALL_Value =(unsigned char)((GPIO_ReadInputData(GPIOB) & 0x0007)); //读转子位置
-
- if(HALL_flag ==1) //正转
- {
- switch(HALL_Value) //根据转子位置,决定CCER输出相位和转子字偏移量
- {
- case 0x05: //配置 T3/T4
-
- /* Channel2 configuration */
- TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable);
- TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);
-
- /* Channel1 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable);
- TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_Active);
-
- /* Channel3 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_3,TIM_CCxN_Enable);
- TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_InActive);
-
- TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
- break;
- case 0x04: //T4 T5
- /* Channel3 configuration */
- TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable);
- TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);
-
- /* Channel1 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable);
- TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_Active);
- /* Channel2 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);
- TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_InActive);
-
- TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
- break;
- case 0x06: //T5 T6
-
- /* Channel3 configuration */
- TIM_SelectOCxM(TIM1, TIM_Channel_3, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Enable);
- TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Disable);
- /* Channel2 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);
- TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_Active);
-
- /* Channel1 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable);
- TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_InActive);
- TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
-
- break;
- case 0x02: //T6 T1
-
- /* Channel1 configuration */
- TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable);
- TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Disable);
-
- /* Channel2 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Enable);
- TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_Active);
-
- /* Channel3 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);
- TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_InActive);
- TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
- break;
- case 0x03: // T1 T2
-
- TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_OCMode_PWM1);
- TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Enable);
- TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);
- /* Channel3 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);
- TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_Active);
- /* Channel2 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_2,TIM_CCxN_Enable);
- TIM_ForcedOC2Config(TIM1,TIM_ForcedAction_InActive);
-
- TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
-
- break;
- case 0x01: //T2 T3
-
- /* Channel2 configuration */
- TIM_SelectOCxM(TIM1, TIM_Channel_2, TIM_OCMode_PWM1 );
- TIM_CCxCmd(TIM1, TIM_Channel_2, TIM_CCx_Enable);
- TIM_CCxNCmd(TIM1, TIM_Channel_2, TIM_CCxN_Disable);
-
- /* Channel3 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_3, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_3, TIM_CCxN_Enable);
- TIM_ForcedOC3Config(TIM1,TIM_ForcedAction_Active);
-
- /* Channel1 configuration */
- TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
- TIM_CCxNCmd(TIM1, TIM_Channel_1, TIM_CCxN_Enable);
- TIM_ForcedOC1Config(TIM1,TIM_ForcedAction_InActive);
-
- TIM_GenerateEvent(TIM1, TIM_EventSource_COM);
- break;
- default:break;
-
-
-
- }
- }
- }
- void Nvic_Init(void)
- {
-
-
-
- NVIC_InitTypeDef NVIC_InitStructure; //中断结构体定?
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能IRQ通道
- NVIC_Init(&NVIC_InitStructure);
-
-
- // 可以继续增加,设置中断优先级
- }
-
- void TIM1_TRG_COM_IRQHandler(void)
- {
- /* Clear TIM1 COM pending bit */
- TIM_ClearITPendingBit(TIM1, TIM_IT_COM);
- BLDC_Hall_Convet();//换向
- }
- int main(void)
- {
- SystemInit(); //系统时钟配置函数,通过不同的时钟定义,来选择不同的主频
- Nvic_Init();
- Timer1_Init();
- BLDC_Hall_Convet();
-
- while(1)
- {
-
-
-
- }
-
- }
-
复制代码
所有资料51hei提供下载:
霍尔六步换向成功.rar
(289.9 KB, 下载次数: 250)
|