?? 基本實現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 + -