当前位置:首页 > 单片机 > 单片机
[导读]ATMEGA8驱动4线步进机程序

#include

#include

#define uchar unsigned char

#define uint unsigned int

uchar np;

//步进电机运行数据表

const uchar motortb[]={0x11,0x99,0x88,0xCC,0x44,0x66,0x22,0x33};

void delay_nms(uint ms)// 每步延时的子程序

{

uint i;

for(i=0;i _delay_loop_2(8*250);

}

void a_step(uchar d,uchar t) //步进电机走一步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);

}

void a_turn(uchar d,uchar t)// 步进电机走一圈

{

uchar i;

for (i=0;i<96;i++)

a_step(d,t);

}

int main(void)

{

DDRD=0xff;

PORTD=0x44;

DDRB=0xff;

PORTB=0xff;

np=4;

while (1)

a_turn(1,200);

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭