ATMEGA8单片机驱动4线步进机C程序
扫描二维码
随时随地手机看文章
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharnp;
//步进电机运行数据表
constucharmotortb[]={0x11,0x99,0x88,0xCC,0x44,0x66,0x22,0x33};
voiddelay_nms(uintms)//每步延时de子程序
{
uinti;
for(i=0;i
}
voida_step(uchard,uchart)//步进电机走一步d=0正转d=1反转t越大走得越慢
{
if(d&0x01)
{
if(np==0)
np=7;
else
np--;
}
else
{
if(np==7)
np=0;
else
np++;
}
PORTD=motortb[np];
delay_nms(t);
}
voida_turn(uchard,uchart)//步进电机走一圈
{
uchari;
for(i=0;i<96;i++)
a_step(d,t);
}
intmain(void)
{
DDRD=0xff;
PORTD=0x44;
DDRB=0xff;
PORTB=0xff;
np=4;
while(1)
a_turn(1,200);
}