/**************************************************************/
#include <AT89x051.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit torque=P3^7; //力矩电机接通信号
sbit CP=P3^4; //脉冲输出
sbit DIR=P3^5; //方向信号
sbit lim_back=P3^2; //极限和手动变向
uchar num,flag;
uchar speed_num,speed_num1,speed_num2;
uchar width_num,width_num1,width_num2;
uint plu_num; //脉冲总量
/***********************************************************/
void init_steptime()
{
TMOD = 0x01; //定时器0为工作方式1
TH0 = 0xFF; //50us初值
TL0 = 0xCE;
EA = 1; //开全局中断
ET0=1; //开定时器0中断
TR0=1; //启动定时器0
EX0=1; //开外部中断0
IT0=1; //外部中断0下降沿触发
}
/***********************************************************/
void delayms(uchar xms)
{
uchar i,j;
for(i=xms;i>0;i--)
{
for(j=110;j>0;j--);
}
}
/***********************************************************/
void read()
{
uchar temp_P1,speed_shi,speed_ge,width_shi,width_ge;
P1=0xef; //读速度给定个位
delayms(2);
temp_P1=~P1;
temp_P1=temp_P1&0x0f;
speed_ge=temp_P1;
P1=0xdf; //读速度给定十位
delayms(2);
temp_P1=~P1;
temp_P1=temp_P1&0x0f;
speed_shi=temp_P1;
speed_num=speed_shi*10+speed_ge; //计算速度设定值
P1=0xbf; //读宽度给定个位
delayms(2);
temp_P1=~P1;
temp_P1=temp_P1&0x0f;
width_ge=temp_P1;
P1=0x7f; //读宽度给定十位
delayms(2);
temp_P1=~P1;
temp_P1=temp_P1&0x0f;
width_shi=temp_P1;
width_num=width_shi*10+width_ge; //计算宽度设定值
plu_num=width_num*360; //由宽度设定计算脉冲总量,得出绕线宽度
}
/***********************************************************/
void get_back()
{
ET0=0; //关定时器0中断
TR0=0; //关定时器0
read();
DIR=~DIR; //改换方向
delayms(100);
ET0=1; //开定时器0中断
TR0=1; //启动定时器0
}
/***********************************************************/
void step_moto() //步进电机驱动
{
while(plu_num>0)
{
if(num==(100-speed_num)) //数字越大,角速度越快
{
num=0;
CP=~CP;
plu_num--;
}
if(torque==1)
{
flag=0;
}
}
get_back();
}
/***********************************************************/
void int_int0() interrupt 0 //外部中断0
{
if(lim_back==0)
{
delayms(15);//消抖
if(lim_back==0);
{
get_back();
}
}
}
/***********************************************************/
void T0_time() interrupt 1
{
TH0 = 0xFF;//50us初值
TL0 = 0xCE;
num++;
}
/***********************************************************/
void main()
{
P1=0xff;
P3=0xff;
init_steptime();
read();
flag=0;
while(1)
{
if(torque==0)
{
delayms(10);//消抖
if(torque==0);
{
flag=1;
}
}
while(flag==1)
{
step_moto();
}
CP=CP;
}
}
/***********************************************************/
虹山艺术高地步进电机控制—拨码开关设定速度和宽度(开关量读入有问题)
最新推荐文章于 2025-01-07 08:55:00 发布
