这是我最近写的一个驱动程序,DA是7位的,不知道细分转换表对不对,我是按这个公式算的,细分值=128sin(90*n/32)n从1取到32.
高手们帮我看看这样写对不对
/*******************************************************************
程序实现功能:对二相步进电机进行32细分;采用定时器中断,每0.5ms电机
走一步;
暂时单片机外部控制部分只有控制正转、反转键
*******************************************************************/
#include <reg51.h>
#include <absacc.h>
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#include <string.h>
//NJU39610端口定义
#define DA_PORT P1
sbit DA_WR=P3^4; //NJU39610数据写入端;
sbit DA_A0=P3^7; //NJU39610通道选择端;
//对外接口引脚定义
sbit ST_CLK =P3^2; //外部脉冲信号输入;
sbit ST_STOP=P3^3; //控制步进电机开关;
sbit ST_DIR =P2^7; //控制步进电机转动方向;
//全局变量定义
unsigned char data xf_step; //细分步数(二相电机单二拍走一循环为64步)
unsigned char xdata xf32_sin[64]={0,6,12,18,24,31,37,43, //32细分后走一个循环的细分数据;
48,54,60,65,71,76,81,85,
90,94,98,102,106,109,112,115,
118,120,122,124,125,126,127,127,
128,127,127,126,125,124,122,120,
118,115,112,109,106,102,98,94,
90,85,81,76,71,65,60,54,
48,43,37,31,24,18,12,6
};
unsigned char xdata xf32_cos[64]={128,127,127,126,125,124,122,120,
118,115,112,109,106,102,98,94,
90,85,81,76,71,65,60,54,
48,43,37,31,24,18,12,6,
0,6,12,18,24,31,37,43,
48,54,60,65,71,76,81,85,
90,94,98,102,106,109,112,115,
118,120,122,124,125,126,127,127,
};
StopMt()
{
DA_A0=0; //选DA通道0;
DA_PORT=0;
DA_WR=0; //DA数据写入;
_nop_();
DA_WR=1;
DA_A0=1; //选DA通道1;
DA_PORT=0;
DA_WR=0; //DA数据写入;
_nop_();
DA_WR=1;
}
void t0(void)interrupt 1 using 0
{
TH0=(65536-500)/256;
TL0=(65536-500)/256;
if(ST_DIR) //如果ST_DIR为1,则电机正转,D7位为低电平
{
DA_A0=0; //选DA通道0;
DA_PORT=xf32_sin[xf_step]|0x80;
DA_WR=0; //DA数据写入;
_nop_();
DA_WR=1;
DA_A0=1; //选DA通道1;
DA_PORT=xf32_cos[xf_step]|0x80;
DA_WR=0; //DA数据写入;
_nop_();
DA_WR=1;
}
else //如果ST_DIR为0,则电机反转,D7位为高电平
{
DA_A0=0; //选DA通道0;
DA_PORT=xf32_sin[xf_step];
DA_WR=0; //DA数据写入;
_nop_();
DA_WR=1;
DA_A0=1; //选DA通道1;
DA_PORT=xf32_cos[xf_step];
DA_WR=0; //DA数据写入;
_nop_();
DA_WR=1;
}
xf_step++; //0.5ms进次中断,电机走一步,每走64步重新循环
if(xf_step>=64) {xf_step=0;}
}
void main(void)
{
TMOD=0x01;
TH0=(65536-500)/256;
TL0=(65536-500)/256;
TR0=1;
ET0=1;
EA=1; //开放系统中断
StopMt();
while(1);
}
(原文件名:驱动电路.jpg)
一周热门 更多>