《完整的单片机控制步进电机程序(共4页).doc》由会员分享,可在线阅读,更多相关《完整的单片机控制步进电机程序(共4页).doc(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上#include reg52.h#include INTRINS.H#include #include #define uint unsigned int#define uchar unsigned charvoid check_addr(void); /*地址核对*/uchar code slave_addr4=00, 01, 02, 255; /*从机地址*/uchar idata T0low, T0high,common_count,input_order,cmd_in_permit,interval;uchar sent_ok,speed_change,sta
2、rt_up,start_end,address_true,i;uint y1;uint code add100=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,6540
3、1,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,6547
4、5,65476,65477,65477,65478,65478,65479,65480,65480,65481;sbit P2_0=P20;/*作输入步进电机的脉冲信号发送口*/sbit P2_2=P22;/*作输入步进电机的旋转方向信号发送口*/sbit P1_0=P10; /*作串口输出信号的使能口, P1_0=0时接通串口,输出信号*/sbit WD=P17; /*看门狗*/main() P2_0=0;P2_2=0;/*步进电机的旋转方向待试验后确定*/P1_0=1;/*开机时需要关断,串口发送功能,需要时再接通*/WD=1; /*看门狗先为1,电平翻转为喂狗*/i=0;common_c
5、ount=0;cmd_in_permit=0;input_order=0;interval=0;address_true=1;speed_change=0;start_up=0;start_end=0;sent_ok=0; /允许发送 EA=1;/*开放总中断*/ TMOD=0x21;TH1 = 0xFD;/ 波特率9600TL1 = 0xFD;SCON = 0xd0;/ 设定串行口工作方式PCON &= 0x00;/ 波特率不倍增SM2=1;TR1 = 1;ES=1; / 启动定时器1T2MOD=00;T2CON=0x00;RCAP2H =0xEE; /赋T2的预置值0xA600,25MS,
6、0xB800,20MS,0xCA00,15MS,0xDC00,10MS,0xEE00,5MSRCAP2L =0x00; TR2=1; /启动定时器 ET2=1; /打开定时器2中断do if(address_true=1) address_true=0;check_addr(); if(start_up=1&start_end=0) /第一次启动 y1=addcommon_count;T0high = (uchar)(y18) ; /*取y1的高8位*/T0low = (uchar)(y1&0x00ff); /*取y1的低8位*/TR0 = 1;ET0=1;/*允许T/C0中断*/start_
7、end=1; if(speed_change=1) if(interval=0&intervalcommon_count) common_count=common_count+1; if(intervalcommon_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_(); TI=0; SBUF=T0high; while(TI=0);TI=0; TI=0; SBUF=T0low; while(TI=0);TI=0; P1_0=1;
8、for(i=0;i8) ; /*取y1的高8位*/T0low = (uchar)(y1&0x00ff); /*取y1的低8位*/TH0=T0high;/*高8位T0high送定时器0的TH0*/TL0=T0low;/*低8位T0low送定时器0的TL0*/void timer2(void) interrupt 5 using 2 TF2=0;/*T2溢出中断需软件清0*/ speed_change=1; /速度可以改变标示,以便主程序处理 WD=!WD; /*MAX813喂狗*/ void inte_SERIAL() interrupt 4 using 1 /*串口0中断服务子程序*/ucha
9、r key_in ;key_in=0;if(RI) key_in=SBUF; RI=0;if (SM2=1) if(key_in=slave_addr2)SM2=0;address_true=1;if (SM2=0)& (RB8=0) if(key_in=0xff)SM2=1; if(key_in=0xfe) /*接收主机命令引导字节,准备接收主机命令*/ cmd_in_permit=1;if(cmd_in_permit=1) input_order=input_order+1;if (input_order=2) /*接收主机命令,使从机开始调节电机*/cmd_in_permit=0;input_order=0;/*interval代表控制器发给电机的转速期望值*/interval= key_in;sent_ok=1;if(start_up=0)start_up=1; void check_addr(void) /*地址核对成功,发送从机地址给主机*/TB8=1;RB8=0;P1_0=0; for(i=0;i=25;i+) _nop_(); SBUF=slave_addr2;/*发送地址核对成功,发送从机地址给主机*/do while(TI=0); TI=0;P1_0=1; for(i=0;i=25;i+) _nop_(); TB8=0;专心-专注-专业