完整的单片机控制步进电机程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{common_count=common_count+1; }
if(interval<common_count)
{common_count=common_count-1; }
}
speed_change=0;
}
if(sent_ok==1)
{
sent_ok=0;
P1_0=0;
for(i=0;i<=20;i++) {_nop_();}
}
void timer2(void) interrupt 5 using 2
{
TF2=0;/*T2溢出中断需软件清0*/
speed_change=1; //速度可以改变标示,以便主程序处理
speed_change=0;
start_up=0;
start_end=0;
sent_ok=0; //允许发送
EA=1;/*开放总中断*/
TMOD=0x21;
TH1 = 0xFD;//波特率9600
TL1 = 0xFD;
SCON = 0xd0;//设定串行口工作方式
PCON &= 0x00;//波特率不倍增
{
P2_0=~P2_0;
y1=add[common_count];
T0high = (uchar)(y1>>8) ;/*取y1的高8位*/
T0low = (uchar)(y1&0x00ff);/*取y1的低8位*/
TH0=T0high;/*高8位T0higFra Baidu bibliotek送定时器0的TH0*/
TL0=T0low;/*低8位T0low送定时器0的TL0*/
#include "reg52.h"
#include "INTRINS.H"
#include <absacc.h>
#include <math.h>
#define uint unsigned int
#define uchar unsigned char
void check_addr(void); /*地址核对*/
P2_2=0;/*步进电机的旋转方向待试验后确定*/
P1_0=1;/*开机时需要关断,串口发送功能,需要时再接通*/
WD=1; /*看门狗先为1,电平翻转为喂狗*/
i=0;
common_count=0;
cmd_in_permit=0;
input_order=0;
interval=0;
address_true=1;
ET2=1; //打开定时器2中断
do
{
if(address_true==1)
{
address_true=0;
check_addr();
}
if(start_up==1&&start_end==0) //第一次启动
{
y1=add[common_count];
T0high = (uchar)(y1>>8) ;/*取y1的高8位*/
SM2=1;
TR1 = 1;
ES=1;//启动定时器1
T2MOD=00;
T2CON=0x00;
RCAP2H =0xEE; //赋T2的预置值0xA600,25MS,0xB800,20MS,0xCA00,15MS,0xDC00,10MS,0xEE00,5MS
RCAP2L =0x00;
TR2=1; //启动定时器
sbit P2_0=P2^0;/*作输入步进电机的脉冲信号发送口*/
sbit P2_2=P2^2;/*作输入步进电机的旋转方向信号发送口*/
sbit P1_0=P1^0;/*作串口输出信号的使能口, P1_0=0时接通串口,输出信号*/
sbit WD=P1^7; /*看门狗*/
main()
{
P2_0=0;
uchar code slave_addr[4]={00, 01, 02, 255}; /*从机地址*/
uchar idata T0low, T0high,common_count,input_order,cmd_in_permit,interval;
uchar sent_ok,speed_change,start_up,start_end,address_true,i;
TI=0; SBUF=T0high;
while(TI==0);TI=0;
TI=0; SBUF=T0low;
while(TI==0);TI=0;
P1_0=1;
for(i=0;i<=20;i++) {_nop_();}
SM2=1;
}
}
while(1);
}
void timer0(void) interrupt 1 using 3
T0low = (uchar)(y1&0x00ff);/*取y1的低8位*/
TR0 = 1;
ET0=1;/*允许T/C0中断*/
start_end=1;
}
if(speed_change==1)
{
if(interval>=0&&interval<=0x63)
{
if(interval>common_count)
uint y1;
uint code add[100]={60006,62771,63693,64154,64430,64614,64746,64845,64922,64983,65033,65075,65111,65141,65167,65190,65211,65229,65245,65260,65273,65285,65296,65306,65315,65323,65331,65339,65345,65352,65358,65363,65368,65373,65378,65382,65387,65390,65394,65398,65401,65404,65407,65410,65413,65416,65418,65421,65423,65425,65428,65430,65432,65434,65435,65437,65439,65441,65442,65444,65445,65447,65448,65450,65451,65452,65453,65455,65456,65457,65458,65459,65460,65461,65462,65463,65464,65465,65466,65467,65468,65469,65469,65470,65471,65472,65472,65473,65474,65475,65475,65476,65477,65477,65478,65478,65479,65480,65480,65481};
相关文档
最新文档