步进电机的控制问题可总结为两点:
1、产生工作方式需要的时序脉冲;
2、控制步进电机的速度,使它始终遵循加速、匀速、减速的规律工作。
系统:
接口:
程序:
#include
#include
const char zhzhuan[7]={0x06,0x04,0x05,0x01,0x03,0x02,0x00};
const char fanzhuan[7]={0x06,0x02,0x03,0x01,0x05,0x004,0x00};
const int time[5]={4000,3200,2400,1600,800};/*脉冲宽度:0.05m,0.04ms,0.03ms,0.02ms,0.01ms*/
char num; /*将要转动的步数*/
char direct; /*转动方向,0为反转,1为正转*/
char num_run; /*已经转动的步数*/
void main(void)
{
DDRA=0x07;
TCCR1A=0x00;
TCCR1B=0x09;
TIMSK=0x10;
SREG|=0x80;
while(1)
{
;
}
}
#pragma interrupt_handler time1coma:5
void time1coma(void)
{
char i,j;
if(num>9)
{
if(num_run<5) OCR1A=time[num_run]; /*增速*/
else if(num_run>(num-5)) OCR1A=time[num-num_run];/*减速*/
}
else
{
i=(num+1)/2;
if(num_run<(i)) OCR1A=time[num_run]; /*增速*/
else OCR1A=time[num-num_run]; /*减速*/
}
if(num_run<num)
{
j=num_run%6;
if(direct==1) PORTA=zhzhuan[j];
else PORTA=fanzhuan[j];
num_run+=1;
}
else
{
OCR1A=0x0fa0;
}
}