最新51单片机控制的步进电机C语言程序汇总
- 格式:doc
- 大小:39.50 KB
- 文档页数:11
51单片机控制的步进电机C语言程序
51单片机控制的步进电机C语言程序
用的是L298驱动的和ULN2003一样,你把它换成2003就行拉
#include
unsigned char code
table[]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9,0x00,0xf1,0xf9,0xf8,0xfc,0xf4, 0xf6,0xf2,0xf3,0x00};
unsigned char temp,temp_old;
unsigned char key;
unsigned char i,j,k,m,s;
void delay(int i)
{
for(m=i;m>0;m--)
for(j=250;j>0;j--)
for(k=10;k>0;k--);
}
void saomiao()
{
P3=0xff;
P3_4=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case 0x0e:
key=1;
break;
case 0x0d:
key=2;
break;
case 0x0b:
key=3;
break;
case 0x07:
key=4;
break;
}
temp=P3;
temp=temp&0x0f; while(temp!=0x0f) {
temp=P3;
temp=temp&0x0f; }
}
}
P3=0xff;
P3_5=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f; switch(temp)
{
case 0x0d:
key=5;
break;
case 0x0b:
key=6;
break;
case 0x07:
key=7;
break;
}
temp=P3;
temp=temp&0x0f; while(temp!=0x0f) {
temp=P3;
temp=temp&0x0f; }
}
}
}
void main(void)
{
while(1)
{
saomiao();
if(key==1)
{ P1=0;
P2=0;
saomiao();
}
if(key==2)
{
temp_old=key;
for(s=0;s<8;s++)
{ P2=table[s]; P1_4=0;
delay(13);
saomiao();
if(key!=temp_old) {
P1_4=1;
break;
}
}
}
if(key==3)
{
temp_old=key;
for(s=0;s<8;s++)
{ P2=table[s]; P1_5=0;
delay(5);
saomiao();
if(key!=temp_old) {
P1_5=1;
break;
}
}
}
if(key==4)
{
temp_old=key; for(s=0;s<8;s++) { P2=table[s]; P1_6=0;
delay(20);
saomiao();
if(key!=temp_old) {
P1_6=1;
break;
}
}
}
if(key==5)
{
temp_old=key; for(s=9;s<17;s++) { P2=table[s]; P1_7=0;
delay(13);
saomiao();
if(key!=temp_old) {
P1_7=1;
break;
}
}
}
if(key==6)
{
temp_old=key;
for(s=9;s<17;s++) { P2=table[s];
P1_5=0;
delay(5);
saomiao();
if(key!=temp_old) {
P1_5=1;
break;
}
}
}
if(key==7)
{
temp_old=key; for(s=9;s<17;s++) { P2=table[s];
P1_6=0;
delay(20);
saomiao();
if(key!=temp_old) {
P1_6=1;
break;
}
}
}
}
}