MSP430单片机步进电机程序
扫描二维码
随时随地手机看文章
MSP430单片机步进电机程序
#include"msp430x22x4.h"
#defineucharunsignedchar
#defineuintunsignedint
uchari=0;
//扫描式4x4键盘
#definekeyin(P2IN&0x0F)
//数码管值0~f
unsignedcharkey[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
unsignedcharhang[]={0xef,0xdf,0xbf,0x7f};//选中行码
unsignedcharlie[]={0x0e,0x0d,0x0b,0x07};//相应列值
unsignedcharsave,a,b;
voidDELAY(uintaj)
{
while(aj--);
}
voidinit_port(void)
{
P1SEL|=BIT2+BIT3;//TA1,TA2输出功能
P1DIR|=BIT2+BIT3;
P2DIR=0XF0;
P2SEL=0;
P2OUT=0;
P2IE=0X0F;
P2IES=0XF0;
P2IFG=0;
P3DIR=0XFF;//p3.0--AP3.1---BP3.2---CP3.3---D
P3SEL=0X00;
P3OUT=0XF0;
_EINT();
return;
}
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//关看门狗
BCSCTL1=CALBC1_1MHz;//设定DCO为1MHZ
DCOCTL=CALBC1_1MHZ;
init_port();
while(1)
{
while(i==1)//单四拍
{
P3OUT=0XF1;//11110100
DELAY(390);
P3OUT=0XF2;//11110010
DELAY(390);
P3OUT=0XF4;//11110001
DELAY(390);
P3OUT=0XF8;//11111000
DELAY(390);
}
while(i==2)//单四拍反转
{
P3OUT=0X08;//D
DELAY(390);
P3OUT=0X04;//C
DELAY(390);
P3OUT=0X02;//B
DELAY(390);
P3OUT=0X01;//A
DELAY(390);
}
while(i==3)//双四拍
{
P3OUT=0X03;//AB
DELAY(390);
P3OUT=0X06;//BC
DELAY(390);
P3OUT=0X0c;//CD
DELAY(390);
P3OUT=0X09;//DA
DELAY(390);
}
while(i==4)//双四拍反转
{
P3OUT=0X09;//DA
DELAY(390);
P3OUT=0X0c;//CD
DELAY(390);
P3OUT=0X06;//BC
DELAY(390);
P3OUT=0X03;//AB
DELAY(390);
}
while(i==5)//单八拍
{
P3OUT=0X01;//A
DELAY(390);
P3OUT=0X03;//AB
DELAY(390);
P3OUT=0X02;//B
DELAY(390);
P3OUT=0X06;//BC
DELAY(390);
P3OUT=0X04;//C
DELAY(390);
P3OUT=0X0c;//CD
DELAY(390);
P3OUT=0X08;//D
DELAY(390);
P3OUT=0X09;//DA
DELAY(390);
}
while(i==6)//单八拍反转
{
P3OUT=0X09;//DA
DELAY(390);
P3OUT=0X08;//D
DELAY(390);
P3OUT=0X0c;//CD
DELAY(390);
P3OUT=0X04;//C
DELAY(390);
P3OUT=0X06;//BC
DELAY(390);
P3OUT=0X02;//B
DELAY(390);
P3OUT=0X03;//AB
DELAY(390);
P3OUT=0X01;//A
DELAY(390);
}
while(i==7)//双八拍
{
P3OUT=0X03;//AB
DELAY(390);
P3OUT=0X07;//ABC
DELAY(390);
P3OUT=0X06;//BC
DELAY(390);
P3OUT=0X0E;//BCD
DELAY(390);
P3OUT=0X0c;//CD
DELAY(390);
P3OUT=0X0D;//CDA
DELAY(390);
P3OUT=0X09;//DA
DELAY(390);
P3OUT=0X0B;//DAB
DELAY(390);
}
while(i==8)//双八拍反转
{
P3OUT=0X0B;//DAB
DELAY(390);
P3OUT=0X09;//DA
DELAY(390);
P3OUT=0X0D;//CDA
DELAY(390);
P3OUT=0X0c;//CD
DELAY(390);
P3OUT=0X0E;//BCD
DELAY(390);
P3OUT=0X06;//BC
DELAY(390);
P3OUT=0X07;//ABC
DELAY(390);
P3OUT=0X03;//AB
DELAY(390);
}
}
}
voidkeyprocess(void)
{
for(a=0;a<4;a++)
{
P2OUT=hang[a];//逐行扫描
if(keyin!=0x0f)break;//检测该行有键按下则退出循环
}
for(b=0;b<4;b++)//逐列扫描
{
if(keyin==lie[b])
{
save=key[a][b];break;//检测该列有键按下则退出循环
}
}
switch(save)
{
case1:i=1;break;
case2:i=2;break;
case3:i=3;break;
case4:i=4;break;
case5:i=5;break;
case6:i=6;break;
case7:i=7;break;
case8:i=8;break;
case9:
case10:
case11:
case12:
case13:
case14:
case15:
case16:break;
}
return;
}
#pragmavector=PORT2_VECTOR
__interruptvoidPORT_ISR(void)
{
keyprocess();
P2IFG=0;
P2OUT=0;
return;
}