#include "hall.h"
unsigned char FaultF=0;//故障标志
unsigned char Run_dir=1;//运转方向,0-1对立
unsigned char bHallStartStep;
unsigned int OutPwmValue=0;
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};
const unsigned char PWM_EN2_TAB[6]={0x0,0x00,0x00,0x0,0x1,0x1};
void Init_TIM2(void)
{
//counter disabled, ARR preload register disabled, up counting, edge aligned mode
TIM2->CR1 = BIT2;
//disable all interrupts
TIM2->IER = 0;//禁止中断
TIM2->CCMR1 = 0x01;//通道配置为输入
TIM2->CCMR2 = 0x01;
TIM2->CCMR3 = 0x01;
#define IC_FILTER (u8)(5 << 4)//0101 0000
TIM2->CCMR1 |= IC_FILTER; //输入捕获滤波器
TIM2->CCMR2 |= IC_FILTER;
TIM2->CCMR3 |= IC_FILTER;
//prescale = div3
@ 16MHz -> 0.5us/count * 24MHz -> 0.33us/count
// 8/16MHz=0.5us, 8/24MHz=0.33us
TIM2->PSCR = 0;
TIM2->ARRH=0xff;//计数周期设为最大
TIM2->ARRL=0xff;
TIM2->CCER1 |= 0x01;//使能捕获功能
TIM2->CCER1 |= 0x10;
TIM2->CCER2 |= 0x01;
TIM2->CR1 |= 0x01;//使能定时器
}
void TIM2_InitCapturePolarity(void)
{
u8 bHStatus = 0;
GPIOD->DDR &= (u8)(~(0x1c));
// GPIOD->CR1|=(u8)(0x1c);
// 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();
}
@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)
{
FaultF=1;
}
ComHandler();
return;
}
void ComHandler(void)
{
TIM1->BKR &= (uint8_t)(~TIM1_BKR_MOE);//禁止PWM输出
PWM_A_OFF;
PWM_B_OFF;
PWM_C_OFF;
if(FaultF!=0)return;
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输出
}
此帖出自
小平头技术问答
一周热门 更多>