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;