《单片机控制步进电机程序及硬件电路图.docx》由会员分享,可在线阅读,更多相关《单片机控制步进电机程序及硬件电路图.docx(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include static unsigned int count; /计数static int step_index; /步进索引数,值为07static bit turn; /步进电机转动方向static bit stop_flag; /步进电机停顿标志static int speedlevel; /步进电机转速参数,数值越大速度越慢,最小值为1,速度最快static int spcount; /步进电机转速参数计数void gorun(); /步进电机控制步进函数void main(void) count = 0; step_index = 0; spcount = 0; stop_f
2、lag = 0; P1_0 = 0; P1_1 = 0; P1_2 = 0; P1_3 = 0; EA = 1; /允许CPU中断 TMOD = 0x11; /设定时器0与1为16位模式1 ET0 = 1; /定时器0中断允许 TH0 = 0xFE; TR0 = 1; /开场计数 turn = 0; speedlevel = 2; delay(10000); speedlevel = 1; do speedlevel = 2; delay(10000); speedlevel = 1; delay(10000); stop_flag=1; delay(10000); stop_flag=0; while(1);/定时器0中断处理 void timeint(void) interrupt 1 TH0=0xFE; count+; spcount-; if(spcount=0) spcount = speedlevel; gorun();void delay(unsigned int endcount) count=0; dowhile(count7) step_index=0; else step_index-; if (step_index0) step_index=7;第 5 页