#include "hall.h"
unsigned char bHallStartStep;//换相步序变量定义
//换相步序值0-5分别对应ABACBCBACACB
unsigned int OutPwmValue=0;//PWM输出值变量定义
unsigned char St_F=0;//启停标志,0时为停止,1时为启动
unsigned char FaultF=0;//故障标志
unsigned char Run_dir=0;//运转方向,0-1对立
unsigned char bHallSteps[2][8]={
{7,5,3,4,1,0,2,7},//正转
{7,2,0,1,4,3,5,7}//反转
};
const unsigned char PWM_EN1_TAB[6]={0x01,0x01,0x10,0x10,0x00,0x00};
//六步法中,CH1CH2通道极性及使能配置
const unsigned char PWM_EN2_TAB[6]={0x0,0x00,0x00,0x0,0x1,0x1};
//六步法中,CH3通道极性及使能配置
此帖出自
小平头技术问答
#include "hall.h"
unsigned char bHallStartStep;//换相步序变量定义
//换相步序值0-5分别对应ABACBCBACACB
unsigned int OutPwmValue=0;//PWM输出值变量定义
unsigned char St_F=0;//启停标志,0时为停止,1时为启动
unsigned char FaultF=0;//故障标志
unsigned char Run_dir=0;//运转方向,0-1对立
unsigned char bHallSteps[2][8]={
{7,5,3,4,1,0,2,7},//正转
{7,2,0,1,4,3,5,7}//反转
};
const unsigned char PWM_EN1_TAB[6]={0x01,0x01,0x10,0x10,0x00,0x00};
//六步法中,CH1CH2通道极性及使能配置
const unsigned char PWM_EN2_TAB[6]={0x0,0x00,0x00,0x0,0x1,0x1};
//六步法中,CH3通道极性及使能配置
//初始化HALL捕获输入定义器
void Init_TIM2(void)
{
//计数器禁止、ARR预装载禁止、向上计数、边沿对齐模式
TIM2->CR1 = BIT2;
//禁止TIM2所有中断
TIM2->IER = 0;//禁止中断
TIM2->CCMR1 = 0x01;//TIM2的CH1CH2CH3通道配置为输入
TIM2->CCMR2 = 0x01;
TIM2->CCMR3 = 0x01;
#define IC_FILTER (u8)(5 << 4)
TIM2->CCMR1 |= IC_FILTER; //输入捕获滤波器
TIM2->CCMR2 |= IC_FILTER;
TIM2->CCMR3 |= IC_FILTER;
//prescale = div3 @ 16MHz -> 0.5us/count * 24MHz -> 0.33us/count
TIM2->PSCR = 0;
TIM2->ARRH=0xff;//计数周期设为最大
TIM2->ARRL=0xff;
TIM2->CCER1 |= 0x01;//TIM2的CH1CH2CH3通道使能捕获功能
TIM2->CCER1 |= 0x10;
TIM2->CCER2 |= 0x01;
TIM2->CR1 |= 0x01;//使能TIM2定时器
}
//根据HALL状态换相,启动电机运行
void TIM2_InitCapturePolarity(void)
{
u8 bHStatus = 0;
GPIOD->DDR &= (u8)(~(0x1c));//HALL端口模式配置
// Read status of H1 and set the expected polarity
if (H1_PORT & H1_PIN)
{
TIM2->CCER1 |= BIT1;
bHStatus |= BIT2;
}
else
{
TIM2->CCER1 &= (u8)(~(BIT1));
}
// Read status of H2 and set the expected polarity
if (H2_PORT & H2_PIN)
{
TIM2->CCER1 |= BIT5;
bHStatus |= BIT1;
}
else
{
TIM2->CCER1 &= (u8)(~(BIT5));
}
// Read status of H3 and set the expected polarity
if (H3_PORT & H3_PIN)
{
TIM2->CCER2 |= BIT1;
bHStatus |= BIT0;
}
else
{
TIM2->CCER2 &= (u8)(~(BIT1));
}
bHallStartStep = bHallSteps[Run_dir][bHStatus];//得到换相步序
if (bHallStartStep == 7)//不该出现的HALL状态
{
FaultF=1;
return;
}
TIM2->SR1=(u8)~(TIM2_IT_CC3|TIM2_IT_CC2|TIM2_IT_CC1);
TIM2->IER = 0x0e;//使能输入捕获中断
ComHandler();//输出PWM信号,启动电机
}
//捕获中断,即HALL状态变化时,进入此中断
@near @interrupt @svlreg void TIM2_CAP_COM_IRQHandler(void)
{
u8 bHStatus = 0;
// Read status of H1 and set the expected polarity
if (H1_PORT & H1_PIN)
{
TIM2->CCER1 |= BIT1;
bHStatus |= BIT2;
}
else
{
TIM2->CCER1 &= (u8)(~(BIT1));
}
// Read status of H2 and set the expected polarity
if (H2_PORT & H2_PIN)
{
TIM2->CCER1 |= BIT5;
bHStatus |= BIT1;
}
else
{
TIM2->CCER1 &= (u8)(~(BIT5));
}
// Read status of H3 and set the expected polarity
if (H3_PORT & H3_PIN)
{
TIM2->CCER2 |= BIT1;
bHStatus |= BIT0;
}
else
{
TIM2->CCER2 &= (u8)(~(BIT1));
}
if (TIM2->SR1 & BIT2)
{
TIM2->SR1=(u8)(~TIM2_IT_CC2);
}
if (TIM2->SR1 & BIT1)
{
TIM2->SR1=(u8)(~TIM2_IT_CC1);
}
if (TIM2->SR1 & BIT3)
{
TIM2->SR1=(u8)(~TIM2_IT_CC3);
}
bHallStartStep = bHallSteps[Run_dir][bHStatus];//得到换相步序
if (bHallStartStep == 7)
{ //故障,停止输出
TIM1->BKR &= (uint8_t)(~TIM1_BKR_MOE);//禁止PWM输出
PWM_A_OFF;
PWM_B_OFF;
PWM_C_OFF;
FaultF=1;
return;
}
ComHandler(); //换相
return;
}
//换相子函数
void ComHandler(void)
{
TIM1->BKR &= (uint8_t)(~TIM1_BKR_MOE);//禁止PWM输出
if(FaultF!=0||St_F==0)
{PWM_A_OFF;PWM_B_OFF;PWM_C_OFF;return;}
if(bHallStartStep!=3&&bHallStartStep!=4)
PWM_A_OFF;
if(bHallStartStep!=0&&bHallStartStep!=5)
PWM_B_OFF;
if(bHallStartStep!=1&&bHallStartStep!=2)
PWM_C_OFF;
//根据换相步序,打开不同的开关管,并施加正确的PWM信号
if(bHallStartStep==0)//AB
{
TIM1->CCR1H = (uint8_t)(OutPwmValue >> 8);
TIM1->CCR1L = (uint8_t)(OutPwmValue);
PWM_B_ON;
}
else if(bHallStartStep==1) //AC
{
TIM1->CCR1H = (uint8_t)(OutPwmValue >> 8);
TIM1->CCR1L = (uint8_t)(OutPwmValue);
PWM_C_ON;
}
else if(bHallStartStep==2) //BC
{
TIM1->CCR2H = (uint8_t)(OutPwmValue >> 8);
TIM1->CCR2L = (uint8_t)(OutPwmValue);
PWM_C_ON;
}
else if(bHallStartStep==3) //BA
{
TIM1->CCR2H = (uint8_t)(OutPwmValue >> 8);
TIM1->CCR2L = (uint8_t)(OutPwmValue);
PWM_A_ON;
}
else if(bHallStartStep==4)//CA
{
TIM1->CCR3H = (uint8_t)(OutPwmValue >> 8);
TIM1->CCR3L = (uint8_t)(OutPwmValue);
PWM_A_ON;
}
else if(bHallStartStep==5) //CB
{
TIM1->CCR3H = (uint8_t)(OutPwmValue >> 8);
TIM1->CCR3L = (uint8_t)(OutPwmValue);
PWM_B_ON;
}
TIM1->CCER1=PWM_EN1_TAB[bHallStartStep];
TIM1->CCER2=PWM_EN2_TAB[bHallStartStep];
TIM1->BKR|=TIM1_BKR_MOE;//使能PWM输出
}
这是整个程序
TIM1->CCER2=PWM_EN2_TAB[bHallStartStep];
然后 bHallStartStep是个变量,再结合PWM_EN2_TAB【】这个数组,再看看CCER1和2的寄存器,你就能看明白了
一周热门 更多>