MSP430 单片机 步进电机程序
#include "msp430x22x4.h"
#define uchar unsigned char
#define uint unsigned int
uchar i=0;
// 扫描式4x4键盘
#define keyin (P2IN&0x0F)
//数码管值0~f
unsigned char key[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
unsigned char hang[]={0xef,0xdf,0xbf,0x7f};// 选中行码
unsigned char lie[]={0x0e,0x0d,0x0b,0x07};//相应列值
unsigned char save,a,b;
void DELAY(uint aj)
{
while(aj--);
}
void init_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--A P3.1---B P3.2---C P3.3---D
P3SEL=0X00;
P3OUT=0XF0;
_EINT();
return;
}
void main(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);
}
}
}
void keyprocess(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)
{
case 1:i=1;break;
case 2:i=2;break;
case 3:i=3;break;
case 4:i=4;break;
case 5:i=5;break;
case 6:i=6;break;
case 7:i=7;break;
case 8:i=8;break;
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:break;
}
return;
}
#pragma vector = PORT2_VECTOR
__interrupt void PORT_ISR(void)
{
keyprocess();
P2IFG=0;
P2OUT = 0;
return;
}
上一篇:MSP430单片机步进电机带显示程序
下一篇:MSP430超声波测距
推荐阅读最新更新时间:2024-03-16 15:53