ATMEGA8單片機(jī)驅(qū)動(dòng)4線步進(jìn)機(jī)C程序
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharnp;
//步進(jìn)電機(jī)運(yùn)行數(shù)據(jù)表
constucharmotortb[]={0x11,0x99,0x88,0xCC,0x44,0x66,0x22,0x33};
voiddelay_nms(uintms)//每步延時(shí)de子程序
{
uinti;
for(i=0;i
}
voida_step(uchard,uchart)//步進(jìn)電機(jī)走一步d=0正轉(zhuǎn)d=1反轉(zhuǎn)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)//步進(jìn)電機(jī)走一圈
{
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);
}