📄 基本实现1.txt
字号:
/******************************************************************/
/*****************************************************************/
/*
/* 步进电机加减速运行程序
/* 步进电机启动时,转速由慢到快逐步加速。
/* 步进电机匀速运行
/* 步进电机由快到慢逐步减速到停止
/*
/******************************************************************/
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
uchar code FFW1[4]={0x03,0x09,0x0c,0x06};//正转数组
uchar code REV1[4]={0x06,0x0c,0x09,0x03};//反转数组
uchar code FFW2[4]={0x30,0x90,0xc0,0x60};//正转数组
uchar code REV2[4]={0x60,0xc0,0x90,0x30};//反转数组
bit StartFlag1=0;
bit StartFlag2=0;
bit SingalFlag=0;
sbit sensor_signal=P3^3;
sbit photoelectric_switch=P3^2;
uchar rate=0xff;
uchar motorstep1=0;
uchar motorstep2=0;
uchar con_step;
uchar count1;
uchar count2;
uchar j=0;
/********************************************************/
/*
/* 延时 1ms
/* 11.0592MHz时钟,
/*
/********************************************************/
void delay()
{
uchar k;
uint s;
k = rate;
do
{
for(s = 0 ; s <100 ; s++) ;
}while(--k);
}
/********************************************************/
/*
/*步进电机正转
/*
/********************************************************/
void motor2_ffw()
{
uchar i;
for (i=0; i<4; i++) //一个周期转30度
{ motorstep2++;
P1 = FFW2[i];//取数据
delay(); //调节转速
}
}
/********************************************************/
/*
/*步进电机反转
/*
/********************************************************/
void motor2_rev()
{
uchar i;
for (i=0; i<4; i++) //一个周期转30度
{ motorstep2++;
P1 = REV2[i]; //取数据
delay(); //调节转速
}
}
/********************************************************
*
* 主程序
*
*********************************************************/
void main(void)
{
uint a,b,c;
/*******************定时器0初始化************************/
count1=0;
TMOD=0x61;
TH0=-50000/256;
TL0=-50000%256;
PT0=0;
ET0=1;
TR0=1;
EA=1;
/*****************定时器1初始化**************************/
TL1=0xff;
TH1=0xff;
ET1=1;
TR1=1;
/******************外部中断0初始化——光电对管1**********************/
EX0=1;
PX0=0;
IT0=0;
EA=1;
/******************外部中断1初始化——热释红外********************/
EX1=1;
PX1=1;
IT1=1;
EA=1;
/******************************************************/
while(1)
{
if(SingalFlag==1)
{
EA=0;
con_step=motorstep1;
if(StartFlag1==1) {
do {motor2_rev();}while(motorstep2<=con_step);
}
else {
do
{motor2_ffw();}while(motorstep2<=con_step);
}
SingalFlag=0;
EA=1;
//延时1000*10ms=10s
for(a=0;a<=1000;a++)
for(b=0;b<=10;b++)
for(c=0;c<=120;c++);
if(StartFlag1==1) { do{motor2_ffw();}while(motorstep2!=0);
}
else do {motor2_rev();}while(motorstep2!=0);
}
}
}
/********************************************************外部0中断服务——MOTOR1零角度中断*****************/
int0_srv() interrupt 0 using 0 //电机起始定位ISP
{
EX0=0; //关中断0
StartFlag1^=1;
motorstep1=0;
//开中断
}
/**********************************************************外部中断1服务——热释红外输入 **********************************/
int1_srv() interrupt 2 using 2
{
SingalFlag=1;
con_step=motorstep1;
}
/********************************************定时器0中断服务——MOTOR1步进中断***************************************************/
void timer0(void) interrupt 1 using 1
{
TH0=-50000/256;
TL0=-50000%256;
if( StartFlag1==1)P1 = REV1[j];
else P1 = FFW1[j];
count1++;
if(count1==10)
{count1=0;
motorstep1++;
if(motorstep1==12)EX0=1;
j++;
if(j==4){j=0;}
}
}
void timer1(void) interrupt 3 using 3
{
if(P3^5==0)
{
StartFlag2^=1;
motorstep2=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -