msp430单片机控制步进电机带显示程序
扫描二维码
随时随地手机看文章
MSP430单片机控制步进电机带显示程序
#include"msp430x22x4.h"
#defineucharunsignedchar
#defineuintunsignedint
#definecomm0
#definedat1
#definesclk_1P4OUT|=0x01//sclk=1;001----p4.0
#definesclk_0P4OUT&=0x06//sclk=0;110----p4.1
#definestd_1P4OUT|=0x02//std=1;010
#definestd_0P4OUT&=0x05//std=0;101
#definekeyin(P2IN&0x0F)
unsignedcharkey[4][4]={{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
ucharhang[]={0xef,0xdf,0xbf,0x7f};//选中行码
ucharlie[]={0x0e,0x0d,0x0b,0x07};//相应列值
unsignedcharsave,aa,bb;
ucharm=1,j=0;
ucharM=0,key1=0,key2=0;
ucharflag_key1=0,flag_key2=0,flag_go=0;
floatnquan=0x00;
ucharcaidan[]={
"PleaseInputM"
"电机转动度"
"M="
"****************"
};
ucharcaidan1[]={"0123456789."};
//扫描式4x4键盘
voiddelay(uintaj)//us
{
while(aj--);
}
voidDELAY(uintaj)//ms
{
for(uintfl=0;fl<1000;fl++)
while(aj--);
}
voidinit_port(void)
{
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=0X00;
P4DIR=0XFF;//P4口输出作液晶接口p4.0--SCKp4.1---sid
P4SEL=0;
P4OUT=0;
_EINT();
return;
}
/*
********************************************************************************
*模块名称:wr_LCD定义
*完成日期:08-07-29
*修改内容:none
********************************************************************************/
//********写命令/数据***************************************************
voidwr_lcd(unsignedchardat_comm,unsignedcharcontent)
{
unsignedchara,b,i,j;
delay(50);
a=content;
//cs_1;
sclk_0;
std_0;
std_1;
for(i=0;i<5;i++)
{sclk_1;sclk_0;}
std_0;
sclk_1;
sclk_0;
if(dat_comm)std_1;
else
std_0;
sclk_1;
sclk_0;
std_0;
sclk_1;
sclk_0;
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{b=a;
if((b&=0x80)==0)
{std_0;a=a<<1;}
else
{std_1;a=a<<1;}
sclk_1;sclk_0;
}
std_0;
for(i=0;i<4;i++)
{sclk_1;sclk_0;}
}
}
/*********************************************************************************
*模块名称:初始化定义
*完成日期:08-7-29
*修改内容:none
********************************************************************************
*/
voidinit_lcd(void)
{
wr_lcd(comm,0x30);/*30---基本指令动作*/
wr_lcd(comm,0x01);/*清屏,地址指针指向00H*/
delay(100);
wr_lcd(comm,0x06);/*光标的移动方向*/
wr_lcd(comm,0x0c);/*开显示,关游标*/
}
/*
********************************************************************************
*模块名称:清DDRAM定义
*完成日期:08-7-29
*修改内容:none
********************************************************************************
*/
voidclrram(void)
{
wr_lcd(comm,0x30);
wr_lcd(comm,0x01);
delay(180);
}
/*
********************************************************************************
*模块名称:显示汉字或字符定义
*完成日期:08-7-29
*修改内容:none
********************************************************************************
*/
voidchn_disp(unsignedchar*chn)
{
unsignedchari;
wr_lcd(comm,0x30);
wr_lcd(comm,0x80);
for(i=0;i<64;i++)
wr_lcd(dat,chn[i]);
}
//****************lcd_mesg定点显示***********************************
voidlcd_mesg(uintgao,ucharp,ucharq,ucharw)
{
uintadder1,adder2,adder3,adder4;
adder1=gao/1000,gao=gao-adder1*1000;
adder2=gao/100,gao=gao-adder2*100;
adder3=gao/10,adder4=gao%10;
wr_lcd(comm,p);
wr_lcd(dat,caidan1[adder1]);delay(20);
wr_lcd(dat,caidan1[adder2]);delay(20);
wr_lcd(comm,q);
wr_lcd(dat,caidan1[adder3]);delay(20);
wr_lcd(dat,caidan1[10]);delay(20);
wr_lcd(comm,w);
wr_lcd(dat,caidan1[adder4]);delay(20);
wr_lcd(dat,caidan1[0]);delay(20);
}
voidlcd_mesg1(uintgao,ucharp)
{
uintadder1,adder2;
adder1=gao/10,adder2=gao%10;
wr_lcd(comm,p);
wr_lcd(dat,caidan1[adder1]);delay(20);
wr_lcd(dat,caidan1[adder2]);delay(20);
}
voidlcd_mesg2(uchargao,uchardi,ucharp)
{
wr_lcd(comm,p);
wr_lcd(dat,caidan1[gao]);delay(20);
wr_lcd(dat,caidan1[di]);delay(20);
}
/*******************************************************************************/
voidkeyprocess(void)
{
for(aa=0;aa<4;aa++)
{
P2OUT=hang[aa];//逐行扫描
if(keyin!=0x0f)break;//检测该行有键按下则退出循环
}
for(bb=0;bb<4;bb++)//逐列扫描
{
if(keyin==lie[bb])
{
save=key[aa][bb];break;//检测该列有键按下则退出循环
}
}
switch(save)
{
case0:
if(flag_key1==1)
{key1=0,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=0,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case1:
if(flag_key1==1)
{key1=1,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=1,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case2:
if(flag_key1==1)
{key1=2,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=2,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case3:
if(flag_key1==1)
{key1=3,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=3,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case4:
if(flag_key1==1)
{key1=4,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=4,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case5:
if(flag_key1==1)
{key1=5,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=5,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case6:
if(flag_key1==1)
{key1=6,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=6,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case7:
if(flag_key1==1)
{key1=7,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=7,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case8:
if(flag_key1==1)
{key1=8,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=8,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case9:
if(flag_key1==1)
{key1=9,flag_key1=0;lcd_mesg2(key1,key2,0x92);}
if(flag_key2==1)
{key2=9,flag_key2=0;lcd_mesg2(key1,key2,0x92);}
break;
case12:flag_key1=1;break;
case13:flag_key2=1;break;
case14:flag_go=1;break;
case10:
case11:
case15:break;
}
return;
}
#pragmavector=PORT2_VECTOR
__interruptvoidPORT_ISR(void)
{
keyprocess();
P2IFG=0;
P2OUT=0;
return;
}
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//关看门狗
BCSCTL1=CALBC1_1MHz;//设定DCO为1MHZ
DCOCTL=CALBC1_1MHZ;
init_port();
init_lcd();
chn_disp(caidan);
while(1){
while((key1|key2==0)&&(flag_go==0));
key1=key1*10;
M=key1+key2;
nquan=50/M*72;
lcd_mesg(nquan,0x8c,0x8d,0x8e);delay(10);//显示转动度数
for(;j<50/M;j++)//双8拍j=50刚好一圈360最小度数为7.2度---j=1
{
P3OUT=0X03;//AB
delay(420);
P3OUT=0X07;//ABC
delay(420);
P3OUT=0X06;//BC
delay(420);
P3OUT=0X0E;//BCD
delay(420);
P3OUT=0X0c;//CD
delay(420);
P3OUT=0X0D;//CDA
delay(420);
P3OUT=0X09;//DA
delay(420);
P3OUT=0X0B;//DAB
delay(420);
}
}}