⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 单片机控制步进电机驱动器的程序.txt

📁 步进电机具有控制简便、定位准确等特点。随着科学技术的发展,在许多领域将得到广泛的应用。鉴于传统的脉冲系统移植性不好,本文提出微机控制系统代替脉冲发生器和脉冲分配器,用软件的方法产生控制脉冲,通过软件编
💻 TXT
字号:
#include <reg52.h>
#define INITIAL 0xff //INPUT INITIAL POSITION
#define END 0x001ef //THE END OF THE WORK ROUTE
#define SPEED 500 //IT IS THE NUMBER OF THE CIRCLES.
#define HIGHSPEED_H 0xef //THE MOVING SPEED OF THE PLANT CORRESDING TO T0 TIMER
#define HIGHSPEED_L 0xdd
#define WORKSPEED_H 0xda //THE WORK SPEED OF THE PLANT CORRESDING TO T0 TIMER
#define WORKSPEED_L 0xdd


unsigned int position;
//speed is the current T0
unsigned int speed;
//direction is a sbit, 1 means up, 0 means down
bit direction;
//idle indicates whether the plant is moving, 0 map moving, 1 means idle.
bit idle;
//desired_position indicate the position which is desired.
unsigned int desired_position;
//move_mode currently represent the two desired moving style
//0 means work speed(low speed), 1 means move style(high speed).
bit move_mode;


void initialize(void)
{

P1_4 = 0;
while (P1_5)
{ 
unsigned int i;
for (i=0; i < SPEED; i++)
{
}
P1_2 = !P1_2;
}
position = INITIAL;
desired_position = INITIAL;
}

////////////////////////////////////////////////////////////////////////////////////////////
void T0int(void) interrupt 1
{
TR0 = 0; //Need to check if it is necessary. 

if (P1_2 && direction)
{
position++;
}
if (P1_2 && (!direction))
{
position--;
}
if (position != desired_position)
{
if (direction)
{
P1_4 = 1;
}
else
{
P1_4 = 0;
}

if (move_mode)
{
TH0 = HIGHSPEED_H;
TL0 = HIGHSPEED_L;
TR0 = 1;
}
else
{
TH0 = WORKSPEED_H;
TL0 = WORKSPEED_L;
TR0 = 1;
}
}
P1_2 = ! P1_2;
}

//////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
void ready(void)
{ 
desired_position = INITIAL;
move_mode = 1;
if (desired_position > position) 
direction = 1;
else if (desired_position < position)
direction = 0;

}


///////////////////////////////////////////////////////////////////////////////
void work(void)
{
desired_position = END;
direction = 1;
move_mode = 0;
}


///////////////////////////////////////////////////////////////////////////////
void main (void)
{
initialize();
IE = 0xfa;
TMOD = 0x11;
while (1)
{ 
if (!P0_0)
{
unsigned char i;
unsigned char ms;
ms = 10;
while(ms--)
{
for(i = 0; i < 200; i++);
}
if (!P0_0)
{
while(!P0_0){}
ready();
if ( position != desired_position)
{
TH0 = HIGHSPEED_H;
TL0 = HIGHSPEED_L;
TR0 = 1;
}
}
}
if (!P0_3)
{
unsigned char i;
unsigned char ms;
ms=10;
while(ms--)
{
for(i = 0; i < 200; i++);
}
if (!P0_3)
{
while(!P0_3){}
work();
if (position != desired_position)
{
TH0 = WORKSPEED_H;
TL0 = WORKSPEED_L;
TR0 = 1;
}
}
}
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -