MSP430 单片机 步进电机带显示程序
#include "msp430x22x4.h"
#define uchar unsigned char
#define uint unsigned int
#define comm 0
#define dat 1
#define sclk_1 P4OUT|=0x01 //sclk=1; 001----p4.0
#define sclk_0 P4OUT&=0x06 //sclk=0; 110----p4.1
#define std_1 P4OUT|=0x02 //std =1; 010
#define std_0 P4OUT&=0x05 //std =0; 101
#define keyin (P2IN&0x0F)
unsigned char key[4][4]={{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
uchar hang[]={0xef,0xdf,0xbf,0x7f};// 选中行码
uchar lie[]={0x0e,0x0d,0x0b,0x07};//相应列值
unsigned char save,aa,bb;
uchar m=1,j=0;
uchar M=0,key1=0,key2=0;
uchar flag_key1=0,flag_key2=0,flag_go=0;
float nquan=0x00;
uchar caidan[]={
"Please Input M "
"电机转动 度"
" M= "
"****************"
};
uchar caidan1[]={"0123456789."};
// 扫描式4x4键盘
void delay(uint aj) //us
{
while(aj--);
}
void DELAY(uint aj) //ms
{
for(uint fl=0;fl<1000;fl++)
while(aj--);
}
void init_port(void)
{
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=0X00;
P4DIR=0XFF; //P4口输出 作液晶接口p4.0--SCK p4.1---sid
P4SEL=0;
P4OUT=0;
_EINT();
return;
}
/*
********************************************************************************
*模块名称:wr_LCD定义
*完成日期:08-07-29
*修改内容:none
********************************************************************************/
//********写命令/数据***************************************************
void wr_lcd (unsigned char dat_comm,unsigned char content)
{
unsigned char a,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
********************************************************************************
*/
void init_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
********************************************************************************
*/
void clrram (void)
{
wr_lcd (comm,0x30);
wr_lcd (comm,0x01);
delay (180);
}
/*
********************************************************************************
*模块名称:显示汉字或字符定义
*完成日期:08-7-29
*修改内容:none
********************************************************************************
*/
void chn_disp(unsigned char *chn)
{
unsigned char i;
wr_lcd (comm,0x30);
wr_lcd (comm,0x80);
for (i=0;i<64;i++)
wr_lcd (dat,chn[i]);
}
//****************lcd_mesg定点显示***********************************
void lcd_mesg(uint gao,uchar p,uchar q,uchar w)
{
uint adder1,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);
}
void lcd_mesg1(uint gao,uchar p)
{
uint adder1,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);
}
void lcd_mesg2(uchar gao,uchar di,uchar p)
{
wr_lcd(comm,p);
wr_lcd(dat,caidan1[gao]);delay(20);
wr_lcd(dat,caidan1[di]);delay(20);
}
/*******************************************************************************/
void keyprocess(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)
{
case 0:
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;
case 1:
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;
case 2:
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;
case 3:
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;
case 4:
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;
case 5:
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;
case 6:
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;
case 7:
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;
case 8:
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;
case 9:
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;
case 12:flag_key1=1;break;
case 13:flag_key2=1;break;
case 14:flag_go=1;break;
case 10:
case 11:
case 15:break;
}
return;
}
#pragma vector = PORT2_VECTOR
__interrupt void PORT_ISR(void)
{
keyprocess();
P2IFG=0;
P2OUT = 0;
return;
}
void main(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);
}
}}
上一篇:MSP430单片机1602液晶显示程序
下一篇:MSP430单片机步进电机程序
推荐阅读最新更新时间:2024-03-16 15:53