百度知道 - 信息提示
求单片机控制步进电机简单的c程序
C语言程序如下:
#include
#define uint unsigned int
Sbit k1=p3.4;//启动开关
Sbit k2=p3.5;//换向开关
Sbit s2=p3.2;//加速按钮
Sbit s3=p3.3;//减速按钮
Void idr_int0(void);//外部中断0中断服务函数声明
Void idr_int1(void);
Void zd_t0ist(void);
Uint speed,count,r1,I,t,k;
Main()
{k=0;
T=0;
r1=0x11;
speed=0;
count=1;
tmod=0x01;
et0=1;
ea=1;
ex0=1;
ex1=1;
if(k2==0)
p0=0xbf;
else p0=0xf9;
if (speed==0)
{p2=0xc0;
Tr0=0;
}
Else tr0=1;
}
}
}
Void isr_int0(void)interrupt();
{
If(speed<7)
Speed=speed+1;
While(s2==0)
{For(i=0;i<10;i++);}
}
Void isr_int0(void)interrupt2;
Th0=0xcf;
Tl0=0x2c;
For(;;)
{
If (k1==0)
{p0=0xff;
P2=0xff;
Speed=0;
Tr0=0}
Else
{
If(k2==0)
P0=0xbf;
Else p0=0xf9;
If(speed==0)
{p2=0xc0;
Tr0=0;
}
Else tr0=1;
}
}
}
Void isr_int0(void)interrupt()
{
If(speed<7)
Speed=speed+1;
While(s2==0)
{for(i=0;i<10;i++);}
}
Void isr_int1(void)interrupt2
{
If(speed>0)
Speed=speed-1;
While(s3==0)
{for(i=0;;i<10;i++);}
}
Void zd_t0ist(void)interrupt 1
{
Th0=0xd8;
Tl0=0xf0;
Switch(speed)
{
Case0;p2=0xc0;count=0;break;
Case1;p2=0xf9;count=60;break;
Case2;p2=0xa4;count=40;break;
Case3;p2=0xb0;count=35break;
Case4;p2=0x99;count=30;break;
Case5;p2=0x92;count=28;break;
Case6;p2=0x82;count=25;break;
Case7;p2=0xf8;count=21;break;
Default:break;
}
If(t==0)
T=count;
If(t>0)
T=t-1;
If(k2==0)
{
If(t==0)
{
Switch(k)
{
Case0;p1=0x01;break;
Case1;p1=0x02;break;
Case2;p1=0x04;break;
Case3;p1=0x08;break;
Default:break;
}
k=k+1;
if(k==4)
k=0;
}
}
Else
{
If(t==0)
{
Switch(k)
{
Case0;p1=0x08;break;
Case1;p1=0x04;break;
Case2;p1=0x02;break;
Case3;p1=0x01;break;
Default:break;
}
K=k+1;
If(==4)
K=0;
}
}
}
51单片机驱动带有驱动器的步进电机C语言编程
#include#define uchar unsigned charsbit key=P3^2;sbit dir=P0^3;sbit pluse=P0^2;sbit en=P0^4;void delay(int a){ while(a--);}main(){uchar i;en=0;pluse=0; while(1) { if(key==0) { delay(1000); if(key==0); { while(key==0); for(i=0;i<200;i++) { dir=0; en=1; pluse=~pluse; delay(800); } } } dir=1; en=0; pluse=0; }}