?? stepmotorcontroller.c.c
字號:
#include <AT89X51.h>
sbit step=P1^0;
sbit direct=P1^1;
sbit speed=P1^2;
sbit set=P3^2;
sbit ok=P1^3;
sbit led1=P1^4;
sbit led2=P1^5;
sbit led3=P1^6;
sbit led4=P1^7;
bit drt=0,dr0=0,tt=0;
unsigned char stp=0,spd=1;
unsigned int count;
unsigned char n=0,m=0;
unsigned int pvalue[4]={0x83,0x86,0x8c,0x89}; /**********步進(jìn)電機(jī)正轉(zhuǎn)輸入值***********/
unsigned int nvalue[4]={0x89,0x8c,0x86,0x83}; /**********步進(jìn)電機(jī)反轉(zhuǎn)輸入值***********/
void start(void);
void wait(unsigned int z);
void display(unsigned char x);
void readkeys(void);
/*********************************************/
/******************主程序********************/
void main(void)
{
IT0=1;
EX0=1; /*********設(shè)定中斷方式*********/
EA=1;
while(1)
{
if(ok==0) start();
}
}
/****************************************************/
/****************中斷處理****************************/
extern0() interrupt 0
{
readkeys();
display(stp);
start();
}
/**************************************************/
/******************讀鍵盤**************************/
void readkeys(void)
{
display(stp);
while(ok!=0) /*************在按下OK鍵前讀********/
{
count=300;
wait(count); /************去抖動(dòng)**************/
if(ok==0) break;
if(step==0) /******************讀步數(shù)值,邊讀邊顯示****************/
{
count=300;
wait(count);
if(step==0)
{
stp++;
if(stp==100) stp=0;
display(stp);
count=10000;
wait(count);
}
}
if(direct==0) /************讀方向值,1為反轉(zhuǎn),0為正轉(zhuǎn),連讀邊顯示**********/
{
count=300;
wait(count);
if(direct==0)
{
drt=~drt;
display(drt);
count=20000;
wait(count);
}
}
if(speed==0) /*******************讀速度值,1~4倍速*********************/
{
count=300;
wait(count);
if(speed==0)
{
spd++;
if(spd==5) spd=1;
display(spd);
count=20000;
wait(count);
}
}
}
}
/*******************************************************/
/*********************對設(shè)定值進(jìn)行顯示*****************/
void display(unsigned char x)
{
unsigned char firstbit=0,secondbit=0;
if(x<10) P2=x+128; /************顯示一位數(shù)*************/
else
{
firstbit=x/10; /************顯示兩位數(shù)*************/
secondbit=x-firstbit*10;
P2=secondbit+192; /******顯示個(gè)位*******/
switch(firstbit) /******顯示十位*******/
{
case 1:led1=1;led2=0;led3=0;led4=0;break;
case 2:led1=0;led2=1;led3=0;led4=0;break;
case 3:led1=1;led2=1;led3=0;led4=0;break;
case 4:led1=0;led2=0;led3=1;led4=0;break;
case 5:led1=1;led2=0;led3=1;led4=0;break;
case 6:led1=0;led2=1;led3=1;led4=0;break;
case 7:led1=1;led2=1;led3=1;led4=0;break;
case 8:led1=0;led2=0;led3=0;led4=1;break;
case 9:led1=1;led2=0;led3=0;led4=1;break;
}
}
}
/****************************************************/
/*************************延時(shí)子程序*****************/
void wait(unsigned int z)
{
unsigned int w;
for (w=0;w<z;w++);
}
/******************************************************/
/**********************啟動(dòng)步進(jìn)電機(jī)********************/
void start(void)
{
unsigned char v;
tt=dr0 ^ drt;
while(stp!=0)
{
if(drt==0) /*****************************正轉(zhuǎn)*******************************/
{
if(tt==1) /***判斷上一次是正轉(zhuǎn)還是反轉(zhuǎn)******/
{
switch(m)
{ /****反轉(zhuǎn),則尋找上次通電位置***/
case 0:n=0;
case 1:n=3;
case 2:n=2;
case 3:n=1;
}
}
for(v=1;v<=stp;v++) /***********開始正轉(zhuǎn) ************/
{
P0=pvalue[n];
n++;
if(n==4) n=0;
wait(40000/spd);
}
dr0=drt;
break;
}
if(drt==1) /********************************反轉(zhuǎn)******************************/
{
if(tt==1) /***判斷上一次是正轉(zhuǎn)還是反轉(zhuǎn)*****/
{
switch(n) /***正轉(zhuǎn),則尋找上次通電位置***/
{
case 0:m=0;
case 1:m=3;
case 2:m=2;
case 3:m=1;
}
}
for(v=1;v<=stp;v++) /***********開始反轉(zhuǎn) ************/
{
P0=nvalue[m];
m++;
if(m==4) m=0;
wait(40000/spd);
}
dr0=drt;
break;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -