#include <GLOBAL.h>
#include <stdlib.h>
#include <stdbool.h>
#include <msp430x21x2.h>
extern void SoftWareDelay(unsigned long time);
#define XTAL 16 //可定义为你所用的晶振频率(单位Mhz)
#define DelayUs(x) __delay_cycles ((unsigned long)(((unsigned long)x) * XTAL))
#define DelayMs(x) __delay_cycles ((unsigned long)(((unsigned long)x) * XTAL * 1000))
#define DelaySs(x) __delay_cycles ((unsigned long)(((unsigned long)x) * XTAL * 1000000))
#define MosMahDirs P1DIR |= BIT1
#define MosMahClos P1OUT |= BIT1
#define MosMahOpen P1OUT &=~ BIT1
#define MosMalDirs P1DIR |= BIT0
#define MosMalClos P1OUT |= BIT0
#define MosMalOpen P1OUT &= ~BIT0
#define MosMbhDirs P2DIR |= BIT4
#define MosMbhClos P2OUT |= BIT4
#define MosMbhOpen P2OUT &=~ BIT4
#define MosMblDirs P2DIR |= BIT3
#define MosMblClos P2OUT |= BIT3
#define MosMblOpen P2OUT &= ~BIT3
#define MosMchDirs P3DIR |= BIT7
#define MosMchClos P3OUT |= BIT7
#define MosMchOpen P3OUT &=~ BIT7
#define MosMclDirs P3DIR |= BIT6
#define MosMclClos P3OUT |= BIT6
#define MosMclOpen P3OUT &=~ BIT6
#define PulseOneDirIn P2DIR &= ~BIT2//---'A'
#define PulseTwoDinIn P2DIR &= ~BIT1//---'B'
#define PulseThrDinIn P2DIR &= ~BIT0//---'C'
#define PulseOneSt P2IN & BIT2
#define PulseTwoSt P2IN & BIT1
#define PulseThrSt P2IN & BIT0
void MotoDelay(unsigned long TimerDelay)//---大概1微秒
{
while(TimerDelay--);
}
void ChkSing(unsigned char SingNo)
{
PulseOneDirIn;
PulseTwoDinIn;
PulseThrDinIn;
SinHigInitEn=0;
//DelayUs(1);
switch(SingNo) //设置PWM通道1的输出模式。
{
case 'A':case 'a': //如果设置为高电平模式
if(PulseOneSt)
{
SinHigInitEn=1;
}
break;
case 'B':case 'b': //如果设置为低电平模式
if(PulseTwoSt)
{
SinHigInitEn=1;
}
break;
case 'C':case 'c': //如果设置为禁用
if(PulseThrSt)
{
SinHigInitEn=1;
}
break;
}
}
void SinHigInt(unsigned char InitMode)
{
SinInitFg=0;
PulseOneDirIn;
PulseTwoDinIn;
PulseThrDinIn;
switch(InitMode) //设置PWM通道1的输出模式。
{
case 'A':case 'a': //如果设置为高电平模式
P2REN = BIT2;
P2OUT = BIT2;
P2IE = BIT2;
P2IES &= ~BIT2;
break;
case 'B':case 'b': //如果设置为低电平模式
P2REN = BIT1;
P2OUT = BIT1;
P2IE = BIT1;
P2IES &= ~BIT1;
break;
case 'C':case 'c': //如果设置为禁用
P2REN = BIT0;
P2OUT = BIT0;
P2IE = BIT0;
P2IES &= ~BIT0;
break;
}
}
void SinLowInt(unsigned char InitMode)
{
SinInitFg=0;
PulseOneDirIn;
PulseTwoDinIn;
PulseThrDinIn;
switch(InitMode) //设置PWM通道1的输出模式。
{
case 'A':case 'a': //如果设置为高电平模式
P2REN = BIT2;
P2OUT = BIT2;
P2IES = BIT2;
P2IE = BIT2;
break;
case 'B':case 'b': //如果设置为低电平模式
P2REN = BIT1;
P2OUT = BIT1;
P2IES = BIT1;
P2IE = BIT1;
break;
case 'C':case 'c': //如果设置为禁用
P2REN = BIT0;
P2OUT = BIT0;
P2IES = BIT0;
P2IE = BIT0;
break;
}
}
void MotoInit(void)
{
MosMahDirs;
MosMbhDirs;
MosMchDirs;
MosMalDirs;
MosMblDirs;
MosMclDirs;
MosMahClos;//---1
MosMbhClos;//---2
MosMchClos;//---3
MosMalClos;//---4
MosMblClos;//---5
MosMclClos;//---6
}
void MotoDrv(unsigned char TempPhase,bool InitEn)
{
switch(TempPhase)
{
case 0://---A+B-
//--先关闭C+B-
MosMchClos;//---3
MosMbhClos;//---2
MosMalClos;//---4
MosMclClos;//---6
//DelayUs(3);
if(InitEn)
{
Phase='C';
SinLowInt(Phase);
}
MosMahOpen;//---1
MosMblOpen;//---5
break;
case 1://---A+C-
//--先关闭B-
MosMblClos;//---5
MosMalClos;//---4
MosMbhClos;//---2
MosMchClos;//---3
//DelayUs(3);
if(InitEn)
{
Phase='B';
SinHigInt(Phase);
}
MosMahOpen;//---1
MosMclOpen;//---6
break;
case 2://---B+C-
//--先关闭A+
MosMahClos;//---1
MosMchClos;//---3
MosMalClos;//---4
MosMblClos;//---5
//DelayUs(3);
if(InitEn)
{
Phase='A';
SinLowInt(Phase);
}
MosMbhOpen;//---2
MosMclOpen;//---6
break;
case 3://---B+A-
//--先关闭C-
MosMclClos;//---6
MosMblClos;//---5
MosMahClos;//---1
MosMchClos;//---3
//DelayUs(3);
if(InitEn)
{
Phase='C';
SinHigInt(Phase);
}
MosMbhOpen;//---2
MosMalOpen;//---4
break;
case 4://---C+A-
//--先关闭B+
MosMbhClos;//---2
MosMahClos;//---1
MosMblClos;//---5
MosMclClos;//---6
//DelayUs(3);
if(InitEn)
{
Phase='B';
SinLowInt(Phase);
}
MosMchOpen;//---3
MosMalOpen;//---4
break;
case 5://---C+B-
//--先关闭A-
MosMalClos;//---4
MosMclClos;//---6
MosMahClos;//---1
MosMbhClos;//---2
//DelayUs(3);
if(InitEn)
{
Phase='A';
SinHigInt(Phase);
}
MosMchOpen;//---3
MosMblOpen;//---5
break;
default:
MosMahClos;//---1
MosMbhClos;//---2
MosMchClos;//---3
MosMalClos;//---4
MosMblClos;//---5
MosMclClos;//---6
DelayUs(5);
break;
}
InitEn=0;
}
void MotoStp(void)
{
MosMahClos;//---1
MosMbhClos;//---2
MosMchClos;//---3
MosMalClos;//---4
MosMblClos;//---5
MosMclClos;//---6
DelayUs(5);
MotoSt=1;
}
void MotoStr(void)
{
unsigned int Timer=300;
unsigned int i=0;
if(!AutoRunEn)
{
MotoStrEn=0;
MotorPhase=0;
PwmInit();
MotoDrv(0,0);//----A+B-
DelayMs(50);
while(1)
{
for(i=0;i<Timer;i++)
{
DelayUs(25);
}
//Timer-=Timer/15+1;
Timer-=15;
if(Timer<25)
{
Timer=5;
AutoRunEn=1;
MotoDrv(MotorPhase,1);
return;
}
MotoDrv(MotorPhase,0);
MotorPhase++;
MotorPhase%=6;
}
}
else
{
MotoSt=2;
}
}
void MotoRun(void)
{
ChkSing(Phase);
if(SinHigInitEn)
{
SenseEn=1;
}
else
{
SenseEn=0;
}
switch(MotorPhase)
{
case 0:
if(!SenseEn)
{
Phase='B';
SinLowInt(Phase);
MotoDrv(1,0);//---A+C-
MotorPhase=1;
MotoStrTime=30;
}
else
{
Phase='C';
MotoDrv(0,0);//---A+B-;
}
break;
case 1:
if(!SenseEn)
{
Phase='A';
SinLowInt(Phase);
MotoDrv(2,0);//---B+C-;
MotorPhase=2;
MotoStrTime=30;
}
else
{
Phase='B';
MotoDrv(1,0);//---A+C-
}
break;
case 2:
if(!SenseEn)
{
Phase='C';
SinLowInt(Phase);
MotoDrv(3,0);//---B+A-
MotorPhase=3;
MotoStrTime=30;
}
else
{
Phase='A';
MotoDrv(2,0);//---B+C-;
}
break;
case 3:
if(!SenseEn)
{
Phase='B';
SinLowInt(Phase);
MotoDrv(4,0);//---C+A-
MotorPhase=4;
MotoStrTime=30;
}
else
{
Phase='C';
MotoDrv(3,0);//---B+A-;
}
break;
case 4:
if(!SenseEn)
{
Phase='A';
SinLowInt(Phase);
MotoDrv(5,0);//---C+B-
MotorPhase=5;
MotoStrTime=30;
}
else
{
Phase='B';
MotoDrv(4,0);//---C+A-;
}
break;
case 5:
if(!SenseEn)
{
Phase='C';
SinLowInt(Phase);
MotoDrv(0,0);//---A+B-
MotorPhase=0;
MotoStrTime=30;
}
else
{
Phase='A';
MotoDrv(5,0);//---C+B-;
}
break;
}
}
void MotoCon(unsigned int *DataDptr,unsigned int TimesliceFg)
{
unsigned int i;
i=~TimesliceFg;
if(*DataDptr&TimesliceFg)
{
*DataDptr&=i;
}
switch(MotoSt)
{
case 0: //---MotoStop
MotoStp();
break;
case 1: //---MotoStr
MotoStr();
break;
case 2: //---MotoRun
MotoRun();
break;
case 3: //---MotoRun
MotoSt=0;
MotoStrTime=16;
break;
}
}
#pragma vector=PORT2_VECTOR
__interrupt void P2ISR(void)
{
if((Phase=='A')|(Phase=='a'))
{
if(P2IFG&BIT2)
{
MotoEn=1;
}
}
else if((Phase=='B')|(Phase=='b'))
{
if(P2IFG&BIT1)
{
MotoEn=1;
}
}
else if((Phase=='C')|(Phase=='c'))
{
if(P2IFG&BIT0)
{
MotoEn=1;
}
}
MotoErrTime=5;
P2IFG = 0;
}
|